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, &idata);
74 /* Relay the packet */
75 silc_server_relay_packet(server, dst_sock, idata->send_key,
76 idata->hmac_send, idata->psn_send++,
80 /* Parse the Notify Payload */
81 payload = silc_notify_payload_parse(packet->buffer->data,
86 /* If we are router and this packet is not already broadcast packet
87 we will broadcast it. The sending socket really cannot be router or
88 the router is buggy. If this packet is coming from router then it must
89 have the broadcast flag set already and we won't do anything. */
90 if (!server->standalone && server->server_type == SILC_ROUTER &&
91 sock->type == SILC_SOCKET_TYPE_SERVER &&
92 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
93 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
94 if (packet->dst_id_type == SILC_ID_CHANNEL) {
95 /* Packet is destined to channel */
96 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
101 silc_server_packet_send_dest(server, server->router->connection,
103 packet->flags | SILC_PACKET_FLAG_BROADCAST,
104 channel_id, SILC_ID_CHANNEL,
105 packet->buffer->data, packet->buffer->len,
107 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
108 packet->type, packet->flags,
109 channel_id, SILC_ID_CHANNEL,
110 packet->buffer->data, packet->buffer->len,
113 /* Packet is destined to client or server */
114 silc_server_packet_send(server, server->router->connection,
116 packet->flags | SILC_PACKET_FLAG_BROADCAST,
117 packet->buffer->data, packet->buffer->len,
119 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
120 packet->type, packet->flags,
121 packet->buffer->data, packet->buffer->len,
126 type = silc_notify_get_type(payload);
127 args = silc_notify_get_args(payload);
132 case SILC_NOTIFY_TYPE_JOIN:
134 * Distribute the notify to local clients on the channel
136 SILC_LOG_DEBUG(("JOIN notify"));
139 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
142 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
146 /* Get channel entry */
147 channel = silc_idlist_find_channel_by_id(server->global_list,
150 channel = silc_idlist_find_channel_by_id(server->local_list,
153 silc_free(channel_id);
157 silc_free(channel_id);
160 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
163 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
167 /* If the the client is not in local list we check global list (ie. the
168 channel will be global channel) and if it does not exist then create
169 entry for the client. */
170 client = silc_idlist_find_client_by_id(server->global_list,
171 client_id, server->server_type,
174 client = silc_idlist_find_client_by_id(server->local_list,
175 client_id, server->server_type,
178 /* If router did not find the client the it is bogus */
179 if (server->server_type != SILC_SERVER)
183 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
184 silc_id_dup(client_id, SILC_ID_CLIENT),
185 sock->user_data, NULL, 0);
187 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
188 silc_free(client_id);
192 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
196 /* Do not process the notify if the client is not registered */
197 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
200 /* Do not add client to channel if it is there already */
201 if (silc_server_client_on_channel(client, channel, NULL)) {
202 SILC_LOG_DEBUG(("Client already on channel"));
206 /* Send to channel */
207 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
208 FALSE, packet->buffer->data,
209 packet->buffer->len, FALSE);
211 if (server->server_type != SILC_ROUTER &&
212 sock->type == SILC_SOCKET_TYPE_ROUTER)
213 /* The channel is global now */
214 channel->global_users = TRUE;
216 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
218 /* JOIN the global client to the channel (local clients (if router
219 created the channel) is joined in the pending JOIN command). */
220 chl = silc_calloc(1, sizeof(*chl));
221 chl->client = client;
222 chl->channel = channel;
224 /* If this is the first one on the channel then it is the founder of
226 if (!silc_hash_table_count(channel->user_list))
227 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
229 silc_hash_table_add(channel->user_list, client, chl);
230 silc_hash_table_add(client->channels, channel, chl);
231 silc_free(client_id);
232 channel->user_count++;
236 case SILC_NOTIFY_TYPE_LEAVE:
238 * Distribute the notify to local clients on the channel
240 SILC_LOG_DEBUG(("LEAVE notify"));
243 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
244 packet->dst_id_type);
249 /* Get channel entry */
250 channel = silc_idlist_find_channel_by_id(server->global_list,
253 channel = silc_idlist_find_channel_by_id(server->local_list,
256 silc_free(channel_id);
262 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
264 silc_free(channel_id);
267 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
269 silc_free(channel_id);
273 /* Get client entry */
274 client = silc_idlist_find_client_by_id(server->global_list,
275 client_id, TRUE, NULL);
277 client = silc_idlist_find_client_by_id(server->local_list,
278 client_id, TRUE, NULL);
280 silc_free(client_id);
281 silc_free(channel_id);
285 silc_free(client_id);
287 /* Check if on channel */
288 if (!silc_server_client_on_channel(client, channel, NULL))
291 /* Send the leave notify to channel */
292 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
293 FALSE, packet->buffer->data,
294 packet->buffer->len, FALSE);
296 /* Remove the user from channel */
297 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
300 case SILC_NOTIFY_TYPE_SIGNOFF:
302 * Distribute the notify to local clients on the channel
304 SILC_LOG_DEBUG(("SIGNOFF notify"));
307 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
310 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
314 /* Get client entry */
315 client = silc_idlist_find_client_by_id(server->global_list,
316 client_id, TRUE, &cache);
318 client = silc_idlist_find_client_by_id(server->local_list,
319 client_id, TRUE, &cache);
321 silc_free(client_id);
325 silc_free(client_id);
327 /* Get signoff message */
328 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
332 /* Update statistics */
333 server->stat.clients--;
334 if (server->server_type == SILC_ROUTER)
335 server->stat.cell_clients--;
336 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
337 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
339 /* Remove the client from all channels. */
340 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
342 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
343 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
346 case SILC_NOTIFY_TYPE_TOPIC_SET:
348 * Distribute the notify to local clients on the channel
351 SILC_LOG_DEBUG(("TOPIC SET notify"));
354 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
357 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
361 /* Get client entry */
362 client = silc_idlist_find_client_by_id(server->global_list,
363 client_id, TRUE, &cache);
365 client = silc_idlist_find_client_by_id(server->local_list,
366 client_id, TRUE, &cache);
368 silc_free(client_id);
372 silc_free(client_id);
375 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
377 silc_free(channel_id);
382 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
383 packet->dst_id_type);
388 /* Get channel entry */
389 channel = silc_idlist_find_channel_by_id(server->global_list,
392 channel = silc_idlist_find_channel_by_id(server->local_list,
395 silc_free(channel_id);
400 if (channel->topic && !strcmp(channel->topic, tmp))
403 /* Get user's channel entry and check that topic set is allowed. */
404 if (!silc_server_client_on_channel(client, channel, &chl))
406 if (chl->mode == SILC_CHANNEL_UMODE_NONE &&
407 channel->mode & SILC_CHANNEL_MODE_TOPIC) {
408 SILC_LOG_DEBUG(("Topic change is not allowed"));
412 /* Change the topic */
413 silc_free(channel->topic);
414 channel->topic = strdup(tmp);
416 /* Send the same notify to the channel */
417 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
418 FALSE, packet->buffer->data,
419 packet->buffer->len, FALSE);
420 silc_free(channel_id);
423 case SILC_NOTIFY_TYPE_NICK_CHANGE:
426 * Distribute the notify to local clients on the channel
428 unsigned char *id, *id2;
430 SilcUInt32 nickname_len;
432 SILC_LOG_DEBUG(("NICK CHANGE notify"));
434 /* Get old client ID */
435 id = silc_argument_get_arg_type(args, 1, &tmp_len);
438 client_id = silc_id_payload_parse_id(id, tmp_len, NULL);
442 /* Get new client ID */
443 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
446 client_id2 = silc_id_payload_parse_id(id2, tmp_len, NULL);
450 SILC_LOG_DEBUG(("Old Client ID id(%s)",
451 silc_id_render(client_id, SILC_ID_CLIENT)));
452 SILC_LOG_DEBUG(("New Client ID id(%s)",
453 silc_id_render(client_id2, SILC_ID_CLIENT)));
455 /* From protocol version 1.1 we also get the new nickname */
456 nickname = silc_argument_get_arg_type(args, 3, &nickname_len);;
458 /* Replace the Client ID */
459 client = silc_idlist_replace_client_id(server->global_list, client_id,
460 client_id2, nickname);
462 client = silc_idlist_replace_client_id(server->local_list, client_id,
463 client_id2, nickname);
466 /* Send the NICK_CHANGE notify type to local clients on the channels
467 this client is joined to. */
468 silc_server_send_notify_on_channels(server, NULL, client,
469 SILC_NOTIFY_TYPE_NICK_CHANGE, 3,
470 id, tmp_len, id2, tmp_len,
475 silc_free(client_id);
477 silc_free(client_id2);
481 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
483 * Distribute the notify to local clients on the channel
486 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
489 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
492 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
496 /* Get client entry */
497 if (id_type == SILC_ID_CLIENT) {
498 client = silc_idlist_find_client_by_id(server->global_list,
499 client_id, TRUE, &cache);
501 client = silc_idlist_find_client_by_id(server->local_list,
502 client_id, TRUE, &cache);
504 silc_free(client_id);
508 silc_free(client_id);
512 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
513 packet->dst_id_type);
518 /* Get channel entry */
519 channel = silc_idlist_find_channel_by_id(server->global_list,
522 channel = silc_idlist_find_channel_by_id(server->local_list,
525 silc_free(channel_id);
529 silc_free(channel_id);
532 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
535 SILC_GET32_MSB(mode, tmp);
537 /* Check if mode changed */
538 if (channel->mode == mode)
541 /* Get user's channel entry and check that mode change is allowed */
543 if (!silc_server_client_on_channel(client, channel, &chl))
545 if (!silc_server_check_cmode_rights(server, channel, chl, mode)) {
546 SILC_LOG_DEBUG(("CMODE change is not allowed"));
551 /* Send the same notify to the channel */
552 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
553 FALSE, packet->buffer->data,
554 packet->buffer->len, FALSE);
556 /* If the channel had private keys set and the mode was removed then
557 we must re-generate and re-distribute a new channel key */
558 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
559 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
560 /* Re-generate channel key */
561 if (!silc_server_create_channel_key(server, channel, 0))
564 /* Send the channel key. This sends it to our local clients and if
565 we are normal server to our router as well. */
566 silc_server_send_channel_key(server, NULL, channel,
567 server->server_type == SILC_ROUTER ?
568 FALSE : !server->standalone);
572 channel->mode = mode;
575 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
577 unsigned char hash[32];
580 silc_hmac_free(channel->hmac);
581 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
584 /* Set the HMAC key out of current channel key. The client must do
586 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
587 channel->key_len / 8,
589 silc_hmac_set_key(channel->hmac, hash,
590 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
591 memset(hash, 0, sizeof(hash));
594 /* Get the passphrase */
595 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
597 silc_free(channel->passphrase);
598 channel->passphrase = strdup(tmp);
603 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
606 * Distribute the notify to local clients on the channel
608 SilcChannelClientEntry chl2 = NULL;
609 bool notify_sent = FALSE;
611 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
614 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
617 client_id = silc_id_payload_parse_id(tmp, tmp_len, &id_type);
621 /* Get client entry */
622 if (id_type == SILC_ID_CLIENT) {
623 client = silc_idlist_find_client_by_id(server->global_list,
624 client_id, TRUE, &cache);
626 client = silc_idlist_find_client_by_id(server->local_list,
627 client_id, TRUE, &cache);
629 silc_free(client_id);
633 silc_free(client_id);
637 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
638 packet->dst_id_type);
643 /* Get channel entry */
644 channel = silc_idlist_find_channel_by_id(server->global_list,
647 channel = silc_idlist_find_channel_by_id(server->local_list,
650 silc_free(channel_id);
656 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
658 silc_free(channel_id);
662 SILC_GET32_MSB(mode, tmp);
664 /* Get target client */
665 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
668 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
672 /* Get client entry */
673 client2 = silc_idlist_find_client_by_id(server->global_list,
674 client_id, TRUE, NULL);
676 client2 = silc_idlist_find_client_by_id(server->local_list,
677 client_id, TRUE, NULL);
679 silc_free(client_id);
683 silc_free(client_id);
686 /* Check that sender is on channel */
687 if (!silc_server_client_on_channel(client, channel, &chl))
690 if (client != client2) {
691 /* Sender must be operator */
692 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
693 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
697 /* Check that target is on channel */
698 if (!silc_server_client_on_channel(client2, channel, &chl))
701 /* If target is founder mode change is not allowed. */
702 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
703 SILC_LOG_DEBUG(("CUMODE change is not allowed"));
709 /* Get entry to the channel user list */
710 silc_hash_table_list(channel->user_list, &htl);
711 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
712 /* If the mode is channel founder and we already find a client
713 to have that mode on the channel we will enforce the sender
714 to change the channel founder mode away. There can be only one
715 channel founder on the channel. */
716 if (server->server_type == SILC_ROUTER &&
717 mode & SILC_CHANNEL_UMODE_CHANFO &&
718 chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
720 unsigned char cumode[4];
722 if (chl->client == client && chl->mode == mode) {
727 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
728 silc_server_send_notify_cumode(server, sock, FALSE, channel, mode,
729 client2->id, SILC_ID_CLIENT,
732 idp = silc_id_payload_encode(client2->id, SILC_ID_CLIENT);
733 SILC_PUT32_MSB(mode, cumode);
734 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
735 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
736 3, idp->data, idp->len,
738 idp->data, idp->len);
739 silc_buffer_free(idp);
742 /* Force the mode change if we alredy set the mode */
745 silc_free(channel_id);
746 silc_hash_table_list_reset(&htl);
751 if (chl->client == client2) {
752 if (chl->mode == mode) {
757 SILC_LOG_DEBUG(("Changing the channel user mode"));
759 /* Change the mode */
761 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
767 silc_hash_table_list_reset(&htl);
769 /* Send the same notify to the channel */
771 silc_server_packet_send_to_channel(server, sock, channel,
773 FALSE, packet->buffer->data,
774 packet->buffer->len, FALSE);
776 silc_free(channel_id);
780 case SILC_NOTIFY_TYPE_INVITE:
782 if (packet->dst_id_type == SILC_ID_CLIENT)
785 SILC_LOG_DEBUG(("INVITE notify"));
788 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
791 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
795 /* Get channel entry */
796 channel = silc_idlist_find_channel_by_id(server->global_list,
799 channel = silc_idlist_find_channel_by_id(server->local_list,
802 silc_free(channel_id);
806 silc_free(channel_id);
809 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
812 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
816 /* Get client entry */
817 client = silc_idlist_find_client_by_id(server->global_list,
818 client_id, TRUE, &cache);
820 client = silc_idlist_find_client_by_id(server->local_list,
821 client_id, TRUE, &cache);
823 silc_free(client_id);
827 silc_free(client_id);
829 /* Get user's channel entry and check that inviting is allowed. */
830 if (!silc_server_client_on_channel(client, channel, &chl))
832 if (chl->mode == SILC_CHANNEL_UMODE_NONE &&
833 channel->mode & SILC_CHANNEL_MODE_INVITE) {
834 SILC_LOG_DEBUG(("Inviting is not allowed"));
838 /* Get the added invite */
839 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
841 if (!channel->invite_list)
842 channel->invite_list = silc_calloc(tmp_len + 2,
843 sizeof(*channel->invite_list));
845 channel->invite_list = silc_realloc(channel->invite_list,
846 sizeof(*channel->invite_list) *
848 strlen(channel->invite_list) +
850 if (tmp[tmp_len - 1] == ',')
851 tmp[tmp_len - 1] = '\0';
853 strncat(channel->invite_list, tmp, tmp_len);
854 strncat(channel->invite_list, ",", 1);
857 /* Get the deleted invite */
858 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
859 if (tmp && channel->invite_list) {
860 char *start, *end, *n;
862 if (!strncmp(channel->invite_list, tmp,
863 strlen(channel->invite_list) - 1)) {
864 silc_free(channel->invite_list);
865 channel->invite_list = NULL;
867 start = strstr(channel->invite_list, tmp);
868 if (start && strlen(start) >= tmp_len) {
869 end = start + tmp_len;
870 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
871 strncat(n, channel->invite_list, start - channel->invite_list);
872 strncat(n, end + 1, ((channel->invite_list +
873 strlen(channel->invite_list)) - end) - 1);
874 silc_free(channel->invite_list);
875 channel->invite_list = n;
882 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
884 * Distribute to the local clients on the channel and change the
888 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
890 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
893 /* Get the old Channel ID */
894 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
897 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
901 /* Get the channel entry */
902 channel = silc_idlist_find_channel_by_id(server->local_list,
905 channel = silc_idlist_find_channel_by_id(server->global_list,
908 silc_free(channel_id);
913 /* Send the notify to the channel */
914 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
915 FALSE, packet->buffer->data,
916 packet->buffer->len, FALSE);
918 /* Get the new Channel ID */
919 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
922 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len, NULL);
926 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
927 silc_id_render(channel_id, SILC_ID_CHANNEL)));
928 SILC_LOG_DEBUG(("New Channel ID id(%s)",
929 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
931 /* Replace the Channel ID */
932 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
934 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
936 silc_free(channel_id2);
941 SilcBuffer users = NULL, users_modes = NULL;
943 /* Re-announce this channel which ID was changed. */
944 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
946 silc_id_get_len(channel->id,
950 /* Re-announce our clients on the channel as the ID has changed now */
951 silc_server_announce_get_channel_users(server, channel, &users,
954 silc_buffer_push(users, users->data - users->head);
955 silc_server_packet_send(server, sock,
956 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
957 users->data, users->len, FALSE);
958 silc_buffer_free(users);
961 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
962 silc_server_packet_send_dest(server, sock,
963 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
964 channel->id, SILC_ID_CHANNEL,
966 users_modes->len, FALSE);
967 silc_buffer_free(users_modes);
970 /* Re-announce channel's topic */
971 if (channel->topic) {
972 silc_server_send_notify_topic_set(server, sock,
973 server->server_type == SILC_ROUTER ?
974 TRUE : FALSE, channel,
975 channel->id, SILC_ID_CHANNEL,
980 silc_free(channel_id);
984 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
986 * Remove the server entry and all clients that this server owns.
989 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
992 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
995 server_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
999 /* Get server entry */
1000 server_entry = silc_idlist_find_server_by_id(server->global_list,
1001 server_id, TRUE, NULL);
1003 if (!server_entry) {
1004 server_entry = silc_idlist_find_server_by_id(server->local_list,
1005 server_id, TRUE, NULL);
1007 if (!server_entry) {
1008 /* If we are normal server then we might not have the server. Check
1009 whether router was kind enough to send the list of all clients
1010 that actually was to be removed. Remove them if the list is
1012 if (server->server_type != SILC_ROUTER &&
1013 silc_argument_get_arg_num(args) > 1) {
1016 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
1018 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
1021 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1025 /* Get client entry */
1026 client = silc_idlist_find_client_by_id(server->global_list,
1027 client_id, TRUE, &cache);
1030 client = silc_idlist_find_client_by_id(server->local_list,
1031 client_id, TRUE, &cache);
1034 silc_free(client_id);
1038 silc_free(client_id);
1040 /* Update statistics */
1041 server->stat.clients--;
1042 if (server->server_type == SILC_ROUTER)
1043 server->stat.cell_clients--;
1044 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
1045 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
1047 /* Remove the client from all channels. */
1048 silc_server_remove_from_channels(server, NULL, client,
1051 /* Remove the client */
1052 silc_idlist_del_client(local ? server->local_list :
1053 server->global_list, client);
1057 silc_free(server_id);
1061 silc_free(server_id);
1063 /* Free all client entries that this server owns as they will
1064 become invalid now as well. */
1065 silc_server_remove_clients_by_server(server, server_entry, TRUE);
1067 /* Remove the server entry */
1068 silc_idlist_del_server(local ? server->local_list :
1069 server->global_list, server_entry);
1071 /* XXX update statistics */
1075 case SILC_NOTIFY_TYPE_KICKED:
1077 * Distribute the notify to local clients on the channel
1080 SILC_LOG_DEBUG(("KICKED notify"));
1083 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1084 packet->dst_id_type);
1089 /* Get channel entry */
1090 channel = silc_idlist_find_channel_by_id(server->global_list,
1093 channel = silc_idlist_find_channel_by_id(server->local_list,
1096 silc_free(channel_id);
1100 silc_free(channel_id);
1103 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1106 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1110 /* If the the client is not in local list we check global list */
1111 client = silc_idlist_find_client_by_id(server->global_list,
1112 client_id, TRUE, NULL);
1114 client = silc_idlist_find_client_by_id(server->local_list,
1115 client_id, TRUE, NULL);
1117 silc_free(client_id);
1121 silc_free(client_id);
1123 /* If target is founder they cannot be kicked */
1124 if (!silc_server_client_on_channel(client, channel, &chl))
1126 if (chl->mode & SILC_CHANNEL_UMODE_CHANFO)
1129 /* From protocol version 1.1 we get the kicker's ID as well. */
1130 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1132 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1136 /* If the the client is not in local list we check global list */
1137 client2 = silc_idlist_find_client_by_id(server->global_list,
1138 client_id, TRUE, NULL);
1140 client2 = silc_idlist_find_client_by_id(server->local_list,
1141 client_id, TRUE, NULL);
1143 silc_free(client_id);
1147 silc_free(client_id);
1149 /* Kicker must be operator on channel */
1150 if (!silc_server_client_on_channel(client2, channel, &chl))
1152 if (chl->mode == SILC_CHANNEL_UMODE_NONE) {
1153 SILC_LOG_DEBUG(("Kicking is not allowed"));
1158 /* Send to channel */
1159 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
1160 FALSE, packet->buffer->data,
1161 packet->buffer->len, FALSE);
1163 /* Remove the client from channel */
1164 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1168 case SILC_NOTIFY_TYPE_KILLED:
1171 * Distribute the notify to local clients on channels
1173 unsigned char *id, *comment;
1174 SilcUInt32 id_len, comment_len;
1176 SILC_LOG_DEBUG(("KILLED notify"));
1179 id = silc_argument_get_arg_type(args, 1, &id_len);
1182 client_id = silc_id_payload_parse_id(id, id_len, NULL);
1186 /* If the the client is not in local list we check global list */
1187 client = silc_idlist_find_client_by_id(server->global_list,
1188 client_id, TRUE, NULL);
1190 client = silc_idlist_find_client_by_id(server->local_list,
1191 client_id, TRUE, NULL);
1193 silc_free(client_id);
1197 silc_free(client_id);
1199 /* If the client is one of ours, then close the connection to the
1200 client now. This removes the client from all channels as well. */
1201 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1202 sock = client->connection;
1203 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1204 silc_server_close_connection(server, sock);
1209 comment = silc_argument_get_arg_type(args, 2, &comment_len);
1210 if (comment_len > 128)
1213 /* From protocol version 1.1 we get the killer's ID as well. */
1214 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1216 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1220 /* If the the client is not in local list we check global list */
1221 client2 = silc_idlist_find_client_by_id(server->global_list,
1222 client_id, TRUE, NULL);
1224 client2 = silc_idlist_find_client_by_id(server->local_list,
1225 client_id, TRUE, NULL);
1227 silc_free(client_id);
1231 silc_free(client_id);
1233 /* Killer must be router operator */
1234 if (!(client2->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1235 SILC_LOG_DEBUG(("Killing is not allowed"));
1240 /* Send the notify to local clients on the channels except to the
1241 client who is killed. */
1242 silc_server_send_notify_on_channels(server, client, client,
1243 SILC_NOTIFY_TYPE_KILLED, 3,
1244 id, id_len, comment, comment_len,
1247 /* Remove the client from all channels */
1248 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1254 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1256 * Save the mode of the client.
1259 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1262 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1265 client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1269 /* Get client entry */
1270 client = silc_idlist_find_client_by_id(server->global_list,
1271 client_id, TRUE, NULL);
1273 client = silc_idlist_find_client_by_id(server->local_list,
1274 client_id, TRUE, NULL);
1276 silc_free(client_id);
1280 silc_free(client_id);
1283 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1286 SILC_GET32_MSB(mode, tmp);
1288 /* Check that mode changing is allowed. */
1289 if (!silc_server_check_umode_rights(server, client, mode)) {
1290 SILC_LOG_DEBUG(("UMODE change is not allowed"));
1294 /* Change the mode */
1295 client->mode = mode;
1299 case SILC_NOTIFY_TYPE_BAN:
1304 SILC_LOG_DEBUG(("BAN notify"));
1306 /* Get Channel ID */
1307 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1310 channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
1314 /* Get channel entry */
1315 channel = silc_idlist_find_channel_by_id(server->global_list,
1318 channel = silc_idlist_find_channel_by_id(server->local_list,
1321 silc_free(channel_id);
1325 silc_free(channel_id);
1327 /* Get the new ban and add it to the ban list */
1328 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1330 if (!channel->ban_list)
1331 channel->ban_list = silc_calloc(tmp_len + 2,
1332 sizeof(*channel->ban_list));
1334 channel->ban_list = silc_realloc(channel->ban_list,
1335 sizeof(*channel->ban_list) *
1337 strlen(channel->ban_list) + 2));
1338 strncat(channel->ban_list, tmp, tmp_len);
1339 strncat(channel->ban_list, ",", 1);
1342 /* Get the ban to be removed and remove it from the list */
1343 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1344 if (tmp && channel->ban_list) {
1345 char *start, *end, *n;
1347 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1348 silc_free(channel->ban_list);
1349 channel->ban_list = NULL;
1351 start = strstr(channel->ban_list, tmp);
1352 if (start && strlen(start) >= tmp_len) {
1353 end = start + tmp_len;
1354 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1355 strncat(n, channel->ban_list, start - channel->ban_list);
1356 strncat(n, end + 1, ((channel->ban_list +
1357 strlen(channel->ban_list)) - end) - 1);
1358 silc_free(channel->ban_list);
1359 channel->ban_list = n;
1365 /* Ignore rest of the notify types for now */
1366 case SILC_NOTIFY_TYPE_NONE:
1367 case SILC_NOTIFY_TYPE_MOTD:
1374 silc_notify_payload_free(payload);
1377 void silc_server_notify_list(SilcServer server,
1378 SilcSocketConnection sock,
1379 SilcPacketContext *packet)
1381 SilcPacketContext *new;
1385 SILC_LOG_DEBUG(("Processing Notify List"));
1387 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1388 packet->src_id_type != SILC_ID_SERVER)
1391 /* Make copy of the original packet context, except for the actual
1392 data buffer, which we will here now fetch from the original buffer. */
1393 new = silc_packet_context_alloc();
1394 new->type = SILC_PACKET_NOTIFY;
1395 new->flags = packet->flags;
1396 new->src_id = packet->src_id;
1397 new->src_id_len = packet->src_id_len;
1398 new->src_id_type = packet->src_id_type;
1399 new->dst_id = packet->dst_id;
1400 new->dst_id_len = packet->dst_id_len;
1401 new->dst_id_type = packet->dst_id_type;
1403 buffer = silc_buffer_alloc(1024);
1404 new->buffer = buffer;
1406 while (packet->buffer->len) {
1407 SILC_GET16_MSB(len, packet->buffer->data + 2);
1408 if (len > packet->buffer->len)
1411 if (len > buffer->truelen) {
1412 silc_buffer_free(buffer);
1413 buffer = silc_buffer_alloc(1024 + len);
1416 silc_buffer_pull_tail(buffer, len);
1417 silc_buffer_put(buffer, packet->buffer->data, len);
1419 /* Process the Notify */
1420 silc_server_notify(server, sock, new);
1422 silc_buffer_push_tail(buffer, len);
1423 silc_buffer_pull(packet->buffer, len);
1426 silc_buffer_free(buffer);
1430 /* Received private message. This resolves the destination of the message
1431 and sends the packet. This is used by both server and router. If the
1432 destination is our locally connected client this sends the packet to
1433 the client. This may also send the message for further routing if
1434 the destination is not in our server (or router). */
1436 void silc_server_private_message(SilcServer server,
1437 SilcSocketConnection sock,
1438 SilcPacketContext *packet)
1440 SilcSocketConnection dst_sock;
1441 SilcIDListData idata;
1443 SILC_LOG_DEBUG(("Start"));
1445 if (packet->src_id_type != SILC_ID_CLIENT ||
1446 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1449 /* Get the route to the client */
1450 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1451 packet->dst_id_len, NULL, &idata);
1453 /* Send IDENTIFY command reply with error status to indicate that
1454 such destination ID does not exist or is invalid */
1455 SilcBuffer idp = silc_id_payload_encode_data(packet->dst_id,
1457 packet->dst_id_type);
1461 if (packet->src_id_type == SILC_ID_CLIENT) {
1462 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1464 packet->src_id_type);
1465 silc_server_send_dest_command_reply(server, sock,
1466 client_id, SILC_ID_CLIENT,
1467 SILC_COMMAND_IDENTIFY,
1468 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1469 0, 1, 2, idp->data, idp->len);
1470 silc_free(client_id);
1472 silc_server_send_command_reply(server, sock, SILC_COMMAND_IDENTIFY,
1473 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1474 0, 1, 2, idp->data, idp->len);
1477 silc_buffer_free(idp);
1481 /* Send the private message */
1482 silc_server_send_private_message(server, dst_sock, idata->send_key,
1483 idata->hmac_send, idata->psn_send++,
1487 /* Received private message key packet.. This packet is never for us. It is to
1488 the client in the packet's destination ID. Sending of this sort of packet
1489 equals sending private message, ie. it is sent point to point from
1490 one client to another. */
1492 void silc_server_private_message_key(SilcServer server,
1493 SilcSocketConnection sock,
1494 SilcPacketContext *packet)
1496 SilcSocketConnection dst_sock;
1497 SilcIDListData idata;
1499 SILC_LOG_DEBUG(("Start"));
1501 if (packet->src_id_type != SILC_ID_CLIENT ||
1502 packet->dst_id_type != SILC_ID_CLIENT)
1505 if (!packet->dst_id)
1508 /* Get the route to the client */
1509 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1510 packet->dst_id_len, NULL, &idata);
1514 /* Relay the packet */
1515 silc_server_relay_packet(server, dst_sock, idata->send_key,
1516 idata->hmac_send, idata->psn_send++, packet, FALSE);
1519 /* Processes incoming command reply packet. The command reply packet may
1520 be destined to one of our clients or it may directly for us. We will
1521 call the command reply routine after processing the packet. */
1523 void silc_server_command_reply(SilcServer server,
1524 SilcSocketConnection sock,
1525 SilcPacketContext *packet)
1527 SilcBuffer buffer = packet->buffer;
1528 SilcClientEntry client = NULL;
1529 SilcSocketConnection dst_sock;
1530 SilcIDListData idata;
1531 SilcClientID *id = NULL;
1533 SILC_LOG_DEBUG(("Start"));
1535 /* Source must be server or router */
1536 if (packet->src_id_type != SILC_ID_SERVER &&
1537 sock->type != SILC_SOCKET_TYPE_ROUTER)
1540 if (packet->dst_id_type == SILC_ID_CHANNEL)
1543 if (packet->dst_id_type == SILC_ID_CLIENT) {
1544 /* Destination must be one of ours */
1545 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1548 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1550 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1556 if (packet->dst_id_type == SILC_ID_SERVER) {
1557 /* For now this must be for us */
1558 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1559 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1564 /* Execute command reply locally for the command */
1565 silc_server_command_reply_process(server, sock, buffer);
1567 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1568 /* Relay the packet to the client */
1569 const SilcBufferStruct p;
1571 dst_sock = (SilcSocketConnection)client->connection;
1572 idata = (SilcIDListData)client;
1574 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1575 + packet->dst_id_len + packet->padlen);
1576 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1577 idata->hmac_send, (const SilcBuffer)&p)) {
1578 SILC_LOG_ERROR(("Cannot send packet"));
1581 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1583 /* Encrypt packet */
1584 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1585 (SilcBuffer)&p, buffer->len);
1587 /* Send the packet */
1588 silc_server_packet_send_real(server, dst_sock, TRUE);
1594 /* Process received channel message. The message can be originated from
1595 client or server. */
1597 void silc_server_channel_message(SilcServer server,
1598 SilcSocketConnection sock,
1599 SilcPacketContext *packet)
1601 SilcChannelEntry channel = NULL;
1602 SilcChannelID *id = NULL;
1603 void *sender_id = NULL;
1604 SilcClientEntry sender_entry = NULL;
1605 SilcChannelClientEntry chl;
1608 SILC_LOG_DEBUG(("Processing channel message"));
1611 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1612 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1616 /* Find channel entry */
1617 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1620 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1622 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1624 SILC_LOG_DEBUG(("Could not find channel"));
1629 /* See that this client is on the channel. If the original sender is
1630 not client (as it can be server as well) we don't do the check. */
1631 sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1632 packet->src_id_type);
1635 if (packet->src_id_type == SILC_ID_CLIENT) {
1636 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1637 sender_id, TRUE, NULL);
1638 if (!sender_entry) {
1640 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1641 sender_id, TRUE, NULL);
1643 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1645 SILC_LOG_DEBUG(("Client not on channel"));
1649 /* If channel is moderated check that client is allowed to send
1651 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_USERS && !chl->mode) {
1652 SILC_LOG_DEBUG(("Channel is silenced from normal users"));
1655 if (channel->mode & SILC_CHANNEL_MODE_SILENCE_OPERS &&
1656 chl->mode & SILC_CHANNEL_UMODE_CHANOP &&
1657 !(chl->mode & SILC_CHANNEL_UMODE_CHANFO)) {
1658 SILC_LOG_DEBUG(("Channel is silenced from operators"));
1662 /* If the packet is coming from router, but the client entry is local
1663 entry to us then some router is rerouting this to us and it is not
1664 allowed. When the client is local to us it means that we've routed
1665 this packet to network, and now someone is routing it back to us. */
1666 if (server->server_type == SILC_ROUTER &&
1667 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1668 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1673 /* Distribute the packet to our local clients. This will send the
1674 packet for further routing as well, if needed. */
1675 silc_server_packet_relay_to_channel(server, sock, channel, sender_id,
1676 packet->src_id_type, sender_entry,
1677 packet->buffer->data,
1678 packet->buffer->len, FALSE);
1681 silc_free(sender_id);
1685 /* Received channel key packet. We distribute the key to all of our locally
1686 connected clients on the channel. */
1688 void silc_server_channel_key(SilcServer server,
1689 SilcSocketConnection sock,
1690 SilcPacketContext *packet)
1692 SilcBuffer buffer = packet->buffer;
1693 SilcChannelEntry channel;
1695 if (packet->src_id_type != SILC_ID_SERVER ||
1696 (server->server_type == SILC_ROUTER &&
1697 sock->type == SILC_SOCKET_TYPE_ROUTER))
1700 /* Save the channel key */
1701 channel = silc_server_save_channel_key(server, buffer, NULL);
1705 /* Distribute the key to everybody who is on the channel. If we are router
1706 we will also send it to locally connected servers. */
1707 silc_server_send_channel_key(server, sock, channel, FALSE);
1709 if (server->server_type != SILC_BACKUP_ROUTER) {
1710 /* Distribute to local cell backup routers. */
1711 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1712 SILC_PACKET_CHANNEL_KEY, 0,
1713 buffer->data, buffer->len, FALSE, TRUE);
1717 /* Received New Client packet and processes it. Creates Client ID for the
1718 client. Client becomes registered after calling this functions. */
1720 SilcClientEntry silc_server_new_client(SilcServer server,
1721 SilcSocketConnection sock,
1722 SilcPacketContext *packet)
1724 SilcBuffer buffer = packet->buffer;
1725 SilcClientEntry client;
1726 SilcClientID *client_id;
1728 SilcIDListData idata;
1729 char *username = NULL, *realname = NULL, *id_string;
1730 SilcUInt16 username_len;
1733 char *hostname, *nickname;
1736 SILC_LOG_DEBUG(("Creating new client"));
1738 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1741 /* Take client entry */
1742 client = (SilcClientEntry)sock->user_data;
1743 idata = (SilcIDListData)client;
1745 /* Remove the old cache entry. */
1746 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1747 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
1748 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1749 "You have not been authenticated");
1753 /* Parse incoming packet */
1754 ret = silc_buffer_unformat(buffer,
1755 SILC_STR_UI16_NSTRING_ALLOC(&username,
1757 SILC_STR_UI16_STRING_ALLOC(&realname),
1760 silc_free(username);
1761 silc_free(realname);
1762 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1763 "connection", sock->hostname, sock->ip));
1764 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1765 "Incomplete client information");
1770 silc_free(username);
1771 silc_free(realname);
1772 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
1773 "connection", sock->hostname, sock->ip));
1774 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1775 "Incomplete client information");
1779 if (username_len > 128)
1780 username[128] = '\0';
1782 /* Check for bad characters for nickname, and modify the nickname if
1783 it includes those. */
1784 if (silc_server_name_bad_chars(username, username_len)) {
1785 nickname = silc_server_name_modify_bad(username, username_len);
1787 nickname = strdup(username);
1790 /* Make sanity checks for the hostname of the client. If the hostname
1791 is provided in the `username' check that it is the same than the
1792 resolved hostname, or if not resolved the hostname that appears in
1793 the client's public key. If the hostname is not present then put
1794 it from the resolved name or from the public key. */
1795 if (strchr(username, '@')) {
1796 SilcPublicKeyIdentifier pident;
1797 int tlen = strcspn(username, "@");
1798 char *phostname = NULL;
1800 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
1802 if (strcmp(sock->hostname, sock->ip) &&
1803 strcmp(sock->hostname, hostname)) {
1804 silc_free(username);
1805 silc_free(hostname);
1806 silc_free(realname);
1807 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1808 "connection", sock->hostname, sock->ip));
1809 silc_server_disconnect_remote(server, sock,
1810 "Server closed connection: "
1811 "Incomplete client information");
1815 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1817 phostname = strdup(pident->host);
1818 silc_pkcs_free_identifier(pident);
1821 if (!strcmp(sock->hostname, sock->ip) &&
1822 phostname && strcmp(phostname, hostname)) {
1823 silc_free(username);
1824 silc_free(hostname);
1825 silc_free(phostname);
1826 silc_free(realname);
1827 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1828 "connection", sock->hostname, sock->ip));
1829 silc_server_disconnect_remote(server, sock,
1830 "Server closed connection: "
1831 "Incomplete client information");
1835 silc_free(phostname);
1837 /* The hostname is not present, add it. */
1839 /* XXX For now we cannot take the host name from the public key since
1840 they are not trusted or we cannot verify them as trusted. Just take
1841 what the resolved name or address is. */
1843 if (strcmp(sock->hostname, sock->ip)) {
1845 newusername = silc_calloc(strlen(username) +
1846 strlen(sock->hostname) + 2,
1847 sizeof(*newusername));
1848 strncat(newusername, username, strlen(username));
1849 strncat(newusername, "@", 1);
1850 strncat(newusername, sock->hostname, strlen(sock->hostname));
1851 silc_free(username);
1852 username = newusername;
1855 SilcPublicKeyIdentifier pident =
1856 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1859 newusername = silc_calloc(strlen(username) +
1860 strlen(pident->host) + 2,
1861 sizeof(*newusername));
1862 strncat(newusername, username, strlen(username));
1863 strncat(newusername, "@", 1);
1864 strncat(newusername, pident->host, strlen(pident->host));
1865 silc_free(username);
1866 username = newusername;
1867 silc_pkcs_free_identifier(pident);
1873 /* Create Client ID */
1874 while (!silc_id_create_client_id(server, server->id, server->rng,
1875 server->md5hash, nickname, &client_id)) {
1877 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
1880 /* Update client entry */
1881 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1882 client->nickname = nickname;
1883 client->username = username;
1884 client->userinfo = realname ? realname : strdup(" ");
1885 client->id = client_id;
1886 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1888 /* Add the client again to the ID cache */
1889 silc_idcache_add(server->local_list->clients, client->nickname,
1890 client_id, client, 0, NULL);
1892 /* Notify our router about new client on the SILC network */
1893 if (!server->standalone)
1894 silc_server_send_new_id(server, (SilcSocketConnection)
1895 server->router->connection,
1896 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1897 client->id, SILC_ID_CLIENT, id_len);
1899 /* Send the new client ID to the client. */
1900 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1901 reply = silc_buffer_alloc(2 + 2 + id_len);
1902 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1903 silc_buffer_format(reply,
1904 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1905 SILC_STR_UI_SHORT(id_len),
1906 SILC_STR_UI_XNSTRING(id_string, id_len),
1908 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1909 reply->data, reply->len, FALSE);
1910 silc_free(id_string);
1911 silc_buffer_free(reply);
1913 /* Send some nice info to the client */
1914 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1915 ("Welcome to the SILC Network %s",
1917 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1918 ("Your host is %s, running version %s",
1919 server->server_name, server_version));
1920 if (server->server_type == SILC_ROUTER) {
1921 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1922 ("There are %d clients on %d servers in SILC "
1923 "Network", server->stat.clients,
1924 server->stat.servers + 1));
1925 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1926 ("There are %d clients on %d server in our cell",
1927 server->stat.cell_clients,
1928 server->stat.cell_servers + 1));
1929 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1930 ("I have %d clients, %d channels, %d servers and "
1932 server->stat.my_clients,
1933 server->stat.my_channels,
1934 server->stat.my_servers,
1935 server->stat.my_routers));
1936 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1937 ("There are %d server operators and %d router "
1939 server->stat.server_ops,
1940 server->stat.router_ops));
1941 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1942 ("I have %d operators online",
1943 server->stat.my_router_ops +
1944 server->stat.my_server_ops));
1946 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1947 ("I have %d clients and %d channels formed",
1948 server->stat.my_clients,
1949 server->stat.my_channels));
1950 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1951 ("%d operators online",
1952 server->stat.my_server_ops));
1954 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1955 ("Your connection is secured with %s cipher, "
1956 "key length %d bits",
1957 idata->send_key->cipher->name,
1958 idata->send_key->cipher->key_len));
1959 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1960 ("Your current nickname is %s",
1964 silc_server_send_motd(server, sock);
1969 /* Create new server. This processes received New Server packet and
1970 saves the received Server ID. The server is our locally connected
1971 server thus we save all the information and save it to local list.
1972 This funtion can be used by both normal server and router server.
1973 If normal server uses this it means that its router has connected
1974 to the server. If router uses this it means that one of the cell's
1975 servers is connected to the router. */
1977 SilcServerEntry silc_server_new_server(SilcServer server,
1978 SilcSocketConnection sock,
1979 SilcPacketContext *packet)
1981 SilcBuffer buffer = packet->buffer;
1982 SilcServerEntry new_server, server_entry;
1983 SilcServerID *server_id;
1984 SilcIDListData idata;
1985 unsigned char *server_name, *id_string;
1986 SilcUInt16 id_len, name_len;
1990 SILC_LOG_DEBUG(("Creating new server"));
1992 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1993 sock->type != SILC_SOCKET_TYPE_ROUTER)
1996 /* Take server entry */
1997 new_server = (SilcServerEntry)sock->user_data;
1998 idata = (SilcIDListData)new_server;
2000 /* Remove the old cache entry */
2001 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
2002 if (!silc_idcache_del_by_context(server->global_list->servers,
2004 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
2005 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
2006 "server" : "router")));
2007 silc_server_disconnect_remote(server, sock, "Server closed connection: "
2008 "You have not been authenticated");
2014 /* Parse the incoming packet */
2015 ret = silc_buffer_unformat(buffer,
2016 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
2017 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
2022 silc_free(id_string);
2024 silc_free(server_name);
2028 if (id_len > buffer->len) {
2029 silc_free(id_string);
2030 silc_free(server_name);
2035 server_name[255] = '\0';
2038 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
2040 silc_free(id_string);
2041 silc_free(server_name);
2044 silc_free(id_string);
2046 /* Check for valid server ID */
2047 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
2048 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
2049 sock->ip, sock->hostname));
2050 silc_server_disconnect_remote(server, sock, "Server closed connection: "
2051 "Your Server ID is not valid");
2052 silc_free(server_name);
2056 /* Check that we do not have this ID already */
2057 server_entry = silc_idlist_find_server_by_id(server->local_list,
2058 server_id, TRUE, NULL);
2060 silc_idcache_del_by_context(server->local_list->servers, server_entry);
2062 server_entry = silc_idlist_find_server_by_id(server->global_list,
2063 server_id, TRUE, NULL);
2065 silc_idcache_del_by_context(server->global_list->servers, server_entry);
2068 /* Update server entry */
2069 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
2070 new_server->server_name = server_name;
2071 new_server->id = server_id;
2073 SILC_LOG_DEBUG(("New server id(%s)",
2074 silc_id_render(server_id, SILC_ID_SERVER)));
2076 /* Add again the entry to the ID cache. */
2077 silc_idcache_add(local ? server->local_list->servers :
2078 server->global_list->servers, server_name, server_id,
2079 new_server, 0, NULL);
2081 /* Distribute the information about new server in the SILC network
2082 to our router. If we are normal server we won't send anything
2083 since this connection must be our router connection. */
2084 if (server->server_type == SILC_ROUTER && !server->standalone &&
2085 server->router->connection != sock)
2086 silc_server_send_new_id(server, server->router->connection,
2087 TRUE, new_server->id, SILC_ID_SERVER,
2088 silc_id_get_len(server_id, SILC_ID_SERVER));
2090 if (server->server_type == SILC_ROUTER)
2091 server->stat.cell_servers++;
2093 /* Check whether this router connection has been replaced by an
2094 backup router. If it has been then we'll disable the server and will
2095 ignore everything it will send until the backup router resuming
2096 protocol has been completed. */
2097 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2098 silc_server_backup_replaced_get(server, server_id, NULL)) {
2099 /* Send packet to the server indicating that it cannot use this
2100 connection as it has been replaced by backup router. */
2101 SilcBuffer packet = silc_buffer_alloc(2);
2102 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2103 silc_buffer_format(packet,
2104 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
2105 SILC_STR_UI_CHAR(0),
2107 silc_server_packet_send(server, sock,
2108 SILC_PACKET_RESUME_ROUTER, 0,
2109 packet->data, packet->len, TRUE);
2110 silc_buffer_free(packet);
2112 /* Mark the router disabled. The data sent earlier will go but nothing
2113 after this does not go to this connection. */
2114 idata->status |= SILC_IDLIST_STATUS_DISABLED;
2116 /* If it is router announce our stuff to it. */
2117 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
2118 server->server_type == SILC_ROUTER) {
2119 silc_server_announce_servers(server, FALSE, 0, sock);
2120 silc_server_announce_clients(server, 0, sock);
2121 silc_server_announce_channels(server, 0, sock);
2128 /* Processes incoming New ID packet. New ID Payload is used to distribute
2129 information about newly registered clients and servers. */
2131 static void silc_server_new_id_real(SilcServer server,
2132 SilcSocketConnection sock,
2133 SilcPacketContext *packet,
2136 SilcBuffer buffer = packet->buffer;
2138 SilcServerEntry router, server_entry;
2139 SilcSocketConnection router_sock;
2144 SILC_LOG_DEBUG(("Processing new ID"));
2146 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2147 server->server_type == SILC_SERVER ||
2148 packet->src_id_type != SILC_ID_SERVER)
2151 idp = silc_id_payload_parse(buffer->data, buffer->len);
2155 id_type = silc_id_payload_get_type(idp);
2157 /* Normal server cannot have other normal server connections */
2158 server_entry = (SilcServerEntry)sock->user_data;
2159 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
2160 server_entry->server_type == SILC_SERVER)
2163 id = silc_id_payload_get_id(idp);
2167 /* If the packet is coming from server then use the sender as the
2168 origin of the the packet. If it came from router then check the real
2169 sender of the packet and use that as the origin. */
2170 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2171 id_list = server->local_list;
2173 router = sock->user_data;
2175 /* If the sender is backup router and ID is server (and we are not
2176 backup router) then switch the entry to global list. */
2177 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
2178 id_type == SILC_ID_SERVER &&
2179 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
2180 id_list = server->global_list;
2181 router_sock = server->router ? server->router->connection : sock;
2184 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2185 packet->src_id_type);
2186 router = silc_idlist_find_server_by_id(server->global_list,
2187 sender_id, TRUE, NULL);
2189 router = silc_idlist_find_server_by_id(server->local_list,
2190 sender_id, TRUE, NULL);
2191 silc_free(sender_id);
2193 id_list = server->global_list;
2200 case SILC_ID_CLIENT:
2202 SilcClientEntry entry;
2204 /* Check that we do not have this client already */
2205 entry = silc_idlist_find_client_by_id(server->global_list,
2206 id, server->server_type,
2209 entry = silc_idlist_find_client_by_id(server->local_list,
2210 id, server->server_type,
2213 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2217 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2218 silc_id_render(id, SILC_ID_CLIENT),
2219 sock->type == SILC_SOCKET_TYPE_SERVER ?
2220 "Server" : "Router", sock->hostname));
2222 /* As a router we keep information of all global information in our
2223 global list. Cell wide information however is kept in the local
2225 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2226 id, router, NULL, 0);
2228 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2230 /* Inform the sender that the ID is not usable */
2231 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2234 entry->nickname = NULL;
2235 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2237 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2238 server->stat.cell_clients++;
2239 server->stat.clients++;
2243 case SILC_ID_SERVER:
2245 SilcServerEntry entry;
2247 /* If the ID is mine, ignore it. */
2248 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2249 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2253 /* If the ID is the sender's ID, ignore it (we have it already) */
2254 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2255 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2259 /* Check that we do not have this server already */
2260 entry = silc_idlist_find_server_by_id(server->global_list,
2261 id, server->server_type,
2264 entry = silc_idlist_find_server_by_id(server->local_list,
2265 id, server->server_type,
2268 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2272 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2273 silc_id_render(id, SILC_ID_SERVER),
2274 sock->type == SILC_SOCKET_TYPE_SERVER ?
2275 "Server" : "Router", sock->hostname));
2277 /* As a router we keep information of all global information in our
2278 global list. Cell wide information however is kept in the local
2280 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2283 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2286 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2288 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2289 server->stat.cell_servers++;
2290 server->stat.servers++;
2294 case SILC_ID_CHANNEL:
2295 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2304 /* If the sender of this packet is server and we are router we need to
2305 broadcast this packet to other routers in the network. */
2306 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2307 sock->type == SILC_SOCKET_TYPE_SERVER &&
2308 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2309 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2310 silc_server_packet_send(server, server->router->connection,
2312 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2313 buffer->data, buffer->len, FALSE);
2314 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2315 packet->type, packet->flags,
2316 packet->buffer->data, packet->buffer->len,
2321 silc_id_payload_free(idp);
2325 /* Processes incoming New ID packet. New ID Payload is used to distribute
2326 information about newly registered clients and servers. */
2328 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2329 SilcPacketContext *packet)
2331 silc_server_new_id_real(server, sock, packet, TRUE);
2334 /* Receoved New Id List packet, list of New ID payloads inside one
2335 packet. Process the New ID payloads one by one. */
2337 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2338 SilcPacketContext *packet)
2340 SilcPacketContext *new_id;
2344 SILC_LOG_DEBUG(("Processing New ID List"));
2346 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2347 packet->src_id_type != SILC_ID_SERVER)
2350 /* If the sender of this packet is server and we are router we need to
2351 broadcast this packet to other routers in the network. Broadcast
2352 this list packet instead of multiple New ID packets. */
2353 if (!server->standalone && server->server_type == SILC_ROUTER &&
2354 sock->type == SILC_SOCKET_TYPE_SERVER &&
2355 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2356 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2357 silc_server_packet_send(server, server->router->connection,
2359 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2360 packet->buffer->data, packet->buffer->len, FALSE);
2361 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2362 packet->type, packet->flags,
2363 packet->buffer->data, packet->buffer->len,
2367 /* Make copy of the original packet context, except for the actual
2368 data buffer, which we will here now fetch from the original buffer. */
2369 new_id = silc_packet_context_alloc();
2370 new_id->type = SILC_PACKET_NEW_ID;
2371 new_id->flags = packet->flags;
2372 new_id->src_id = packet->src_id;
2373 new_id->src_id_len = packet->src_id_len;
2374 new_id->src_id_type = packet->src_id_type;
2375 new_id->dst_id = packet->dst_id;
2376 new_id->dst_id_len = packet->dst_id_len;
2377 new_id->dst_id_type = packet->dst_id_type;
2379 idp = silc_buffer_alloc(256);
2380 new_id->buffer = idp;
2382 while (packet->buffer->len) {
2383 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2384 if ((id_len > packet->buffer->len) ||
2385 (id_len > idp->truelen))
2388 silc_buffer_pull_tail(idp, 4 + id_len);
2389 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2391 /* Process the New ID */
2392 silc_server_new_id_real(server, sock, new_id, FALSE);
2394 silc_buffer_push_tail(idp, 4 + id_len);
2395 silc_buffer_pull(packet->buffer, 4 + id_len);
2398 silc_buffer_free(idp);
2402 /* Received New Channel packet. Information about new channels in the
2403 network are distributed using this packet. Save the information about
2404 the new channel. This usually comes from router but also normal server
2405 can send this to notify channels it has when it connects to us. */
2407 void silc_server_new_channel(SilcServer server,
2408 SilcSocketConnection sock,
2409 SilcPacketContext *packet)
2411 SilcChannelPayload payload;
2412 SilcChannelID *channel_id;
2414 SilcUInt32 name_len;
2418 SilcServerEntry server_entry;
2419 SilcChannelEntry channel;
2421 SILC_LOG_DEBUG(("Processing New Channel"));
2423 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2424 packet->src_id_type != SILC_ID_SERVER ||
2425 server->server_type == SILC_SERVER)
2428 /* Parse the channel payload */
2429 payload = silc_channel_payload_parse(packet->buffer->data,
2430 packet->buffer->len);
2434 /* Get the channel ID */
2435 channel_id = silc_channel_get_id_parse(payload);
2437 silc_channel_payload_free(payload);
2441 channel_name = silc_channel_get_name(payload, &name_len);
2443 channel_name[255] = '\0';
2445 id = silc_channel_get_id(payload, &id_len);
2447 server_entry = (SilcServerEntry)sock->user_data;
2449 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2450 /* Add the channel to global list as it is coming from router. It
2451 cannot be our own channel as it is coming from router. */
2453 /* Check that we don't already have this channel */
2454 channel = silc_idlist_find_channel_by_name(server->local_list,
2455 channel_name, NULL);
2457 channel = silc_idlist_find_channel_by_name(server->global_list,
2458 channel_name, NULL);
2460 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2461 silc_id_render(channel_id, SILC_ID_CHANNEL),
2464 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2465 0, channel_id, sock->user_data, NULL, NULL, 0);
2466 server->stat.channels++;
2469 /* The channel is coming from our server, thus it is in our cell
2470 we will add it to our local list. */
2473 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2474 silc_id_render(channel_id, SILC_ID_CHANNEL),
2477 /* Check that we don't already have this channel */
2478 channel = silc_idlist_find_channel_by_name(server->local_list,
2479 channel_name, NULL);
2481 channel = silc_idlist_find_channel_by_name(server->global_list,
2482 channel_name, NULL);
2484 /* If the channel does not exist, then create it. This creates a new
2485 key to the channel as well that we will send to the server. */
2487 /* The protocol says that the Channel ID's IP address must be based
2488 on the router's IP address. Check whether the ID is based in our
2489 IP and if it is not then create a new ID and enforce the server
2490 to switch the ID. */
2491 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2492 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2494 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2496 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2497 silc_server_send_notify_channel_change(server, sock, FALSE,
2499 silc_free(channel_id);
2504 /* Create the channel with the provided Channel ID */
2505 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2509 silc_channel_payload_free(payload);
2510 silc_free(channel_id);
2514 /* Get the mode and set it to the channel */
2515 channel->mode = silc_channel_get_mode(payload);
2517 /* Send the new channel key to the server */
2518 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2519 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2520 chk = silc_channel_key_payload_encode(id_len, id,
2521 strlen(channel->channel_key->
2523 channel->channel_key->cipher->name,
2524 channel->key_len / 8,
2526 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2527 chk->data, chk->len, FALSE);
2528 silc_buffer_free(chk);
2531 /* The channel exist by that name, check whether the ID's match.
2532 If they don't then we'll force the server to use the ID we have.
2533 We also create a new key for the channel. */
2534 SilcBuffer users = NULL, users_modes = NULL;
2536 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2537 /* They don't match, send CHANNEL_CHANGE notify to the server to
2538 force the ID change. */
2539 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2540 silc_server_send_notify_channel_change(server, sock, FALSE,
2541 channel_id, channel->id);
2544 /* If the mode is different from what we have then enforce the
2546 mode = silc_channel_get_mode(payload);
2547 if (channel->mode != mode) {
2548 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2549 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2550 channel->mode, server->id,
2552 channel->cipher, channel->hmac_name,
2553 channel->passphrase);
2556 /* Create new key for the channel and send it to the server and
2557 everybody else possibly on the channel. */
2559 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2560 if (!silc_server_create_channel_key(server, channel, 0))
2563 /* Send to the channel */
2564 silc_server_send_channel_key(server, sock, channel, FALSE);
2565 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2566 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2568 /* Send to the server */
2569 chk = silc_channel_key_payload_encode(id_len, id,
2570 strlen(channel->channel_key->
2572 channel->channel_key->
2574 channel->key_len / 8,
2576 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2577 chk->data, chk->len, FALSE);
2578 silc_buffer_free(chk);
2582 silc_free(channel_id);
2584 /* Since the channel is coming from server and we also know about it
2585 then send the JOIN notify to the server so that it see's our
2586 users on the channel "joining" the channel. */
2587 silc_server_announce_get_channel_users(server, channel, &users,
2590 silc_buffer_push(users, users->data - users->head);
2591 silc_server_packet_send(server, sock,
2592 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2593 users->data, users->len, FALSE);
2594 silc_buffer_free(users);
2597 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2598 silc_server_packet_send_dest(server, sock,
2599 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2600 channel->id, SILC_ID_CHANNEL,
2602 users_modes->len, FALSE);
2603 silc_buffer_free(users_modes);
2608 silc_channel_payload_free(payload);
2611 /* Received New Channel List packet, list of New Channel List payloads inside
2612 one packet. Process the New Channel payloads one by one. */
2614 void silc_server_new_channel_list(SilcServer server,
2615 SilcSocketConnection sock,
2616 SilcPacketContext *packet)
2618 SilcPacketContext *new;
2620 SilcUInt16 len1, len2;
2622 SILC_LOG_DEBUG(("Processing New Channel List"));
2624 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2625 packet->src_id_type != SILC_ID_SERVER ||
2626 server->server_type == SILC_SERVER)
2629 /* If the sender of this packet is server and we are router we need to
2630 broadcast this packet to other routers in the network. Broadcast
2631 this list packet instead of multiple New Channel packets. */
2632 if (!server->standalone && server->server_type == SILC_ROUTER &&
2633 sock->type == SILC_SOCKET_TYPE_SERVER &&
2634 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2635 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2636 silc_server_packet_send(server, server->router->connection,
2638 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2639 packet->buffer->data, packet->buffer->len, FALSE);
2640 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2641 packet->type, packet->flags,
2642 packet->buffer->data, packet->buffer->len,
2646 /* Make copy of the original packet context, except for the actual
2647 data buffer, which we will here now fetch from the original buffer. */
2648 new = silc_packet_context_alloc();
2649 new->type = SILC_PACKET_NEW_CHANNEL;
2650 new->flags = packet->flags;
2651 new->src_id = packet->src_id;
2652 new->src_id_len = packet->src_id_len;
2653 new->src_id_type = packet->src_id_type;
2654 new->dst_id = packet->dst_id;
2655 new->dst_id_len = packet->dst_id_len;
2656 new->dst_id_type = packet->dst_id_type;
2658 buffer = silc_buffer_alloc(512);
2659 new->buffer = buffer;
2661 while (packet->buffer->len) {
2662 SILC_GET16_MSB(len1, packet->buffer->data);
2663 if ((len1 > packet->buffer->len) ||
2664 (len1 > buffer->truelen))
2667 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2668 if ((len2 > packet->buffer->len) ||
2669 (len2 > buffer->truelen))
2672 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2673 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2675 /* Process the New Channel */
2676 silc_server_new_channel(server, sock, new);
2678 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2679 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2682 silc_buffer_free(buffer);
2686 /* Received key agreement packet. This packet is never for us. It is to
2687 the client in the packet's destination ID. Sending of this sort of packet
2688 equals sending private message, ie. it is sent point to point from
2689 one client to another. */
2691 void silc_server_key_agreement(SilcServer server,
2692 SilcSocketConnection sock,
2693 SilcPacketContext *packet)
2695 SilcSocketConnection dst_sock;
2696 SilcIDListData idata;
2698 SILC_LOG_DEBUG(("Start"));
2700 if (packet->src_id_type != SILC_ID_CLIENT ||
2701 packet->dst_id_type != SILC_ID_CLIENT)
2704 if (!packet->dst_id)
2707 /* Get the route to the client */
2708 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2709 packet->dst_id_len, NULL, &idata);
2713 /* Relay the packet */
2714 silc_server_relay_packet(server, dst_sock, idata->send_key,
2715 idata->hmac_send, idata->psn_send++,
2719 /* Received connection auth request packet that is used during connection
2720 phase to resolve the mandatory authentication method. This packet can
2721 actually be received at anytime but usually it is used only during
2722 the connection authentication phase. Now, protocol says that this packet
2723 can come from client or server, however, we support only this coming
2724 from client and expect that server always knows what authentication
2727 void silc_server_connection_auth_request(SilcServer server,
2728 SilcSocketConnection sock,
2729 SilcPacketContext *packet)
2731 SilcServerConfigClient *client = NULL;
2732 SilcUInt16 conn_type;
2734 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
2736 SILC_LOG_DEBUG(("Start"));
2738 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2741 /* Parse the payload */
2742 ret = silc_buffer_unformat(packet->buffer,
2743 SILC_STR_UI_SHORT(&conn_type),
2744 SILC_STR_UI_SHORT(NULL),
2749 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2752 /* Get the authentication method for the client */
2753 auth_meth = SILC_AUTH_NONE;
2754 client = silc_server_config_find_client(server, sock->ip);
2756 client = silc_server_config_find_client(server, sock->hostname);
2758 if (client->passphrase) {
2759 if (client->publickeys && !server->config->prefer_passphrase_auth)
2760 auth_meth = SILC_AUTH_PUBLIC_KEY;
2762 auth_meth = SILC_AUTH_PASSWORD;
2763 } else if (client->publickeys)
2764 auth_meth = SILC_AUTH_PUBLIC_KEY;
2767 /* Send it back to the client */
2768 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
2771 /* Received REKEY packet. The sender of the packet wants to regenerate
2772 its session keys. This starts the REKEY protocol. */
2774 void silc_server_rekey(SilcServer server,
2775 SilcSocketConnection sock,
2776 SilcPacketContext *packet)
2778 SilcProtocol protocol;
2779 SilcServerRekeyInternalContext *proto_ctx;
2780 SilcIDListData idata = (SilcIDListData)sock->user_data;
2782 SILC_LOG_DEBUG(("Start"));
2784 /* Allocate internal protocol context. This is sent as context
2786 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2787 proto_ctx->server = (void *)server;
2788 proto_ctx->sock = sock;
2789 proto_ctx->responder = TRUE;
2790 proto_ctx->pfs = idata->rekey->pfs;
2792 /* Perform rekey protocol. Will call the final callback after the
2793 protocol is over. */
2794 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2795 &protocol, proto_ctx, silc_server_rekey_final);
2796 sock->protocol = protocol;
2798 if (proto_ctx->pfs == FALSE)
2799 /* Run the protocol */
2800 silc_protocol_execute(protocol, server->schedule, 0, 0);
2803 /* Received file transger packet. This packet is never for us. It is to
2804 the client in the packet's destination ID. Sending of this sort of packet
2805 equals sending private message, ie. it is sent point to point from
2806 one client to another. */
2808 void silc_server_ftp(SilcServer server,
2809 SilcSocketConnection sock,
2810 SilcPacketContext *packet)
2812 SilcSocketConnection dst_sock;
2813 SilcIDListData idata;
2815 SILC_LOG_DEBUG(("Start"));
2817 if (packet->src_id_type != SILC_ID_CLIENT ||
2818 packet->dst_id_type != SILC_ID_CLIENT)
2821 if (!packet->dst_id)
2824 /* Get the route to the client */
2825 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2826 packet->dst_id_len, NULL, &idata);
2830 /* Relay the packet */
2831 silc_server_relay_packet(server, dst_sock, idata->send_key,
2832 idata->hmac_send, idata->psn_send++,