5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2009 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 silc_server_remove_from_channels(server, NULL, client, TRUE,
155 silc_dlist_del(server->expired_clients, client);
156 silc_idlist_del_data(client);
157 silc_idlist_del_client(server->global_list, client);
162 /* Caches the received WHOIS information. */
165 silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
167 SilcServer server = cmd->server;
168 unsigned char *id_data, *umodes;
169 char *nickname, *username, *realname, *tmp;
170 unsigned char *fingerprint;
172 SilcClientEntry client;
174 char nick[128 + 1], servername[256 + 1], uname[128 + 1];
175 SilcUInt32 mode = 0, len, len2, id_len, flen;
176 const char *hostname, *ip;
178 silc_socket_stream_get_info(silc_packet_stream_get_stream(cmd->sock),
179 NULL, &hostname, &ip, NULL);
181 id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
182 nickname = silc_argument_get_arg_type(cmd->args, 3, &len);
183 username = silc_argument_get_arg_type(cmd->args, 4, &len);
184 realname = silc_argument_get_arg_type(cmd->args, 5, &len);
185 if (!id_data || !nickname || !username || !realname)
188 tmp = silc_argument_get_arg_type(cmd->args, 7, &len);
190 SILC_GET32_MSB(mode, tmp);
192 if (!silc_id_payload_parse_id(id_data, id_len, &id))
195 fingerprint = silc_argument_get_arg_type(cmd->args, 9, &flen);
197 /* Check if we have this client cached already. */
199 client = silc_idlist_find_client_by_id(server->local_list,
203 client = silc_idlist_find_client_by_id(server->global_list,
210 /* If router did not find such Client ID in its lists then this must
211 be bogus client or some router in the net is buggy. */
212 if (server->server_type != SILC_SERVER)
215 /* Take hostname out of nick string if it includes it. */
216 silc_parse_userfqdn(nickname, nick, sizeof(nick), servername,
219 /* We don't have that client anywhere, add it. The client is added
220 to global list since server didn't have it in the lists so it must be
221 global. This will check for valid nickname and username strings. */
222 client = silc_idlist_add_client(server->global_list,
223 strdup(nick), username,
225 silc_id_dup(SILC_ID_GET_ID(id),
227 silc_packet_get_context(cmd->sock),
230 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
234 client->data.status |=
235 (SILC_IDLIST_STATUS_REGISTERED | SILC_IDLIST_STATUS_RESOLVED);
236 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
238 client->servername = servername[0] ? strdup(servername) : NULL;
240 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
241 server->stat.clients + 1));
242 server->stat.clients++;
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));
270 silc_idcache_update_by_context(global ? server->global_list->clients :
271 server->local_list->clients, client, NULL,
274 silc_free(client->nickname);
275 silc_free(client->username);
276 silc_free(client->userinfo);
277 silc_free(client->servername);
279 client->nickname = strdup(nick);
280 client->username = strdup(username);
281 client->userinfo = strdup(realname);
282 client->servername = servername[0] ? strdup(servername) : NULL;
284 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
285 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
288 /* Save channel list if it was sent to us */
289 if (server->server_type == SILC_SERVER) {
290 tmp = silc_argument_get_arg_type(cmd->args, 6, &len);
291 umodes = silc_argument_get_arg_type(cmd->args, 10, &len2);
293 SilcBufferStruct channels_buf, umodes_buf;
294 silc_buffer_set(&channels_buf, tmp, len);
295 silc_buffer_set(&umodes_buf, umodes, len2);
296 silc_server_save_user_channels(server, cmd->sock, client, &channels_buf,
299 silc_server_save_user_channels(server, cmd->sock, client, NULL, NULL);
303 if (fingerprint && flen == sizeof(client->data.fingerprint))
304 memcpy(client->data.fingerprint, fingerprint, flen);
306 /* Take Requested Attributes if set. */
307 tmp = silc_argument_get_arg_type(cmd->args, 11, &len);
309 silc_free(client->attrs);
310 client->attrs = silc_memdup(tmp, len);
311 client->attrs_len = len;
313 /* Try to take public key from attributes if present and we don't have
314 the key already. Do this only on normal server. Routers do GETKEY
315 for all clients anyway. */
316 if (server->server_type != SILC_ROUTER && !client->data.public_key) {
317 SilcAttributePayload attr;
318 SilcAttributeObjPk pk;
319 unsigned char f[SILC_HASH_MAXLEN];
320 SilcDList attrs = silc_attribute_payload_parse(tmp, len);
322 SILC_LOG_DEBUG(("Take client public key from attributes"));
325 silc_dlist_start(attrs);
326 while ((attr = silc_dlist_get(attrs)) != SILC_LIST_END) {
327 if (silc_attribute_get_attribute(attr) ==
328 SILC_ATTRIBUTE_USER_PUBLIC_KEY) {
330 if (!silc_attribute_get_object(attr, &pk, sizeof(pk)))
333 /* Take only SILC public keys */
334 if (strcmp(pk.type, "silc-rsa")) {
340 /* Verify that the server provided fingerprint matches the key */
341 silc_hash_make(server->sha1hash, pk.data, pk.data_len, f);
342 if (memcmp(f, client->data.fingerprint, sizeof(f))) {
348 /* Save the public key. */
349 if (!silc_pkcs_public_key_alloc(SILC_PKCS_SILC,
350 pk.data, pk.data_len,
351 &client->data.public_key)) {
357 SILC_LOG_DEBUG(("Saved client public key from attributes"));
359 /* Add client's public key to repository */
360 if (!silc_server_get_public_key_by_client(server, client, NULL))
361 silc_skr_add_public_key_simple(server->repository,
362 client->data.public_key,
363 SILC_SKR_USAGE_IDENTIFICATION,
372 silc_attribute_payload_list_free(attrs);
380 /* Handle requested attributes reply in WHOIS from client */
383 silc_server_command_reply_whois_save_client(SilcServerCommandReplyContext cmd)
387 SilcClientEntry client = silc_packet_get_context(cmd->sock);
389 /* Take Requested Attributes if set. */
390 tmp = silc_argument_get_arg_type(cmd->args, 11, &len);
392 silc_free(client->attrs);
393 client->attrs = silc_memdup(tmp, len);
394 client->attrs_len = len;
397 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
402 /* Reiceved reply for WHOIS command. We sent the whois request to our
403 primary router, if we are normal server, and thus has now received reply
404 to the command. We will figure out what client originally sent us the
405 command and will send the reply to it. If we are router we will figure
406 out who server sent us the command and send reply to that one. */
408 SILC_SERVER_CMD_REPLY_FUNC(whois)
410 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
411 SilcIDListData idata = silc_packet_get_context(cmd->sock);
412 SilcStatus status, error;
414 COMMAND_CHECK_STATUS;
416 if (idata->conn_type != SILC_CONN_CLIENT) {
417 if (!silc_server_command_reply_whois_save(cmd))
420 if (!silc_server_command_reply_whois_save_client(cmd))
424 /* Pending callbacks are not executed if this was an list entry */
425 if (status != SILC_STATUS_OK &&
426 status != SILC_STATUS_LIST_END) {
427 silc_server_command_reply_free(cmd);
432 silc_server_command_process_error(cmd, error);
433 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOIS);
434 silc_server_command_reply_free(cmd);
438 silc_server_command_process_error(cmd, error);
439 silc_server_command_reply_free(cmd);
442 /* Caches the received WHOWAS information for a short period of time. */
445 silc_server_command_reply_whowas_save(SilcServerCommandReplyContext cmd)
447 SilcServer server = cmd->server;
448 SilcUInt32 len, id_len;
449 unsigned char *id_data;
450 char *nickname, *username, *realname;
452 SilcClientEntry client;
453 SilcIDCacheEntry cache = NULL;
454 char nick[128 + 1], servername[256 + 1], uname[128 + 1];
456 const char *hostname, *ip;
458 silc_socket_stream_get_info(silc_packet_stream_get_stream(cmd->sock),
459 NULL, &hostname, &ip, NULL);
461 id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
462 nickname = silc_argument_get_arg_type(cmd->args, 3, &len);
463 username = silc_argument_get_arg_type(cmd->args, 4, &len);
464 if (!id_data || !nickname || !username)
467 realname = silc_argument_get_arg_type(cmd->args, 5, &len);
469 if (!silc_id_payload_parse_id(id_data, id_len, &id))
472 /* Check if we have this client cached already. */
474 client = silc_idlist_find_client_by_id(server->local_list,
478 client = silc_idlist_find_client_by_id(server->global_list,
485 /* If router did not find such Client ID in its lists then this must
486 be bogus client or some router in the net is buggy. */
487 if (server->server_type != SILC_SERVER)
490 /* Take hostname out of nick string if it includes it. */
491 silc_parse_userfqdn(nickname, nick, sizeof(nick), servername,
494 /* We don't have that client anywhere, add it. The client is added
495 to global list since server didn't have it in the lists so it must be
497 client = silc_idlist_add_client(server->global_list,
498 strdup(nick), username,
500 silc_id_dup(SILC_ID_GET_ID(id),
502 silc_packet_get_context(cmd->sock), NULL);
504 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
508 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
509 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
510 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
511 client->servername = servername[0] ? strdup(servername) : NULL;
513 /* We have the client already, update the data */
516 silc_parse_userfqdn(nickname, nick, sizeof(nick), servername,
518 nickname = silc_identifier_check(nick, strlen(nick), SILC_STRING_UTF8,
521 SILC_LOG_ERROR(("Malformed nickname '%s' received in WHOWAS reply "
523 nick, hostname ? hostname : ""));
528 silc_parse_userfqdn(username, uname, sizeof(uname), NULL, 0);
529 if (!silc_identifier_verify(uname, strlen(uname), SILC_STRING_UTF8, 128))
532 silc_free(client->nickname);
533 silc_free(client->username);
534 silc_free(client->servername);
536 client->nickname = strdup(nick);
537 client->username = strdup(username);
538 client->servername = servername[0] ? strdup(servername) : NULL;
539 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
540 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
541 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
543 /* Update cache entry */
544 silc_idcache_update_by_context(global ? server->global_list->clients :
545 server->local_list->clients, client, NULL,
549 /* If client is global and is not on any channel then add that we'll
550 expire the entry after a while. */
552 client = silc_idlist_find_client_by_id(server->global_list, client->id,
554 if (client && !silc_hash_table_count(client->channels)) {
555 client->data.created = silc_time();
556 silc_dlist_del(server->expired_clients, client);
557 silc_dlist_add(server->expired_clients, client);
564 /* Received reply for WHOWAS command. Cache the client information only for
565 a short period of time. */
567 SILC_SERVER_CMD_REPLY_FUNC(whowas)
569 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
570 SilcStatus status, error;
572 COMMAND_CHECK_STATUS;
574 if (!silc_server_command_reply_whowas_save(cmd))
577 /* Pending callbacks are not executed if this was an list entry */
578 if (status != SILC_STATUS_OK &&
579 status != SILC_STATUS_LIST_END) {
580 silc_server_command_reply_free(cmd);
585 silc_server_command_process_error(cmd, error);
586 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOWAS);
587 silc_server_command_reply_free(cmd);
591 silc_server_command_process_error(cmd, error);
592 silc_server_command_reply_free(cmd);
595 /* Caches the received IDENTIFY information. */
598 silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
600 SilcServer server = cmd->server;
601 SilcUInt32 len, id_len;
602 unsigned char *id_data;
604 SilcClientID client_id;
605 SilcServerID server_id;
606 SilcChannelID*channel_id;
607 SilcClientEntry client;
608 SilcServerEntry server_entry;
609 SilcChannelEntry channel;
612 SilcIDPayload idp = NULL;
615 id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
618 idp = silc_id_payload_parse(id_data, id_len);
622 name = silc_argument_get_arg_type(cmd->args, 3, &len);
623 info = silc_argument_get_arg_type(cmd->args, 4, &len);
625 id_type = silc_id_payload_get_type(idp);
629 if (!silc_id_payload_get_id(idp, &client_id, sizeof(client_id)))
632 SILC_LOG_DEBUG(("Received client information"));
634 client = silc_idlist_find_client_by_id(server->local_list,
635 &client_id, FALSE, NULL);
637 client = silc_idlist_find_client_by_id(server->global_list, &client_id,
642 /* If router did not find such Client ID in its lists then this must
643 be bogus client or some router in the net is buggy. */
644 if (server->server_type != SILC_SERVER)
649 silc_parse_userfqdn(name, nick, sizeof(nick), NULL, 0);
651 /* We don't have that client anywhere, add it. The client is added
652 to global list since server didn't have it in the lists so it must be
654 client = silc_idlist_add_client(server->global_list,
655 nick[0] ? nick : NULL, info, NULL,
656 silc_id_dup(&client_id, SILC_ID_CLIENT),
657 silc_packet_get_context(cmd->sock),
660 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
664 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
665 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
666 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
668 SILC_LOG_DEBUG(("stat.clients %d->%d", server->stat.clients,
669 server->stat.clients + 1));
670 server->stat.clients++;
672 /* We have the client already, update the data */
674 SILC_LOG_DEBUG(("Updating client data"));
678 silc_parse_userfqdn(name, nick, sizeof(nick), NULL, 0);
681 name = silc_identifier_check(nick, strlen(nick), SILC_STRING_UTF8,
684 SILC_LOG_ERROR(("Malformed nickname '%s' received in IDENTIFY "
689 silc_free(client->nickname);
690 client->nickname = strdup(nick);
692 /* Update the context */
693 silc_idcache_update_by_context(global ? server->global_list->clients :
694 server->local_list->clients, client,
699 silc_free(client->username);
700 client->username = strdup(info);
703 client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
704 client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
713 if (!silc_id_payload_get_id(idp, &server_id, sizeof(server_id)))
716 SILC_LOG_DEBUG(("Received server information"));
718 server_entry = silc_idlist_find_server_by_id(server->local_list,
719 &server_id, FALSE, NULL);
721 server_entry = silc_idlist_find_server_by_id(server->global_list,
722 &server_id, FALSE, NULL);
724 /* If router did not find such Server ID in its lists then this must
725 be bogus server or some router in the net is buggy. */
726 if (server->server_type != SILC_SERVER)
729 /* We don't have that server anywhere, add it. */
730 server_entry = silc_idlist_add_server(server->global_list,
732 silc_id_dup(&server_id,
735 SILC_PRIMARY_ROUTE(server));
739 server_entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
740 server_entry->data.status |= SILC_IDLIST_STATUS_RESOLVED;
741 server_entry->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
746 case SILC_ID_CHANNEL:
750 if (!silc_id_payload_get_id(idp, &channel_id, sizeof(channel_id)))
753 SILC_LOG_DEBUG(("Received channel information"));
755 /* Check channel name */
756 info = silc_channel_name_check(name, strlen(name), SILC_STRING_UTF8,
761 channel = silc_idlist_find_channel_by_name(server->local_list,
764 channel = silc_idlist_find_channel_by_name(server->global_list,
767 /* If router did not find such Channel ID in its lists then this must
768 be bogus channel or some router in the net is buggy. */
769 if (server->server_type != SILC_SERVER) {
774 /* We don't have that channel anywhere, add it. */
775 channel = silc_idlist_add_channel(server->global_list, strdup(name),
776 SILC_CHANNEL_MODE_NONE,
777 silc_id_dup(&channel_id,
779 server->router, NULL, NULL, 0);
790 silc_id_payload_free(idp);
794 silc_id_payload_free(idp);
798 /* Received reply for forwarded IDENTIFY command. We have received the
799 requested identify information now and we will cache it. After this we
800 will call the pending command so that the requestee gets the information
803 SILC_SERVER_CMD_REPLY_FUNC(identify)
805 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
806 SilcStatus status, error;
808 COMMAND_CHECK_STATUS;
810 if (!silc_server_command_reply_identify_save(cmd))
813 /* Pending callbacks are not executed if this was an list entry */
814 if (status != SILC_STATUS_OK &&
815 status != SILC_STATUS_LIST_END) {
816 silc_server_command_reply_free(cmd);
821 silc_server_command_process_error(cmd, error);
822 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_IDENTIFY);
823 silc_server_command_reply_free(cmd);
827 silc_server_command_process_error(cmd, error);
828 silc_server_command_reply_free(cmd);
831 /* Received reply fro INFO command. Cache the server and its information */
833 SILC_SERVER_CMD_REPLY_FUNC(info)
835 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
836 SilcServer server = cmd->server;
837 SilcStatus status, error;
838 SilcServerEntry entry;
841 unsigned char *tmp, *name;
843 COMMAND_CHECK_STATUS;
846 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL))
850 name = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
854 entry = silc_idlist_find_server_by_id(server->local_list,
858 entry = silc_idlist_find_server_by_id(server->global_list,
862 /* Add the server to global list */
863 entry = silc_idlist_add_server(server->global_list, strdup(name), 0,
864 silc_id_dup(SILC_ID_GET_ID(id),
866 silc_packet_get_context(cmd->sock),
871 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
875 /* Get the info string */
876 tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
880 entry->server_info = tmp ? strdup(tmp) : NULL;
883 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_INFO);
885 silc_server_command_reply_free(cmd);
888 /* Received reply fro MOTD command. */
890 SILC_SERVER_CMD_REPLY_FUNC(motd)
892 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
893 SilcServer server = cmd->server;
894 SilcStatus status, error;
895 SilcServerEntry entry = NULL;
900 COMMAND_CHECK_STATUS;
903 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL))
906 entry = silc_idlist_find_server_by_id(server->local_list,
910 entry = silc_idlist_find_server_by_id(server->global_list,
916 /* If router did not find such Server ID in its lists then this must
917 be bogus client or some router in the net is buggy. */
918 if (server->server_type != SILC_SERVER)
922 cmd->server->stat.commands_sent++;
924 /* entry isn't known so we IDENTIFY it. otherwise the
925 silc_server_command_motd won't know about it and tell
926 the client that there is no such server */
927 tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
928 buffer = silc_command_payload_encode_va(SILC_COMMAND_IDENTIFY,
929 ++server->cmd_ident, 5,
930 1, NULL, 0, 2, NULL, 0,
931 3, NULL, 0, 4, NULL, 0,
933 silc_server_packet_send(server, SILC_PRIMARY_ROUTE(server),
934 SILC_PACKET_COMMAND, 0, buffer->data,
935 silc_buffer_len(buffer));
936 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
938 silc_server_command_reply_motd,
940 silc_buffer_free(buffer);
946 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
953 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_MOTD);
955 silc_server_command_reply_free(cmd);
961 /* Received reply for forwarded JOIN command. Router has created or joined
962 the client to the channel. We save some channel information locally
965 SILC_SERVER_CMD_REPLY_FUNC(join)
967 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
968 SilcServer server = cmd->server;
969 SilcIDCacheEntry cache = NULL;
970 SilcStatus status, error;
972 SilcChannelEntry entry;
973 SilcHmac hmac = NULL;
974 SilcUInt32 len, list_count;
975 char *channel_name, *channel_namec = NULL, *tmp;
976 SilcUInt32 mode, created;
977 SilcBuffer keyp = NULL, client_id_list = NULL, client_mode_list = NULL;
978 SilcPublicKey founder_key = NULL;
980 COMMAND_CHECK_STATUS;
982 /* Get channel name */
983 channel_name = silc_argument_get_arg_type(cmd->args, 2, NULL);
988 if (!silc_argument_get_decoded(cmd->args, 3, SILC_ARGUMENT_ID, &id, NULL))
992 if (!silc_argument_get_decoded(cmd->args, 4, SILC_ARGUMENT_ID, &id2, NULL))
996 tmp = silc_argument_get_arg_type(cmd->args, 5, NULL);
999 SILC_GET32_MSB(mode, tmp);
1001 /* Get created boolean value */
1002 tmp = silc_argument_get_arg_type(cmd->args, 6, NULL);
1005 SILC_GET32_MSB(created, tmp);
1006 if (created != 0 && created != 1)
1009 /* Get channel key */
1010 tmp = silc_argument_get_arg_type(cmd->args, 7, &len);
1012 keyp = silc_buffer_alloc(len);
1013 silc_buffer_pull_tail(keyp, silc_buffer_truelen(keyp));
1014 silc_buffer_put(keyp, tmp, len);
1018 tmp = silc_argument_get_arg_type(cmd->args, 11, NULL);
1020 if (!silc_hmac_alloc(tmp, NULL, &hmac))
1024 /* Get the list count */
1025 tmp = silc_argument_get_arg_type(cmd->args, 12, &len);
1028 SILC_GET32_MSB(list_count, tmp);
1030 /* Get Client ID list */
1031 tmp = silc_argument_get_arg_type(cmd->args, 13, &len);
1035 client_id_list = silc_buffer_alloc(len);
1036 silc_buffer_pull_tail(client_id_list, len);
1037 silc_buffer_put(client_id_list, tmp, len);
1039 /* Get client mode list */
1040 tmp = silc_argument_get_arg_type(cmd->args, 14, &len);
1044 client_mode_list = silc_buffer_alloc(len);
1045 silc_buffer_pull_tail(client_mode_list, len);
1046 silc_buffer_put(client_mode_list, tmp, len);
1048 /* Get founder key */
1049 tmp = silc_argument_get_arg_type(cmd->args, 15, &len);
1051 silc_public_key_payload_decode(tmp, len, &founder_key);
1053 /* See whether we already have the channel. */
1054 channel_namec = silc_channel_name_check(channel_name, strlen(channel_name),
1055 SILC_STRING_UTF8, 256, NULL);
1058 entry = silc_idlist_find_channel_by_name(server->local_list,
1059 channel_namec, &cache);
1061 /* Add new channel */
1063 SILC_LOG_DEBUG(("Adding new [%s] channel %s id(%s)",
1064 (created == 0 ? "existing" : "created"), channel_name,
1065 silc_id_render(SILC_ID_GET_ID(id), SILC_ID_CHANNEL)));
1067 /* If the channel is found from global list we must move it to the
1069 entry = silc_idlist_find_channel_by_name(server->global_list,
1070 channel_namec, &cache);
1072 silc_idlist_del_channel(server->global_list, entry);
1074 /* Add the channel to our local list. */
1075 entry = silc_idlist_add_channel(server->local_list, strdup(channel_name),
1076 SILC_CHANNEL_MODE_NONE,
1077 silc_id_dup(SILC_ID_GET_ID(id),
1079 server->router, NULL, NULL, hmac);
1084 server->stat.my_channels++;
1085 server->stat.channels++;
1087 /* The entry exists. */
1089 /* If ID has changed, then update it to the cache too. */
1090 if (!SILC_ID_CHANNEL_COMPARE(entry->id, SILC_ID_GET_ID(id)))
1091 silc_idlist_replace_channel_id(server->local_list, entry->id,
1092 SILC_ID_GET_ID(id));
1094 entry->disabled = FALSE;
1096 /* Remove the founder auth data if the mode is not set but we have
1097 them in the entry */
1098 if (!(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) && entry->founder_key) {
1099 silc_pkcs_public_key_free(entry->founder_key);
1100 entry->founder_key = NULL;
1105 if (entry->founder_key)
1106 silc_pkcs_public_key_free(entry->founder_key);
1107 entry->founder_key = founder_key;
1111 if (entry->hmac_name && (hmac || (!hmac && entry->hmac))) {
1112 silc_free(entry->hmac_name);
1113 entry->hmac_name = strdup(silc_hmac_get_name(hmac ? hmac : entry->hmac));
1116 /* Get the ban list */
1117 tmp = silc_argument_get_arg_type(cmd->args, 8, &len);
1118 if (tmp && len > 2) {
1119 SilcArgumentPayload iargs;
1121 SILC_GET16_MSB(iargc, tmp);
1122 iargs = silc_argument_payload_parse(tmp + 2, len - 2, iargc);
1124 /* Delete old ban list */
1125 if (entry->ban_list)
1126 silc_hash_table_free(entry->ban_list);
1128 silc_hash_table_alloc(0, silc_hash_ptr,
1130 silc_server_inviteban_destruct, entry, TRUE);
1132 /* Add new ban list */
1133 silc_server_inviteban_process(server, entry->ban_list, 0, iargs);
1134 silc_argument_payload_free(iargs);
1138 /* Get the invite list */
1139 tmp = silc_argument_get_arg_type(cmd->args, 9, &len);
1140 if (tmp && len > 2) {
1141 SilcArgumentPayload iargs;
1143 SILC_GET16_MSB(iargc, tmp);
1144 iargs = silc_argument_payload_parse(tmp + 2, len - 2, iargc);
1146 /* Delete old invite list */
1147 if (entry->invite_list)
1148 silc_hash_table_free(entry->invite_list);
1149 entry->invite_list =
1150 silc_hash_table_alloc(0, silc_hash_ptr,
1152 silc_server_inviteban_destruct, entry, TRUE);
1154 /* Add new invite list */
1155 silc_server_inviteban_process(server, entry->invite_list, 0, iargs);
1156 silc_argument_payload_free(iargs);
1161 tmp = silc_argument_get_arg_type(cmd->args, 10, &len);
1163 silc_free(entry->topic);
1164 entry->topic = strdup(tmp);
1167 /* Get channel public key list */
1168 tmp = silc_argument_get_arg_type(cmd->args, 16, &len);
1169 if (tmp && server->server_type != SILC_ROUTER)
1170 silc_server_set_channel_pk_list(server, NULL, entry, tmp, len);
1172 /* The the user limit */
1173 tmp = silc_argument_get_arg_type(cmd->args, 17, &len);
1174 if (tmp && len == 4)
1175 SILC_GET32_MSB(entry->user_limit, tmp);
1177 /* If channel was not created we know there is global users on the
1179 entry->global_users = (created == 0 ? TRUE : FALSE);
1181 /* If channel was just created the mask must be zero */
1182 if (!entry->global_users && mode)
1185 /* Save channel mode */
1188 /* Save channel key */
1190 if (!(entry->mode & SILC_CHANNEL_MODE_PRIVKEY))
1191 silc_server_save_channel_key(server, keyp, entry);
1192 silc_buffer_free(keyp);
1195 /* Save the users to the channel */
1196 silc_server_save_users_on_channel(server, cmd->sock, entry,
1197 SILC_ID_GET_ID(id2), client_id_list,
1198 client_mode_list, list_count);
1199 entry->users_resolved = TRUE;
1202 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_JOIN);
1204 silc_free(channel_namec);
1206 silc_hmac_free(hmac);
1207 silc_server_command_reply_free(cmd);
1210 silc_pkcs_public_key_free(founder_key);
1212 silc_buffer_free(client_id_list);
1213 if (client_mode_list)
1214 silc_buffer_free(client_mode_list);
1217 /* Received reply to STATS command. */
1219 SILC_SERVER_CMD_REPLY_FUNC(stats)
1221 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1222 SilcServer server = cmd->server;
1223 SilcStatus status, error;
1226 SilcBufferStruct buf;
1228 COMMAND_CHECK_STATUS;
1230 /* Get statistics structure */
1231 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
1232 if (server->server_type != SILC_ROUTER && tmp) {
1233 silc_buffer_set(&buf, tmp, tmp_len);
1234 silc_buffer_unformat(&buf,
1235 SILC_STR_UI_INT(NULL),
1236 SILC_STR_UI_INT(NULL),
1237 SILC_STR_UI_INT(NULL),
1238 SILC_STR_UI_INT(NULL),
1239 SILC_STR_UI_INT(NULL),
1240 SILC_STR_UI_INT(NULL),
1241 SILC_STR_UI_INT(&server->stat.cell_clients),
1242 SILC_STR_UI_INT(&server->stat.cell_channels),
1243 SILC_STR_UI_INT(&server->stat.cell_servers),
1244 SILC_STR_UI_INT(&server->stat.clients),
1245 SILC_STR_UI_INT(&server->stat.channels),
1246 SILC_STR_UI_INT(&server->stat.servers),
1247 SILC_STR_UI_INT(&server->stat.routers),
1248 SILC_STR_UI_INT(&server->stat.server_ops),
1249 SILC_STR_UI_INT(&server->stat.router_ops),
1253 SILC_LOG_DEBUG(("stat.clients = %d", server->stat.clients));
1256 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_STATS);
1258 silc_server_command_reply_free(cmd);
1261 SILC_SERVER_CMD_REPLY_FUNC(users)
1263 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1264 SilcServer server = cmd->server;
1265 SilcStatus status, error;
1266 SilcChannelEntry channel;
1268 SilcBuffer client_id_list;
1269 SilcBuffer client_mode_list;
1272 SilcUInt32 list_count;
1274 COMMAND_CHECK_STATUS;
1276 /* Get channel ID */
1277 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL))
1280 /* Get channel entry */
1281 channel = silc_idlist_find_channel_by_id(server->local_list,
1282 SILC_ID_GET_ID(id), NULL);
1284 channel = silc_idlist_find_channel_by_id(server->global_list,
1285 SILC_ID_GET_ID(id), NULL);
1289 if (server->server_type != SILC_SERVER)
1292 idp = silc_id_payload_encode(SILC_ID_GET_ID(id), SILC_ID_CHANNEL);
1293 silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
1294 SILC_COMMAND_IDENTIFY, ++server->cmd_ident,
1295 1, 5, idp->data, silc_buffer_len(idp));
1296 silc_buffer_free(idp);
1298 /* Register pending command callback. After we've received the channel
1299 information we will reprocess this command reply by re-calling this
1300 USERS command reply callback. */
1301 silc_server_command_pending(server, SILC_COMMAND_IDENTIFY,
1303 silc_server_command_reply_users, cmd);
1308 /* Get the list count */
1309 tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
1312 SILC_GET32_MSB(list_count, tmp);
1314 /* Get Client ID list */
1315 tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
1319 client_id_list = silc_buffer_alloc(tmp_len);
1320 silc_buffer_pull_tail(client_id_list, tmp_len);
1321 silc_buffer_put(client_id_list, tmp, tmp_len);
1323 /* Get client mode list */
1324 tmp = silc_argument_get_arg_type(cmd->args, 5, &tmp_len);
1328 client_mode_list = silc_buffer_alloc(tmp_len);
1329 silc_buffer_pull_tail(client_mode_list, tmp_len);
1330 silc_buffer_put(client_mode_list, tmp, tmp_len);
1332 /* Save the users to the channel */
1333 silc_server_save_users_on_channel(server, cmd->sock, channel, NULL,
1334 client_id_list, client_mode_list,
1337 channel->global_users = silc_server_channel_has_global(channel);
1338 channel->users_resolved = TRUE;
1340 silc_buffer_free(client_id_list);
1341 silc_buffer_free(client_mode_list);
1344 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
1346 silc_server_command_reply_free(cmd);
1349 SILC_SERVER_CMD_REPLY_FUNC(getkey)
1351 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1352 SilcServer server = cmd->server;
1353 SilcStatus status, error;
1354 SilcClientEntry client = NULL;
1355 SilcServerEntry server_entry = NULL;
1356 SilcClientID client_id;
1357 SilcServerID server_id;
1360 SilcIDPayload idp = NULL;
1362 SilcPublicKey public_key = NULL;
1364 COMMAND_CHECK_STATUS;
1366 tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
1369 idp = silc_id_payload_parse(tmp, len);
1373 /* Get the public key payload */
1374 tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
1378 /* Decode the public key payload */
1379 if (!silc_public_key_payload_decode(tmp, len, &public_key))
1382 id_type = silc_id_payload_get_type(idp);
1383 if (id_type == SILC_ID_CLIENT) {
1384 silc_id_payload_get_id(idp, &client_id, sizeof(client_id));
1386 client = silc_idlist_find_client_by_id(server->local_list, &client_id,
1389 client = silc_idlist_find_client_by_id(server->global_list,
1390 &client_id, TRUE, NULL);
1395 if (!client->data.public_key) {
1396 /* Add client's public key to repository */
1397 if (!silc_server_get_public_key_by_client(server, client, NULL))
1398 silc_skr_add_public_key_simple(server->repository,
1400 SILC_SKR_USAGE_IDENTIFICATION,
1402 client->data.public_key = public_key;
1405 } else if (id_type == SILC_ID_SERVER) {
1406 silc_id_payload_get_id(idp, &server_id, sizeof(server_id));
1408 server_entry = silc_idlist_find_server_by_id(server->local_list,
1409 &server_id, TRUE, NULL);
1410 if (!server_entry) {
1411 server_entry = silc_idlist_find_server_by_id(server->global_list,
1412 &server_id, TRUE, NULL);
1417 server_entry->data.public_key = public_key;
1424 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_GETKEY);
1426 silc_id_payload_free(idp);
1428 silc_pkcs_public_key_free(public_key);
1430 silc_server_command_reply_free(cmd);
1433 SILC_SERVER_CMD_REPLY_FUNC(list)
1435 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1436 SilcServer server = cmd->server;
1437 SilcStatus status, error;
1439 SilcChannelEntry channel;
1440 SilcIDCacheEntry cache;
1441 unsigned char *tmp, *name, *namec = NULL, *topic;
1442 SilcUInt32 usercount = 0;
1443 SilcBool global_list = FALSE;
1445 COMMAND_CHECK_STATUS;
1447 if (!silc_argument_get_decoded(cmd->args, 2, SILC_ARGUMENT_ID, &id, NULL))
1450 name = silc_argument_get_arg_type(cmd->args, 3, NULL);
1451 topic = silc_argument_get_arg_type(cmd->args, 4, NULL);
1452 tmp = silc_argument_get_arg_type(cmd->args, 5, NULL);
1454 SILC_GET32_MSB(usercount, tmp);
1456 namec = silc_channel_name_check(name, strlen(name), SILC_STRING_UTF8,
1461 /* Add the channel entry if we do not have it already */
1462 channel = silc_idlist_find_channel_by_name(server->local_list,
1465 channel = silc_idlist_find_channel_by_name(server->global_list,
1470 /* If router did not find such channel in its lists then this must
1471 be bogus channel or some router in the net is buggy. */
1472 if (server->server_type != SILC_SERVER)
1475 channel = silc_idlist_add_channel(server->global_list, strdup(name),
1476 SILC_CHANNEL_MODE_NONE,
1477 silc_id_dup(SILC_ID_GET_ID(id),
1479 server->router, NULL, NULL, NULL);
1484 channel->user_count = usercount;
1487 silc_free(channel->topic);
1488 channel->topic = strdup(topic);
1491 /* Pending callbacks are not executed if this was an list entry */
1492 if (status != SILC_STATUS_OK &&
1493 status != SILC_STATUS_LIST_END) {
1494 silc_server_command_reply_free(cmd);
1499 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_LIST);
1502 silc_server_command_reply_free(cmd);
1505 SILC_SERVER_CMD_REPLY_FUNC(watch)
1507 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1508 SilcStatus status, error;
1510 COMMAND_CHECK_STATUS;
1513 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WATCH);
1515 silc_server_command_reply_free(cmd);
1518 SILC_SERVER_CMD_REPLY_FUNC(ping)
1520 SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
1521 SilcStatus status, error;
1523 COMMAND_CHECK_STATUS;
1526 SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_PING);
1528 silc_server_command_reply_free(cmd);