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 = NULL, *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;
54 SILC_LOG_DEBUG(("Start"));
56 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
57 packet->src_id_type != SILC_ID_SERVER)
63 /* If the packet is destined directly to a client then relay the packet
64 before processing it. */
65 if (packet->dst_id_type == SILC_ID_CLIENT) {
67 SilcSocketConnection dst_sock;
69 /* Get the route to the client */
70 dst_sock = silc_server_get_client_route(server, packet->dst_id,
71 packet->dst_id_len, NULL, &idata);
73 /* Relay the packet */
74 silc_server_relay_packet(server, dst_sock, idata->send_key,
75 idata->hmac_receive, idata->psn_send++,
79 /* Parse the Notify Payload */
80 payload = silc_notify_payload_parse(packet->buffer->data,
85 /* If we are router and this packet is not already broadcast packet
86 we will broadcast it. The sending socket really cannot be router or
87 the router is buggy. If this packet is coming from router then it must
88 have the broadcast flag set already and we won't do anything. */
89 if (!server->standalone && server->server_type == SILC_ROUTER &&
90 sock->type == SILC_SOCKET_TYPE_SERVER &&
91 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
92 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
93 if (packet->dst_id_type == SILC_ID_CHANNEL) {
94 /* Packet is destined to channel */
95 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
100 silc_server_packet_send_dest(server, server->router->connection,
102 packet->flags | SILC_PACKET_FLAG_BROADCAST,
103 channel_id, SILC_ID_CHANNEL,
104 packet->buffer->data, packet->buffer->len,
106 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
107 packet->type, packet->flags,
108 channel_id, SILC_ID_CHANNEL,
109 packet->buffer->data, packet->buffer->len,
112 /* Packet is destined to client or server */
113 silc_server_packet_send(server, server->router->connection,
115 packet->flags | SILC_PACKET_FLAG_BROADCAST,
116 packet->buffer->data, packet->buffer->len,
118 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
119 packet->type, packet->flags,
120 packet->buffer->data, packet->buffer->len,
125 type = silc_notify_get_type(payload);
126 args = silc_notify_get_args(payload);
131 case SILC_NOTIFY_TYPE_JOIN:
133 * Distribute the notify to local clients on the channel
135 SILC_LOG_DEBUG(("JOIN notify"));
138 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
141 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
145 /* Get channel entry */
146 channel = silc_idlist_find_channel_by_id(server->global_list,
149 channel = silc_idlist_find_channel_by_id(server->local_list,
152 silc_free(channel_id);
156 silc_free(channel_id);
159 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
162 client_id = silc_id_payload_parse_id(tmp, tmp_len);
166 /* If the the client is not in local list we check global list (ie. the
167 channel will be global channel) and if it does not exist then create
168 entry for the client. */
169 client = silc_idlist_find_client_by_id(server->global_list,
170 client_id, server->server_type,
173 client = silc_idlist_find_client_by_id(server->local_list,
174 client_id, server->server_type,
177 /* If router did not find the client the it is bogus */
178 if (server->server_type != SILC_SERVER)
182 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
183 silc_id_dup(client_id, SILC_ID_CLIENT),
184 sock->user_data, NULL, 0);
186 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
187 silc_free(client_id);
191 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
195 /* Do not process the notify if the client is not registered */
196 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
199 /* Do not add client to channel if it is there already */
200 if (silc_server_client_on_channel(client, channel)) {
201 SILC_LOG_DEBUG(("Client already on channel"));
205 /* Send to channel */
206 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
207 FALSE, packet->buffer->data,
208 packet->buffer->len, FALSE);
210 if (server->server_type != SILC_ROUTER &&
211 sock->type == SILC_SOCKET_TYPE_ROUTER)
212 /* The channel is global now */
213 channel->global_users = TRUE;
215 SILC_LOG_DEBUG(("Joining to channel %s", channel->channel_name));
217 /* JOIN the global client to the channel (local clients (if router
218 created the channel) is joined in the pending JOIN command). */
219 chl = silc_calloc(1, sizeof(*chl));
220 chl->client = client;
221 chl->channel = channel;
223 /* If this is the first one on the channel then it is the founder of
225 if (!silc_hash_table_count(channel->user_list))
226 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
228 silc_hash_table_add(channel->user_list, client, chl);
229 silc_hash_table_add(client->channels, channel, chl);
230 silc_free(client_id);
231 channel->user_count++;
235 case SILC_NOTIFY_TYPE_LEAVE:
237 * Distribute the notify to local clients on the channel
239 SILC_LOG_DEBUG(("LEAVE notify"));
242 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
243 packet->dst_id_type);
248 /* Get channel entry */
249 channel = silc_idlist_find_channel_by_id(server->global_list,
252 channel = silc_idlist_find_channel_by_id(server->local_list,
255 silc_free(channel_id);
261 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
263 silc_free(channel_id);
266 client_id = silc_id_payload_parse_id(tmp, tmp_len);
268 silc_free(channel_id);
272 /* Get client entry */
273 client = silc_idlist_find_client_by_id(server->global_list,
274 client_id, TRUE, NULL);
276 client = silc_idlist_find_client_by_id(server->local_list,
277 client_id, TRUE, NULL);
279 silc_free(client_id);
280 silc_free(channel_id);
284 silc_free(client_id);
286 /* Check if on channel */
287 if (!silc_server_client_on_channel(client, channel))
290 /* Send the leave notify to channel */
291 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
292 FALSE, packet->buffer->data,
293 packet->buffer->len, FALSE);
295 /* Remove the user from channel */
296 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
299 case SILC_NOTIFY_TYPE_SIGNOFF:
301 * Distribute the notify to local clients on the channel
303 SILC_LOG_DEBUG(("SIGNOFF notify"));
306 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
309 client_id = silc_id_payload_parse_id(tmp, tmp_len);
313 /* Get client entry */
314 client = silc_idlist_find_client_by_id(server->global_list,
315 client_id, TRUE, &cache);
317 client = silc_idlist_find_client_by_id(server->local_list,
318 client_id, TRUE, &cache);
320 silc_free(client_id);
324 silc_free(client_id);
326 /* Get signoff message */
327 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
331 /* Update statistics */
332 server->stat.clients--;
333 if (server->server_type == SILC_ROUTER)
334 server->stat.cell_clients--;
335 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
336 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
338 /* Remove the client from all channels. */
339 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
341 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
342 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
345 case SILC_NOTIFY_TYPE_TOPIC_SET:
347 * Distribute the notify to local clients on the channel
350 SILC_LOG_DEBUG(("TOPIC SET notify"));
353 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
354 packet->dst_id_type);
359 /* Get channel entry */
360 channel = silc_idlist_find_channel_by_id(server->global_list,
363 channel = silc_idlist_find_channel_by_id(server->local_list,
366 silc_free(channel_id);
372 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
374 silc_free(channel_id);
378 silc_free(channel->topic);
379 channel->topic = strdup(tmp);
381 /* Send the same notify to the channel */
382 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
383 FALSE, packet->buffer->data,
384 packet->buffer->len, FALSE);
385 silc_free(channel_id);
388 case SILC_NOTIFY_TYPE_NICK_CHANGE:
391 * Distribute the notify to local clients on the channel
393 unsigned char *id, *id2;
395 SILC_LOG_DEBUG(("NICK CHANGE notify"));
397 /* Get old client ID */
398 id = silc_argument_get_arg_type(args, 1, &tmp_len);
401 client_id = silc_id_payload_parse_id(id, tmp_len);
405 /* Get new client ID */
406 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
409 client_id2 = silc_id_payload_parse_id(id2, tmp_len);
413 SILC_LOG_DEBUG(("Old Client ID id(%s)",
414 silc_id_render(client_id, SILC_ID_CLIENT)));
415 SILC_LOG_DEBUG(("New Client ID id(%s)",
416 silc_id_render(client_id2, SILC_ID_CLIENT)));
418 /* Replace the Client ID */
419 client = silc_idlist_replace_client_id(server->global_list, client_id,
422 client = silc_idlist_replace_client_id(server->local_list, client_id,
426 /* The nickname is not valid anymore, set it NULL. This causes that
427 the nickname will be queried if someone wants to know it. */
428 if (client->nickname)
429 silc_free(client->nickname);
430 client->nickname = NULL;
432 /* Send the NICK_CHANGE notify type to local clients on the channels
433 this client is joined to. */
434 silc_server_send_notify_on_channels(server, NULL, client,
435 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
440 silc_free(client_id);
442 silc_free(client_id2);
446 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
448 * Distribute the notify to local clients on the channel
451 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
454 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
455 packet->dst_id_type);
460 /* Get channel entry */
461 channel = silc_idlist_find_channel_by_id(server->global_list,
464 channel = silc_idlist_find_channel_by_id(server->local_list,
467 silc_free(channel_id);
473 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
475 silc_free(channel_id);
479 SILC_GET32_MSB(mode, tmp);
481 /* Check if mode changed */
482 if (channel->mode == mode)
485 /* Send the same notify to the channel */
486 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
487 FALSE, packet->buffer->data,
488 packet->buffer->len, FALSE);
490 /* If the channel had private keys set and the mode was removed then
491 we must re-generate and re-distribute a new channel key */
492 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
493 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
494 /* Re-generate channel key */
495 if (!silc_server_create_channel_key(server, channel, 0))
498 /* Send the channel key. This sends it to our local clients and if
499 we are normal server to our router as well. */
500 silc_server_send_channel_key(server, NULL, channel,
501 server->server_type == SILC_ROUTER ?
502 FALSE : !server->standalone);
506 channel->mode = mode;
507 silc_free(channel_id);
510 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
512 unsigned char hash[32];
515 silc_hmac_free(channel->hmac);
516 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
519 /* Set the HMAC key out of current channel key. The client must do
521 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
522 channel->key_len / 8,
524 silc_hmac_set_key(channel->hmac, hash,
525 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
526 memset(hash, 0, sizeof(hash));
529 /* Get the passphrase */
530 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
532 silc_free(channel->passphrase);
533 channel->passphrase = strdup(tmp);
538 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
541 * Distribute the notify to local clients on the channel
543 SilcChannelClientEntry chl2 = NULL;
544 bool notify_sent = FALSE;
546 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
549 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
550 packet->dst_id_type);
555 /* Get channel entry */
556 channel = silc_idlist_find_channel_by_id(server->global_list,
559 channel = silc_idlist_find_channel_by_id(server->local_list,
562 silc_free(channel_id);
568 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
570 silc_free(channel_id);
574 SILC_GET32_MSB(mode, tmp);
576 /* Get target client */
577 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
580 client_id = silc_id_payload_parse_id(tmp, tmp_len);
584 /* Get client entry */
585 client = silc_idlist_find_client_by_id(server->global_list,
586 client_id, TRUE, NULL);
588 client = silc_idlist_find_client_by_id(server->local_list,
589 client_id, TRUE, NULL);
591 silc_free(client_id);
595 silc_free(client_id);
597 /* Get entry to the channel user list */
598 silc_hash_table_list(channel->user_list, &htl);
599 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
600 /* If the mode is channel founder and we already find a client
601 to have that mode on the channel we will enforce the sender
602 to change the channel founder mode away. There can be only one
603 channel founder on the channel. */
604 if (server->server_type == SILC_ROUTER &&
605 mode & SILC_CHANNEL_UMODE_CHANFO &&
606 chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
608 unsigned char cumode[4];
610 if (chl->client == client && chl->mode == mode) {
615 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
616 silc_server_send_notify_cumode(server, sock, FALSE, channel, mode,
617 client->id, SILC_ID_CLIENT,
620 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
621 SILC_PUT32_MSB(mode, cumode);
622 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
623 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
624 3, idp->data, idp->len,
626 idp->data, idp->len);
627 silc_buffer_free(idp);
630 /* Force the mode change if we alredy set the mode */
633 silc_free(channel_id);
634 silc_hash_table_list_reset(&htl);
639 if (chl->client == client) {
640 if (chl->mode == mode) {
645 SILC_LOG_DEBUG(("Changing the channel user mode"));
647 /* Change the mode */
649 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
655 silc_hash_table_list_reset(&htl);
657 /* Send the same notify to the channel */
659 silc_server_packet_send_to_channel(server, sock, channel,
661 FALSE, packet->buffer->data,
662 packet->buffer->len, FALSE);
664 silc_free(channel_id);
668 case SILC_NOTIFY_TYPE_INVITE:
670 if (packet->dst_id_type == SILC_ID_CLIENT)
673 SILC_LOG_DEBUG(("INVITE notify"));
676 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
679 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
683 /* Get channel entry */
684 channel = silc_idlist_find_channel_by_id(server->global_list,
687 channel = silc_idlist_find_channel_by_id(server->local_list,
690 silc_free(channel_id);
694 silc_free(channel_id);
696 /* Get the added invite */
697 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
699 if (!channel->invite_list)
700 channel->invite_list = silc_calloc(tmp_len + 2,
701 sizeof(*channel->invite_list));
703 channel->invite_list = silc_realloc(channel->invite_list,
704 sizeof(*channel->invite_list) *
706 strlen(channel->invite_list) +
708 if (tmp[tmp_len - 1] == ',')
709 tmp[tmp_len - 1] = '\0';
711 strncat(channel->invite_list, tmp, tmp_len);
712 strncat(channel->invite_list, ",", 1);
715 /* Get the deleted invite */
716 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
717 if (tmp && channel->invite_list) {
718 char *start, *end, *n;
720 if (!strncmp(channel->invite_list, tmp,
721 strlen(channel->invite_list) - 1)) {
722 silc_free(channel->invite_list);
723 channel->invite_list = NULL;
725 start = strstr(channel->invite_list, tmp);
726 if (start && strlen(start) >= tmp_len) {
727 end = start + tmp_len;
728 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
729 strncat(n, channel->invite_list, start - channel->invite_list);
730 strncat(n, end + 1, ((channel->invite_list +
731 strlen(channel->invite_list)) - end) - 1);
732 silc_free(channel->invite_list);
733 channel->invite_list = n;
740 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
742 * Distribute to the local clients on the channel and change the
746 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
748 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
751 /* Get the old Channel ID */
752 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
755 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
759 /* Get the channel entry */
760 channel = silc_idlist_find_channel_by_id(server->local_list,
763 channel = silc_idlist_find_channel_by_id(server->global_list,
766 silc_free(channel_id);
771 /* Send the notify to the channel */
772 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
773 FALSE, packet->buffer->data,
774 packet->buffer->len, FALSE);
776 /* Get the new Channel ID */
777 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
780 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len);
784 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
785 silc_id_render(channel_id, SILC_ID_CHANNEL)));
786 SILC_LOG_DEBUG(("New Channel ID id(%s)",
787 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
789 /* Replace the Channel ID */
790 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
792 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
794 silc_free(channel_id2);
799 SilcBuffer users = NULL, users_modes = NULL;
801 /* Re-announce this channel which ID was changed. */
802 silc_server_send_new_channel(server, sock, FALSE, channel->channel_name,
804 silc_id_get_len(channel->id,
808 /* Re-announce our clients on the channel as the ID has changed now */
809 silc_server_announce_get_channel_users(server, channel, &users,
812 silc_buffer_push(users, users->data - users->head);
813 silc_server_packet_send(server, sock,
814 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
815 users->data, users->len, FALSE);
816 silc_buffer_free(users);
819 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
820 silc_server_packet_send_dest(server, sock,
821 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
822 channel->id, SILC_ID_CHANNEL,
824 users_modes->len, FALSE);
825 silc_buffer_free(users_modes);
828 /* Re-announce channel's topic */
829 if (channel->topic) {
830 silc_server_send_notify_topic_set(server, sock,
831 server->server_type == SILC_ROUTER ?
832 TRUE : FALSE, channel,
833 channel->id, SILC_ID_CHANNEL,
838 silc_free(channel_id);
842 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
844 * Remove the server entry and all clients that this server owns.
847 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
850 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
853 server_id = silc_id_payload_parse_id(tmp, tmp_len);
857 /* Get server entry */
858 server_entry = silc_idlist_find_server_by_id(server->global_list,
859 server_id, TRUE, NULL);
862 server_entry = silc_idlist_find_server_by_id(server->local_list,
863 server_id, TRUE, NULL);
866 /* If we are normal server then we might not have the server. Check
867 whether router was kind enough to send the list of all clients
868 that actually was to be removed. Remove them if the list is
870 if (server->server_type != SILC_ROUTER &&
871 silc_argument_get_arg_num(args) > 1) {
874 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
876 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
879 client_id = silc_id_payload_parse_id(tmp, tmp_len);
883 /* Get client entry */
884 client = silc_idlist_find_client_by_id(server->global_list,
885 client_id, TRUE, &cache);
888 client = silc_idlist_find_client_by_id(server->local_list,
889 client_id, TRUE, &cache);
892 silc_free(client_id);
896 silc_free(client_id);
898 /* Update statistics */
899 server->stat.clients--;
900 if (server->server_type == SILC_ROUTER)
901 server->stat.cell_clients--;
902 SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
903 SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
905 /* Remove the client from all channels. */
906 silc_server_remove_from_channels(server, NULL, client,
909 /* Remove the client */
910 silc_idlist_del_client(local ? server->local_list :
911 server->global_list, client);
915 silc_free(server_id);
919 silc_free(server_id);
921 /* Free all client entries that this server owns as they will
922 become invalid now as well. */
923 silc_server_remove_clients_by_server(server, server_entry, TRUE);
925 /* Remove the server entry */
926 silc_idlist_del_server(local ? server->local_list :
927 server->global_list, server_entry);
929 /* XXX update statistics */
933 case SILC_NOTIFY_TYPE_KICKED:
935 * Distribute the notify to local clients on the channel
938 SILC_LOG_DEBUG(("KICKED notify"));
941 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
942 packet->dst_id_type);
947 /* Get channel entry */
948 channel = silc_idlist_find_channel_by_id(server->global_list,
951 channel = silc_idlist_find_channel_by_id(server->local_list,
954 silc_free(channel_id);
958 silc_free(channel_id);
961 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
964 client_id = silc_id_payload_parse_id(tmp, tmp_len);
968 /* If the the client is not in local list we check global list */
969 client = silc_idlist_find_client_by_id(server->global_list,
970 client_id, TRUE, NULL);
972 client = silc_idlist_find_client_by_id(server->local_list,
973 client_id, TRUE, NULL);
975 silc_free(client_id);
980 /* Send to channel */
981 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
982 FALSE, packet->buffer->data,
983 packet->buffer->len, FALSE);
985 /* Remove the client from channel */
986 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
990 case SILC_NOTIFY_TYPE_KILLED:
993 * Distribute the notify to local clients on channels
998 SILC_LOG_DEBUG(("KILLED notify"));
1001 id = silc_argument_get_arg_type(args, 1, &id_len);
1004 client_id = silc_id_payload_parse_id(id, id_len);
1008 /* If the the client is not in local list we check global list */
1009 client = silc_idlist_find_client_by_id(server->global_list,
1010 client_id, TRUE, NULL);
1012 client = silc_idlist_find_client_by_id(server->local_list,
1013 client_id, TRUE, NULL);
1015 silc_free(client_id);
1019 silc_free(client_id);
1021 /* If the client is one of ours, then close the connection to the
1022 client now. This removes the client from all channels as well. */
1023 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
1024 sock = client->connection;
1025 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
1026 silc_server_close_connection(server, sock);
1031 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1035 /* Send the notify to local clients on the channels except to the
1036 client who is killed. */
1037 silc_server_send_notify_on_channels(server, client, client,
1038 SILC_NOTIFY_TYPE_KILLED,
1043 /* Remove the client from all channels */
1044 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1050 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1052 * Save the mode of the client.
1055 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1058 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1061 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1065 /* Get client entry */
1066 client = silc_idlist_find_client_by_id(server->global_list,
1067 client_id, TRUE, NULL);
1069 client = silc_idlist_find_client_by_id(server->local_list,
1070 client_id, TRUE, NULL);
1072 silc_free(client_id);
1076 silc_free(client_id);
1079 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1082 SILC_GET32_MSB(mode, tmp);
1084 #define SILC_UMODE_STATS_UPDATE(oper, mod) \
1086 if (client->mode & (mod)) { \
1087 if (!(mode & (mod))) { \
1088 if (client->connection) \
1089 server->stat.my_ ## oper ## _ops--; \
1090 if (server->server_type == SILC_ROUTER) \
1091 server->stat. oper ## _ops--; \
1094 if (mode & (mod)) { \
1095 if (client->connection) \
1096 server->stat.my_ ## oper ## _ops++; \
1097 if (server->server_type == SILC_ROUTER) \
1098 server->stat. oper ## _ops++; \
1103 /* Update statistics */
1104 SILC_UMODE_STATS_UPDATE(server, SILC_UMODE_SERVER_OPERATOR);
1105 SILC_UMODE_STATS_UPDATE(router, SILC_UMODE_ROUTER_OPERATOR);
1108 client->mode = mode;
1112 case SILC_NOTIFY_TYPE_BAN:
1117 SILC_LOG_DEBUG(("BAN notify"));
1119 /* Get Channel ID */
1120 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1123 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1127 /* Get channel entry */
1128 channel = silc_idlist_find_channel_by_id(server->global_list,
1131 channel = silc_idlist_find_channel_by_id(server->local_list,
1134 silc_free(channel_id);
1138 silc_free(channel_id);
1140 /* Get the new ban and add it to the ban list */
1141 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1143 if (!channel->ban_list)
1144 channel->ban_list = silc_calloc(tmp_len + 2,
1145 sizeof(*channel->ban_list));
1147 channel->ban_list = silc_realloc(channel->ban_list,
1148 sizeof(*channel->ban_list) *
1150 strlen(channel->ban_list) + 2));
1151 strncat(channel->ban_list, tmp, tmp_len);
1152 strncat(channel->ban_list, ",", 1);
1155 /* Get the ban to be removed and remove it from the list */
1156 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1157 if (tmp && channel->ban_list) {
1158 char *start, *end, *n;
1160 if (!strncmp(channel->ban_list, tmp, strlen(channel->ban_list) - 1)) {
1161 silc_free(channel->ban_list);
1162 channel->ban_list = NULL;
1164 start = strstr(channel->ban_list, tmp);
1165 if (start && strlen(start) >= tmp_len) {
1166 end = start + tmp_len;
1167 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1168 strncat(n, channel->ban_list, start - channel->ban_list);
1169 strncat(n, end + 1, ((channel->ban_list +
1170 strlen(channel->ban_list)) - end) - 1);
1171 silc_free(channel->ban_list);
1172 channel->ban_list = n;
1178 /* Ignore rest of the notify types for now */
1179 case SILC_NOTIFY_TYPE_NONE:
1180 case SILC_NOTIFY_TYPE_MOTD:
1187 silc_notify_payload_free(payload);
1190 void silc_server_notify_list(SilcServer server,
1191 SilcSocketConnection sock,
1192 SilcPacketContext *packet)
1194 SilcPacketContext *new;
1198 SILC_LOG_DEBUG(("Processing Notify List"));
1200 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1201 packet->src_id_type != SILC_ID_SERVER)
1204 /* Make copy of the original packet context, except for the actual
1205 data buffer, which we will here now fetch from the original buffer. */
1206 new = silc_packet_context_alloc();
1207 new->type = SILC_PACKET_NOTIFY;
1208 new->flags = packet->flags;
1209 new->src_id = packet->src_id;
1210 new->src_id_len = packet->src_id_len;
1211 new->src_id_type = packet->src_id_type;
1212 new->dst_id = packet->dst_id;
1213 new->dst_id_len = packet->dst_id_len;
1214 new->dst_id_type = packet->dst_id_type;
1216 buffer = silc_buffer_alloc(1024);
1217 new->buffer = buffer;
1219 while (packet->buffer->len) {
1220 SILC_GET16_MSB(len, packet->buffer->data + 2);
1221 if (len > packet->buffer->len)
1224 if (len > buffer->truelen) {
1225 silc_buffer_free(buffer);
1226 buffer = silc_buffer_alloc(1024 + len);
1229 silc_buffer_pull_tail(buffer, len);
1230 silc_buffer_put(buffer, packet->buffer->data, len);
1232 /* Process the Notify */
1233 silc_server_notify(server, sock, new);
1235 silc_buffer_push_tail(buffer, len);
1236 silc_buffer_pull(packet->buffer, len);
1239 silc_buffer_free(buffer);
1243 /* Received private message. This resolves the destination of the message
1244 and sends the packet. This is used by both server and router. If the
1245 destination is our locally connected client this sends the packet to
1246 the client. This may also send the message for further routing if
1247 the destination is not in our server (or router). */
1249 void silc_server_private_message(SilcServer server,
1250 SilcSocketConnection sock,
1251 SilcPacketContext *packet)
1253 SilcSocketConnection dst_sock;
1254 SilcIDListData idata;
1256 SILC_LOG_DEBUG(("Start"));
1258 if (packet->src_id_type != SILC_ID_CLIENT ||
1259 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1262 /* Get the route to the client */
1263 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1264 packet->dst_id_len, NULL, &idata);
1266 /* Send IDENTIFY command reply with error status to indicate that
1267 such destination ID does not exist or is invalid */
1268 SilcBuffer idp = silc_id_payload_encode_data(packet->dst_id,
1270 packet->dst_id_type);
1274 if (packet->src_id_type == SILC_ID_CLIENT) {
1275 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1277 packet->src_id_type);
1278 silc_server_send_dest_command_reply(server, sock,
1279 client_id, SILC_ID_CLIENT,
1280 SILC_COMMAND_IDENTIFY,
1281 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1282 0, 1, 2, idp->data, idp->len);
1283 silc_free(client_id);
1285 silc_server_send_command_reply(server, sock, SILC_COMMAND_IDENTIFY,
1286 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1287 0, 1, 2, idp->data, idp->len);
1290 silc_buffer_free(idp);
1294 /* Send the private message */
1295 silc_server_send_private_message(server, dst_sock, idata->send_key,
1296 idata->hmac_send, idata->psn_send++,
1300 /* Received private message key packet.. This packet is never for us. It is to
1301 the client in the packet's destination ID. Sending of this sort of packet
1302 equals sending private message, ie. it is sent point to point from
1303 one client to another. */
1305 void silc_server_private_message_key(SilcServer server,
1306 SilcSocketConnection sock,
1307 SilcPacketContext *packet)
1309 SilcSocketConnection dst_sock;
1310 SilcIDListData idata;
1312 SILC_LOG_DEBUG(("Start"));
1314 if (packet->src_id_type != SILC_ID_CLIENT ||
1315 packet->dst_id_type != SILC_ID_CLIENT)
1318 if (!packet->dst_id)
1321 /* Get the route to the client */
1322 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1323 packet->dst_id_len, NULL, &idata);
1327 /* Relay the packet */
1328 silc_server_relay_packet(server, dst_sock, idata->send_key,
1329 idata->hmac_send, idata->psn_send++, packet, FALSE);
1332 /* Processes incoming command reply packet. The command reply packet may
1333 be destined to one of our clients or it may directly for us. We will
1334 call the command reply routine after processing the packet. */
1336 void silc_server_command_reply(SilcServer server,
1337 SilcSocketConnection sock,
1338 SilcPacketContext *packet)
1340 SilcBuffer buffer = packet->buffer;
1341 SilcClientEntry client = NULL;
1342 SilcSocketConnection dst_sock;
1343 SilcIDListData idata;
1344 SilcClientID *id = NULL;
1346 SILC_LOG_DEBUG(("Start"));
1348 /* Source must be server or router */
1349 if (packet->src_id_type != SILC_ID_SERVER &&
1350 sock->type != SILC_SOCKET_TYPE_ROUTER)
1353 if (packet->dst_id_type == SILC_ID_CHANNEL)
1356 if (packet->dst_id_type == SILC_ID_CLIENT) {
1357 /* Destination must be one of ours */
1358 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1361 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1363 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1369 if (packet->dst_id_type == SILC_ID_SERVER) {
1370 /* For now this must be for us */
1371 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1372 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1377 /* Execute command reply locally for the command */
1378 silc_server_command_reply_process(server, sock, buffer);
1380 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1381 /* Relay the packet to the client */
1382 const SilcBufferStruct p;
1384 dst_sock = (SilcSocketConnection)client->connection;
1385 idata = (SilcIDListData)client;
1387 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1388 + packet->dst_id_len + packet->padlen);
1389 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len,
1390 idata->hmac_send, (const SilcBuffer)&p)) {
1391 SILC_LOG_ERROR(("Cannot send packet"));
1394 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1396 /* Encrypt packet */
1397 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1398 (SilcBuffer)&p, buffer->len);
1400 /* Send the packet */
1401 silc_server_packet_send_real(server, dst_sock, TRUE);
1407 /* Process received channel message. The message can be originated from
1408 client or server. */
1410 void silc_server_channel_message(SilcServer server,
1411 SilcSocketConnection sock,
1412 SilcPacketContext *packet)
1414 SilcChannelEntry channel = NULL;
1415 SilcChannelID *id = NULL;
1416 void *sender = NULL;
1417 void *sender_entry = NULL;
1420 SILC_LOG_DEBUG(("Processing channel message"));
1423 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1424 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1428 /* Find channel entry */
1429 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1432 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1434 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1436 SILC_LOG_DEBUG(("Could not find channel"));
1441 /* See that this client is on the channel. If the original sender is
1442 not client (as it can be server as well) we don't do the check. */
1443 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
1444 packet->src_id_type);
1447 if (packet->src_id_type == SILC_ID_CLIENT) {
1448 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1449 sender, TRUE, NULL);
1450 if (!sender_entry) {
1452 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1453 sender, TRUE, NULL);
1455 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1457 SILC_LOG_DEBUG(("Client not on channel"));
1461 /* If the packet is coming from router, but the client entry is
1462 local entry to us then some router is rerouting this to us and it is
1464 if (server->server_type == SILC_ROUTER &&
1465 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1466 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1471 /* Distribute the packet to our local clients. This will send the
1472 packet for further routing as well, if needed. */
1473 silc_server_packet_relay_to_channel(server, sock, channel, sender,
1474 packet->src_id_type, sender_entry,
1475 packet->buffer->data,
1476 packet->buffer->len, FALSE);
1485 /* Received channel key packet. We distribute the key to all of our locally
1486 connected clients on the channel. */
1488 void silc_server_channel_key(SilcServer server,
1489 SilcSocketConnection sock,
1490 SilcPacketContext *packet)
1492 SilcBuffer buffer = packet->buffer;
1493 SilcChannelEntry channel;
1495 if (packet->src_id_type != SILC_ID_SERVER ||
1496 (server->server_type == SILC_ROUTER &&
1497 sock->type == SILC_SOCKET_TYPE_ROUTER))
1500 /* Save the channel key */
1501 channel = silc_server_save_channel_key(server, buffer, NULL);
1505 /* Distribute the key to everybody who is on the channel. If we are router
1506 we will also send it to locally connected servers. */
1507 silc_server_send_channel_key(server, sock, channel, FALSE);
1509 if (server->server_type != SILC_BACKUP_ROUTER) {
1510 /* Distribute to local cell backup routers. */
1511 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1512 SILC_PACKET_CHANNEL_KEY, 0,
1513 buffer->data, buffer->len, FALSE, TRUE);
1517 /* Received New Client packet and processes it. Creates Client ID for the
1518 client. Client becomes registered after calling this functions. */
1520 SilcClientEntry silc_server_new_client(SilcServer server,
1521 SilcSocketConnection sock,
1522 SilcPacketContext *packet)
1524 SilcBuffer buffer = packet->buffer;
1525 SilcClientEntry client;
1526 SilcClientID *client_id;
1528 SilcIDListData idata;
1529 char *username = NULL, *realname = NULL, *id_string;
1530 SilcUInt16 username_len;
1533 char *hostname, *nickname;
1536 SILC_LOG_DEBUG(("Creating new client"));
1538 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1541 /* Take client entry */
1542 client = (SilcClientEntry)sock->user_data;
1543 idata = (SilcIDListData)client;
1545 /* Remove the old cache entry. */
1546 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1547 SILC_LOG_INFO(("Unauthenticated client attempted to register to network"));
1548 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1549 "You have not been authenticated");
1553 /* Parse incoming packet */
1554 ret = silc_buffer_unformat(buffer,
1555 SILC_STR_UI16_NSTRING_ALLOC(&username,
1557 SILC_STR_UI16_STRING_ALLOC(&realname),
1560 silc_free(username);
1561 silc_free(realname);
1562 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1563 "connection", sock->hostname, sock->ip));
1564 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1565 "Incomplete client information");
1570 silc_free(username);
1571 silc_free(realname);
1572 SILC_LOG_ERROR(("Client %s (%s) did not send its username, closing "
1573 "connection", sock->hostname, sock->ip));
1574 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1575 "Incomplete client information");
1579 if (username_len > 128)
1580 username[128] = '\0';
1582 /* Check for bad characters for nickname, and modify the nickname if
1583 it includes those. */
1584 if (silc_server_name_bad_chars(username, username_len)) {
1585 nickname = silc_server_name_modify_bad(username, username_len);
1587 nickname = strdup(username);
1590 /* Make sanity checks for the hostname of the client. If the hostname
1591 is provided in the `username' check that it is the same than the
1592 resolved hostname, or if not resolved the hostname that appears in
1593 the client's public key. If the hostname is not present then put
1594 it from the resolved name or from the public key. */
1595 if (strchr(username, '@')) {
1596 SilcPublicKeyIdentifier pident;
1597 int tlen = strcspn(username, "@");
1598 char *phostname = NULL;
1600 hostname = silc_memdup(username + tlen + 1, strlen(username) - tlen - 1);
1602 if (strcmp(sock->hostname, sock->ip) &&
1603 strcmp(sock->hostname, hostname)) {
1604 silc_free(username);
1605 silc_free(hostname);
1606 silc_free(realname);
1607 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1608 "connection", sock->hostname, sock->ip));
1609 silc_server_disconnect_remote(server, sock,
1610 "Server closed connection: "
1611 "Incomplete client information");
1615 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1617 phostname = strdup(pident->host);
1618 silc_pkcs_free_identifier(pident);
1621 if (!strcmp(sock->hostname, sock->ip) &&
1622 phostname && strcmp(phostname, hostname)) {
1623 silc_free(username);
1624 silc_free(hostname);
1625 silc_free(phostname);
1626 silc_free(realname);
1627 SILC_LOG_ERROR(("Client %s (%s) sent incomplete information, closing "
1628 "connection", sock->hostname, sock->ip));
1629 silc_server_disconnect_remote(server, sock,
1630 "Server closed connection: "
1631 "Incomplete client information");
1635 silc_free(phostname);
1637 /* The hostname is not present, add it. */
1639 /* XXX For now we cannot take the host name from the public key since
1640 they are not trusted or we cannot verify them as trusted. Just take
1641 what the resolved name or address is. */
1643 if (strcmp(sock->hostname, sock->ip)) {
1645 newusername = silc_calloc(strlen(username) +
1646 strlen(sock->hostname) + 2,
1647 sizeof(*newusername));
1648 strncat(newusername, username, strlen(username));
1649 strncat(newusername, "@", 1);
1650 strncat(newusername, sock->hostname, strlen(sock->hostname));
1651 silc_free(username);
1652 username = newusername;
1655 SilcPublicKeyIdentifier pident =
1656 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1659 newusername = silc_calloc(strlen(username) +
1660 strlen(pident->host) + 2,
1661 sizeof(*newusername));
1662 strncat(newusername, username, strlen(username));
1663 strncat(newusername, "@", 1);
1664 strncat(newusername, pident->host, strlen(pident->host));
1665 silc_free(username);
1666 username = newusername;
1667 silc_pkcs_free_identifier(pident);
1673 /* Create Client ID */
1674 while (!silc_id_create_client_id(server, server->id, server->rng,
1675 server->md5hash, nickname, &client_id)) {
1677 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
1680 /* Update client entry */
1681 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1682 client->nickname = nickname;
1683 client->username = username;
1684 client->userinfo = realname ? realname : strdup(" ");
1685 client->id = client_id;
1686 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1688 /* Add the client again to the ID cache */
1689 silc_idcache_add(server->local_list->clients, client->nickname,
1690 client_id, client, 0, NULL);
1692 /* Notify our router about new client on the SILC network */
1693 if (!server->standalone)
1694 silc_server_send_new_id(server, (SilcSocketConnection)
1695 server->router->connection,
1696 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1697 client->id, SILC_ID_CLIENT, id_len);
1699 /* Send the new client ID to the client. */
1700 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1701 reply = silc_buffer_alloc(2 + 2 + id_len);
1702 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1703 silc_buffer_format(reply,
1704 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1705 SILC_STR_UI_SHORT(id_len),
1706 SILC_STR_UI_XNSTRING(id_string, id_len),
1708 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1709 reply->data, reply->len, FALSE);
1710 silc_free(id_string);
1711 silc_buffer_free(reply);
1713 /* Send some nice info to the client */
1714 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1715 ("Welcome to the SILC Network %s",
1717 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1718 ("Your host is %s, running version %s",
1719 server->config->server_info->server_name,
1721 if (server->server_type == SILC_ROUTER) {
1722 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1723 ("There are %d clients on %d servers in SILC "
1724 "Network", server->stat.clients,
1725 server->stat.servers + 1));
1726 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1727 ("There are %d clients on %d server in our cell",
1728 server->stat.cell_clients,
1729 server->stat.cell_servers + 1));
1730 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1731 ("I have %d clients, %d channels, %d servers and "
1733 server->stat.my_clients,
1734 server->stat.my_channels,
1735 server->stat.my_servers,
1736 server->stat.my_routers));
1737 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1738 ("There are %d server operators and %d router "
1740 server->stat.server_ops,
1741 server->stat.router_ops));
1742 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1743 ("I have %d operators online",
1744 server->stat.my_router_ops +
1745 server->stat.my_server_ops));
1747 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1748 ("I have %d clients and %d channels formed",
1749 server->stat.my_clients,
1750 server->stat.my_channels));
1751 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1752 ("%d operators online",
1753 server->stat.my_server_ops));
1755 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1756 ("Your connection is secured with %s cipher, "
1757 "key length %d bits",
1758 idata->send_key->cipher->name,
1759 idata->send_key->cipher->key_len));
1760 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1761 ("Your current nickname is %s",
1765 silc_server_send_motd(server, sock);
1770 /* Create new server. This processes received New Server packet and
1771 saves the received Server ID. The server is our locally connected
1772 server thus we save all the information and save it to local list.
1773 This funtion can be used by both normal server and router server.
1774 If normal server uses this it means that its router has connected
1775 to the server. If router uses this it means that one of the cell's
1776 servers is connected to the router. */
1778 SilcServerEntry silc_server_new_server(SilcServer server,
1779 SilcSocketConnection sock,
1780 SilcPacketContext *packet)
1782 SilcBuffer buffer = packet->buffer;
1783 SilcServerEntry new_server, server_entry;
1784 SilcServerID *server_id;
1785 SilcIDListData idata;
1786 unsigned char *server_name, *id_string;
1787 SilcUInt16 id_len, name_len;
1791 SILC_LOG_DEBUG(("Creating new server"));
1793 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1794 sock->type != SILC_SOCKET_TYPE_ROUTER)
1797 /* Take server entry */
1798 new_server = (SilcServerEntry)sock->user_data;
1799 idata = (SilcIDListData)new_server;
1801 /* Remove the old cache entry */
1802 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
1803 if (!silc_idcache_del_by_context(server->global_list->servers,
1805 SILC_LOG_INFO(("Unauthenticated %s attempted to register to "
1806 "network", (sock->type == SILC_SOCKET_TYPE_SERVER ?
1807 "server" : "router")));
1808 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1809 "You have not been authenticated");
1815 /* Parse the incoming packet */
1816 ret = silc_buffer_unformat(buffer,
1817 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1818 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
1823 silc_free(id_string);
1825 silc_free(server_name);
1829 if (id_len > buffer->len) {
1830 silc_free(id_string);
1831 silc_free(server_name);
1836 server_name[255] = '\0';
1839 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1841 silc_free(id_string);
1842 silc_free(server_name);
1845 silc_free(id_string);
1847 /* Check for valid server ID */
1848 if (!silc_id_is_valid_server_id(server, server_id, sock)) {
1849 SILC_LOG_INFO(("Invalid server ID sent by %s (%s)",
1850 sock->ip, sock->hostname));
1851 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1852 "Your Server ID is not valid");
1853 silc_free(server_name);
1857 /* Check that we do not have this ID already */
1858 server_entry = silc_idlist_find_server_by_id(server->local_list,
1859 server_id, TRUE, NULL);
1861 silc_idcache_del_by_context(server->local_list->servers, server_entry);
1863 server_entry = silc_idlist_find_server_by_id(server->global_list,
1864 server_id, TRUE, NULL);
1866 silc_idcache_del_by_context(server->global_list->servers, server_entry);
1869 /* Update server entry */
1870 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1871 new_server->server_name = server_name;
1872 new_server->id = server_id;
1874 SILC_LOG_DEBUG(("New server id(%s)",
1875 silc_id_render(server_id, SILC_ID_SERVER)));
1877 /* Add again the entry to the ID cache. */
1878 silc_idcache_add(local ? server->local_list->servers :
1879 server->global_list->servers, server_name, server_id,
1880 new_server, 0, NULL);
1882 /* Distribute the information about new server in the SILC network
1883 to our router. If we are normal server we won't send anything
1884 since this connection must be our router connection. */
1885 if (server->server_type == SILC_ROUTER && !server->standalone &&
1886 server->router->connection != sock)
1887 silc_server_send_new_id(server, server->router->connection,
1888 TRUE, new_server->id, SILC_ID_SERVER,
1889 silc_id_get_len(server_id, SILC_ID_SERVER));
1891 if (server->server_type == SILC_ROUTER)
1892 server->stat.cell_servers++;
1894 /* Check whether this router connection has been replaced by an
1895 backup router. If it has been then we'll disable the server and will
1896 ignore everything it will send until the backup router resuming
1897 protocol has been completed. */
1898 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1899 silc_server_backup_replaced_get(server, server_id, NULL)) {
1900 /* Send packet to the server indicating that it cannot use this
1901 connection as it has been replaced by backup router. */
1902 SilcBuffer packet = silc_buffer_alloc(2);
1903 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1904 silc_buffer_format(packet,
1905 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
1906 SILC_STR_UI_CHAR(0),
1908 silc_server_packet_send(server, sock,
1909 SILC_PACKET_RESUME_ROUTER, 0,
1910 packet->data, packet->len, TRUE);
1911 silc_buffer_free(packet);
1913 /* Mark the router disabled. The data sent earlier will go but nothing
1914 after this does not go to this connection. */
1915 idata->status |= SILC_IDLIST_STATUS_DISABLED;
1917 /* If it is router announce our stuff to it. */
1918 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1919 server->server_type == SILC_ROUTER) {
1920 silc_server_announce_servers(server, FALSE, 0, sock);
1921 silc_server_announce_clients(server, 0, sock);
1922 silc_server_announce_channels(server, 0, sock);
1929 /* Processes incoming New ID packet. New ID Payload is used to distribute
1930 information about newly registered clients and servers. */
1932 static void silc_server_new_id_real(SilcServer server,
1933 SilcSocketConnection sock,
1934 SilcPacketContext *packet,
1937 SilcBuffer buffer = packet->buffer;
1939 SilcServerEntry router, server_entry;
1940 SilcSocketConnection router_sock;
1945 SILC_LOG_DEBUG(("Processing new ID"));
1947 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1948 server->server_type == SILC_SERVER ||
1949 packet->src_id_type != SILC_ID_SERVER)
1952 idp = silc_id_payload_parse(buffer->data, buffer->len);
1956 id_type = silc_id_payload_get_type(idp);
1958 /* Normal server cannot have other normal server connections */
1959 server_entry = (SilcServerEntry)sock->user_data;
1960 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
1961 server_entry->server_type == SILC_SERVER)
1964 id = silc_id_payload_get_id(idp);
1968 /* If the packet is coming from server then use the sender as the
1969 origin of the the packet. If it came from router then check the real
1970 sender of the packet and use that as the origin. */
1971 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
1972 id_list = server->local_list;
1974 router = sock->user_data;
1976 /* If the sender is backup router and ID is server (and we are not
1977 backup router) then switch the entry to global list. */
1978 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
1979 id_type == SILC_ID_SERVER &&
1980 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
1981 id_list = server->global_list;
1982 router_sock = server->router ? server->router->connection : sock;
1985 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1986 packet->src_id_type);
1987 router = silc_idlist_find_server_by_id(server->global_list,
1988 sender_id, TRUE, NULL);
1990 router = silc_idlist_find_server_by_id(server->local_list,
1991 sender_id, TRUE, NULL);
1992 silc_free(sender_id);
1994 id_list = server->global_list;
2001 case SILC_ID_CLIENT:
2003 SilcClientEntry entry;
2005 /* Check that we do not have this client already */
2006 entry = silc_idlist_find_client_by_id(server->global_list,
2007 id, server->server_type,
2010 entry = silc_idlist_find_client_by_id(server->local_list,
2011 id, server->server_type,
2014 SILC_LOG_DEBUG(("Ignoring client that we already have"));
2018 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
2019 silc_id_render(id, SILC_ID_CLIENT),
2020 sock->type == SILC_SOCKET_TYPE_SERVER ?
2021 "Server" : "Router", sock->hostname));
2023 /* As a router we keep information of all global information in our
2024 global list. Cell wide information however is kept in the local
2026 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
2027 id, router, NULL, 0);
2029 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
2031 /* Inform the sender that the ID is not usable */
2032 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
2035 entry->nickname = NULL;
2036 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2038 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2039 server->stat.cell_clients++;
2040 server->stat.clients++;
2044 case SILC_ID_SERVER:
2046 SilcServerEntry entry;
2048 /* If the ID is mine, ignore it. */
2049 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
2050 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
2054 /* If the ID is the sender's ID, ignore it (we have it already) */
2055 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
2056 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
2060 /* Check that we do not have this server already */
2061 entry = silc_idlist_find_server_by_id(server->global_list,
2062 id, server->server_type,
2065 entry = silc_idlist_find_server_by_id(server->local_list,
2066 id, server->server_type,
2069 SILC_LOG_DEBUG(("Ignoring server that we already have"));
2073 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
2074 silc_id_render(id, SILC_ID_SERVER),
2075 sock->type == SILC_SOCKET_TYPE_SERVER ?
2076 "Server" : "Router", sock->hostname));
2078 /* As a router we keep information of all global information in our
2079 global list. Cell wide information however is kept in the local
2081 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
2084 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2087 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2089 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2090 server->stat.cell_servers++;
2091 server->stat.servers++;
2095 case SILC_ID_CHANNEL:
2096 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2105 /* If the sender of this packet is server and we are router we need to
2106 broadcast this packet to other routers in the network. */
2107 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2108 sock->type == SILC_SOCKET_TYPE_SERVER &&
2109 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2110 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2111 silc_server_packet_send(server, server->router->connection,
2113 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2114 buffer->data, buffer->len, FALSE);
2115 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2116 packet->type, packet->flags,
2117 packet->buffer->data, packet->buffer->len,
2122 silc_id_payload_free(idp);
2126 /* Processes incoming New ID packet. New ID Payload is used to distribute
2127 information about newly registered clients and servers. */
2129 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2130 SilcPacketContext *packet)
2132 silc_server_new_id_real(server, sock, packet, TRUE);
2135 /* Receoved New Id List packet, list of New ID payloads inside one
2136 packet. Process the New ID payloads one by one. */
2138 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2139 SilcPacketContext *packet)
2141 SilcPacketContext *new_id;
2145 SILC_LOG_DEBUG(("Processing New ID List"));
2147 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2148 packet->src_id_type != SILC_ID_SERVER)
2151 /* If the sender of this packet is server and we are router we need to
2152 broadcast this packet to other routers in the network. Broadcast
2153 this list packet instead of multiple New ID packets. */
2154 if (!server->standalone && server->server_type == SILC_ROUTER &&
2155 sock->type == SILC_SOCKET_TYPE_SERVER &&
2156 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2157 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2158 silc_server_packet_send(server, server->router->connection,
2160 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2161 packet->buffer->data, packet->buffer->len, FALSE);
2162 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2163 packet->type, packet->flags,
2164 packet->buffer->data, packet->buffer->len,
2168 /* Make copy of the original packet context, except for the actual
2169 data buffer, which we will here now fetch from the original buffer. */
2170 new_id = silc_packet_context_alloc();
2171 new_id->type = SILC_PACKET_NEW_ID;
2172 new_id->flags = packet->flags;
2173 new_id->src_id = packet->src_id;
2174 new_id->src_id_len = packet->src_id_len;
2175 new_id->src_id_type = packet->src_id_type;
2176 new_id->dst_id = packet->dst_id;
2177 new_id->dst_id_len = packet->dst_id_len;
2178 new_id->dst_id_type = packet->dst_id_type;
2180 idp = silc_buffer_alloc(256);
2181 new_id->buffer = idp;
2183 while (packet->buffer->len) {
2184 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2185 if ((id_len > packet->buffer->len) ||
2186 (id_len > idp->truelen))
2189 silc_buffer_pull_tail(idp, 4 + id_len);
2190 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2192 /* Process the New ID */
2193 silc_server_new_id_real(server, sock, new_id, FALSE);
2195 silc_buffer_push_tail(idp, 4 + id_len);
2196 silc_buffer_pull(packet->buffer, 4 + id_len);
2199 silc_buffer_free(idp);
2203 /* Received New Channel packet. Information about new channels in the
2204 network are distributed using this packet. Save the information about
2205 the new channel. This usually comes from router but also normal server
2206 can send this to notify channels it has when it connects to us. */
2208 void silc_server_new_channel(SilcServer server,
2209 SilcSocketConnection sock,
2210 SilcPacketContext *packet)
2212 SilcChannelPayload payload;
2213 SilcChannelID *channel_id;
2215 SilcUInt32 name_len;
2219 SilcServerEntry server_entry;
2220 SilcChannelEntry channel;
2222 SILC_LOG_DEBUG(("Processing New Channel"));
2224 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2225 packet->src_id_type != SILC_ID_SERVER ||
2226 server->server_type == SILC_SERVER)
2229 /* Parse the channel payload */
2230 payload = silc_channel_payload_parse(packet->buffer->data,
2231 packet->buffer->len);
2235 /* Get the channel ID */
2236 channel_id = silc_channel_get_id_parse(payload);
2238 silc_channel_payload_free(payload);
2242 channel_name = silc_channel_get_name(payload, &name_len);
2244 channel_name[255] = '\0';
2246 id = silc_channel_get_id(payload, &id_len);
2248 server_entry = (SilcServerEntry)sock->user_data;
2250 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2251 /* Add the channel to global list as it is coming from router. It
2252 cannot be our own channel as it is coming from router. */
2254 /* Check that we don't already have this channel */
2255 channel = silc_idlist_find_channel_by_name(server->local_list,
2256 channel_name, NULL);
2258 channel = silc_idlist_find_channel_by_name(server->global_list,
2259 channel_name, NULL);
2261 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2262 silc_id_render(channel_id, SILC_ID_CHANNEL),
2265 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2266 0, channel_id, sock->user_data, NULL, NULL, 0);
2267 server->stat.channels++;
2270 /* The channel is coming from our server, thus it is in our cell
2271 we will add it to our local list. */
2274 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2275 silc_id_render(channel_id, SILC_ID_CHANNEL),
2278 /* Check that we don't already have this channel */
2279 channel = silc_idlist_find_channel_by_name(server->local_list,
2280 channel_name, NULL);
2282 channel = silc_idlist_find_channel_by_name(server->global_list,
2283 channel_name, NULL);
2285 /* If the channel does not exist, then create it. This creates a new
2286 key to the channel as well that we will send to the server. */
2288 /* The protocol says that the Channel ID's IP address must be based
2289 on the router's IP address. Check whether the ID is based in our
2290 IP and if it is not then create a new ID and enforce the server
2291 to switch the ID. */
2292 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2293 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2295 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2297 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2298 silc_server_send_notify_channel_change(server, sock, FALSE,
2300 silc_free(channel_id);
2305 /* Create the channel with the provided Channel ID */
2306 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2310 silc_channel_payload_free(payload);
2311 silc_free(channel_id);
2315 /* Get the mode and set it to the channel */
2316 channel->mode = silc_channel_get_mode(payload);
2318 /* Send the new channel key to the server */
2319 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2320 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2321 chk = silc_channel_key_payload_encode(id_len, id,
2322 strlen(channel->channel_key->
2324 channel->channel_key->cipher->name,
2325 channel->key_len / 8,
2327 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2328 chk->data, chk->len, FALSE);
2329 silc_buffer_free(chk);
2332 /* The channel exist by that name, check whether the ID's match.
2333 If they don't then we'll force the server to use the ID we have.
2334 We also create a new key for the channel. */
2335 SilcBuffer users = NULL, users_modes = NULL;
2337 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2338 /* They don't match, send CHANNEL_CHANGE notify to the server to
2339 force the ID change. */
2340 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2341 silc_server_send_notify_channel_change(server, sock, FALSE,
2342 channel_id, channel->id);
2345 /* If the mode is different from what we have then enforce the
2347 mode = silc_channel_get_mode(payload);
2348 if (channel->mode != mode) {
2349 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2350 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2351 channel->mode, server->id,
2353 channel->cipher, channel->hmac_name,
2354 channel->passphrase);
2357 /* Create new key for the channel and send it to the server and
2358 everybody else possibly on the channel. */
2360 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2361 if (!silc_server_create_channel_key(server, channel, 0))
2364 /* Send to the channel */
2365 silc_server_send_channel_key(server, sock, channel, FALSE);
2366 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2367 id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
2369 /* Send to the server */
2370 chk = silc_channel_key_payload_encode(id_len, id,
2371 strlen(channel->channel_key->
2373 channel->channel_key->
2375 channel->key_len / 8,
2377 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2378 chk->data, chk->len, FALSE);
2379 silc_buffer_free(chk);
2383 silc_free(channel_id);
2385 /* Since the channel is coming from server and we also know about it
2386 then send the JOIN notify to the server so that it see's our
2387 users on the channel "joining" the channel. */
2388 silc_server_announce_get_channel_users(server, channel, &users,
2391 silc_buffer_push(users, users->data - users->head);
2392 silc_server_packet_send(server, sock,
2393 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2394 users->data, users->len, FALSE);
2395 silc_buffer_free(users);
2398 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2399 silc_server_packet_send_dest(server, sock,
2400 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2401 channel->id, SILC_ID_CHANNEL,
2403 users_modes->len, FALSE);
2404 silc_buffer_free(users_modes);
2409 silc_channel_payload_free(payload);
2412 /* Received New Channel List packet, list of New Channel List payloads inside
2413 one packet. Process the New Channel payloads one by one. */
2415 void silc_server_new_channel_list(SilcServer server,
2416 SilcSocketConnection sock,
2417 SilcPacketContext *packet)
2419 SilcPacketContext *new;
2421 SilcUInt16 len1, len2;
2423 SILC_LOG_DEBUG(("Processing New Channel List"));
2425 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2426 packet->src_id_type != SILC_ID_SERVER ||
2427 server->server_type == SILC_SERVER)
2430 /* If the sender of this packet is server and we are router we need to
2431 broadcast this packet to other routers in the network. Broadcast
2432 this list packet instead of multiple New Channel packets. */
2433 if (!server->standalone && server->server_type == SILC_ROUTER &&
2434 sock->type == SILC_SOCKET_TYPE_SERVER &&
2435 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2436 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2437 silc_server_packet_send(server, server->router->connection,
2439 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2440 packet->buffer->data, packet->buffer->len, FALSE);
2441 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2442 packet->type, packet->flags,
2443 packet->buffer->data, packet->buffer->len,
2447 /* Make copy of the original packet context, except for the actual
2448 data buffer, which we will here now fetch from the original buffer. */
2449 new = silc_packet_context_alloc();
2450 new->type = SILC_PACKET_NEW_CHANNEL;
2451 new->flags = packet->flags;
2452 new->src_id = packet->src_id;
2453 new->src_id_len = packet->src_id_len;
2454 new->src_id_type = packet->src_id_type;
2455 new->dst_id = packet->dst_id;
2456 new->dst_id_len = packet->dst_id_len;
2457 new->dst_id_type = packet->dst_id_type;
2459 buffer = silc_buffer_alloc(512);
2460 new->buffer = buffer;
2462 while (packet->buffer->len) {
2463 SILC_GET16_MSB(len1, packet->buffer->data);
2464 if ((len1 > packet->buffer->len) ||
2465 (len1 > buffer->truelen))
2468 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2469 if ((len2 > packet->buffer->len) ||
2470 (len2 > buffer->truelen))
2473 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2474 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2476 /* Process the New Channel */
2477 silc_server_new_channel(server, sock, new);
2479 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2480 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2483 silc_buffer_free(buffer);
2487 /* Received key agreement packet. This packet is never for us. It is to
2488 the client in the packet's destination ID. Sending of this sort of packet
2489 equals sending private message, ie. it is sent point to point from
2490 one client to another. */
2492 void silc_server_key_agreement(SilcServer server,
2493 SilcSocketConnection sock,
2494 SilcPacketContext *packet)
2496 SilcSocketConnection dst_sock;
2497 SilcIDListData idata;
2499 SILC_LOG_DEBUG(("Start"));
2501 if (packet->src_id_type != SILC_ID_CLIENT ||
2502 packet->dst_id_type != SILC_ID_CLIENT)
2505 if (!packet->dst_id)
2508 /* Get the route to the client */
2509 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2510 packet->dst_id_len, NULL, &idata);
2514 /* Relay the packet */
2515 silc_server_relay_packet(server, dst_sock, idata->send_key,
2516 idata->hmac_send, idata->psn_send++,
2520 /* Received connection auth request packet that is used during connection
2521 phase to resolve the mandatory authentication method. This packet can
2522 actually be received at anytime but usually it is used only during
2523 the connection authentication phase. Now, protocol says that this packet
2524 can come from client or server, however, we support only this coming
2525 from client and expect that server always knows what authentication
2528 void silc_server_connection_auth_request(SilcServer server,
2529 SilcSocketConnection sock,
2530 SilcPacketContext *packet)
2532 SilcServerConfigClient *client = NULL;
2533 SilcUInt16 conn_type;
2535 SilcAuthMethod auth_meth = SILC_AUTH_NONE;
2537 SILC_LOG_DEBUG(("Start"));
2539 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2542 /* Parse the payload */
2543 ret = silc_buffer_unformat(packet->buffer,
2544 SILC_STR_UI_SHORT(&conn_type),
2545 SILC_STR_UI_SHORT(NULL),
2550 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2553 /* Get the authentication method for the client */
2554 auth_meth = SILC_AUTH_NONE;
2555 client = silc_server_config_find_client(server, sock->ip);
2557 client = silc_server_config_find_client(server, sock->hostname);
2559 if (client->passphrase) {
2560 if (client->publickeys && !server->config->prefer_passphrase_auth)
2561 auth_meth = SILC_AUTH_PUBLIC_KEY;
2563 auth_meth = SILC_AUTH_PASSWORD;
2564 } else if (client->publickeys)
2565 auth_meth = SILC_AUTH_PUBLIC_KEY;
2568 /* Send it back to the client */
2569 silc_server_send_connection_auth_request(server, sock, conn_type, auth_meth);
2572 /* Received REKEY packet. The sender of the packet wants to regenerate
2573 its session keys. This starts the REKEY protocol. */
2575 void silc_server_rekey(SilcServer server,
2576 SilcSocketConnection sock,
2577 SilcPacketContext *packet)
2579 SilcProtocol protocol;
2580 SilcServerRekeyInternalContext *proto_ctx;
2581 SilcIDListData idata = (SilcIDListData)sock->user_data;
2583 SILC_LOG_DEBUG(("Start"));
2585 /* Allocate internal protocol context. This is sent as context
2587 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2588 proto_ctx->server = (void *)server;
2589 proto_ctx->sock = sock;
2590 proto_ctx->responder = TRUE;
2591 proto_ctx->pfs = idata->rekey->pfs;
2593 /* Perform rekey protocol. Will call the final callback after the
2594 protocol is over. */
2595 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2596 &protocol, proto_ctx, silc_server_rekey_final);
2597 sock->protocol = protocol;
2599 if (proto_ctx->pfs == FALSE)
2600 /* Run the protocol */
2601 silc_protocol_execute(protocol, server->schedule, 0, 0);
2604 /* Received file transger packet. This packet is never for us. It is to
2605 the client in the packet's destination ID. Sending of this sort of packet
2606 equals sending private message, ie. it is sent point to point from
2607 one client to another. */
2609 void silc_server_ftp(SilcServer server,
2610 SilcSocketConnection sock,
2611 SilcPacketContext *packet)
2613 SilcSocketConnection dst_sock;
2614 SilcIDListData idata;
2616 SILC_LOG_DEBUG(("Start"));
2618 if (packet->src_id_type != SILC_ID_CLIENT ||
2619 packet->dst_id_type != SILC_ID_CLIENT)
2622 if (!packet->dst_id)
2625 /* Get the route to the client */
2626 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2627 packet->dst_id_len, NULL, &idata);
2631 /* Relay the packet */
2632 silc_server_relay_packet(server, dst_sock, idata->send_key,
2633 idata->hmac_send, idata->psn_send++,