+ /* Unset command */
+ if (!strcasecmp(argv[3], "unset")) {
+ command = 2;
+
+ if (type == 1 && client_entry) {
+ /* Unset private message key */
+ silc_client_del_private_message_key(silc_client, conn, client_entry);
+ } else if (type == 2) {
+ /* Unset channel key(s) */
+ SilcChannelPrivateKey *keys;
+ SilcUInt32 keys_count;
+ int number;
+
+ if (argc == 4)
+ silc_client_del_channel_private_keys(silc_client, conn,
+ channel_entry);
+
+ if (argc > 4) {
+ number = atoi(argv[4]);
+ keys = silc_client_list_channel_private_keys(silc_client, conn,
+ channel_entry,
+ &keys_count);
+ if (!keys)
+ goto out;
+
+ if (!number || number > keys_count) {
+ silc_client_free_channel_private_keys(keys, keys_count);
+ goto out;
+ }
+
+ silc_client_del_channel_private_key(silc_client, conn, channel_entry,
+ keys[number - 1]);
+ silc_client_free_channel_private_keys(keys, keys_count);
+ }
+
+ goto out;
+ }
+ }
+
+ /* List command */
+ if (!strcasecmp(argv[3], "list")) {
+ command = 3;
+
+ if (type == 1) {
+ SilcPrivateMessageKeys keys;
+ SilcUInt32 keys_count;
+ int k, i, len;
+ char buf[1024];
+
+ keys = silc_client_list_private_message_keys(silc_client, conn,
+ &keys_count);
+ if (!keys)
+ goto out;
+
+ /* list the private message key(s) */
+ if (nickname[0] == '*') {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_PRIVATE_KEY_LIST);
+ for (k = 0; k < keys_count; k++) {
+ memset(buf, 0, sizeof(buf));
+ strncat(buf, " ", 2);
+ len = strlen(keys[k].client_entry->nickname);
+ strncat(buf, keys[k].client_entry->nickname, len > 30 ? 30 : len);
+ if (len < 30)
+ for (i = 0; i < 30 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ len = strlen(keys[k].cipher);
+ strncat(buf, keys[k].cipher, len > 14 ? 14 : len);
+ if (len < 14)
+ for (i = 0; i < 14 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ if (keys[k].key)
+ strcat(buf, "<hidden>");
+ else
+ strcat(buf, "*generated*");
+
+ silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", buf);
+ }
+ } else {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_PRIVATE_KEY_LIST_NICK,
+ client_entry->nickname);
+ for (k = 0; k < keys_count; k++) {
+ if (keys[k].client_entry != client_entry)
+ continue;
+
+ memset(buf, 0, sizeof(buf));
+ strncat(buf, " ", 2);
+ len = strlen(keys[k].client_entry->nickname);
+ strncat(buf, keys[k].client_entry->nickname, len > 30 ? 30 : len);
+ if (len < 30)
+ for (i = 0; i < 30 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ len = strlen(keys[k].cipher);
+ strncat(buf, keys[k].cipher, len > 14 ? 14 : len);
+ if (len < 14)
+ for (i = 0; i < 14 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ if (keys[k].key)
+ strcat(buf, "<hidden>");
+ else
+ strcat(buf, "*generated*");
+
+ silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", buf);
+ }
+ }
+
+ silc_client_free_private_message_keys(keys, keys_count);
+
+ } else if (type == 2) {
+ SilcChannelPrivateKey *keys;
+ SilcUInt32 keys_count;
+ int k, i, len;
+ char buf[1024];
+
+ keys = silc_client_list_channel_private_keys(silc_client, conn,
+ channel_entry,
+ &keys_count);
+
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_CH_PRIVATE_KEY_LIST,
+ channel_entry->channel_name);
+
+ if (!keys)
+ goto out;
+
+ for (k = 0; k < keys_count; k++) {
+ memset(buf, 0, sizeof(buf));
+ strncat(buf, " ", 2);
+
+ len = strlen(keys[k]->cipher->cipher->name);
+ strncat(buf, keys[k]->cipher->cipher->name, len > 16 ? 16 : len);
+ if (len < 16)
+ for (i = 0; i < 16 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ len = strlen(silc_hmac_get_name(keys[k]->hmac));
+ strncat(buf, silc_hmac_get_name(keys[k]->hmac), len > 16 ? 16 : len);
+ if (len < 16)
+ for (i = 0; i < 16 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ strcat(buf, "<hidden>");
+
+ silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO, "%s", buf);
+ }
+
+ silc_client_free_channel_private_keys(keys, keys_count);
+ }
+
+ goto out;
+ }
+
+ /* Send command is used to send key agreement */
+ if (!strcasecmp(argv[3], "agreement")) {
+ command = 4;
+
+ if (argc >= 5)
+ hostname = argv[4];
+ if (argc >= 6)
+ port = atoi(argv[5]);
+
+ internal = silc_calloc(1, sizeof(*internal));
+ internal->type = type;
+ internal->server = server;
+
+ if (!hostname) {
+ if (settings_get_bool("use_auto_addr")) {
+
+ hostname = (char *)settings_get_str("auto_public_ip");
+
+/* If the hostname isn't set, treat this case as if auto_public_ip wasn't
+ * set.
+ */
+ if ((hostname) && (*hostname == '\0')) {
+ hostname = NULL;
+ }
+ else {
+ bindhost = (char *)settings_get_str("auto_bind_ip");
+
+/* if the bind_ip isn't set, but the public_ip IS, then assume then
+ * public_ip is the same value as the bind_ip.
+ */
+ if ((bindhost) && (*bindhost == '\0')) {
+ bindhost = hostname;
+ }
+ port = settings_get_int("auto_bind_port");
+ }
+ } /* if use_auto_addr */
+ }
+ }
+
+ /* Start command is used to start key agreement (after receiving the
+ key_agreement client operation). */
+ if (!strcasecmp(argv[3], "negotiate")) {
+ command = 5;
+
+ if (argc >= 5)
+ hostname = argv[4];
+ if (argc >= 6)
+ port = atoi(argv[5]);
+
+ internal = silc_calloc(1, sizeof(*internal));
+ internal->type = type;
+ internal->server = server;
+ }
+
+ if (command == 0) {
+ silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO,
+ "Usage: /KEY msg|channel <nickname|channel> "
+ "set|unset|agreement|negotiate [<arguments>]");
+ goto out;
+ }
+
+ if (command == 4 && client_entry) {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_KEY_AGREEMENT, argv[2]);
+ internal->responder = TRUE;
+ silc_client_send_key_agreement(
+ silc_client, conn, client_entry, hostname,
+ bindhost, port,
+ settings_get_int("key_exchange_timeout_secs"),
+ keyagr_completion, internal);
+ if (!hostname)
+ silc_free(internal);
+ goto out;
+ }
+
+ if (command == 5 && client_entry && hostname) {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_KEY_AGREEMENT_NEGOTIATE, argv[2]);
+ internal->responder = FALSE;
+ silc_client_perform_key_agreement(silc_client, conn, client_entry,
+ hostname, port, keyagr_completion,
+ internal);
+ goto out;
+ }
+
+ out:
+ silc_free(nickname);
+}
+
+/* Lists locally saved client and server public keys. */
+
+static void command_listkeys(const char *data, SILC_SERVER_REC *server,
+ WI_ITEM_REC *item)
+{
+