5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to handle received packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 extern char *server_version;
30 /* Received notify packet. Server can receive notify packets from router.
31 Server then relays the notify messages to clients if needed. */
33 void silc_server_notify(SilcServer server,
34 SilcSocketConnection sock,
35 SilcPacketContext *packet)
37 SilcNotifyPayload payload;
39 SilcArgumentPayload args;
40 SilcChannelID *channel_id, *channel_id2;
41 SilcClientID *client_id, *client_id2;
42 SilcServerID *server_id;
43 SilcChannelEntry channel;
44 SilcClientEntry client;
45 SilcServerEntry server_entry;
46 SilcChannelClientEntry chl;
47 SilcIDCacheEntry cache;
48 SilcHashTableList htl;
53 SILC_LOG_DEBUG(("Start"));
55 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
56 packet->src_id_type != SILC_ID_SERVER)
62 /* If the packet is destined directly to a client then relay the packet
63 before processing it. */
64 if (packet->dst_id_type == SILC_ID_CLIENT) {
66 SilcSocketConnection dst_sock;
68 /* Get the route to the client */
69 dst_sock = silc_server_get_client_route(server, packet->dst_id,
70 packet->dst_id_len, NULL, &idata);
72 /* Relay the packet */
73 silc_server_relay_packet(server, dst_sock, idata->send_key,
74 idata->hmac_receive, packet, TRUE);
77 /* If we are router and this packet is not already broadcast packet
78 we will broadcast it. The sending socket really cannot be router or
79 the router is buggy. If this packet is coming from router then it must
80 have the broadcast flag set already and we won't do anything. */
81 if (!server->standalone && server->server_type == SILC_ROUTER &&
82 sock->type == SILC_SOCKET_TYPE_SERVER &&
83 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
84 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
85 silc_server_packet_send(server, server->router->connection, packet->type,
86 packet->flags | SILC_PACKET_FLAG_BROADCAST,
87 packet->buffer->data, packet->buffer->len, FALSE);
90 payload = silc_notify_payload_parse(packet->buffer);
94 type = silc_notify_get_type(payload);
95 args = silc_notify_get_args(payload);
100 case SILC_NOTIFY_TYPE_JOIN:
102 * Distribute the notify to local clients on the channel
104 SILC_LOG_DEBUG(("JOIN notify"));
107 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
110 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
114 /* Get channel entry */
115 channel = silc_idlist_find_channel_by_id(server->global_list,
118 channel = silc_idlist_find_channel_by_id(server->local_list,
121 silc_free(channel_id);
125 silc_free(channel_id);
128 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
131 client_id = silc_id_payload_parse_id(tmp, tmp_len);
135 /* Send to channel */
136 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
137 FALSE, packet->buffer->data,
138 packet->buffer->len, FALSE);
140 /* If the the client is not in local list we check global list (ie. the
141 channel will be global channel) and if it does not exist then create
142 entry for the client. */
143 client = silc_idlist_find_client_by_id(server->global_list,
146 client = silc_idlist_find_client_by_id(server->local_list,
149 /* If router did not find the client the it is bogus */
150 if (server->server_type == SILC_ROUTER)
154 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
155 silc_id_dup(client_id, SILC_ID_CLIENT),
156 sock->user_data, NULL);
158 silc_free(client_id);
162 client->data.registered = TRUE;
166 /* Do not process the notify if the client is not registered */
167 if (client->data.registered == FALSE)
170 /* Do not add client to channel if it is there already */
171 if (silc_server_client_on_channel(client, channel))
174 if (server->server_type == SILC_SERVER &&
175 sock->type == SILC_SOCKET_TYPE_ROUTER)
176 /* The channel is global now */
177 channel->global_users = TRUE;
179 /* JOIN the global client to the channel (local clients (if router
180 created the channel) is joined in the pending JOIN command). */
181 chl = silc_calloc(1, sizeof(*chl));
182 chl->client = client;
183 chl->channel = channel;
184 silc_hash_table_add(channel->user_list, client, chl);
185 silc_hash_table_add(client->channels, channel, chl);
186 silc_free(client_id);
190 case SILC_NOTIFY_TYPE_LEAVE:
192 * Distribute the notify to local clients on the channel
194 SILC_LOG_DEBUG(("LEAVE notify"));
196 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
197 packet->dst_id_type);
201 /* Get channel entry */
202 channel = silc_idlist_find_channel_by_id(server->global_list,
205 channel = silc_idlist_find_channel_by_id(server->local_list,
208 silc_free(channel_id);
214 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
216 silc_free(channel_id);
219 client_id = silc_id_payload_parse_id(tmp, tmp_len);
221 silc_free(channel_id);
225 /* Send to channel */
226 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
227 FALSE, packet->buffer->data,
228 packet->buffer->len, FALSE);
230 /* Get client entry */
231 client = silc_idlist_find_client_by_id(server->global_list,
234 client = silc_idlist_find_client_by_id(server->local_list,
237 silc_free(client_id);
238 silc_free(channel_id);
242 silc_free(client_id);
244 /* Remove the user from channel */
245 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
248 case SILC_NOTIFY_TYPE_SIGNOFF:
250 * Distribute the notify to local clients on the channel
252 SILC_LOG_DEBUG(("SIGNOFF notify"));
255 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
258 client_id = silc_id_payload_parse_id(tmp, tmp_len);
262 /* Get client entry */
263 client = silc_idlist_find_client_by_id(server->global_list,
266 client = silc_idlist_find_client_by_id(server->local_list,
269 silc_free(client_id);
273 silc_free(client_id);
275 /* Get signoff message */
276 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
280 /* Remove the client from all channels. */
281 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
283 client->data.registered = FALSE;
284 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
285 server->stat.clients--;
286 if (server->server_type == SILC_ROUTER)
287 server->stat.cell_clients--;
290 case SILC_NOTIFY_TYPE_TOPIC_SET:
292 * Distribute the notify to local clients on the channel
295 SILC_LOG_DEBUG(("TOPIC SET notify"));
297 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
298 packet->dst_id_type);
302 /* Get channel entry */
303 channel = silc_idlist_find_channel_by_id(server->global_list,
306 channel = silc_idlist_find_channel_by_id(server->local_list,
309 silc_free(channel_id);
315 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
317 silc_free(channel_id);
322 silc_free(channel->topic);
323 channel->topic = silc_calloc(tmp_len + 1, sizeof(*channel->topic));
324 memcpy(channel->topic, tmp, tmp_len);
326 /* Send the same notify to the channel */
327 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
328 FALSE, packet->buffer->data,
329 packet->buffer->len, FALSE);
330 silc_free(channel_id);
333 case SILC_NOTIFY_TYPE_NICK_CHANGE:
336 * Distribute the notify to local clients on the channel
338 unsigned char *id, *id2;
340 SILC_LOG_DEBUG(("NICK CHANGE notify"));
342 /* Get old client ID */
343 id = silc_argument_get_arg_type(args, 1, &tmp_len);
346 client_id = silc_id_payload_parse_id(id, tmp_len);
350 /* Get new client ID */
351 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
354 client_id2 = silc_id_payload_parse_id(id2, tmp_len);
358 SILC_LOG_DEBUG(("Old Client ID id(%s)",
359 silc_id_render(client_id, SILC_ID_CLIENT)));
360 SILC_LOG_DEBUG(("New Client ID id(%s)",
361 silc_id_render(client_id2, SILC_ID_CLIENT)));
363 /* Replace the Client ID */
364 client = silc_idlist_replace_client_id(server->global_list, client_id,
367 client = silc_idlist_replace_client_id(server->local_list, client_id,
371 /* The nickname is not valid anymore, set it NULL. This causes that
372 the nickname will be queried if someone wants to know it. */
373 if (client->nickname)
374 silc_free(client->nickname);
375 client->nickname = NULL;
377 /* Send the NICK_CHANGE notify type to local clients on the channels
378 this client is joined to. */
379 silc_server_send_notify_on_channels(server, NULL, client,
380 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
385 silc_free(client_id);
387 silc_free(client_id2);
391 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
393 * Distribute the notify to local clients on the channel
396 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
398 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
399 packet->dst_id_type);
403 /* Get channel entry */
404 channel = silc_idlist_find_channel_by_id(server->global_list,
407 channel = silc_idlist_find_channel_by_id(server->local_list,
410 silc_free(channel_id);
415 /* Send the same notify to the channel */
416 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
417 FALSE, packet->buffer->data,
418 packet->buffer->len, FALSE);
421 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
423 silc_free(channel_id);
427 SILC_GET32_MSB(mode, tmp);
429 /* If the channel had private keys set and the mode was removed then
430 we must re-generate and re-distribute a new channel key */
431 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
432 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
433 /* Re-generate channel key */
434 silc_server_create_channel_key(server, channel, 0);
436 /* Send the channel key. This sends it to our local clients and if
437 we are normal server to our router as well. */
438 silc_server_send_channel_key(server, NULL, channel,
439 server->server_type == SILC_ROUTER ?
440 FALSE : !server->standalone);
444 channel->mode = mode;
445 silc_free(channel_id);
448 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
450 unsigned char hash[32];
453 silc_hmac_free(channel->hmac);
454 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
457 /* Set the HMAC key out of current channel key. The client must do
459 silc_hash_make(channel->hmac->hash, channel->key, channel->key_len / 8,
461 silc_hmac_set_key(channel->hmac, hash,
462 silc_hash_len(channel->hmac->hash));
463 memset(hash, 0, sizeof(hash));
468 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
471 * Distribute the notify to local clients on the channel
473 SilcChannelClientEntry chl2 = NULL;
474 bool notify_sent = FALSE;
476 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
478 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
479 packet->dst_id_type);
483 /* Get channel entry */
484 channel = silc_idlist_find_channel_by_id(server->global_list,
487 channel = silc_idlist_find_channel_by_id(server->local_list,
490 silc_free(channel_id);
496 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
498 silc_free(channel_id);
502 SILC_GET32_MSB(mode, tmp);
504 /* Get target client */
505 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
508 client_id = silc_id_payload_parse_id(tmp, tmp_len);
512 /* Get client entry */
513 client = silc_idlist_find_client_by_id(server->global_list,
516 client = silc_idlist_find_client_by_id(server->local_list,
519 silc_free(client_id);
523 silc_free(client_id);
525 /* Get entry to the channel user list */
526 silc_hash_table_list(channel->user_list, &htl);
527 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
528 /* If the mode is channel founder and we already find a client
529 to have that mode on the channel we will enforce the sender
530 to change the channel founder mode away. There can be only one
531 channel founder on the channel. */
532 if (server->server_type == SILC_ROUTER &&
533 mode & SILC_CHANNEL_UMODE_CHANFO &&
534 chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
536 unsigned char cumode[4];
538 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
539 silc_server_send_notify_cumode(server, sock, FALSE, channel, mode,
540 client->id, SILC_ID_CLIENT,
543 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
544 SILC_PUT32_MSB(mode, cumode);
545 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
546 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
547 3, idp->data, idp->len,
549 idp->data, idp->len);
550 silc_buffer_free(idp);
553 /* Force the mode change if we alredy set the mode */
556 silc_free(channel_id);
561 if (chl->client == client) {
562 /* Change the mode */
564 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
571 /* Send the same notify to the channel */
573 silc_server_packet_send_to_channel(server, sock, channel,
575 FALSE, packet->buffer->data,
576 packet->buffer->len, FALSE);
578 silc_free(channel_id);
582 case SILC_NOTIFY_TYPE_INVITE:
584 if (packet->dst_id_type == SILC_ID_CLIENT)
587 SILC_LOG_DEBUG(("INVITE notify"));
590 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
593 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
597 /* Get channel entry */
598 channel = silc_idlist_find_channel_by_id(server->global_list,
601 channel = silc_idlist_find_channel_by_id(server->local_list,
604 silc_free(channel_id);
608 silc_free(channel_id);
610 /* Get the added invite */
611 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
613 if (!channel->invite_list)
614 channel->invite_list = silc_calloc(tmp_len + 2,
615 sizeof(*channel->invite_list));
617 channel->invite_list = silc_realloc(channel->invite_list,
618 sizeof(*channel->invite_list) *
620 strlen(channel->invite_list) +
622 if (tmp[tmp_len - 1] == ',')
623 tmp[tmp_len - 1] = '\0';
625 strncat(channel->invite_list, tmp, tmp_len);
626 strncat(channel->invite_list, ",", 1);
629 /* Get the deleted invite */
630 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
631 if (tmp && channel->invite_list) {
632 char *start, *end, *n;
634 if (!strncmp(channel->invite_list, tmp,
635 strlen(channel->invite_list) - 1)) {
636 silc_free(channel->invite_list);
637 channel->invite_list = NULL;
639 start = strstr(channel->invite_list, tmp);
640 if (start && strlen(start) >= tmp_len) {
641 end = start + tmp_len;
642 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
643 strncat(n, channel->invite_list, start - channel->invite_list);
644 strncat(n, end + 1, ((channel->invite_list +
645 strlen(channel->invite_list)) - end) - 1);
646 silc_free(channel->invite_list);
647 channel->invite_list = n;
654 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
656 * Distribute to the local clients on the channel and change the
660 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
662 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
665 /* Get the old Channel ID */
666 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
669 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
673 /* Get the channel entry */
674 channel = silc_idlist_find_channel_by_id(server->global_list,
677 channel = silc_idlist_find_channel_by_id(server->local_list,
680 silc_free(channel_id);
685 /* Send the notify to the channel */
686 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
687 FALSE, packet->buffer->data,
688 packet->buffer->len, FALSE);
690 /* Get the new Channel ID */
691 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
694 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len);
698 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
699 silc_id_render(channel_id, SILC_ID_CHANNEL)));
700 SILC_LOG_DEBUG(("New Channel ID id(%s)",
701 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
703 /* Replace the Channel ID */
704 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
706 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
708 silc_free(channel_id2);
713 SilcBuffer users = NULL, users_modes = NULL;
715 /* Re-announce our clients on the channel as the ID has changed now */
716 silc_server_announce_get_channel_users(server, channel, &users,
719 silc_buffer_push(users, users->data - users->head);
720 silc_server_packet_send(server, sock,
721 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
722 users->data, users->len, FALSE);
723 silc_buffer_free(users);
726 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
727 silc_server_packet_send_dest(server, sock,
728 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
729 channel->id, SILC_ID_CHANNEL,
731 users_modes->len, FALSE);
732 silc_buffer_free(users_modes);
736 silc_free(channel_id);
740 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
742 * Remove the server entry and all clients that this server owns.
745 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
748 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
751 server_id = silc_id_payload_parse_id(tmp, tmp_len);
755 /* Get server entry */
756 server_entry = silc_idlist_find_server_by_id(server->global_list,
759 server_entry = silc_idlist_find_server_by_id(server->local_list,
762 silc_free(server_id);
766 silc_free(server_id);
768 /* Free all client entries that this server owns as they will
769 become invalid now as well. */
770 silc_server_remove_clients_by_server(server, server_entry, TRUE);
772 /* Remove the server entry */
773 if (!silc_idlist_del_server(server->global_list, server_entry))
774 silc_idlist_del_server(server->local_list, server_entry);
776 /* XXX update statistics */
780 case SILC_NOTIFY_TYPE_KICKED:
782 * Distribute the notify to local clients on the channel
785 SILC_LOG_DEBUG(("KICKED notify"));
787 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
788 packet->dst_id_type);
792 /* Get channel entry */
793 channel = silc_idlist_find_channel_by_id(server->global_list,
796 channel = silc_idlist_find_channel_by_id(server->local_list,
799 silc_free(channel_id);
803 silc_free(channel_id);
806 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
809 client_id = silc_id_payload_parse_id(tmp, tmp_len);
813 /* Send to channel */
814 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
815 FALSE, packet->buffer->data,
816 packet->buffer->len, FALSE);
818 /* If the the client is not in local list we check global list */
819 client = silc_idlist_find_client_by_id(server->global_list,
822 client = silc_idlist_find_client_by_id(server->local_list,
825 silc_free(client_id);
830 /* Remove the client from channel */
831 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
835 case SILC_NOTIFY_TYPE_KILLED:
838 * Distribute the notify to local clients on channels
843 SILC_LOG_DEBUG(("KILLED notify"));
846 id = silc_argument_get_arg_type(args, 1, &id_len);
849 client_id = silc_id_payload_parse_id(id, id_len);
853 /* If the the client is not in local list we check global list */
854 client = silc_idlist_find_client_by_id(server->global_list,
857 client = silc_idlist_find_client_by_id(server->local_list,
860 silc_free(client_id);
864 silc_free(client_id);
866 /* If the client is one of ours, then close the connection to the
867 client now. This removes the client from all channels as well. */
868 if (packet->dst_id_type == SILC_ID_CLIENT && client->data.registered &&
869 client->connection) {
870 sock = client->connection;
871 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
872 silc_server_close_connection(server, sock);
877 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
881 /* Send the notify to local clients on the channels except to the
882 client who is killed. */
883 silc_server_send_notify_on_channels(server, client, client,
884 SILC_NOTIFY_TYPE_KILLED,
889 /* Remove the client from all channels */
890 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
896 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
898 * Save the mode of the client.
901 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
904 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
907 client_id = silc_id_payload_parse_id(tmp, tmp_len);
911 /* Get client entry */
912 client = silc_idlist_find_client_by_id(server->global_list,
915 client = silc_idlist_find_client_by_id(server->local_list,
918 silc_free(client_id);
922 silc_free(client_id);
925 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
930 SILC_GET32_MSB(client->mode, tmp);
934 case SILC_NOTIFY_TYPE_BAN:
939 SILC_LOG_DEBUG(("BAN notify"));
942 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
945 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
949 /* Get channel entry */
950 channel = silc_idlist_find_channel_by_id(server->global_list,
953 channel = silc_idlist_find_channel_by_id(server->local_list,
956 silc_free(channel_id);
960 silc_free(channel_id);
962 /* Get the new ban and add it to the ban list */
963 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
965 if (!channel->ban_list)
966 channel->ban_list = silc_calloc(tmp_len + 2,
967 sizeof(*channel->ban_list));
969 channel->ban_list = silc_realloc(channel->ban_list,
970 sizeof(*channel->ban_list) *
972 strlen(channel->ban_list) + 2));
973 strncat(channel->ban_list, tmp, tmp_len);
974 strncat(channel->ban_list, ",", 1);
977 /* Get the ban to be removed and remove it from the list */
978 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
979 if (tmp && channel->ban_list) {
980 char *start, *end, *n;
982 if (!strcmp(channel->ban_list, tmp)) {
983 silc_free(channel->ban_list);
984 channel->ban_list = NULL;
986 start = strstr(channel->ban_list, tmp);
987 if (start && strlen(start) >= tmp_len) {
988 end = start + tmp_len;
989 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
990 strncat(n, channel->ban_list, start - channel->ban_list);
991 strncat(n, end + 1, ((channel->ban_list +
992 strlen(channel->ban_list)) - end) - 1);
993 silc_free(channel->ban_list);
994 channel->ban_list = n;
1001 /* Ignore rest of the notify types for now */
1002 case SILC_NOTIFY_TYPE_NONE:
1003 case SILC_NOTIFY_TYPE_MOTD:
1010 silc_notify_payload_free(payload);
1013 void silc_server_notify_list(SilcServer server,
1014 SilcSocketConnection sock,
1015 SilcPacketContext *packet)
1017 SilcPacketContext *new;
1021 SILC_LOG_DEBUG(("Processing Notify List"));
1023 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1024 packet->src_id_type != SILC_ID_SERVER)
1027 /* Make copy of the original packet context, except for the actual
1028 data buffer, which we will here now fetch from the original buffer. */
1029 new = silc_packet_context_alloc();
1030 new->type = SILC_PACKET_NOTIFY;
1031 new->flags = packet->flags;
1032 new->src_id = packet->src_id;
1033 new->src_id_len = packet->src_id_len;
1034 new->src_id_type = packet->src_id_type;
1035 new->dst_id = packet->dst_id;
1036 new->dst_id_len = packet->dst_id_len;
1037 new->dst_id_type = packet->dst_id_type;
1039 buffer = silc_buffer_alloc(1024);
1040 new->buffer = buffer;
1042 while (packet->buffer->len) {
1043 SILC_GET16_MSB(len, packet->buffer->data + 2);
1044 if (len > packet->buffer->len)
1047 if (len > buffer->truelen) {
1048 silc_buffer_free(buffer);
1049 buffer = silc_buffer_alloc(1024 + len);
1052 silc_buffer_pull_tail(buffer, len);
1053 silc_buffer_put(buffer, packet->buffer->data, len);
1055 /* Process the Notify */
1056 silc_server_notify(server, sock, new);
1058 silc_buffer_push_tail(buffer, len);
1059 silc_buffer_pull(packet->buffer, len);
1062 silc_buffer_free(buffer);
1066 /* Received private message. This resolves the destination of the message
1067 and sends the packet. This is used by both server and router. If the
1068 destination is our locally connected client this sends the packet to
1069 the client. This may also send the message for further routing if
1070 the destination is not in our server (or router). */
1072 void silc_server_private_message(SilcServer server,
1073 SilcSocketConnection sock,
1074 SilcPacketContext *packet)
1076 SilcSocketConnection dst_sock;
1077 SilcIDListData idata;
1079 SILC_LOG_DEBUG(("Start"));
1081 if (packet->src_id_type != SILC_ID_CLIENT ||
1082 packet->dst_id_type != SILC_ID_CLIENT)
1085 if (!packet->dst_id)
1088 /* Get the route to the client */
1089 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1090 packet->dst_id_len, NULL, &idata);
1094 /* Send the private message */
1095 silc_server_send_private_message(server, dst_sock, idata->send_key,
1096 idata->hmac_send, packet);
1099 /* Received private message key packet.. This packet is never for us. It is to
1100 the client in the packet's destination ID. Sending of this sort of packet
1101 equals sending private message, ie. it is sent point to point from
1102 one client to another. */
1104 void silc_server_private_message_key(SilcServer server,
1105 SilcSocketConnection sock,
1106 SilcPacketContext *packet)
1108 SilcSocketConnection dst_sock;
1109 SilcIDListData idata;
1111 SILC_LOG_DEBUG(("Start"));
1113 if (packet->src_id_type != SILC_ID_CLIENT ||
1114 packet->dst_id_type != SILC_ID_CLIENT)
1117 if (!packet->dst_id)
1120 /* Get the route to the client */
1121 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1122 packet->dst_id_len, NULL, &idata);
1126 /* Relay the packet */
1127 silc_server_relay_packet(server, dst_sock, idata->send_key,
1128 idata->hmac_send, packet, FALSE);
1131 /* Processes incoming command reply packet. The command reply packet may
1132 be destined to one of our clients or it may directly for us. We will
1133 call the command reply routine after processing the packet. */
1135 void silc_server_command_reply(SilcServer server,
1136 SilcSocketConnection sock,
1137 SilcPacketContext *packet)
1139 SilcBuffer buffer = packet->buffer;
1140 SilcClientEntry client = NULL;
1141 SilcSocketConnection dst_sock;
1142 SilcIDListData idata;
1143 SilcClientID *id = NULL;
1145 SILC_LOG_DEBUG(("Start"));
1147 /* Source must be server or router */
1148 if (packet->src_id_type != SILC_ID_SERVER &&
1149 sock->type != SILC_SOCKET_TYPE_ROUTER)
1152 if (packet->dst_id_type == SILC_ID_CHANNEL)
1155 if (packet->dst_id_type == SILC_ID_CLIENT) {
1156 /* Destination must be one of ours */
1157 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1160 client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
1162 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1168 if (packet->dst_id_type == SILC_ID_SERVER) {
1169 /* For now this must be for us */
1170 if (memcmp(packet->dst_id, server->id_string, packet->dst_id_len)) {
1171 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1176 /* Execute command reply locally for the command */
1177 silc_server_command_reply_process(server, sock, buffer);
1179 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1180 /* Relay the packet to the client */
1182 dst_sock = (SilcSocketConnection)client->connection;
1183 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1184 + packet->dst_id_len + packet->padlen);
1186 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
1187 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
1189 idata = (SilcIDListData)client;
1191 /* Encrypt packet */
1192 silc_packet_encrypt(idata->send_key, idata->hmac_send, dst_sock->outbuf,
1195 /* Send the packet */
1196 silc_server_packet_send_real(server, dst_sock, TRUE);
1202 /* Process received channel message. The message can be originated from
1203 client or server. */
1205 void silc_server_channel_message(SilcServer server,
1206 SilcSocketConnection sock,
1207 SilcPacketContext *packet)
1209 SilcChannelEntry channel = NULL;
1210 SilcChannelID *id = NULL;
1211 void *sender = NULL;
1212 void *sender_entry = NULL;
1214 SILC_LOG_DEBUG(("Processing channel message"));
1217 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1218 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1222 /* Find channel entry */
1223 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1226 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1228 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1230 SILC_LOG_DEBUG(("Could not find channel"));
1235 /* See that this client is on the channel. If the original sender is
1236 not client (as it can be server as well) we don't do the check. */
1237 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
1238 packet->src_id_type);
1241 if (packet->src_id_type == SILC_ID_CLIENT) {
1242 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1245 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1247 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1249 SILC_LOG_DEBUG(("Client not on channel"));
1254 /* Distribute the packet to our local clients. This will send the
1255 packet for further routing as well, if needed. */
1256 silc_server_packet_relay_to_channel(server, sock, channel, sender,
1257 packet->src_id_type, sender_entry,
1258 packet->buffer->data,
1259 packet->buffer->len, FALSE);
1268 /* Received channel key packet. We distribute the key to all of our locally
1269 connected clients on the channel. */
1271 void silc_server_channel_key(SilcServer server,
1272 SilcSocketConnection sock,
1273 SilcPacketContext *packet)
1275 SilcBuffer buffer = packet->buffer;
1276 SilcChannelEntry channel;
1278 if (packet->src_id_type != SILC_ID_SERVER ||
1279 (server->server_type == SILC_ROUTER &&
1280 sock->type == SILC_SOCKET_TYPE_ROUTER))
1283 /* Save the channel key */
1284 channel = silc_server_save_channel_key(server, buffer, NULL);
1288 /* Distribute the key to everybody who is on the channel. If we are router
1289 we will also send it to locally connected servers. */
1290 silc_server_send_channel_key(server, sock, channel, FALSE);
1293 /* Received New Client packet and processes it. Creates Client ID for the
1294 client. Client becomes registered after calling this functions. */
1296 SilcClientEntry silc_server_new_client(SilcServer server,
1297 SilcSocketConnection sock,
1298 SilcPacketContext *packet)
1300 SilcBuffer buffer = packet->buffer;
1301 SilcClientEntry client;
1302 SilcClientID *client_id;
1304 SilcIDListData idata;
1305 char *username = NULL, *realname = NULL, *id_string;
1308 char *hostname, *nickname;
1310 SILC_LOG_DEBUG(("Creating new client"));
1312 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1315 /* Take client entry */
1316 client = (SilcClientEntry)sock->user_data;
1317 idata = (SilcIDListData)client;
1319 /* Remove the old cache entry */
1320 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1321 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
1322 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1327 /* Parse incoming packet */
1328 ret = silc_buffer_unformat(buffer,
1329 SILC_STR_UI16_STRING_ALLOC(&username),
1330 SILC_STR_UI16_STRING_ALLOC(&realname),
1334 silc_free(username);
1336 silc_free(realname);
1337 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1338 "Incomplete client information");
1343 silc_free(username);
1345 silc_free(realname);
1346 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1347 "Incomplete client information");
1351 if (strlen(username) > 128)
1352 username[127] = '\0';
1354 nickname = strdup(username);
1356 /* Make sanity checks for the hostname of the client. If the hostname
1357 is provided in the `username' check that it is the same than the
1358 resolved hostname, or if not resolved the hostname that appears in
1359 the client's public key. If the hostname is not present then put
1360 it from the resolved name or from the public key. */
1361 if (strchr(username, '@')) {
1362 SilcPublicKeyIdentifier pident;
1363 int tlen = strcspn(username, "@");
1364 char *phostname = NULL;
1366 hostname = silc_calloc((strlen(username) - tlen) + 1, sizeof(char));
1367 memcpy(hostname, username + tlen + 1, strlen(username) - tlen - 1);
1369 if (strcmp(sock->hostname, sock->ip) &&
1370 strcmp(sock->hostname, hostname)) {
1371 silc_free(username);
1372 silc_free(hostname);
1374 silc_free(realname);
1375 silc_server_disconnect_remote(server, sock,
1376 "Server closed connection: "
1377 "Incomplete client information");
1381 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1383 phostname = strdup(pident->host);
1384 silc_pkcs_free_identifier(pident);
1387 if (!strcmp(sock->hostname, sock->ip) &&
1388 phostname && strcmp(phostname, hostname)) {
1389 silc_free(username);
1390 silc_free(hostname);
1392 silc_free(phostname);
1394 silc_free(realname);
1395 silc_server_disconnect_remote(server, sock,
1396 "Server closed connection: "
1397 "Incomplete client information");
1402 silc_free(phostname);
1404 /* The hostname is not present, add it. */
1406 /* XXX For now we cannot take the host name from the public key since
1407 they are not trusted or we cannot verify them as trusted. Just take
1408 what the resolved name or address is. */
1410 if (strcmp(sock->hostname, sock->ip)) {
1412 newusername = silc_calloc(strlen(username) +
1413 strlen(sock->hostname) + 2,
1414 sizeof(*newusername));
1415 strncat(newusername, username, strlen(username));
1416 strncat(newusername, "@", 1);
1417 strncat(newusername, sock->hostname, strlen(sock->hostname));
1418 silc_free(username);
1419 username = newusername;
1422 SilcPublicKeyIdentifier pident =
1423 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1426 newusername = silc_calloc(strlen(username) +
1427 strlen(pident->host) + 2,
1428 sizeof(*newusername));
1429 strncat(newusername, username, strlen(username));
1430 strncat(newusername, "@", 1);
1431 strncat(newusername, pident->host, strlen(pident->host));
1432 silc_free(username);
1433 username = newusername;
1434 silc_pkcs_free_identifier(pident);
1440 /* Create Client ID */
1441 silc_id_create_client_id(server->id, server->rng, server->md5hash,
1442 username, &client_id);
1444 /* Update client entry */
1445 idata->registered = TRUE;
1446 client->nickname = nickname;
1447 client->username = username;
1448 client->userinfo = realname ? realname : strdup(" ");
1449 client->id = client_id;
1450 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1452 /* Add the client again to the ID cache */
1453 silc_idcache_add(server->local_list->clients, client->nickname,
1454 client_id, client, FALSE);
1456 /* Notify our router about new client on the SILC network */
1457 if (!server->standalone)
1458 silc_server_send_new_id(server, (SilcSocketConnection)
1459 server->router->connection,
1460 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1461 client->id, SILC_ID_CLIENT, id_len);
1463 /* Send the new client ID to the client. */
1464 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1465 reply = silc_buffer_alloc(2 + 2 + id_len);
1466 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1467 silc_buffer_format(reply,
1468 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1469 SILC_STR_UI_SHORT(id_len),
1470 SILC_STR_UI_XNSTRING(id_string, id_len),
1472 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1473 reply->data, reply->len, FALSE);
1474 silc_free(id_string);
1475 silc_buffer_free(reply);
1477 /* Send some nice info to the client */
1478 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1479 ("Welcome to the SILC Network %s",
1481 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1482 ("Your host is %s, running version %s",
1483 server->config->server_info->server_name,
1485 if (server->server_type == SILC_ROUTER) {
1486 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1487 ("There are %d clients on %d servers in SILC "
1488 "Network", server->stat.clients,
1489 server->stat.servers + 1));
1490 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1491 ("There are %d clients on %d server in our cell",
1492 server->stat.cell_clients,
1493 server->stat.cell_servers + 1));
1494 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1495 ("I have %d clients, %d channels, %d servers and "
1497 server->stat.my_clients,
1498 server->stat.my_channels,
1499 server->stat.my_servers,
1500 server->stat.my_routers));
1501 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1502 ("%d server operators and %d router operators "
1504 server->stat.my_server_ops,
1505 server->stat.my_router_ops));
1507 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1508 ("I have %d clients and %d channels formed",
1509 server->stat.my_clients,
1510 server->stat.my_channels));
1511 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1512 ("%d operators online",
1513 server->stat.my_server_ops));
1515 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1516 ("Your connection is secured with %s cipher, "
1517 "key length %d bits",
1518 idata->send_key->cipher->name,
1519 idata->send_key->cipher->key_len));
1520 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1521 ("Your current nickname is %s",
1525 silc_server_send_motd(server, sock);
1530 /* Create new server. This processes received New Server packet and
1531 saves the received Server ID. The server is our locally connected
1532 server thus we save all the information and save it to local list.
1533 This funtion can be used by both normal server and router server.
1534 If normal server uses this it means that its router has connected
1535 to the server. If router uses this it means that one of the cell's
1536 servers is connected to the router. */
1538 SilcServerEntry silc_server_new_server(SilcServer server,
1539 SilcSocketConnection sock,
1540 SilcPacketContext *packet)
1542 SilcBuffer buffer = packet->buffer;
1543 SilcServerEntry new_server;
1544 SilcServerID *server_id;
1545 SilcIDListData idata;
1546 unsigned char *server_name, *id_string;
1547 uint16 id_len, name_len;
1550 SILC_LOG_DEBUG(("Creating new server"));
1552 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1553 sock->type != SILC_SOCKET_TYPE_ROUTER)
1556 /* Take server entry */
1557 new_server = (SilcServerEntry)sock->user_data;
1558 idata = (SilcIDListData)new_server;
1560 /* Remove the old cache entry */
1561 silc_idcache_del_by_context(server->local_list->servers, new_server);
1563 /* Parse the incoming packet */
1564 ret = silc_buffer_unformat(buffer,
1565 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1566 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
1571 silc_free(id_string);
1573 silc_free(server_name);
1577 if (id_len > buffer->len) {
1578 silc_free(id_string);
1579 silc_free(server_name);
1584 server_name[255] = '\0';
1587 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1589 silc_free(id_string);
1590 silc_free(server_name);
1593 silc_free(id_string);
1595 /* Update server entry */
1596 idata->registered = TRUE;
1597 new_server->server_name = server_name;
1598 new_server->id = server_id;
1600 /* Add again the entry to the ID cache. */
1601 silc_idcache_add(server->local_list->servers, server_name, server_id,
1604 /* Distribute the information about new server in the SILC network
1605 to our router. If we are normal server we won't send anything
1606 since this connection must be our router connection. */
1607 if (server->server_type == SILC_ROUTER && !server->standalone &&
1608 server->router->connection != sock)
1609 silc_server_send_new_id(server, server->router->connection,
1610 TRUE, new_server->id, SILC_ID_SERVER,
1611 silc_id_get_len(server_id, SILC_ID_SERVER));
1613 if (server->server_type == SILC_ROUTER)
1614 server->stat.cell_servers++;
1619 /* Processes incoming New ID packet. New ID Payload is used to distribute
1620 information about newly registered clients and servers. */
1622 static void silc_server_new_id_real(SilcServer server,
1623 SilcSocketConnection sock,
1624 SilcPacketContext *packet,
1627 SilcBuffer buffer = packet->buffer;
1629 SilcServerEntry router;
1630 SilcSocketConnection router_sock;
1635 SILC_LOG_DEBUG(("Processing new ID"));
1637 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1638 server->server_type == SILC_SERVER ||
1639 packet->src_id_type != SILC_ID_SERVER)
1642 idp = silc_id_payload_parse(buffer);
1646 id_type = silc_id_payload_get_type(idp);
1648 /* Normal server cannot have other normal server connections */
1649 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
1652 id = silc_id_payload_get_id(idp);
1656 /* If the sender of this packet is server and we are router we need to
1657 broadcast this packet to other routers in the network. */
1658 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
1659 sock->type == SILC_SOCKET_TYPE_SERVER &&
1660 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1661 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
1662 silc_server_packet_send(server, server->router->connection,
1664 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1665 buffer->data, buffer->len, FALSE);
1668 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1669 id_list = server->local_list;
1671 id_list = server->global_list;
1673 /* If the packet is coming from server then use the sender as the
1674 origin of the the packet. If it came from router then check the real
1675 sender of the packet and use that as the origin. */
1676 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
1678 router = sock->user_data;
1680 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1681 packet->src_id_type);
1682 router = silc_idlist_find_server_by_id(server->global_list,
1685 router = silc_idlist_find_server_by_id(server->local_list,
1687 silc_free(sender_id);
1694 case SILC_ID_CLIENT:
1696 SilcClientEntry entry;
1698 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1699 silc_id_render(id, SILC_ID_CLIENT),
1700 sock->type == SILC_SOCKET_TYPE_SERVER ?
1701 "Server" : "Router", sock->hostname));
1703 /* As a router we keep information of all global information in our
1704 global list. Cell wide information however is kept in the local
1706 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
1708 entry->nickname = NULL;
1709 entry->data.registered = TRUE;
1711 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1712 server->stat.cell_clients++;
1713 server->stat.clients++;
1717 case SILC_ID_SERVER:
1718 /* If the ID is mine, ignore it. */
1719 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
1720 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
1724 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
1725 silc_id_render(id, SILC_ID_SERVER),
1726 sock->type == SILC_SOCKET_TYPE_SERVER ?
1727 "Server" : "Router", sock->hostname));
1729 /* As a router we keep information of all global information in our global
1730 list. Cell wide information however is kept in the local list. */
1731 silc_idlist_add_server(id_list, NULL, 0, id, router, router_sock);
1733 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1734 server->stat.cell_servers++;
1735 server->stat.servers++;
1738 case SILC_ID_CHANNEL:
1739 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
1747 silc_id_payload_free(idp);
1751 /* Processes incoming New ID packet. New ID Payload is used to distribute
1752 information about newly registered clients and servers. */
1754 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
1755 SilcPacketContext *packet)
1757 silc_server_new_id_real(server, sock, packet, TRUE);
1760 /* Receoved New Id List packet, list of New ID payloads inside one
1761 packet. Process the New ID payloads one by one. */
1763 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
1764 SilcPacketContext *packet)
1766 SilcPacketContext *new_id;
1770 SILC_LOG_DEBUG(("Processing New ID List"));
1772 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1773 packet->src_id_type != SILC_ID_SERVER)
1776 /* If the sender of this packet is server and we are router we need to
1777 broadcast this packet to other routers in the network. Broadcast
1778 this list packet instead of multiple New ID packets. */
1779 if (!server->standalone && server->server_type == SILC_ROUTER &&
1780 sock->type == SILC_SOCKET_TYPE_SERVER &&
1781 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1782 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
1783 silc_server_packet_send(server, server->router->connection,
1785 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1786 packet->buffer->data, packet->buffer->len, FALSE);
1789 /* Make copy of the original packet context, except for the actual
1790 data buffer, which we will here now fetch from the original buffer. */
1791 new_id = silc_packet_context_alloc();
1792 new_id->type = SILC_PACKET_NEW_ID;
1793 new_id->flags = packet->flags;
1794 new_id->src_id = packet->src_id;
1795 new_id->src_id_len = packet->src_id_len;
1796 new_id->src_id_type = packet->src_id_type;
1797 new_id->dst_id = packet->dst_id;
1798 new_id->dst_id_len = packet->dst_id_len;
1799 new_id->dst_id_type = packet->dst_id_type;
1801 idp = silc_buffer_alloc(256);
1802 new_id->buffer = idp;
1804 while (packet->buffer->len) {
1805 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
1806 if ((id_len > packet->buffer->len) ||
1807 (id_len > idp->truelen))
1810 silc_buffer_pull_tail(idp, 4 + id_len);
1811 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
1813 /* Process the New ID */
1814 silc_server_new_id_real(server, sock, new_id, FALSE);
1816 silc_buffer_push_tail(idp, 4 + id_len);
1817 silc_buffer_pull(packet->buffer, 4 + id_len);
1820 silc_buffer_free(idp);
1824 /* Received New Channel packet. Information about new channels in the
1825 network are distributed using this packet. Save the information about
1826 the new channel. This usually comes from router but also normal server
1827 can send this to notify channels it has when it connects to us. */
1829 void silc_server_new_channel(SilcServer server,
1830 SilcSocketConnection sock,
1831 SilcPacketContext *packet)
1833 SilcChannelPayload payload;
1834 SilcChannelID *channel_id;
1841 SILC_LOG_DEBUG(("Processing New Channel"));
1843 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1844 packet->src_id_type != SILC_ID_SERVER ||
1845 server->server_type == SILC_SERVER)
1848 /* Parse the channel payload */
1849 payload = silc_channel_payload_parse(packet->buffer);
1853 /* Get the channel ID */
1854 channel_id = silc_channel_get_id_parse(payload);
1856 silc_channel_payload_free(payload);
1860 channel_name = silc_channel_get_name(payload, &name_len);
1862 channel_name[255] = '\0';
1864 id = silc_channel_get_id(payload, &id_len);
1866 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
1867 /* Add the channel to global list as it is coming from router. It
1868 cannot be our own channel as it is coming from router. */
1870 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
1871 silc_id_render(channel_id, SILC_ID_CHANNEL),
1874 silc_idlist_add_channel(server->global_list, strdup(channel_name),
1875 0, channel_id, sock->user_data,
1878 server->stat.channels++;
1880 /* The channel is coming from our server, thus it is in our cell
1881 we will add it to our local list. */
1882 SilcChannelEntry channel;
1885 SILC_LOG_DEBUG(("New channel id(%s) from [Server] %s",
1886 silc_id_render(channel_id, SILC_ID_CHANNEL),
1889 /* Check that we don't already have this channel */
1890 channel = silc_idlist_find_channel_by_name(server->local_list,
1891 channel_name, NULL);
1893 channel = silc_idlist_find_channel_by_name(server->global_list,
1894 channel_name, NULL);
1896 /* If the channel does not exist, then create it. We create the channel
1897 with the channel ID provided by the server. This creates a new
1898 key to the channel as well that we will send to the server. */
1900 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
1904 silc_channel_payload_free(payload);
1905 silc_free(channel_id);
1909 /* Get the mode and set it to the channel */
1910 channel->mode = silc_channel_get_mode(payload);
1912 /* Send the new channel key to the server */
1913 chk = silc_channel_key_payload_encode(id_len, id,
1914 strlen(channel->channel_key->
1916 channel->channel_key->cipher->name,
1917 channel->key_len / 8,
1919 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1920 chk->data, chk->len, FALSE);
1921 silc_buffer_free(chk);
1924 /* The channel exist by that name, check whether the ID's match.
1925 If they don't then we'll force the server to use the ID we have.
1926 We also create a new key for the channel. */
1927 SilcBuffer users = NULL, users_modes = NULL;
1930 channel_id = silc_id_dup(channel_id, SILC_ID_CHANNEL);
1932 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
1933 /* They don't match, send CHANNEL_CHANGE notify to the server to
1934 force the ID change. */
1935 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
1936 silc_server_send_notify_channel_change(server, sock, FALSE,
1937 channel_id, channel->id);
1940 /* If the mode is different from what we have then enforce the
1942 mode = silc_channel_get_mode(payload);
1943 if (channel->mode != mode) {
1944 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
1945 silc_server_send_notify_cmode(server, sock, FALSE, channel,
1946 channel->mode, server->id,
1948 channel->cipher, channel->hmac_name);
1951 /* Create new key for the channel and send it to the server and
1952 everybody else possibly on the channel. */
1954 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
1955 silc_server_create_channel_key(server, channel, 0);
1957 /* Send to the channel */
1958 silc_server_send_channel_key(server, sock, channel, FALSE);
1959 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1960 id_len = SILC_ID_CHANNEL_LEN;
1962 /* Send to the server */
1963 chk = silc_channel_key_payload_encode(id_len, id,
1964 strlen(channel->channel_key->
1966 channel->channel_key->
1968 channel->key_len / 8,
1970 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1971 chk->data, chk->len, FALSE);
1972 silc_buffer_free(chk);
1976 silc_free(channel_id);
1978 /* Since the channel is coming from server and we also know about it
1979 then send the JOIN notify to the server so that it see's our
1980 users on the channel "joining" the channel. */
1981 silc_server_announce_get_channel_users(server, channel, &users,
1984 silc_buffer_push(users, users->data - users->head);
1985 silc_server_packet_send(server, sock,
1986 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1987 users->data, users->len, FALSE);
1988 silc_buffer_free(users);
1991 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
1992 silc_server_packet_send_dest(server, sock,
1993 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
1994 channel->id, SILC_ID_CHANNEL,
1996 users_modes->len, FALSE);
1997 silc_buffer_free(users_modes);
2002 silc_channel_payload_free(payload);
2005 /* Received New Channel List packet, list of New Channel List payloads inside
2006 one packet. Process the New Channel payloads one by one. */
2008 void silc_server_new_channel_list(SilcServer server,
2009 SilcSocketConnection sock,
2010 SilcPacketContext *packet)
2012 SilcPacketContext *new;
2016 SILC_LOG_DEBUG(("Processing New Channel List"));
2018 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2019 packet->src_id_type != SILC_ID_SERVER ||
2020 server->server_type == SILC_SERVER)
2023 /* If the sender of this packet is server and we are router we need to
2024 broadcast this packet to other routers in the network. Broadcast
2025 this list packet instead of multiple New Channel packets. */
2026 if (!server->standalone && server->server_type == SILC_ROUTER &&
2027 sock->type == SILC_SOCKET_TYPE_SERVER &&
2028 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2029 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2030 silc_server_packet_send(server, server->router->connection,
2032 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2033 packet->buffer->data, packet->buffer->len, FALSE);
2036 /* Make copy of the original packet context, except for the actual
2037 data buffer, which we will here now fetch from the original buffer. */
2038 new = silc_packet_context_alloc();
2039 new->type = SILC_PACKET_NEW_CHANNEL;
2040 new->flags = packet->flags;
2041 new->src_id = packet->src_id;
2042 new->src_id_len = packet->src_id_len;
2043 new->src_id_type = packet->src_id_type;
2044 new->dst_id = packet->dst_id;
2045 new->dst_id_len = packet->dst_id_len;
2046 new->dst_id_type = packet->dst_id_type;
2048 buffer = silc_buffer_alloc(512);
2049 new->buffer = buffer;
2051 while (packet->buffer->len) {
2052 SILC_GET16_MSB(len1, packet->buffer->data);
2053 if ((len1 > packet->buffer->len) ||
2054 (len1 > buffer->truelen))
2057 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2058 if ((len2 > packet->buffer->len) ||
2059 (len2 > buffer->truelen))
2062 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2063 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2065 /* Process the New Channel */
2066 silc_server_new_channel(server, sock, new);
2068 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2069 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2072 silc_buffer_free(buffer);
2076 /* Received key agreement packet. This packet is never for us. It is to
2077 the client in the packet's destination ID. Sending of this sort of packet
2078 equals sending private message, ie. it is sent point to point from
2079 one client to another. */
2081 void silc_server_key_agreement(SilcServer server,
2082 SilcSocketConnection sock,
2083 SilcPacketContext *packet)
2085 SilcSocketConnection dst_sock;
2086 SilcIDListData idata;
2088 SILC_LOG_DEBUG(("Start"));
2090 if (packet->src_id_type != SILC_ID_CLIENT ||
2091 packet->dst_id_type != SILC_ID_CLIENT)
2094 if (!packet->dst_id)
2097 /* Get the route to the client */
2098 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2099 packet->dst_id_len, NULL, &idata);
2103 /* Relay the packet */
2104 silc_server_relay_packet(server, dst_sock, idata->send_key,
2105 idata->hmac_send, packet, FALSE);
2108 /* Received connection auth request packet that is used during connection
2109 phase to resolve the mandatory authentication method. This packet can
2110 actually be received at anytime but usually it is used only during
2111 the connection authentication phase. Now, protocol says that this packet
2112 can come from client or server, however, we support only this coming
2113 from client and expect that server's always knows what authentication
2116 void silc_server_connection_auth_request(SilcServer server,
2117 SilcSocketConnection sock,
2118 SilcPacketContext *packet)
2120 SilcServerConfigSectionClientConnection *client = NULL;
2123 SilcAuthMethod auth_meth;
2125 SILC_LOG_DEBUG(("Start"));
2127 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2130 /* Parse the payload */
2131 ret = silc_buffer_unformat(packet->buffer,
2132 SILC_STR_UI_SHORT(&conn_type),
2133 SILC_STR_UI_SHORT(NULL),
2138 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2141 /* Get the authentication method for the client */
2142 auth_meth = SILC_AUTH_NONE;
2143 client = silc_server_config_find_client_conn(server->config,
2147 client = silc_server_config_find_client_conn(server->config,
2151 auth_meth = client->auth_meth;
2153 /* Send it back to the client */
2154 silc_server_send_connection_auth_request(server, sock,
2159 /* Received REKEY packet. The sender of the packet wants to regenerate
2160 its session keys. This starts the REKEY protocol. */
2162 void silc_server_rekey(SilcServer server,
2163 SilcSocketConnection sock,
2164 SilcPacketContext *packet)
2166 SilcProtocol protocol;
2167 SilcServerRekeyInternalContext *proto_ctx;
2168 SilcIDListData idata = (SilcIDListData)sock->user_data;
2170 SILC_LOG_DEBUG(("Start"));
2172 /* Allocate internal protocol context. This is sent as context
2174 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2175 proto_ctx->server = (void *)server;
2176 proto_ctx->sock = sock;
2177 proto_ctx->responder = TRUE;
2178 proto_ctx->pfs = idata->rekey->pfs;
2180 /* Perform rekey protocol. Will call the final callback after the
2181 protocol is over. */
2182 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2183 &protocol, proto_ctx, silc_server_rekey_final);
2184 sock->protocol = protocol;
2186 if (proto_ctx->pfs == FALSE)
2187 /* Run the protocol */
2188 silc_protocol_execute(protocol, server->timeout_queue, 0, 0);