5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005, 2007 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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 #include "serverincludes.h"
22 #include "server_internal.h"
23 #include "command_reply.h"
25 /* All functions that call the COMMAND_CHECK_STATUS macros must have
26 out: and err: goto labels. */
28 #define COMMAND_CHECK_STATUS \
30 SILC_LOG_DEBUG(("Start")); \
31 if (!silc_command_get_status(cmd->payload, &status, &error)) { \
32 if (SILC_STATUS_IS_ERROR(status)) \
34 if (status == SILC_STATUS_LIST_END) \
40 /* Server command reply list. Not all commands have reply function as
41 they are never sent by server. More maybe added later if need appears. */
42 SilcServerCommandReply silc_command_reply_list[] =
44 SILC_SERVER_CMD_REPLY(whois, WHOIS),
45 SILC_SERVER_CMD_REPLY(whowas, WHOWAS),
46 SILC_SERVER_CMD_REPLY(identify, IDENTIFY),
47 SILC_SERVER_CMD_REPLY(info, INFO),
48 SILC_SERVER_CMD_REPLY(motd, MOTD),
49 SILC_SERVER_CMD_REPLY(join, JOIN),
50 SILC_SERVER_CMD_REPLY(stats, STATS),
51 SILC_SERVER_CMD_REPLY(users, USERS),
52 SILC_SERVER_CMD_REPLY(getkey, GETKEY),
53 SILC_SERVER_CMD_REPLY(list, LIST),
54 SILC_SERVER_CMD_REPLY(watch, WATCH),
55 SILC_SERVER_CMD_REPLY(ping, PING),
60 /* Process received command reply. */
62 void silc_server_command_reply_process(SilcServer server,
63 SilcPacketStream sock,
66 SilcIDListData idata = silc_packet_get_context(sock);
67 SilcServerCommandReply *cmd;
68 SilcServerCommandReplyContext ctx;
69 SilcCommandPayload payload;
72 SILC_LOG_DEBUG(("Start"));
74 /* Get command reply payload from packet */
75 payload = silc_command_payload_parse(buffer->data, silc_buffer_len(buffer));
77 /* Silently ignore bad reply packet */
78 SILC_LOG_DEBUG(("Bad command reply packet"));
82 /* Allocate command reply context. This must be free'd by the
83 command reply routine receiving it. */
84 ctx = silc_calloc(1, sizeof(*ctx));
87 ctx->payload = payload;
88 ctx->args = silc_command_get_args(ctx->payload);
89 ctx->ident = silc_command_get_ident(ctx->payload);
90 command = silc_command_get(ctx->payload);
91 silc_packet_stream_ref(sock);
93 /* Client is not allowed to send reply to all commands */
94 if (idata->conn_type == SILC_CONN_CLIENT &&
95 command != SILC_COMMAND_WHOIS) {
96 silc_server_command_reply_free(ctx);
100 /* Check for pending commands and mark to be exeucted */
102 silc_server_command_pending_check(server, command,
103 ctx->ident, &ctx->callbacks_count);
105 /* Execute command reply */
106 for (cmd = silc_command_reply_list; cmd->cb; cmd++)
107 if (cmd->cmd == command)
110 if (cmd == NULL || !cmd->cb) {
111 silc_server_command_reply_free(ctx);
118 /* Free command reply context and its internals. */
120 void silc_server_command_reply_free(SilcServerCommandReplyContext cmd)
123 silc_command_payload_free(cmd->payload);
125 silc_packet_stream_unref(cmd->sock);
126 silc_free(cmd->callbacks);
132 silc_server_command_process_error(SilcServerCommandReplyContext cmd,
135 SilcServer server = cmd->server;
136 SilcIDListData idata = silc_packet_get_context(cmd->sock);
138 /* If we received notify for invalid ID we'll remove the ID if we
140 if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
141 idata->conn_type == SILC_CONN_ROUTER) {
142 SilcClientEntry client;
144 if (silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id,
146 SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
147 "the entry from cache"));
148 client = silc_idlist_find_client_by_id(server->global_list,
149 SILC_ID_GET_ID(id), FALSE, NULL);
153 if (client->data.public_key)
154 silc_hash_table_del_by_context(server->pk_hash,
155 client->data.public_key,
158 silc_server_remove_from_channels(server, NULL, client, TRUE,
160 silc_idlist_del_data(client);
161 silc_idlist_del_client(server->global_list, client);
166 /* Caches the received WHOIS information. */
169 silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
171 SilcServer server = cmd->server;
172 unsigned char *id_data, *umodes;
173 char *nickname, *username, *realname, *tmp;
174 unsigned char *fingerprint;
176 SilcClientEntry client;
178 char nick[128 + 1], servername[256 + 1], uname[128 + 1];
179 SilcUInt32 mode = 0, len, len2, id_len, flen;
180 const char *hostname, *ip;
182 silc_socket_stream_get_info(silc_packet_stream_get_stream(cmd->sock),
183 NULL, &hostname, &ip, NULL);
185 id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
186 nickname = silc_argument_get_arg_type(cmd->args, 3, &len);
187 username = silc_argument_get_arg_type(cmd->args, 4, &len);
188 realname = silc_argument_get_arg_type(cmd->args, 5, &len);
189 if (!id_data || !nickname || !username || !realname)
192 tmp = silc_argument_get_arg_type(cmd->args, 7, &len);
194 SILC_GET32_MSB(mode, tmp);
196 if (!silc_id_payload_parse_id(id_data, id_len, &id))
199 fingerprint = silc_argument_get_arg_type(cmd->args, 9, &flen);
201 /* Check if we have this client cached already. */
203 client = silc_idlist_find_client_by_id(server->local_list,
207 client = silc_idlist_find_client_by_id(server->global_list,
214 /* If router did not find such Client ID in its lists then this must
215 be bogus client or some router in the net is buggy. */
216 if (server->server_type != SILC_SERVER)
219 /* Take hostname out of nick string if it includes it. */
220 silc_parse_userfqdn(nickname, nick, sizeof(nick), servername,
223 /* We don't have that client anywhere, add it. The client is added
224 to global list since server didn't have it in the lists so it must be
225 global. This will check for valid nickname and username strings. */
226 client = silc_idlist_add_client(server->global_list,
227 strdup(nick), username,
229 silc_id_dup(SILC_ID_GET_ID(id),
231 silc_packet_get_context(cmd->sock),
234 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
238 client->data.status |=
239 (SILC_IDLIST_STATUS_REGISTERED | SILC_IDLIST_STATUS_RESOLVED);
240 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
242 client->servername = servername[0] ? strdup(servername) : NULL;
244 /* We have the client already, update the data */
246 SILC_LOG_DEBUG(("Updating client data"));
249 silc_parse_userfqdn(nickname, nick, sizeof(nick), servername,
251 nickname = silc_identifier_check(nick, strlen(nick), SILC_STRING_UTF8,
254 SILC_LOG_ERROR(("Malformed nickname '%s' received in WHOIS reply "
256 hostname ? hostname : "", nick));
261 silc_parse_userfqdn(username, uname, sizeof(uname), NULL, 0);
262 if (!silc_identifier_verify(uname, strlen(uname), SILC_STRING_UTF8, 128)) {
263 SILC_LOG_ERROR(("Malformed username '%s' received in WHOIS reply "
265 hostname ? hostname : "", tmp));
269 /* Remove the old cache entry */
270 silc_idcache_del_by_context(global ? server->global_list->clients :
271 server->local_list->clients, client, NULL);
273 silc_free(client->nickname);
274 silc_free(client->username);
275 silc_free(client->userinfo);
276 silc_free(client->servername);
278 client->nickname = strdup(nick);
279 client->username = strdup(username);
280 client->userinfo = strdup(realname);
281 client->servername = servername[0] ? strdup(servername) : NULL;
283 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
284 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
286 /* Create new cache entry */
287 silc_idcache_add(global ? server->global_list->clients :
288 server->local_list->clients, nickname, client->id,
292 /* Save channel list if it was sent to us */
293 if (server->server_type == SILC_SERVER) {
294 tmp = silc_argument_get_arg_type(cmd->args, 6, &len);
295 umodes = silc_argument_get_arg_type(cmd->args, 10, &len2);
297 SilcBufferStruct channels_buf, umodes_buf;
298 silc_buffer_set(&channels_buf, tmp, len);
299 silc_buffer_set(&umodes_buf, umodes, len2);
300 silc_server_save_user_channels(server, cmd->sock, client, &channels_buf,
303 silc_server_save_user_channels(server, cmd->sock, client, NULL, NULL);
307 if (fingerprint && flen == sizeof(client->data.fingerprint))
308 memcpy(client->data.fingerprint, fingerprint, flen);
310 /* Take Requested Attributes if set. */
311 tmp = silc_argument_get_arg_type(cmd->args, 11, &len);
313 silc_free(client->attrs);
314 client->attrs = silc_memdup(tmp, len);
315 client->attrs_len = len;
317 /* Try to take public key from attributes if present and we don't have
318 the key already. Do this only on normal server. Routers do GETKEY
319 for all clients anyway. */
320 if (server->server_type != SILC_ROUTER && !client->data.public_key) {
321 SilcAttributePayload attr;
322 SilcAttributeObjPk pk;
323 unsigned char f[SILC_HASH_MAXLEN];
324 SilcDList attrs = silc_attribute_payload_parse(tmp, len);
326 SILC_LOG_DEBUG(("Take client public key from attributes"));
329 silc_dlist_start(attrs);
330 while ((attr = silc_dlist_get(attrs)) != SILC_LIST_END) {
331 if (silc_attribute_get_attribute(attr) ==
332 SILC_ATTRIBUTE_USER_PUBLIC_KEY) {
334 if (!silc_attribute_get_object(attr, &pk, sizeof(pk)))
337 /* Take only SILC public keys */
338 if (strcmp(pk.type, "silc-rsa")) {
344 /* Verify that the server provided fingerprint matches the key */
345 silc_hash_make(server->sha1hash, pk.data, pk.data_len, f);
346 if (memcmp(f, client->data.fingerprint, sizeof(f))) {
352 /* Save the public key. */
353 if (!silc_pkcs_public_key_alloc(SILC_PKCS_SILC,
354 pk.data, pk.data_len,
355 &client->data.public_key)) {
361 SILC_LOG_DEBUG(("Saved client public key from attributes"));
363 /* Add to public key hash table */
364 if (!silc_hash_table_find_by_context(server->pk_hash,
365 client->data.public_key,
367 silc_hash_table_add(server->pk_hash,
368 client->data.public_key, client);
376 silc_attribute_payload_list_free(attrs);
384 /* Handle requested attributes reply in WHOIS from client */
387 silc_server_command_reply_whois_save_client(SilcServerCommandReplyContext cmd)
391 SilcClientEntry client = silc_packet_get_context(cmd->sock);
393 /* Take Requested Attributes if set. */
394 tmp = silc_argument_get_arg_type(cmd->args, 11, &len);
396 silc_free(client->attrs);
397 client->attrs = silc_memdup(tmp, len);
398 client->attrs_len = len;
401 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
406 /* Reiceved reply for WHOIS command. We sent the whois request to our
407 primary router, if we are normal server, and thus has now received reply
408 to the command. We will figure out what client originally sent us the
409 command and will send the reply to it. If we are router we will figure
410 out who server sent us the command and send reply to that one. */
412 SILC_SERVER_CMD_REPLY_FUNC(whois)
414 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
415 SilcIDListData idata = silc_packet_get_context(cmd->sock);
416 SilcStatus status, error;
418 COMMAND_CHECK_STATUS;
420 if (idata->conn_type != SILC_CONN_CLIENT) {
421 if (!silc_server_command_reply_whois_save(cmd))
424 if (!silc_server_command_reply_whois_save_client(cmd))
428 /* Pending callbacks are not executed if this was an list entry */
429 if (status != SILC_STATUS_OK &&
430 status != SILC_STATUS_LIST_END) {
431 silc_server_command_reply_free(cmd);
436 silc_server_command_process_error(cmd, error);
437 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOIS);
438 silc_server_command_reply_free(cmd);
442 silc_server_command_process_error(cmd, error);
443 silc_server_command_reply_free(cmd);
446 /* Caches the received WHOWAS information for a short period of time. */
449 silc_server_command_reply_whowas_save(SilcServerCommandReplyContext cmd)
451 SilcServer server = cmd->server;
452 SilcUInt32 len, id_len;
453 unsigned char *id_data;
454 char *nickname, *username, *realname;
456 SilcClientEntry client;
457 SilcIDCacheEntry cache = NULL;
458 char nick[128 + 1], servername[256 + 1], uname[128 + 1];
460 const char *hostname, *ip;
462 silc_socket_stream_get_info(silc_packet_stream_get_stream(cmd->sock),
463 NULL, &hostname, &ip, NULL);
465 id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
466 nickname = silc_argument_get_arg_type(cmd->args, 3, &len);
467 username = silc_argument_get_arg_type(cmd->args, 4, &len);
468 if (!id_data || !nickname || !username)
471 realname = silc_argument_get_arg_type(cmd->args, 5, &len);
473 if (!silc_id_payload_parse_id(id_data, id_len, &id))
476 /* Check if we have this client cached already. */
478 client = silc_idlist_find_client_by_id(server->local_list,
482 client = silc_idlist_find_client_by_id(server->global_list,
489 /* If router did not find such Client ID in its lists then this must
490 be bogus client or some router in the net is buggy. */
491 if (server->server_type != SILC_SERVER)
494 /* Take hostname out of nick string if it includes it. */
495 silc_parse_userfqdn(nickname, nick, sizeof(nick), servername,
498 /* We don't have that client anywhere, add it. The client is added
499 to global list since server didn't have it in the lists so it must be
501 client = silc_idlist_add_client(server->global_list,
502 strdup(nick), username,
504 silc_id_dup(SILC_ID_GET_ID(id),
506 silc_packet_get_context(cmd->sock), NULL);
508 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
512 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
513 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
514 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
515 client->servername = servername[0] ? strdup(servername) : NULL;
517 /* We have the client already, update the data */
520 silc_parse_userfqdn(nickname, nick, sizeof(nick), servername,
522 nickname = silc_identifier_check(nick, strlen(nick), SILC_STRING_UTF8,
525 SILC_LOG_ERROR(("Malformed nickname '%s' received in WHOWAS reply "
527 hostname ? hostname : "", nick));
532 silc_parse_userfqdn(username, uname, sizeof(uname), NULL, 0);
533 if (!silc_identifier_verify(uname, strlen(uname), SILC_STRING_UTF8, 128))
536 silc_free(client->nickname);
537 silc_free(client->username);
538 silc_free(client->servername);
540 client->nickname = strdup(nick);
541 client->username = strdup(username);
542 client->servername = servername[0] ? strdup(servername) : NULL;
543 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
544 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
546 /* Remove the old cache entry and create a new one */
547 silc_idcache_del_by_context(global ? server->global_list->clients :
548 server->local_list->clients, client, NULL);
549 silc_idcache_add(global ? server->global_list->clients :
550 server->local_list->clients, nickname, client->id,
557 /* Received reply for WHOWAS command. Cache the client information only for
558 a short period of time. */
560 SILC_SERVER_CMD_REPLY_FUNC(whowas)
562 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
563 SilcStatus status, error;
565 COMMAND_CHECK_STATUS;
567 if (!silc_server_command_reply_whowas_save(cmd))
570 /* Pending callbacks are not executed if this was an list entry */
571 if (status != SILC_STATUS_OK &&
572 status != SILC_STATUS_LIST_END) {
573 silc_server_command_reply_free(cmd);
578 silc_server_command_process_error(cmd, error);
579 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOWAS);
580 silc_server_command_reply_free(cmd);
584 silc_server_command_process_error(cmd, error);
585 silc_server_command_reply_free(cmd);
588 /* Caches the received IDENTIFY information. */
591 silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
593 SilcServer server = cmd->server;
594 SilcUInt32 len, id_len;
595 unsigned char *id_data;
597 SilcClientID client_id;
598 SilcServerID server_id;
599 SilcChannelID*channel_id;
600 SilcClientEntry client;
601 SilcServerEntry server_entry;
602 SilcChannelEntry channel;
605 SilcIDPayload idp = NULL;
608 id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
611 idp = silc_id_payload_parse(id_data, id_len);
615 name = silc_argument_get_arg_type(cmd->args, 3, &len);
616 info = silc_argument_get_arg_type(cmd->args, 4, &len);
618 id_type = silc_id_payload_get_type(idp);
622 if (!silc_id_payload_get_id(idp, &client_id, sizeof(client_id)))
625 SILC_LOG_DEBUG(("Received client information"));
627 client = silc_idlist_find_client_by_id(server->local_list,
628 &client_id, FALSE, NULL);
630 client = silc_idlist_find_client_by_id(server->global_list, &client_id,
635 /* If router did not find such Client ID in its lists then this must
636 be bogus client or some router in the net is buggy. */
637 if (server->server_type != SILC_SERVER)
642 silc_parse_userfqdn(name, nick, sizeof(nick), NULL, 0);
644 /* We don't have that client anywhere, add it. The client is added
645 to global list since server didn't have it in the lists so it must be
647 client = silc_idlist_add_client(server->global_list,
648 nick[0] ? nick : NULL, info, NULL,
649 silc_id_dup(&client_id, SILC_ID_CLIENT),
650 silc_packet_get_context(cmd->sock),
653 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
657 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
658 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
659 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
661 /* We have the client already, update the data */
663 SILC_LOG_DEBUG(("Updating client data"));
667 silc_parse_userfqdn(name, nick, sizeof(nick), NULL, 0);
670 name = silc_identifier_check(nick, strlen(nick), SILC_STRING_UTF8,
673 SILC_LOG_ERROR(("Malformed nickname '%s' received in IDENTIFY "
678 /* Remove the old cache entry */
679 silc_idcache_del_by_context(global ? server->global_list->clients :
680 server->local_list->clients, client, NULL);
682 silc_free(client->nickname);
683 client->nickname = strdup(nick);
685 /* Add new cache entry */
686 silc_idcache_add(global ? server->global_list->clients :
687 server->local_list->clients, name, client->id,
692 silc_free(client->username);
693 client->username = strdup(info);
696 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
697 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
706 if (!silc_id_payload_get_id(idp, &server_id, sizeof(server_id)))
709 SILC_LOG_DEBUG(("Received server information"));
711 server_entry = silc_idlist_find_server_by_id(server->local_list,
712 &server_id, FALSE, NULL);
714 server_entry = silc_idlist_find_server_by_id(server->global_list,
715 &server_id, FALSE, NULL);
717 /* If router did not find such Server ID in its lists then this must
718 be bogus server or some router in the net is buggy. */
719 if (server->server_type != SILC_SERVER)
722 /* We don't have that server anywhere, add it. */
723 server_entry = silc_idlist_add_server(server->global_list,
725 silc_id_dup(&server_id,
728 SILC_PRIMARY_ROUTE(server));
732 server_entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
733 server_entry->data.status |= SILC_IDLIST_STATUS_RESOLVED;
734 server_entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
739 case SILC_ID_CHANNEL:
743 if (!silc_id_payload_get_id(idp, &channel_id, sizeof(channel_id)))
746 SILC_LOG_DEBUG(("Received channel information"));
748 /* Check channel name */
749 info = silc_channel_name_check(name, strlen(name), SILC_STRING_UTF8,
754 channel = silc_idlist_find_channel_by_name(server->local_list,
757 channel = silc_idlist_find_channel_by_name(server->global_list,
760 /* If router did not find such Channel ID in its lists then this must
761 be bogus channel or some router in the net is buggy. */
762 if (server->server_type != SILC_SERVER) {
767 /* We don't have that channel anywhere, add it. */
768 channel = silc_idlist_add_channel(server->global_list, strdup(name),
769 SILC_CHANNEL_MODE_NONE,
770 silc_id_dup(&channel_id,
772 server->router, NULL, NULL, 0);
783 silc_id_payload_free(idp);
787 silc_id_payload_free(idp);
791 /* Received reply for forwarded IDENTIFY command. We have received the
792 requested identify information now and we will cache it. After this we
793 will call the pending command so that the requestee gets the information
796 SILC_SERVER_CMD_REPLY_FUNC(identify)
798 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
799 SilcStatus status, error;
801 COMMAND_CHECK_STATUS;
803 if (!silc_server_command_reply_identify_save(cmd))
806 /* Pending callbacks are not executed if this was an list entry */
807 if (status != SILC_STATUS_OK &&
808 status != SILC_STATUS_LIST_END) {
809 silc_server_command_reply_free(cmd);
814 silc_server_command_process_error(cmd, error);
815 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_IDENTIFY);
816 silc_server_command_reply_free(cmd);
820 silc_server_command_process_error(cmd, error);
821 silc_server_command_reply_free(cmd);
824 /* Received reply fro INFO command. Cache the server and its information */
826 SILC_SERVER_CMD_REPLY_FUNC(info)
828 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
829 SilcServer server = cmd->server;
830 SilcStatus status, error;
831 SilcServerEntry entry;
834 unsigned char *tmp, *name;
836 COMMAND_CHECK_STATUS;
839 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL))
843 name = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
847 entry = silc_idlist_find_server_by_id(server->local_list,
851 entry = silc_idlist_find_server_by_id(server->global_list,
855 /* Add the server to global list */
856 entry = silc_idlist_add_server(server->global_list, strdup(name), 0,
857 silc_id_dup(SILC_ID_GET_ID(id),
859 silc_packet_get_context(cmd->sock),
864 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
868 /* Get the info string */
869 tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
873 entry->server_info = tmp ? strdup(tmp) : NULL;
876 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_INFO);
878 silc_server_command_reply_free(cmd);
881 /* Received reply fro MOTD command. */
883 SILC_SERVER_CMD_REPLY_FUNC(motd)
885 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
886 SilcServer server = cmd->server;
887 SilcStatus status, error;
888 SilcServerEntry entry = NULL;
893 COMMAND_CHECK_STATUS;
896 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL))
899 entry = silc_idlist_find_server_by_id(server->local_list,
903 entry = silc_idlist_find_server_by_id(server->global_list,
909 /* If router did not find such Server ID in its lists then this must
910 be bogus client or some router in the net is buggy. */
911 if (server->server_type != SILC_SERVER)
915 cmd->server->stat.commands_sent++;
917 /* entry isn't known so we IDENTIFY it. otherwise the
918 silc_server_command_motd won't know about it and tell
919 the client that there is no such server */
920 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
921 buffer = silc_command_payload_encode_va(SILC_COMMAND_IDENTIFY,
922 ++server->cmd_ident, 5,
923 1, NULL, 0, 2, NULL, 0,
924 3, NULL, 0, 4, NULL, 0,
926 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
927 SILC_PACKET_COMMAND, 0, buffer->data,
928 silc_buffer_len(buffer));
929 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
931 silc_server_command_reply_motd,
933 silc_buffer_free(buffer);
939 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
946 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_MOTD);
948 silc_server_command_reply_free(cmd);
954 /* Received reply for forwarded JOIN command. Router has created or joined
955 the client to the channel. We save some channel information locally
958 SILC_SERVER_CMD_REPLY_FUNC(join)
960 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
961 SilcServer server = cmd->server;
962 SilcIDCacheEntry cache = NULL;
963 SilcStatus status, error;
965 SilcChannelEntry entry;
966 SilcHmac hmac = NULL;
967 SilcUInt32 len, list_count;
968 char *channel_name, *channel_namec = NULL, *tmp;
969 SilcUInt32 mode, created;
970 SilcBuffer keyp = NULL, client_id_list = NULL, client_mode_list = NULL;
971 SilcPublicKey founder_key = NULL;
973 COMMAND_CHECK_STATUS;
975 /* Get channel name */
976 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
981 if (!silc_argument_get_decoded(cmd->args, 3, SILC_ARGUMENT_ID, &id, NULL))
985 if (!silc_argument_get_decoded(cmd->args, 4, SILC_ARGUMENT_ID, &id2, NULL))
989 tmp = silc_argument_get_arg_type(cmd->args, 5, NULL);
992 SILC_GET32_MSB(mode, tmp);
994 /* Get created boolean value */
995 tmp = silc_argument_get_arg_type(cmd->args, 6, NULL);
998 SILC_GET32_MSB(created, tmp);
999 if (created != 0 && created != 1)
1002 /* Get channel key */
1003 tmp = silc_argument_get_arg_type(cmd->args, 7, &len);
1005 keyp = silc_buffer_alloc(len);
1006 silc_buffer_pull_tail(keyp, silc_buffer_truelen(keyp));
1007 silc_buffer_put(keyp, tmp, len);
1011 tmp = silc_argument_get_arg_type(cmd->args, 11, NULL);
1013 if (!silc_hmac_alloc(tmp, NULL, &hmac))
1017 /* Get the list count */
1018 tmp = silc_argument_get_arg_type(cmd->args, 12, &len);
1021 SILC_GET32_MSB(list_count, tmp);
1023 /* Get Client ID list */
1024 tmp = silc_argument_get_arg_type(cmd->args, 13, &len);
1028 client_id_list = silc_buffer_alloc(len);
1029 silc_buffer_pull_tail(client_id_list, len);
1030 silc_buffer_put(client_id_list, tmp, len);
1032 /* Get client mode list */
1033 tmp = silc_argument_get_arg_type(cmd->args, 14, &len);
1037 client_mode_list = silc_buffer_alloc(len);
1038 silc_buffer_pull_tail(client_mode_list, len);
1039 silc_buffer_put(client_mode_list, tmp, len);
1041 /* Get founder key */
1042 tmp = silc_argument_get_arg_type(cmd->args, 15, &len);
1044 silc_public_key_payload_decode(tmp, len, &founder_key);
1046 /* See whether we already have the channel. */
1047 channel_namec = silc_channel_name_check(channel_name, strlen(channel_name),
1048 SILC_STRING_UTF8, 256, NULL);
1051 entry = silc_idlist_find_channel_by_name(server->local_list,
1052 channel_namec, &cache);
1054 /* Add new channel */
1056 SILC_LOG_DEBUG(("Adding new [%s] channel %s id(%s)",
1057 (created == 0 ? "existing" : "created"), channel_name,
1058 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1060 /* If the channel is found from global list we must move it to the
1062 entry = silc_idlist_find_channel_by_name(server->global_list,
1063 channel_namec, &cache);
1065 silc_idlist_del_channel(server->global_list, entry);
1067 /* Add the channel to our local list. */
1068 entry = silc_idlist_add_channel(server->local_list, strdup(channel_name),
1069 SILC_CHANNEL_MODE_NONE,
1070 silc_id_dup(SILC_ID_GET_ID(id),
1072 server->router, NULL, NULL, hmac);
1077 server->stat.my_channels++;
1078 server->stat.channels++;
1080 /* The entry exists. */
1082 /* If ID has changed, then update it to the cache too. */
1083 if (!SILC_ID_CHANNEL_COMPARE(entry->id, SILC_ID_GET_ID(id)))
1084 silc_idlist_replace_channel_id(server->local_list, entry->id,
1085 SILC_ID_GET_ID(id));
1087 entry->disabled = FALSE;
1089 /* Remove the founder auth data if the mode is not set but we have
1090 them in the entry */
1091 if (!(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) && entry->founder_key) {
1092 silc_pkcs_public_key_free(entry->founder_key);
1093 entry->founder_key = NULL;
1098 if (entry->founder_key)
1099 silc_pkcs_public_key_free(entry->founder_key);
1100 entry->founder_key = founder_key;
1104 if (entry->hmac_name && (hmac || (!hmac && entry->hmac))) {
1105 silc_free(entry->hmac_name);
1106 entry->hmac_name = strdup(silc_hmac_get_name(hmac ? hmac : entry->hmac));
1109 /* Get the ban list */
1110 tmp = silc_argument_get_arg_type(cmd->args, 8, &len);
1111 if (tmp && len > 2) {
1112 SilcArgumentPayload iargs;
1114 SILC_GET16_MSB(iargc, tmp);
1115 iargs = silc_argument_payload_parse(tmp + 2, len - 2, iargc);
1117 /* Delete old ban list */
1118 if (entry->ban_list)
1119 silc_hash_table_free(entry->ban_list);
1121 silc_hash_table_alloc(0, silc_hash_ptr,
1123 silc_server_inviteban_destruct, entry, TRUE);
1125 /* Add new ban list */
1126 silc_server_inviteban_process(server, entry->ban_list, 0, iargs);
1127 silc_argument_payload_free(iargs);
1131 /* Get the invite list */
1132 tmp = silc_argument_get_arg_type(cmd->args, 9, &len);
1133 if (tmp && len > 2) {
1134 SilcArgumentPayload iargs;
1136 SILC_GET16_MSB(iargc, tmp);
1137 iargs = silc_argument_payload_parse(tmp + 2, len - 2, iargc);
1139 /* Delete old invite list */
1140 if (entry->invite_list)
1141 silc_hash_table_free(entry->invite_list);
1142 entry->invite_list =
1143 silc_hash_table_alloc(0, silc_hash_ptr,
1145 silc_server_inviteban_destruct, entry, TRUE);
1147 /* Add new invite list */
1148 silc_server_inviteban_process(server, entry->invite_list, 0, iargs);
1149 silc_argument_payload_free(iargs);
1154 tmp = silc_argument_get_arg_type(cmd->args, 10, &len);
1156 silc_free(entry->topic);
1157 entry->topic = strdup(tmp);
1160 /* Get channel public key list */
1161 tmp = silc_argument_get_arg_type(cmd->args, 16, &len);
1162 if (tmp && server->server_type != SILC_ROUTER)
1163 silc_server_set_channel_pk_list(server, NULL, entry, tmp, len);
1165 /* The the user limit */
1166 tmp = silc_argument_get_arg_type(cmd->args, 17, &len);
1167 if (tmp && len == 4)
1168 SILC_GET32_MSB(entry->user_limit, tmp);
1170 /* If channel was not created we know there is global users on the
1172 entry->global_users = (created == 0 ? TRUE : FALSE);
1174 /* If channel was just created the mask must be zero */
1175 if (!entry->global_users && mode)
1178 /* Save channel mode */
1181 /* Save channel key */
1183 if (!(entry->mode & SILC_CHANNEL_MODE_PRIVKEY))
1184 silc_server_save_channel_key(server, keyp, entry);
1185 silc_buffer_free(keyp);
1188 /* Save the users to the channel */
1189 silc_server_save_users_on_channel(server, cmd->sock, entry,
1190 SILC_ID_GET_ID(id2), client_id_list,
1191 client_mode_list, list_count);
1192 entry->users_resolved = TRUE;
1195 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_JOIN);
1197 silc_free(channel_namec);
1199 silc_hmac_free(hmac);
1200 silc_server_command_reply_free(cmd);
1202 silc_pkcs_public_key_free(founder_key);
1204 silc_buffer_free(client_id_list);
1205 if (client_mode_list)
1206 silc_buffer_free(client_mode_list);
1209 /* Received reply to STATS command. */
1211 SILC_SERVER_CMD_REPLY_FUNC(stats)
1213 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1214 SilcServer server = cmd->server;
1215 SilcStatus status, error;
1218 SilcBufferStruct buf;
1220 COMMAND_CHECK_STATUS;
1222 /* Get statistics structure */
1223 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
1224 if (server->server_type != SILC_ROUTER && tmp) {
1225 silc_buffer_set(&buf, tmp, tmp_len);
1226 silc_buffer_unformat(&buf,
1227 SILC_STR_UI_INT(NULL),
1228 SILC_STR_UI_INT(NULL),
1229 SILC_STR_UI_INT(NULL),
1230 SILC_STR_UI_INT(NULL),
1231 SILC_STR_UI_INT(NULL),
1232 SILC_STR_UI_INT(NULL),
1233 SILC_STR_UI_INT(&server->stat.cell_clients),
1234 SILC_STR_UI_INT(&server->stat.cell_channels),
1235 SILC_STR_UI_INT(&server->stat.cell_servers),
1236 SILC_STR_UI_INT(&server->stat.clients),
1237 SILC_STR_UI_INT(&server->stat.channels),
1238 SILC_STR_UI_INT(&server->stat.servers),
1239 SILC_STR_UI_INT(&server->stat.routers),
1240 SILC_STR_UI_INT(&server->stat.server_ops),
1241 SILC_STR_UI_INT(&server->stat.router_ops),
1246 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_STATS);
1248 silc_server_command_reply_free(cmd);
1251 SILC_SERVER_CMD_REPLY_FUNC(users)
1253 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1254 SilcServer server = cmd->server;
1255 SilcStatus status, error;
1256 SilcChannelEntry channel;
1258 SilcBuffer client_id_list;
1259 SilcBuffer client_mode_list;
1262 SilcUInt32 list_count;
1264 COMMAND_CHECK_STATUS;
1266 /* Get channel ID */
1267 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL))
1270 /* Get channel entry */
1271 channel = silc_idlist_find_channel_by_id(server->local_list,
1272 SILC_ID_GET_ID(id), NULL);
1274 channel = silc_idlist_find_channel_by_id(server->global_list,
1275 SILC_ID_GET_ID(id), NULL);
1279 if (server->server_type != SILC_SERVER)
1282 idp = silc_id_payload_encode(SILC_ID_GET_ID(id), SILC_ID_CHANNEL);
1283 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
1284 SILC_COMMAND_IDENTIFY, ++server->cmd_ident,
1285 1, 5, idp->data, silc_buffer_len(idp));
1286 silc_buffer_free(idp);
1288 /* Register pending command callback. After we've received the channel
1289 information we will reprocess this command reply by re-calling this
1290 USERS command reply callback. */
1291 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
1293 silc_server_command_reply_users, cmd);
1298 /* Get the list count */
1299 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
1302 SILC_GET32_MSB(list_count, tmp);
1304 /* Get Client ID list */
1305 tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
1309 client_id_list = silc_buffer_alloc(tmp_len);
1310 silc_buffer_pull_tail(client_id_list, tmp_len);
1311 silc_buffer_put(client_id_list, tmp, tmp_len);
1313 /* Get client mode list */
1314 tmp = silc_argument_get_arg_type(cmd->args, 5, &tmp_len);
1318 client_mode_list = silc_buffer_alloc(tmp_len);
1319 silc_buffer_pull_tail(client_mode_list, tmp_len);
1320 silc_buffer_put(client_mode_list, tmp, tmp_len);
1322 /* Save the users to the channel */
1323 silc_server_save_users_on_channel(server, cmd->sock, channel, NULL,
1324 client_id_list, client_mode_list,
1327 channel->global_users = silc_server_channel_has_global(channel);
1328 channel->users_resolved = TRUE;
1330 silc_buffer_free(client_id_list);
1331 silc_buffer_free(client_mode_list);
1334 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
1336 silc_server_command_reply_free(cmd);
1339 SILC_SERVER_CMD_REPLY_FUNC(getkey)
1341 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1342 SilcServer server = cmd->server;
1343 SilcStatus status, error;
1344 SilcClientEntry client = NULL;
1345 SilcServerEntry server_entry = NULL;
1346 SilcClientID client_id;
1347 SilcServerID server_id;
1350 SilcIDPayload idp = NULL;
1352 SilcPublicKey public_key = NULL;
1354 COMMAND_CHECK_STATUS;
1356 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1359 idp = silc_id_payload_parse(tmp, len);
1363 /* Get the public key payload */
1364 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
1368 /* Decode the public key payload */
1369 if (!silc_public_key_payload_decode(tmp, len, &public_key))
1372 id_type = silc_id_payload_get_type(idp);
1373 if (id_type == SILC_ID_CLIENT) {
1374 silc_id_payload_get_id(idp, &client_id, sizeof(client_id));
1376 client = silc_idlist_find_client_by_id(server->local_list, &client_id,
1379 client = silc_idlist_find_client_by_id(server->global_list,
1380 &client_id, TRUE, NULL);
1385 if (!client->data.public_key) {
1386 if (!silc_hash_table_find_by_context(server->pk_hash, public_key,
1388 silc_hash_table_add(server->pk_hash, public_key, client);
1390 client->data.public_key = public_key;
1393 } else if (id_type == SILC_ID_SERVER) {
1394 silc_id_payload_get_id(idp, &server_id, sizeof(server_id));
1396 server_entry = silc_idlist_find_server_by_id(server->local_list,
1397 &server_id, TRUE, NULL);
1398 if (!server_entry) {
1399 server_entry = silc_idlist_find_server_by_id(server->global_list,
1400 &server_id, TRUE, NULL);
1405 server_entry->data.public_key = public_key;
1412 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_GETKEY);
1414 silc_id_payload_free(idp);
1416 silc_pkcs_public_key_free(public_key);
1418 silc_server_command_reply_free(cmd);
1421 SILC_SERVER_CMD_REPLY_FUNC(list)
1423 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1424 SilcServer server = cmd->server;
1425 SilcStatus status, error;
1427 SilcChannelEntry channel;
1428 SilcIDCacheEntry cache;
1429 unsigned char *tmp, *name, *namec = NULL, *topic;
1430 SilcUInt32 usercount = 0;
1431 SilcBool global_list = FALSE;
1433 COMMAND_CHECK_STATUS;
1435 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL))
1438 name = silc_argument_get_arg_type(cmd->args, 3, NULL);
1439 topic = silc_argument_get_arg_type(cmd->args, 4, NULL);
1440 tmp = silc_argument_get_arg_type(cmd->args, 5, NULL);
1442 SILC_GET32_MSB(usercount, tmp);
1444 namec = silc_channel_name_check(name, strlen(name), SILC_STRING_UTF8,
1449 /* Add the channel entry if we do not have it already */
1450 channel = silc_idlist_find_channel_by_name(server->local_list,
1453 channel = silc_idlist_find_channel_by_name(server->global_list,
1458 /* If router did not find such channel in its lists then this must
1459 be bogus channel or some router in the net is buggy. */
1460 if (server->server_type != SILC_SERVER)
1463 channel = silc_idlist_add_channel(server->global_list, strdup(name),
1464 SILC_CHANNEL_MODE_NONE,
1465 silc_id_dup(SILC_ID_GET_ID(id),
1467 server->router, NULL, NULL, NULL);
1472 channel->user_count = usercount;
1475 silc_free(channel->topic);
1476 channel->topic = strdup(topic);
1479 /* Pending callbacks are not executed if this was an list entry */
1480 if (status != SILC_STATUS_OK &&
1481 status != SILC_STATUS_LIST_END) {
1482 silc_server_command_reply_free(cmd);
1487 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_LIST);
1490 silc_server_command_reply_free(cmd);
1493 SILC_SERVER_CMD_REPLY_FUNC(watch)
1495 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1496 SilcStatus status, error;
1498 COMMAND_CHECK_STATUS;
1501 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WATCH);
1503 silc_server_command_reply_free(cmd);
1506 SILC_SERVER_CMD_REPLY_FUNC(ping)
1508 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1509 SilcStatus status, error;
1511 COMMAND_CHECK_STATUS;
1514 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_PING);
1516 silc_server_command_reply_free(cmd);