5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to handle received packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 extern char *server_version;
30 /* Received notify packet. Server can receive notify packets from router.
31 Server then relays the notify messages to clients if needed. */
33 void silc_server_notify(SilcServer server,
34 SilcSocketConnection sock,
35 SilcPacketContext *packet)
37 SilcNotifyPayload payload;
39 SilcArgumentPayload args;
40 SilcChannelID *channel_id = NULL, *channel_id2;
41 SilcClientID *client_id, *client_id2;
42 SilcServerID *server_id;
44 SilcChannelEntry channel = NULL;
45 SilcClientEntry client = NULL, client2 = NULL;
46 SilcServerEntry server_entry = NULL;
47 SilcChannelClientEntry chl;
48 SilcIDCacheEntry cache;
49 SilcHashTableList htl;
55 SILC_LOG_DEBUG(("Start"));
57 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
58 packet->src_id_type != SILC_ID_SERVER)
64 /* If the packet is destined directly to a client then relay the packet
65 before processing it. */
66 if (packet->dst_id_type == SILC_ID_CLIENT) {
68 SilcSocketConnection dst_sock;
70 /* Get the route to the client */
71 dst_sock = silc_server_get_client_route(server, packet->dst_id,
72 packet->dst_id_len, NULL,
75 /* Relay the packet */
76 silc_server_relay_packet(server, dst_sock, idata->send_key,
77 idata->hmac_send, idata->psn_send++,
81 /* Parse the Notify Payload */
82 payload = silc_notify_payload_parse(packet->buffer->data,
87 /* If we are router and this packet is not already broadcast packet
88 we will broadcast it. The sending socket really cannot be router or
89 the router is buggy. If this packet is coming from router then it must
90 have the broadcast flag set already and we won't do anything. */
91 if (!server->standalone && server->server_type == SILC_ROUTER &&
92 sock->type == SILC_SOCKET_TYPE_SERVER &&
93 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
94 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
95 if (packet->dst_id_type == SILC_ID_CHANNEL) {
96 /* Packet is destined to channel */
97 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
102 silc_server_packet_send_dest(server, server->router->connection,
104 packet->flags | SILC_PACKET_FLAG_BROADCAST,
105 channel_id, SILC_ID_CHANNEL,
106 packet->buffer->data, packet->buffer->len,
108 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
109 packet->type, packet->flags,
110 channel_id, SILC_ID_CHANNEL,
111 packet->buffer->data, packet->buffer->len,
114 /* Packet is destined to client or server */
115 silc_server_packet_send(server, server->router->connection,
117 packet->flags | SILC_PACKET_FLAG_BROADCAST,
118 packet->buffer->data, packet->buffer->len,
120 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
121 packet->type, packet->flags,
122 packet->buffer->data, packet->buffer->len,
127 type = silc_notify_get_type(payload);
128 args = silc_notify_get_args(payload);
133 case SILC_NOTIFY_TYPE_JOIN:
135 * Distribute the notify to local clients on the channel
137 SILC_LOG_DEBUG(("JOIN notify"));
140 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
143 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
147 /* Get channel entry */
148 channel = silc_idlist_find_channel_by_id(server->global_list,
151 channel = silc_idlist_find_channel_by_id(server->local_list,
154 silc_free(channel_id);
158 silc_free(channel_id);
161 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
164 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
168 /* If the the client is not in local list we check global list (ie. the
169 channel will be global channel) and if it does not exist then create
170 entry for the client. */
171 client = silc_idlist_find_client_by_id(server->global_list,
172 client_id, server->server_type,
175 client = silc_idlist_find_client_by_id(server->local_list,
176 client_id, server->server_type,
179 /* If router did not find the client the it is bogus */
180 if (server->server_type != SILC_SERVER)
184 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
185 silc_id_dup(client_id, SILC_ID_CLIENT),
186 sock->user_data, NULL, 0);
188 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
189 silc_free(client_id);
193 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
197 /* Do not process the notify if the client is not registered */
198 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
201 /* Do not add client to channel if it is there already */
202 if (silc_server_client_on_channel(client, channel, NULL)) {
203 SILC_LOG_DEBUG(("Client already on channel"));
207 /* Send to channel */
208 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
209 FALSE, packet->buffer->data,
210 packet->buffer->len, FALSE);
212 if (server->server_type != SILC_ROUTER &&
213 sock->type == SILC_SOCKET_TYPE_ROUTER)
214 /* The channel is global now */
215 channel->global_users = TRUE;
217 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
219 /* JOIN the global client to the channel (local clients (if router
220 created the channel) is joined in the pending JOIN command). */
221 chl = silc_calloc(1, sizeof(*chl));
222 chl->client = client;
223 chl->channel = channel;
225 /* If this is the first one on the channel then it is the founder of
227 if (!silc_hash_table_count(channel->user_list))
228 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
230 silc_hash_table_add(channel->user_list, client, chl);
231 silc_hash_table_add(client->channels, channel, chl);
232 silc_free(client_id);
233 channel->user_count++;
237 case SILC_NOTIFY_TYPE_LEAVE:
239 * Distribute the notify to local clients on the channel
241 SILC_LOG_DEBUG(("LEAVE notify"));
244 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
245 packet->dst_id_type);
250 /* Get channel entry */
251 channel = silc_idlist_find_channel_by_id(server->global_list,
254 channel = silc_idlist_find_channel_by_id(server->local_list,
257 silc_free(channel_id);
263 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
265 silc_free(channel_id);
268 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
270 silc_free(channel_id);
274 /* Get client entry */
275 client = silc_idlist_find_client_by_id(server->global_list,
276 client_id, TRUE, NULL);
278 client = silc_idlist_find_client_by_id(server->local_list,
279 client_id, TRUE, NULL);
281 silc_free(client_id);
282 silc_free(channel_id);
286 silc_free(client_id);
288 /* Check if on channel */
289 if (!silc_server_client_on_channel(client, channel, NULL))
292 /* Send the leave notify to channel */
293 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
294 FALSE, packet->buffer->data,
295 packet->buffer->len, FALSE);
297 /* Remove the user from channel */
298 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
301 case SILC_NOTIFY_TYPE_SIGNOFF:
303 * Distribute the notify to local clients on the channel
305 SILC_LOG_DEBUG(("SIGNOFF notify"));
308 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
311 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
315 /* Get client entry */
316 client = silc_idlist_find_client_by_id(server->global_list,
317 client_id, TRUE, &cache);
319 client = silc_idlist_find_client_by_id(server->local_list,
320 client_id, TRUE, &cache);
322 silc_free(client_id);
326 silc_free(client_id);
328 /* Get signoff message */
329 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
333 /* Update statistics */
334 server->stat.clients--;
335 if (server->stat.cell_clients)
336 server->stat.cell_clients--;
337 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
338 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
340 /* Remove the client from all channels. */
341 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
343 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
344 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
347 case SILC_NOTIFY_TYPE_TOPIC_SET:
349 * Distribute the notify to local clients on the channel
352 SILC_LOG_DEBUG(("TOPIC SET notify"));
355 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
358 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
362 /* Get client entry */
363 client = silc_idlist_find_client_by_id(server->global_list,
364 client_id, TRUE, &cache);
366 client = silc_idlist_find_client_by_id(server->local_list,
367 client_id, TRUE, &cache);
369 silc_free(client_id);
373 silc_free(client_id);
376 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
378 silc_free(channel_id);
383 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
384 packet->dst_id_type);
389 /* Get channel entry */
390 channel = silc_idlist_find_channel_by_id(server->global_list,
393 channel = silc_idlist_find_channel_by_id(server->local_list,
396 silc_free(channel_id);
401 if (channel->topic && !strcmp(channel->topic, tmp))
404 /* Get user's channel entry and check that topic set is allowed. */
405 if (!silc_server_client_on_channel(client, channel, &chl))
407 if (chl->mode == SILC_CHANNEL_UMODE_NONE &&
408 channel->mode & SILC_CHANNEL_MODE_TOPIC) {
409 SILC_LOG_DEBUG(("Topic change is not allowed"));
413 /* Change the topic */
414 silc_free(channel->topic);
415 channel->topic = strdup(tmp);
417 /* Send the same notify to the channel */
418 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
419 FALSE, packet->buffer->data,
420 packet->buffer->len, FALSE);
421 silc_free(channel_id);
424 case SILC_NOTIFY_TYPE_NICK_CHANGE:
427 * Distribute the notify to local clients on the channel
429 unsigned char *id, *id2;
431 SilcUInt32 nickname_len;
433 SILC_LOG_DEBUG(("NICK CHANGE notify"));
435 /* Get old client ID */
436 id = silc_argument_get_arg_type(args, 1, &tmp_len);
439 client_id = silc_id_payload_parse_id(id, tmp_len, NULL);
443 /* Get new client ID */
444 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
447 client_id2 = silc_id_payload_parse_id(id2, tmp_len, NULL);
451 SILC_LOG_DEBUG(("Old Client ID id(%s)",
452 silc_id_render(client_id, SILC_ID_CLIENT)));
453 SILC_LOG_DEBUG(("New Client ID id(%s)",
454 silc_id_render(client_id2, SILC_ID_CLIENT)));
456 /* From protocol version 1.1 we also get the new nickname */
457 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
459 /* Replace the Client ID */
460 client = silc_idlist_replace_client_id(server->global_list, client_id,
461 client_id2, nickname);
463 client = silc_idlist_replace_client_id(server->local_list, client_id,
464 client_id2, nickname);
467 /* Send the NICK_CHANGE notify type to local clients on the channels
468 this client is joined to. */
469 silc_server_send_notify_on_channels(server, NULL, client,
470 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
471 id, tmp_len, id2, tmp_len,
476 silc_free(client_id);
478 silc_free(client_id2);
482 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
484 * Distribute the notify to local clients on the channel
487 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
490 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
493 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
497 /* Get client entry */
498 if (id_type == SILC_ID_CLIENT) {
499 client = silc_idlist_find_client_by_id(server->global_list,
500 client_id, TRUE, &cache);
502 client = silc_idlist_find_client_by_id(server->local_list,
503 client_id, TRUE, &cache);
505 silc_free(client_id);
509 silc_free(client_id);
513 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
514 packet->dst_id_type);
519 /* Get channel entry */
520 channel = silc_idlist_find_channel_by_id(server->global_list,
523 channel = silc_idlist_find_channel_by_id(server->local_list,
526 silc_free(channel_id);
530 silc_free(channel_id);
533 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
536 SILC_GET32_MSB(mode, tmp);
538 /* Check if mode changed */
539 if (channel->mode == mode)
542 /* Get user's channel entry and check that mode change is allowed */
544 if (!silc_server_client_on_channel(client, channel, &chl))
546 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
547 SILC_LOG_DEBUG(("CMODE change is not allowed"));
552 /* Send the same notify to the channel */
553 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
554 FALSE, packet->buffer->data,
555 packet->buffer->len, FALSE);
557 /* If the channel had private keys set and the mode was removed then
558 we must re-generate and re-distribute a new channel key */
559 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
560 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
561 /* Re-generate channel key */
562 if (!silc_server_create_channel_key(server, channel, 0))
565 /* Send the channel key. This sends it to our local clients and if
566 we are normal server to our router as well. */
567 silc_server_send_channel_key(server, NULL, channel,
568 server->server_type == SILC_ROUTER ?
569 FALSE : !server->standalone);
573 channel->mode = mode;
576 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
578 unsigned char hash[32];
581 silc_hmac_free(channel->hmac);
582 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
585 /* Set the HMAC key out of current channel key. The client must do
587 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
588 channel->key_len / 8,
590 silc_hmac_set_key(channel->hmac, hash,
591 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
592 memset(hash, 0, sizeof(hash));
595 /* Get the passphrase */
596 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
598 silc_free(channel->passphrase);
599 channel->passphrase = silc_memdup(tmp, tmp_len);
604 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
607 * Distribute the notify to local clients on the channel
609 SilcChannelClientEntry chl2 = NULL;
610 bool notify_sent = FALSE;
612 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
615 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
618 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
622 /* Get client entry */
623 if (id_type == SILC_ID_CLIENT) {
624 client = silc_idlist_find_client_by_id(server->global_list,
625 client_id, TRUE, &cache);
627 client = silc_idlist_find_client_by_id(server->local_list,
628 client_id, TRUE, &cache);
630 silc_free(client_id);
634 silc_free(client_id);
638 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
639 packet->dst_id_type);
644 /* Get channel entry */
645 channel = silc_idlist_find_channel_by_id(server->global_list,
648 channel = silc_idlist_find_channel_by_id(server->local_list,
651 silc_free(channel_id);
657 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
659 silc_free(channel_id);
663 SILC_GET32_MSB(mode, tmp);
665 /* Get target client */
666 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
669 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
673 /* Get client entry */
674 client2 = silc_idlist_find_client_by_id(server->global_list,
675 client_id, TRUE, NULL);
677 client2 = silc_idlist_find_client_by_id(server->local_list,
678 client_id, TRUE, NULL);
680 silc_free(client_id);
684 silc_free(client_id);
687 /* Check that sender is on channel */
688 if (!silc_server_client_on_channel(client, channel, &chl))
691 if (client != client2) {
692 /* Sender must be operator */
693 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
694 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
698 /* Check that target is on channel */
699 if (!silc_server_client_on_channel(client2, channel, &chl))
702 /* If target is founder mode change is not allowed. */
703 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
704 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
710 /* Get entry to the channel user list */
711 silc_hash_table_list(channel->user_list, &htl);
712 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
713 /* If the mode is channel founder and we already find a client
714 to have that mode on the channel we will enforce the sender
715 to change the channel founder mode away. There can be only one
716 channel founder on the channel. */
717 if (server->server_type == SILC_ROUTER &&
718 mode & SILC_CHANNEL_UMODE_CHANFO &&
719 chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
721 unsigned char cumode[4];
723 if (chl->client == client && chl->mode == mode) {
728 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
729 silc_server_send_notify_cumode(server, sock, FALSE, channel, mode,
730 client2->id, SILC_ID_CLIENT,
733 idp = silc_id_payload_encode(client2->id, SILC_ID_CLIENT);
734 SILC_PUT32_MSB(mode, cumode);
735 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
736 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
737 3, idp->data, idp->len,
739 idp->data, idp->len);
740 silc_buffer_free(idp);
743 /* Force the mode change if we alredy set the mode */
746 silc_free(channel_id);
747 silc_hash_table_list_reset(&htl);
752 if (chl->client == client2) {
753 if (chl->mode == mode) {
758 SILC_LOG_DEBUG(("Changing the channel user mode"));
760 /* Change the mode */
762 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
768 silc_hash_table_list_reset(&htl);
770 /* Send the same notify to the channel */
772 silc_server_packet_send_to_channel(server, sock, channel,
774 FALSE, packet->buffer->data,
775 packet->buffer->len, FALSE);
777 silc_free(channel_id);
781 case SILC_NOTIFY_TYPE_INVITE:
783 if (packet->dst_id_type == SILC_ID_CLIENT)
786 SILC_LOG_DEBUG(("INVITE notify"));
789 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
792 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
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, 3, &tmp_len);
813 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
817 /* Get client entry */
818 client = silc_idlist_find_client_by_id(server->global_list,
819 client_id, TRUE, &cache);
821 client = silc_idlist_find_client_by_id(server->local_list,
822 client_id, TRUE, &cache);
824 silc_free(client_id);
828 silc_free(client_id);
830 /* Get user's channel entry and check that inviting is allowed. */
831 if (!silc_server_client_on_channel(client, channel, &chl))
833 if (chl->mode == SILC_CHANNEL_UMODE_NONE &&
834 channel->mode & SILC_CHANNEL_MODE_INVITE) {
835 SILC_LOG_DEBUG(("Inviting is not allowed"));
839 /* Get the added invite */
840 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
842 if (!channel->invite_list)
843 channel->invite_list = silc_calloc(tmp_len + 2,
844 sizeof(*channel->invite_list));
846 channel->invite_list = silc_realloc(channel->invite_list,
847 sizeof(*channel->invite_list) *
849 strlen(channel->invite_list) +
851 if (tmp[tmp_len - 1] == ',')
852 tmp[tmp_len - 1] = '\0';
854 strncat(channel->invite_list, tmp, tmp_len);
855 strncat(channel->invite_list, ",", 1);
858 /* Get the deleted invite */
859 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
860 if (tmp && channel->invite_list) {
861 char *start, *end, *n;
863 if (!strncmp(channel->invite_list, tmp,
864 strlen(channel->invite_list) - 1)) {
865 silc_free(channel->invite_list);
866 channel->invite_list = NULL;
868 start = strstr(channel->invite_list, tmp);
869 if (start && strlen(start) >= tmp_len) {
870 end = start + tmp_len;
871 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
872 strncat(n, channel->invite_list, start - channel->invite_list);
873 strncat(n, end + 1, ((channel->invite_list +
874 strlen(channel->invite_list)) - end) - 1);
875 silc_free(channel->invite_list);
876 channel->invite_list = n;
883 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
885 * Distribute to the local clients on the channel and change the
889 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
891 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
894 /* Get the old Channel ID */
895 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
898 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
902 /* Get the channel entry */
903 channel = silc_idlist_find_channel_by_id(server->local_list,
906 channel = silc_idlist_find_channel_by_id(server->global_list,
909 silc_free(channel_id);
914 /* Send the notify to the channel */
915 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
916 FALSE, packet->buffer->data,
917 packet->buffer->len, FALSE);
919 /* Get the new Channel ID */
920 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
923 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
927 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
928 silc_id_render(channel_id, SILC_ID_CHANNEL)));
929 SILC_LOG_DEBUG(("New Channel ID id(%s)",
930 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
932 /* Replace the Channel ID */
933 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
935 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
937 silc_free(channel_id2);
942 SilcBuffer users = NULL, users_modes = NULL;
944 /* Re-announce this channel which ID was changed. */
945 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
947 silc_id_get_len(channel->id,
951 /* Re-announce our clients on the channel as the ID has changed now */
952 silc_server_announce_get_channel_users(server, channel, &users,
955 silc_buffer_push(users, users->data - users->head);
956 silc_server_packet_send(server, sock,
957 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
958 users->data, users->len, FALSE);
959 silc_buffer_free(users);
962 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
963 silc_server_packet_send_dest(server, sock,
964 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
965 channel->id, SILC_ID_CHANNEL,
967 users_modes->len, FALSE);
968 silc_buffer_free(users_modes);
971 /* Re-announce channel's topic */
972 if (channel->topic) {
973 silc_server_send_notify_topic_set(server, sock,
974 server->server_type == SILC_ROUTER ?
975 TRUE : FALSE, channel,
976 channel->id, SILC_ID_CHANNEL,
981 silc_free(channel_id);
985 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
987 * Remove the server entry and all clients that this server owns.
990 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
993 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
996 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1000 /* Get server entry */
1001 server_entry = silc_idlist_find_server_by_id(server->global_list,
1002 server_id, TRUE, NULL);
1004 if (!server_entry) {
1005 server_entry = silc_idlist_find_server_by_id(server->local_list,
1006 server_id, TRUE, NULL);
1008 if (!server_entry) {
1009 /* If we are normal server then we might not have the server. Check
1010 whether router was kind enough to send the list of all clients
1011 that actually was to be removed. Remove them if the list is
1013 if (server->server_type != SILC_ROUTER &&
1014 silc_argument_get_arg_num(args) > 1) {
1017 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1019 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1022 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1026 /* Get client entry */
1027 client = silc_idlist_find_client_by_id(server->global_list,
1028 client_id, TRUE, &cache);
1031 client = silc_idlist_find_client_by_id(server->local_list,
1032 client_id, TRUE, &cache);
1035 silc_free(client_id);
1039 silc_free(client_id);
1041 /* Update statistics */
1042 server->stat.clients--;
1043 if (server->stat.cell_clients)
1044 server->stat.cell_clients--;
1045 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1046 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1048 /* Remove the client from all channels. */
1049 silc_server_remove_from_channels(server, NULL, client,
1052 /* Remove the client */
1053 silc_idlist_del_client(local ? server->local_list :
1054 server->global_list, client);
1058 silc_free(server_id);
1062 silc_free(server_id);
1064 /* Free all client entries that this server owns as they will
1065 become invalid now as well. */
1066 silc_server_remove_clients_by_server(server, server_entry, TRUE);
1068 /* Remove the server entry */
1069 silc_idlist_del_server(local ? server->local_list :
1070 server->global_list, server_entry);
1072 /* XXX update statistics */
1076 case SILC_NOTIFY_TYPE_KICKED:
1078 * Distribute the notify to local clients on the channel
1081 SILC_LOG_DEBUG(("KICKED notify"));
1084 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1085 packet->dst_id_type);
1090 /* Get channel entry */
1091 channel = silc_idlist_find_channel_by_id(server->global_list,
1094 channel = silc_idlist_find_channel_by_id(server->local_list,
1097 silc_free(channel_id);
1101 silc_free(channel_id);
1104 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1107 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1111 /* If the the client is not in local list we check global list */
1112 client = silc_idlist_find_client_by_id(server->global_list,
1113 client_id, TRUE, NULL);
1115 client = silc_idlist_find_client_by_id(server->local_list,
1116 client_id, TRUE, NULL);
1118 silc_free(client_id);
1122 silc_free(client_id);
1124 /* If target is founder they cannot be kicked */
1125 if (!silc_server_client_on_channel(client, channel, &chl))
1127 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1130 /* From protocol version 1.1 we get the kicker's ID as well. */
1131 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1133 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1137 /* If the the client is not in local list we check global list */
1138 client2 = silc_idlist_find_client_by_id(server->global_list,
1139 client_id, TRUE, NULL);
1141 client2 = silc_idlist_find_client_by_id(server->local_list,
1142 client_id, TRUE, NULL);
1144 silc_free(client_id);
1148 silc_free(client_id);
1150 /* Kicker must be operator on channel */
1151 if (!silc_server_client_on_channel(client2, channel, &chl))
1153 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
1154 SILC_LOG_DEBUG(("Kicking is not allowed"));
1159 /* Send to channel */
1160 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1161 FALSE, packet->buffer->data,
1162 packet->buffer->len, FALSE);
1164 /* Remove the client from channel */
1165 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1169 case SILC_NOTIFY_TYPE_KILLED:
1172 * Distribute the notify to local clients on channels
1174 unsigned char *id, *comment;
1175 SilcUInt32 id_len, comment_len;
1177 SILC_LOG_DEBUG(("KILLED notify"));
1180 id = silc_argument_get_arg_type(args, 1, &id_len);
1183 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1187 /* If the the client is not in local list we check global list */
1188 client = silc_idlist_find_client_by_id(server->global_list,
1189 client_id, TRUE, NULL);
1191 client = silc_idlist_find_client_by_id(server->local_list,
1192 client_id, TRUE, NULL);
1194 silc_free(client_id);
1198 silc_free(client_id);
1200 /* If the client is one of ours, then close the connection to the
1201 client now. This removes the client from all channels as well. */
1202 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1203 sock = client->connection;
1204 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1205 silc_server_close_connection(server, sock);
1210 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1211 if (comment_len > 128)
1214 /* From protocol version 1.1 we get the killer's ID as well. */
1215 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1217 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1221 /* If the the client is not in local list we check global list */
1222 client2 = silc_idlist_find_client_by_id(server->global_list,
1223 client_id, TRUE, NULL);
1225 client2 = silc_idlist_find_client_by_id(server->local_list,
1226 client_id, TRUE, NULL);
1228 silc_free(client_id);
1232 silc_free(client_id);
1234 /* Killer must be router operator */
1235 if (!(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1236 SILC_LOG_DEBUG(("Killing is not allowed"));
1241 /* Send the notify to local clients on the channels except to the
1242 client who is killed. */
1243 silc_server_send_notify_on_channels(server, client, client,
1244 SILC_NOTIFY_TYPE_KILLED, 3,
1245 id, id_len, comment, comment_len,
1248 /* Remove the client from all channels */
1249 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1255 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1257 * Save the mode of the client.
1260 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1263 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1266 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1270 /* Get client entry */
1271 client = silc_idlist_find_client_by_id(server->global_list,
1272 client_id, TRUE, NULL);
1274 client = silc_idlist_find_client_by_id(server->local_list,
1275 client_id, TRUE, NULL);
1277 silc_free(client_id);
1281 silc_free(client_id);
1284 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1287 SILC_GET32_MSB(mode, tmp);
1289 /* Check that mode changing is allowed. */
1290 if (!silc_server_check_umode_rights(server, client, mode)) {
1291 SILC_LOG_DEBUG(("UMODE change is not allowed"));
1295 /* Change the mode */
1296 client->mode = mode;
1300 case SILC_NOTIFY_TYPE_BAN:
1305 SILC_LOG_DEBUG(("BAN notify"));
1307 /* Get Channel ID */
1308 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1311 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1315 /* Get channel entry */
1316 channel = silc_idlist_find_channel_by_id(server->global_list,
1319 channel = silc_idlist_find_channel_by_id(server->local_list,
1322 silc_free(channel_id);
1326 silc_free(channel_id);
1328 /* Get the new ban and add it to the ban list */
1329 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1331 if (!channel->ban_list)
1332 channel->ban_list = silc_calloc(tmp_len + 2,
1333 sizeof(*channel->ban_list));
1335 channel->ban_list = silc_realloc(channel->ban_list,
1336 sizeof(*channel->ban_list) *
1338 strlen(channel->ban_list) + 2));
1339 strncat(channel->ban_list, tmp, tmp_len);
1340 strncat(channel->ban_list, ",", 1);
1343 /* Get the ban to be removed and remove it from the list */
1344 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1345 if (tmp && channel->ban_list) {
1346 char *start, *end, *n;
1348 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1349 silc_free(channel->ban_list);
1350 channel->ban_list = NULL;
1352 start = strstr(channel->ban_list, tmp);
1353 if (start && strlen(start) >= tmp_len) {
1354 end = start + tmp_len;
1355 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1356 strncat(n, channel->ban_list, start - channel->ban_list);
1357 strncat(n, end + 1, ((channel->ban_list +
1358 strlen(channel->ban_list)) - end) - 1);
1359 silc_free(channel->ban_list);
1360 channel->ban_list = n;
1366 /* Ignore rest of the notify types for now */
1367 case SILC_NOTIFY_TYPE_NONE:
1368 case SILC_NOTIFY_TYPE_MOTD:
1375 silc_notify_payload_free(payload);
1378 void silc_server_notify_list(SilcServer server,
1379 SilcSocketConnection sock,
1380 SilcPacketContext *packet)
1382 SilcPacketContext *new;
1386 SILC_LOG_DEBUG(("Processing Notify List"));
1388 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1389 packet->src_id_type != SILC_ID_SERVER)
1392 /* Make copy of the original packet context, except for the actual
1393 data buffer, which we will here now fetch from the original buffer. */
1394 new = silc_packet_context_alloc();
1395 new->type = SILC_PACKET_NOTIFY;
1396 new->flags = packet->flags;
1397 new->src_id = packet->src_id;
1398 new->src_id_len = packet->src_id_len;
1399 new->src_id_type = packet->src_id_type;
1400 new->dst_id = packet->dst_id;
1401 new->dst_id_len = packet->dst_id_len;
1402 new->dst_id_type = packet->dst_id_type;
1404 buffer = silc_buffer_alloc(1024);
1405 new->buffer = buffer;
1407 while (packet->buffer->len) {
1408 SILC_GET16_MSB(len, packet->buffer->data + 2);
1409 if (len > packet->buffer->len)
1412 if (len > buffer->truelen) {
1413 silc_buffer_free(buffer);
1414 buffer = silc_buffer_alloc(1024 + len);
1417 silc_buffer_pull_tail(buffer, len);
1418 silc_buffer_put(buffer, packet->buffer->data, len);
1420 /* Process the Notify */
1421 silc_server_notify(server, sock, new);
1423 silc_buffer_push_tail(buffer, len);
1424 silc_buffer_pull(packet->buffer, len);
1427 silc_buffer_free(buffer);
1431 /* Received private message. This resolves the destination of the message
1432 and sends the packet. This is used by both server and router. If the
1433 destination is our locally connected client this sends the packet to
1434 the client. This may also send the message for further routing if
1435 the destination is not in our server (or router). */
1437 void silc_server_private_message(SilcServer server,
1438 SilcSocketConnection sock,
1439 SilcPacketContext *packet)
1441 SilcSocketConnection dst_sock;
1442 SilcIDListData idata;
1443 SilcClientEntry client;
1445 SILC_LOG_DEBUG(("Start"));
1447 if (packet->src_id_type != SILC_ID_CLIENT ||
1448 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1451 /* Get the route to the client */
1452 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1453 packet->dst_id_len, NULL,
1456 /* Send IDENTIFY command reply with error status to indicate that
1457 such destination ID does not exist or is invalid */
1458 SilcBuffer idp = silc_id_payload_encode_data(packet->dst_id,
1460 packet->dst_id_type);
1464 if (packet->src_id_type == SILC_ID_CLIENT) {
1465 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1467 packet->src_id_type);
1468 silc_server_send_dest_command_reply(server, sock,
1469 client_id, SILC_ID_CLIENT,
1470 SILC_COMMAND_IDENTIFY,
1471 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1472 0, 0, 1, 2, idp->data, idp->len);
1473 silc_free(client_id);
1475 silc_server_send_command_reply(server, sock, SILC_COMMAND_IDENTIFY,
1476 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID, 0,
1477 0, 1, 2, idp->data, idp->len);
1480 silc_buffer_free(idp);
1484 /* Check whether destination client wishes to receive private messages */
1485 if (client && !(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) &&
1486 client->mode & SILC_UMODE_BLOCK_PRIVMSG) {
1487 SILC_LOG_DEBUG(("Client blocks private messages, discarding packet"));
1491 /* Send the private message */
1492 silc_server_send_private_message(server, dst_sock, idata->send_key,
1493 idata->hmac_send, idata->psn_send++,
1497 /* Received private message key packet.. This packet is never for us. It is to
1498 the client in the packet's destination ID. Sending of this sort of packet
1499 equals sending private message, ie. it is sent point to point from
1500 one client to another. */
1502 void silc_server_private_message_key(SilcServer server,
1503 SilcSocketConnection sock,
1504 SilcPacketContext *packet)
1506 SilcSocketConnection dst_sock;
1507 SilcIDListData idata;
1509 SILC_LOG_DEBUG(("Start"));
1511 if (packet->src_id_type != SILC_ID_CLIENT ||
1512 packet->dst_id_type != SILC_ID_CLIENT)
1515 if (!packet->dst_id)
1518 /* Get the route to the client */
1519 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1520 packet->dst_id_len, NULL,
1525 /* Relay the packet */
1526 silc_server_relay_packet(server, dst_sock, idata->send_key,
1527 idata->hmac_send, idata->psn_send++, packet, FALSE);
1530 /* Processes incoming command reply packet. The command reply packet may
1531 be destined to one of our clients or it may directly for us. We will
1532 call the command reply routine after processing the packet. */
1534 void silc_server_command_reply(SilcServer server,
1535 SilcSocketConnection sock,
1536 SilcPacketContext *packet)
1538 SilcBuffer buffer = packet->buffer;
1539 SilcClientEntry client = NULL;
1540 SilcSocketConnection dst_sock;
1541 SilcIDListData idata;
1542 SilcClientID *id = NULL;
1544 SILC_LOG_DEBUG(("Start"));
1546 /* Source must be server or router */
1547 if (packet->src_id_type != SILC_ID_SERVER &&
1548 sock->type != SILC_SOCKET_TYPE_ROUTER)
1551 if (packet->dst_id_type == SILC_ID_CHANNEL)
1554 if (packet->dst_id_type == SILC_ID_CLIENT) {
1555 /* Destination must be one of ours */
1556 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1559 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1561 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1567 if (packet->dst_id_type == SILC_ID_SERVER) {
1568 /* For now this must be for us */
1569 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1570 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1575 /* Execute command reply locally for the command */
1576 silc_server_command_reply_process(server, sock, buffer);
1578 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1579 /* Relay the packet to the client */
1580 const SilcBufferStruct p;
1582 dst_sock = (SilcSocketConnection)client->connection;
1583 idata = (SilcIDListData)client;
1585 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1586 + packet->dst_id_len + packet->padlen);
1587 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1588 idata->hmac_send, (const SilcBuffer)&p)) {
1589 SILC_LOG_ERROR(("Cannot send packet"));
1592 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1594 /* Encrypt packet */
1595 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1596 (SilcBuffer)&p, buffer->len);
1598 /* Send the packet */
1599 silc_server_packet_send_real(server, dst_sock, TRUE);
1605 /* Process received channel message. The message can be originated from
1606 client or server. */
1608 void silc_server_channel_message(SilcServer server,
1609 SilcSocketConnection sock,
1610 SilcPacketContext *packet)
1612 SilcChannelEntry channel = NULL;
1613 SilcChannelID *id = NULL;
1614 void *sender_id = NULL;
1615 SilcClientEntry sender_entry = NULL;
1616 SilcChannelClientEntry chl;
1619 SILC_LOG_DEBUG(("Processing channel message"));
1622 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1623 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1627 /* Find channel entry */
1628 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1631 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1633 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1635 SILC_LOG_DEBUG(("Could not find channel"));
1640 /* See that this client is on the channel. If the original sender is
1641 not client (as it can be server as well) we don't do the check. */
1642 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1643 packet->src_id_type);
1646 if (packet->src_id_type == SILC_ID_CLIENT) {
1647 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1648 sender_id, TRUE, NULL);
1649 if (!sender_entry) {
1651 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1652 sender_id, TRUE, NULL);
1654 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1656 SILC_LOG_DEBUG(("Client not on channel"));
1660 /* If channel is moderated check that client is allowed to send
1662 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS && !chl->mode) {
1663 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
1666 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
1667 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
1668 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1669 SILC_LOG_DEBUG(("Channel is silenced from operators"));
1673 /* If the packet is coming from router, but the client entry is local
1674 entry to us then some router is rerouting this to us and it is not
1675 allowed. When the client is local to us it means that we've routed
1676 this packet to network, and now someone is routing it back to us. */
1677 if (server->server_type == SILC_ROUTER &&
1678 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1679 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1684 /* Distribute the packet to our local clients. This will send the
1685 packet for further routing as well, if needed. */
1686 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
1687 packet->src_id_type, sender_entry,
1688 packet->buffer->data,
1689 packet->buffer->len, FALSE);
1692 silc_free(sender_id);
1696 /* Received channel key packet. We distribute the key to all of our locally
1697 connected clients on the channel. */
1699 void silc_server_channel_key(SilcServer server,
1700 SilcSocketConnection sock,
1701 SilcPacketContext *packet)
1703 SilcBuffer buffer = packet->buffer;
1704 SilcChannelEntry channel;
1706 if (packet->src_id_type != SILC_ID_SERVER ||
1707 (server->server_type == SILC_ROUTER &&
1708 sock->type == SILC_SOCKET_TYPE_ROUTER))
1711 /* Save the channel key */
1712 channel = silc_server_save_channel_key(server, buffer, NULL);
1716 /* Distribute the key to everybody who is on the channel. If we are router
1717 we will also send it to locally connected servers. */
1718 silc_server_send_channel_key(server, sock, channel, FALSE);
1720 if (server->server_type != SILC_BACKUP_ROUTER) {
1721 /* Distribute to local cell backup routers. */
1722 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1723 SILC_PACKET_CHANNEL_KEY, 0,
1724 buffer->data, buffer->len, FALSE, TRUE);
1728 /* Received New Client packet and processes it. Creates Client ID for the
1729 client. Client becomes registered after calling this functions. */
1731 SilcClientEntry silc_server_new_client(SilcServer server,
1732 SilcSocketConnection sock,
1733 SilcPacketContext *packet)
1735 SilcBuffer buffer = packet->buffer;
1736 SilcClientEntry client;
1737 SilcClientID *client_id;
1739 SilcIDListData idata;
1740 char *username = NULL, *realname = NULL, *id_string;
1741 SilcUInt16 username_len;
1744 char *hostname, *nickname;
1747 SILC_LOG_DEBUG(("Creating new client"));
1749 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1752 /* Take client entry */
1753 client = (SilcClientEntry)sock->user_data;
1754 idata = (SilcIDListData)client;
1756 /* Remove the old cache entry. */
1757 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1758 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
1759 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1760 "You have not been authenticated");
1764 /* Parse incoming packet */
1765 ret = silc_buffer_unformat(buffer,
1766 SILC_STR_UI16_NSTRING_ALLOC(&username,
1768 SILC_STR_UI16_STRING_ALLOC(&realname),
1771 silc_free(username);
1772 silc_free(realname);
1773 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1774 "connection", sock->hostname, sock->ip));
1775 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1776 "Incomplete client information");
1781 silc_free(username);
1782 silc_free(realname);
1783 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
1784 "connection", sock->hostname, sock->ip));
1785 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1786 "Incomplete client information");
1790 if (username_len > 128)
1791 username[128] = '\0';
1793 /* Check for bad characters for nickname, and modify the nickname if
1794 it includes those. */
1795 if (silc_server_name_bad_chars(username, username_len)) {
1796 nickname = silc_server_name_modify_bad(username, username_len);
1798 nickname = strdup(username);
1801 /* Make sanity checks for the hostname of the client. If the hostname
1802 is provided in the `username' check that it is the same than the
1803 resolved hostname, or if not resolved the hostname that appears in
1804 the client's public key. If the hostname is not present then put
1805 it from the resolved name or from the public key. */
1806 if (strchr(username, '@')) {
1807 SilcPublicKeyIdentifier pident;
1808 int tlen = strcspn(username, "@");
1809 char *phostname = NULL;
1811 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
1813 if (strcmp(sock->hostname, sock->ip) &&
1814 strcmp(sock->hostname, hostname)) {
1815 silc_free(username);
1816 silc_free(hostname);
1817 silc_free(realname);
1818 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1819 "connection", sock->hostname, sock->ip));
1820 silc_server_disconnect_remote(server, sock,
1821 "Server closed connection: "
1822 "Incomplete client information");
1826 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1828 phostname = strdup(pident->host);
1829 silc_pkcs_free_identifier(pident);
1832 if (!strcmp(sock->hostname, sock->ip) &&
1833 phostname && strcmp(phostname, hostname)) {
1834 silc_free(username);
1835 silc_free(hostname);
1836 silc_free(phostname);
1837 silc_free(realname);
1838 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1839 "connection", sock->hostname, sock->ip));
1840 silc_server_disconnect_remote(server, sock,
1841 "Server closed connection: "
1842 "Incomplete client information");
1846 silc_free(phostname);
1848 /* The hostname is not present, add it. */
1850 /* XXX For now we cannot take the host name from the public key since
1851 they are not trusted or we cannot verify them as trusted. Just take
1852 what the resolved name or address is. */
1854 if (strcmp(sock->hostname, sock->ip)) {
1856 newusername = silc_calloc(strlen(username) +
1857 strlen(sock->hostname) + 2,
1858 sizeof(*newusername));
1859 strncat(newusername, username, strlen(username));
1860 strncat(newusername, "@", 1);
1861 strncat(newusername, sock->hostname, strlen(sock->hostname));
1862 silc_free(username);
1863 username = newusername;
1866 SilcPublicKeyIdentifier pident =
1867 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1870 newusername = silc_calloc(strlen(username) +
1871 strlen(pident->host) + 2,
1872 sizeof(*newusername));
1873 strncat(newusername, username, strlen(username));
1874 strncat(newusername, "@", 1);
1875 strncat(newusername, pident->host, strlen(pident->host));
1876 silc_free(username);
1877 username = newusername;
1878 silc_pkcs_free_identifier(pident);
1884 /* Create Client ID */
1885 while (!silc_id_create_client_id(server, server->id, server->rng,
1886 server->md5hash, nickname, &client_id)) {
1888 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
1891 /* Update client entry */
1892 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1893 client->nickname = nickname;
1894 client->username = username;
1895 client->userinfo = realname ? realname : strdup(" ");
1896 client->id = client_id;
1897 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1899 /* Add the client again to the ID cache */
1900 silc_idcache_add(server->local_list->clients, client->nickname,
1901 client_id, client, 0, NULL);
1903 /* Notify our router about new client on the SILC network */
1904 if (!server->standalone)
1905 silc_server_send_new_id(server, (SilcSocketConnection)
1906 server->router->connection,
1907 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1908 client->id, SILC_ID_CLIENT, id_len);
1910 /* Send the new client ID to the client. */
1911 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1912 reply = silc_buffer_alloc(2 + 2 + id_len);
1913 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1914 silc_buffer_format(reply,
1915 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1916 SILC_STR_UI_SHORT(id_len),
1917 SILC_STR_UI_XNSTRING(id_string, id_len),
1919 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1920 reply->data, reply->len, FALSE);
1921 silc_free(id_string);
1922 silc_buffer_free(reply);
1924 /* Send some nice info to the client */
1925 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1926 ("Welcome to the SILC Network %s",
1928 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1929 ("Your host is %s, running version %s",
1930 server->server_name, server_version));
1932 if (server->stat.clients && server->stat.servers + 1)
1933 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1934 ("There are %d clients on %d servers in SILC "
1935 "Network", server->stat.clients,
1936 server->stat.servers + 1));
1937 if (server->stat.cell_clients && server->stat.cell_servers + 1)
1938 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1939 ("There are %d clients on %d server in our cell",
1940 server->stat.cell_clients,
1941 server->stat.cell_servers + 1));
1942 if (server->server_type == SILC_ROUTER) {
1943 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1944 ("I have %d clients, %d channels, %d servers and "
1946 server->stat.my_clients,
1947 server->stat.my_channels,
1948 server->stat.my_servers,
1949 server->stat.my_routers));
1951 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1952 ("I have %d clients and %d channels formed",
1953 server->stat.my_clients,
1954 server->stat.my_channels));
1957 if (server->stat.server_ops || server->stat.router_ops)
1958 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1959 ("There are %d server operators and %d router "
1961 server->stat.server_ops,
1962 server->stat.router_ops));
1963 if (server->stat.my_router_ops + server->stat.my_server_ops)
1964 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1965 ("I have %d operators online",
1966 server->stat.my_router_ops +
1967 server->stat.my_server_ops));
1969 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1970 ("Your connection is secured with %s cipher, "
1971 "key length %d bits",
1972 idata->send_key->cipher->name,
1973 idata->send_key->cipher->key_len));
1974 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1975 ("Your current nickname is %s",
1979 silc_server_send_motd(server, sock);
1984 /* Create new server. This processes received New Server packet and
1985 saves the received Server ID. The server is our locally connected
1986 server thus we save all the information and save it to local list.
1987 This funtion can be used by both normal server and router server.
1988 If normal server uses this it means that its router has connected
1989 to the server. If router uses this it means that one of the cell's
1990 servers is connected to the router. */
1992 SilcServerEntry silc_server_new_server(SilcServer server,
1993 SilcSocketConnection sock,
1994 SilcPacketContext *packet)
1996 SilcBuffer buffer = packet->buffer;
1997 SilcServerEntry new_server, server_entry;
1998 SilcServerID *server_id;
1999 SilcIDListData idata;
2000 unsigned char *server_name, *id_string;
2001 SilcUInt16 id_len, name_len;
2005 SILC_LOG_DEBUG(("Creating new server"));
2007 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
2008 sock->type != SILC_SOCKET_TYPE_ROUTER)
2011 /* Take server entry */
2012 new_server = (SilcServerEntry)sock->user_data;
2013 idata = (SilcIDListData)new_server;
2015 /* Remove the old cache entry */
2016 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2017 if (!silc_idcache_del_by_context(server->global_list->servers,
2019 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2020 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2021 "server" : "router")));
2022 silc_server_disconnect_remote(server, sock, "Server closed connection: "
2023 "You have not been authenticated");
2029 /* Parse the incoming packet */
2030 ret = silc_buffer_unformat(buffer,
2031 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2032 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2037 silc_free(id_string);
2039 silc_free(server_name);
2043 if (id_len > buffer->len) {
2044 silc_free(id_string);
2045 silc_free(server_name);
2050 server_name[255] = '\0';
2053 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2055 silc_free(id_string);
2056 silc_free(server_name);
2059 silc_free(id_string);
2061 /* Check for valid server ID */
2062 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2063 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2064 sock->ip, sock->hostname));
2065 silc_server_disconnect_remote(server, sock, "Server closed connection: "
2066 "Your Server ID is not valid");
2067 silc_free(server_name);
2071 /* Check that we do not have this ID already */
2072 server_entry = silc_idlist_find_server_by_id(server->local_list,
2073 server_id, TRUE, NULL);
2075 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2077 server_entry = silc_idlist_find_server_by_id(server->global_list,
2078 server_id, TRUE, NULL);
2080 silc_idcache_del_by_context(server->global_list->servers, server_entry);
2083 /* Update server entry */
2084 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2085 new_server->server_name = server_name;
2086 new_server->id = server_id;
2088 SILC_LOG_DEBUG(("New server id(%s)",
2089 silc_id_render(server_id, SILC_ID_SERVER)));
2091 /* Add again the entry to the ID cache. */
2092 silc_idcache_add(local ? server->local_list->servers :
2093 server->global_list->servers, server_name, server_id,
2094 new_server, 0, NULL);
2096 /* Distribute the information about new server in the SILC network
2097 to our router. If we are normal server we won't send anything
2098 since this connection must be our router connection. */
2099 if (server->server_type == SILC_ROUTER && !server->standalone &&
2100 server->router->connection != sock)
2101 silc_server_send_new_id(server, server->router->connection,
2102 TRUE, new_server->id, SILC_ID_SERVER,
2103 silc_id_get_len(server_id, SILC_ID_SERVER));
2105 if (server->server_type == SILC_ROUTER)
2106 server->stat.cell_servers++;
2108 /* Check whether this router connection has been replaced by an
2109 backup router. If it has been then we'll disable the server and will
2110 ignore everything it will send until the backup router resuming
2111 protocol has been completed. */
2112 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2113 silc_server_backup_replaced_get(server, server_id, NULL)) {
2114 /* Send packet to the server indicating that it cannot use this
2115 connection as it has been replaced by backup router. */
2116 SilcBuffer packet = silc_buffer_alloc(2);
2117 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2118 silc_buffer_format(packet,
2119 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2120 SILC_STR_UI_CHAR(0),
2122 silc_server_packet_send(server, sock,
2123 SILC_PACKET_RESUME_ROUTER, 0,
2124 packet->data, packet->len, TRUE);
2125 silc_buffer_free(packet);
2127 /* Mark the router disabled. The data sent earlier will go but nothing
2128 after this does not go to this connection. */
2129 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2131 /* If it is router announce our stuff to it. */
2132 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2133 server->server_type == SILC_ROUTER) {
2134 silc_server_announce_servers(server, FALSE, 0, sock);
2135 silc_server_announce_clients(server, 0, sock);
2136 silc_server_announce_channels(server, 0, sock);
2143 /* Processes incoming New ID packet. New ID Payload is used to distribute
2144 information about newly registered clients and servers. */
2146 static void silc_server_new_id_real(SilcServer server,
2147 SilcSocketConnection sock,
2148 SilcPacketContext *packet,
2151 SilcBuffer buffer = packet->buffer;
2153 SilcServerEntry router, server_entry;
2154 SilcSocketConnection router_sock;
2159 SILC_LOG_DEBUG(("Processing new ID"));
2161 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2162 server->server_type == SILC_SERVER ||
2163 packet->src_id_type != SILC_ID_SERVER)
2166 idp = silc_id_payload_parse(buffer->data, buffer->len);
2170 id_type = silc_id_payload_get_type(idp);
2172 /* Normal server cannot have other normal server connections */
2173 server_entry = (SilcServerEntry)sock->user_data;
2174 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2175 server_entry->server_type == SILC_SERVER)
2178 id = silc_id_payload_get_id(idp);
2182 /* If the packet is coming from server then use the sender as the
2183 origin of the the packet. If it came from router then check the real
2184 sender of the packet and use that as the origin. */
2185 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2186 id_list = server->local_list;
2188 router = sock->user_data;
2190 /* If the sender is backup router and ID is server (and we are not
2191 backup router) then switch the entry to global list. */
2192 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2193 id_type == SILC_ID_SERVER &&
2194 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2195 id_list = server->global_list;
2196 router_sock = server->router ? server->router->connection : sock;
2199 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2200 packet->src_id_type);
2201 router = silc_idlist_find_server_by_id(server->global_list,
2202 sender_id, TRUE, NULL);
2204 router = silc_idlist_find_server_by_id(server->local_list,
2205 sender_id, TRUE, NULL);
2206 silc_free(sender_id);
2208 id_list = server->global_list;
2215 case SILC_ID_CLIENT:
2217 SilcClientEntry entry;
2219 /* Check that we do not have this client already */
2220 entry = silc_idlist_find_client_by_id(server->global_list,
2221 id, server->server_type,
2224 entry = silc_idlist_find_client_by_id(server->local_list,
2225 id, server->server_type,
2228 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2232 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2233 silc_id_render(id, SILC_ID_CLIENT),
2234 sock->type == SILC_SOCKET_TYPE_SERVER ?
2235 "Server" : "Router", sock->hostname));
2237 /* As a router we keep information of all global information in our
2238 global list. Cell wide information however is kept in the local
2240 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2241 id, router, NULL, 0);
2243 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2245 /* Inform the sender that the ID is not usable */
2246 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2249 entry->nickname = NULL;
2250 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2252 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2253 server->stat.cell_clients++;
2254 server->stat.clients++;
2258 case SILC_ID_SERVER:
2260 SilcServerEntry entry;
2262 /* If the ID is mine, ignore it. */
2263 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2264 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2268 /* If the ID is the sender's ID, ignore it (we have it already) */
2269 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2270 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2274 /* Check that we do not have this server already */
2275 entry = silc_idlist_find_server_by_id(server->global_list,
2276 id, server->server_type,
2279 entry = silc_idlist_find_server_by_id(server->local_list,
2280 id, server->server_type,
2283 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2287 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2288 silc_id_render(id, SILC_ID_SERVER),
2289 sock->type == SILC_SOCKET_TYPE_SERVER ?
2290 "Server" : "Router", sock->hostname));
2292 /* As a router we keep information of all global information in our
2293 global list. Cell wide information however is kept in the local
2295 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2298 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2301 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2303 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2304 server->stat.cell_servers++;
2305 server->stat.servers++;
2309 case SILC_ID_CHANNEL:
2310 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2319 /* If the sender of this packet is server and we are router we need to
2320 broadcast this packet to other routers in the network. */
2321 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2322 sock->type == SILC_SOCKET_TYPE_SERVER &&
2323 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2324 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2325 silc_server_packet_send(server, server->router->connection,
2327 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2328 buffer->data, buffer->len, FALSE);
2329 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2330 packet->type, packet->flags,
2331 packet->buffer->data, packet->buffer->len,
2336 silc_id_payload_free(idp);
2340 /* Processes incoming New ID packet. New ID Payload is used to distribute
2341 information about newly registered clients and servers. */
2343 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2344 SilcPacketContext *packet)
2346 silc_server_new_id_real(server, sock, packet, TRUE);
2349 /* Receoved New Id List packet, list of New ID payloads inside one
2350 packet. Process the New ID payloads one by one. */
2352 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2353 SilcPacketContext *packet)
2355 SilcPacketContext *new_id;
2359 SILC_LOG_DEBUG(("Processing New ID List"));
2361 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2362 packet->src_id_type != SILC_ID_SERVER)
2365 /* If the sender of this packet is server and we are router we need to
2366 broadcast this packet to other routers in the network. Broadcast
2367 this list packet instead of multiple New ID packets. */
2368 if (!server->standalone && server->server_type == SILC_ROUTER &&
2369 sock->type == SILC_SOCKET_TYPE_SERVER &&
2370 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2371 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2372 silc_server_packet_send(server, server->router->connection,
2374 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2375 packet->buffer->data, packet->buffer->len, FALSE);
2376 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2377 packet->type, packet->flags,
2378 packet->buffer->data, packet->buffer->len,
2382 /* Make copy of the original packet context, except for the actual
2383 data buffer, which we will here now fetch from the original buffer. */
2384 new_id = silc_packet_context_alloc();
2385 new_id->type = SILC_PACKET_NEW_ID;
2386 new_id->flags = packet->flags;
2387 new_id->src_id = packet->src_id;
2388 new_id->src_id_len = packet->src_id_len;
2389 new_id->src_id_type = packet->src_id_type;
2390 new_id->dst_id = packet->dst_id;
2391 new_id->dst_id_len = packet->dst_id_len;
2392 new_id->dst_id_type = packet->dst_id_type;
2394 idp = silc_buffer_alloc(256);
2395 new_id->buffer = idp;
2397 while (packet->buffer->len) {
2398 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2399 if ((id_len > packet->buffer->len) ||
2400 (id_len > idp->truelen))
2403 silc_buffer_pull_tail(idp, 4 + id_len);
2404 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2406 /* Process the New ID */
2407 silc_server_new_id_real(server, sock, new_id, FALSE);
2409 silc_buffer_push_tail(idp, 4 + id_len);
2410 silc_buffer_pull(packet->buffer, 4 + id_len);
2413 silc_buffer_free(idp);
2417 /* Received New Channel packet. Information about new channels in the
2418 network are distributed using this packet. Save the information about
2419 the new channel. This usually comes from router but also normal server
2420 can send this to notify channels it has when it connects to us. */
2422 void silc_server_new_channel(SilcServer server,
2423 SilcSocketConnection sock,
2424 SilcPacketContext *packet)
2426 SilcChannelPayload payload;
2427 SilcChannelID *channel_id;
2429 SilcUInt32 name_len;
2433 SilcServerEntry server_entry;
2434 SilcChannelEntry channel;
2436 SILC_LOG_DEBUG(("Processing New Channel"));
2438 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2439 packet->src_id_type != SILC_ID_SERVER ||
2440 server->server_type == SILC_SERVER)
2443 /* Parse the channel payload */
2444 payload = silc_channel_payload_parse(packet->buffer->data,
2445 packet->buffer->len);
2449 /* Get the channel ID */
2450 channel_id = silc_channel_get_id_parse(payload);
2452 silc_channel_payload_free(payload);
2456 channel_name = silc_channel_get_name(payload, &name_len);
2458 channel_name[255] = '\0';
2460 id = silc_channel_get_id(payload, &id_len);
2462 server_entry = (SilcServerEntry)sock->user_data;
2464 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2465 /* Add the channel to global list as it is coming from router. It
2466 cannot be our own channel as it is coming from router. */
2468 /* Check that we don't already have this channel */
2469 channel = silc_idlist_find_channel_by_name(server->local_list,
2470 channel_name, NULL);
2472 channel = silc_idlist_find_channel_by_name(server->global_list,
2473 channel_name, NULL);
2475 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2476 silc_id_render(channel_id, SILC_ID_CHANNEL),
2479 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2480 0, channel_id, sock->user_data, NULL, NULL, 0);
2481 server->stat.channels++;
2484 /* The channel is coming from our server, thus it is in our cell
2485 we will add it to our local list. */
2488 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2489 silc_id_render(channel_id, SILC_ID_CHANNEL),
2492 /* Check that we don't already have this channel */
2493 channel = silc_idlist_find_channel_by_name(server->local_list,
2494 channel_name, NULL);
2496 channel = silc_idlist_find_channel_by_name(server->global_list,
2497 channel_name, NULL);
2499 /* If the channel does not exist, then create it. This creates a new
2500 key to the channel as well that we will send to the server. */
2502 /* The protocol says that the Channel ID's IP address must be based
2503 on the router's IP address. Check whether the ID is based in our
2504 IP and if it is not then create a new ID and enforce the server
2505 to switch the ID. */
2506 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2507 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2509 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2511 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2512 silc_server_send_notify_channel_change(server, sock, FALSE,
2514 silc_free(channel_id);
2519 /* Create the channel with the provided Channel ID */
2520 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2524 silc_channel_payload_free(payload);
2525 silc_free(channel_id);
2529 /* Get the mode and set it to the channel */
2530 channel->mode = silc_channel_get_mode(payload);
2532 /* Send the new channel key to the server */
2533 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2534 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2535 chk = silc_channel_key_payload_encode(id_len, id,
2536 strlen(channel->channel_key->
2538 channel->channel_key->cipher->name,
2539 channel->key_len / 8,
2541 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2542 chk->data, chk->len, FALSE);
2543 silc_buffer_free(chk);
2546 /* The channel exist by that name, check whether the ID's match.
2547 If they don't then we'll force the server to use the ID we have.
2548 We also create a new key for the channel. */
2549 SilcBuffer users = NULL, users_modes = NULL;
2551 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2552 /* They don't match, send CHANNEL_CHANGE notify to the server to
2553 force the ID change. */
2554 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2555 silc_server_send_notify_channel_change(server, sock, FALSE,
2556 channel_id, channel->id);
2559 /* If the mode is different from what we have then enforce the
2561 mode = silc_channel_get_mode(payload);
2562 if (channel->mode != mode) {
2563 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2564 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2565 channel->mode, server->id,
2567 channel->cipher, channel->hmac_name,
2568 channel->passphrase);
2571 /* Create new key for the channel and send it to the server and
2572 everybody else possibly on the channel. */
2574 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2575 if (!silc_server_create_channel_key(server, channel, 0))
2578 /* Send to the channel */
2579 silc_server_send_channel_key(server, sock, channel, FALSE);
2580 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2581 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2583 /* Send to the server */
2584 chk = silc_channel_key_payload_encode(id_len, id,
2585 strlen(channel->channel_key->
2587 channel->channel_key->
2589 channel->key_len / 8,
2591 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2592 chk->data, chk->len, FALSE);
2593 silc_buffer_free(chk);
2597 silc_free(channel_id);
2599 /* Since the channel is coming from server and we also know about it
2600 then send the JOIN notify to the server so that it see's our
2601 users on the channel "joining" the channel. */
2602 silc_server_announce_get_channel_users(server, channel, &users,
2605 silc_buffer_push(users, users->data - users->head);
2606 silc_server_packet_send(server, sock,
2607 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2608 users->data, users->len, FALSE);
2609 silc_buffer_free(users);
2612 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2613 silc_server_packet_send_dest(server, sock,
2614 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2615 channel->id, SILC_ID_CHANNEL,
2617 users_modes->len, FALSE);
2618 silc_buffer_free(users_modes);
2623 silc_channel_payload_free(payload);
2626 /* Received New Channel List packet, list of New Channel List payloads inside
2627 one packet. Process the New Channel payloads one by one. */
2629 void silc_server_new_channel_list(SilcServer server,
2630 SilcSocketConnection sock,
2631 SilcPacketContext *packet)
2633 SilcPacketContext *new;
2635 SilcUInt16 len1, len2;
2637 SILC_LOG_DEBUG(("Processing New Channel List"));
2639 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2640 packet->src_id_type != SILC_ID_SERVER ||
2641 server->server_type == SILC_SERVER)
2644 /* If the sender of this packet is server and we are router we need to
2645 broadcast this packet to other routers in the network. Broadcast
2646 this list packet instead of multiple New Channel packets. */
2647 if (!server->standalone && server->server_type == SILC_ROUTER &&
2648 sock->type == SILC_SOCKET_TYPE_SERVER &&
2649 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2650 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2651 silc_server_packet_send(server, server->router->connection,
2653 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2654 packet->buffer->data, packet->buffer->len, FALSE);
2655 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2656 packet->type, packet->flags,
2657 packet->buffer->data, packet->buffer->len,
2661 /* Make copy of the original packet context, except for the actual
2662 data buffer, which we will here now fetch from the original buffer. */
2663 new = silc_packet_context_alloc();
2664 new->type = SILC_PACKET_NEW_CHANNEL;
2665 new->flags = packet->flags;
2666 new->src_id = packet->src_id;
2667 new->src_id_len = packet->src_id_len;
2668 new->src_id_type = packet->src_id_type;
2669 new->dst_id = packet->dst_id;
2670 new->dst_id_len = packet->dst_id_len;
2671 new->dst_id_type = packet->dst_id_type;
2673 buffer = silc_buffer_alloc(512);
2674 new->buffer = buffer;
2676 while (packet->buffer->len) {
2677 SILC_GET16_MSB(len1, packet->buffer->data);
2678 if ((len1 > packet->buffer->len) ||
2679 (len1 > buffer->truelen))
2682 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2683 if ((len2 > packet->buffer->len) ||
2684 (len2 > buffer->truelen))
2687 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2688 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2690 /* Process the New Channel */
2691 silc_server_new_channel(server, sock, new);
2693 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2694 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2697 silc_buffer_free(buffer);
2701 /* Received key agreement packet. This packet is never for us. It is to
2702 the client in the packet's destination ID. Sending of this sort of packet
2703 equals sending private message, ie. it is sent point to point from
2704 one client to another. */
2706 void silc_server_key_agreement(SilcServer server,
2707 SilcSocketConnection sock,
2708 SilcPacketContext *packet)
2710 SilcSocketConnection dst_sock;
2711 SilcIDListData idata;
2713 SILC_LOG_DEBUG(("Start"));
2715 if (packet->src_id_type != SILC_ID_CLIENT ||
2716 packet->dst_id_type != SILC_ID_CLIENT)
2719 if (!packet->dst_id)
2722 /* Get the route to the client */
2723 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2724 packet->dst_id_len, NULL,
2729 /* Relay the packet */
2730 silc_server_relay_packet(server, dst_sock, idata->send_key,
2731 idata->hmac_send, idata->psn_send++,
2735 /* Received connection auth request packet that is used during connection
2736 phase to resolve the mandatory authentication method. This packet can
2737 actually be received at anytime but usually it is used only during
2738 the connection authentication phase. Now, protocol says that this packet
2739 can come from client or server, however, we support only this coming
2740 from client and expect that server always knows what authentication
2743 void silc_server_connection_auth_request(SilcServer server,
2744 SilcSocketConnection sock,
2745 SilcPacketContext *packet)
2747 SilcServerConfigClient *client = NULL;
2748 SilcUInt16 conn_type;
2750 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
2752 SILC_LOG_DEBUG(("Start"));
2754 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2757 /* Parse the payload */
2758 ret = silc_buffer_unformat(packet->buffer,
2759 SILC_STR_UI_SHORT(&conn_type),
2760 SILC_STR_UI_SHORT(NULL),
2765 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2768 /* Get the authentication method for the client */
2769 auth_meth = SILC_AUTH_NONE;
2770 client = silc_server_config_find_client(server, sock->ip);
2772 client = silc_server_config_find_client(server, sock->hostname);
2774 if (client->passphrase) {
2775 if (client->publickeys && !server->config->prefer_passphrase_auth)
2776 auth_meth = SILC_AUTH_PUBLIC_KEY;
2778 auth_meth = SILC_AUTH_PASSWORD;
2779 } else if (client->publickeys)
2780 auth_meth = SILC_AUTH_PUBLIC_KEY;
2783 /* Send it back to the client */
2784 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
2787 /* Received REKEY packet. The sender of the packet wants to regenerate
2788 its session keys. This starts the REKEY protocol. */
2790 void silc_server_rekey(SilcServer server,
2791 SilcSocketConnection sock,
2792 SilcPacketContext *packet)
2794 SilcProtocol protocol;
2795 SilcServerRekeyInternalContext *proto_ctx;
2796 SilcIDListData idata = (SilcIDListData)sock->user_data;
2798 SILC_LOG_DEBUG(("Start"));
2800 /* Allocate internal protocol context. This is sent as context
2802 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2803 proto_ctx->server = (void *)server;
2804 proto_ctx->sock = sock;
2805 proto_ctx->responder = TRUE;
2806 proto_ctx->pfs = idata->rekey->pfs;
2808 /* Perform rekey protocol. Will call the final callback after the
2809 protocol is over. */
2810 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2811 &protocol, proto_ctx, silc_server_rekey_final);
2812 sock->protocol = protocol;
2814 if (proto_ctx->pfs == FALSE)
2815 /* Run the protocol */
2816 silc_protocol_execute(protocol, server->schedule, 0, 0);
2819 /* Received file transger packet. This packet is never for us. It is to
2820 the client in the packet's destination ID. Sending of this sort of packet
2821 equals sending private message, ie. it is sent point to point from
2822 one client to another. */
2824 void silc_server_ftp(SilcServer server,
2825 SilcSocketConnection sock,
2826 SilcPacketContext *packet)
2828 SilcSocketConnection dst_sock;
2829 SilcIDListData idata;
2831 SILC_LOG_DEBUG(("Start"));
2833 if (packet->src_id_type != SILC_ID_CLIENT ||
2834 packet->dst_id_type != SILC_ID_CLIENT)
2837 if (!packet->dst_id)
2840 /* Get the route to the client */
2841 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2842 packet->dst_id_len, NULL,
2847 /* Relay the packet */
2848 silc_server_relay_packet(server, dst_sock, idata->send_key,
2849 idata->hmac_send, idata->psn_send++,