+ /* Get type */
+ if (!strcasecmp(argv[1], "msg"))
+ type = 1;
+ if (!strcasecmp(argv[1], "channel"))
+ type = 2;
+
+ if (type == 0)
+ cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
+
+ if (type == 1) {
+ if (argv[2][0] == '*') {
+ nickname = strdup("*");
+ } else {
+ /* Parse the typed nickname. */
+ silc_client_nickname_parse(silc_client, conn, argv[2], &nickname);
+ if (!nickname)
+ nickname = strdup(argv[2]);
+
+ /* Find client entry */
+ clients = silc_client_get_clients_local(silc_client, conn, argv[2],
+ FALSE);
+ if (!clients) {
+ KeyGetClients inter = silc_calloc(1, sizeof(*inter));
+ inter->server = server;
+ inter->data = strdup(data);
+ inter->nick = strdup(nickname);
+ inter->item = item;
+ silc_client_get_clients(silc_client, conn, nickname, NULL,
+ silc_client_command_key_get_clients, inter);
+ goto out;
+ }
+
+ client_entry = silc_dlist_get(clients);
+ silc_client_list_free(silc_client, conn, clients);
+ }
+ }
+
+ if (type == 2) {
+ /* Get channel entry */
+ char *name;
+
+ if (argv[2][0] == '*') {
+ if (!conn->current_channel)
+ cmd_return_error(CMDERR_NOT_JOINED);
+ name = conn->current_channel->channel_name;
+ } else {
+ name = argv[2];
+ }
+
+ chanrec = silc_channel_find(server, name);
+ if (chanrec == NULL)
+ cmd_return_error(CMDERR_CHAN_NOT_FOUND);
+ channel_entry = chanrec->entry;
+ }
+
+ /* Set command */
+ if (!strcasecmp(argv[3], "set")) {
+ command = 1;
+
+ if (argc >= 5) {
+ char *cipher = NULL, *hmac = NULL;
+
+ if (argc >= 6)
+ cipher = argv[5];
+ if (argc >= 7)
+ hmac = argv[6];
+
+ if (type == 1 && client_entry) {
+ /* Set private message key */
+ silc_client_del_private_message_key(silc_client, conn, client_entry);
+ silc_client_add_private_message_key(silc_client, conn, client_entry,
+ cipher, hmac,
+ argv[4], argv_lens[4]);
+ } else if (type == 2) {
+ /* Set private channel key */
+ if (!(channel_entry->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_CH_PRIVATE_KEY_NOMODE,
+ channel_entry->channel_name);
+ goto out;
+ }
+
+ if (!silc_client_add_channel_private_key(silc_client, conn,
+ channel_entry, NULL,
+ cipher, hmac,
+ argv[4],
+ argv_lens[4], NULL)) {
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_CH_PRIVATE_KEY_ERROR,
+ channel_entry->channel_name);
+ goto out;
+ }
+
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_CH_PRIVATE_KEY_ADD,
+ channel_entry->channel_name);
+ }
+ }
+
+ goto out;
+ }
+
+ /* 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) */
+ int number;
+
+ if (argc == 4)
+ silc_client_del_channel_private_keys(silc_client, conn,
+ channel_entry);
+
+ if (argc > 4) {
+ number = atoi(argv[4]);
+ ckeys = silc_client_list_channel_private_keys(silc_client, conn,
+ channel_entry);
+ if (!ckeys)
+ goto out;
+
+ silc_dlist_start(ckeys);
+ if (!number || number > silc_dlist_count(ckeys)) {
+ silc_dlist_uninit(ckeys);
+ goto out;
+ }
+
+ for (i = 0; i < number; i++)
+ ch = silc_dlist_get(ckeys);
+ if (!ch)
+ goto out;
+
+ silc_client_del_channel_private_key(silc_client, conn, channel_entry,
+ ch);
+ silc_dlist_uninit(ckeys);
+ }
+
+ 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) {
+ int len;
+ char buf[1024];
+
+ ckeys = silc_client_list_channel_private_keys(silc_client, conn,
+ channel_entry);
+
+ printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+ SILCTXT_CH_PRIVATE_KEY_LIST,
+ channel_entry->channel_name);
+
+ if (!ckeys)
+ goto out;
+
+ silc_dlist_start(ckeys);
+ while ((ch = silc_dlist_get(ckeys))) {
+ memset(buf, 0, sizeof(buf));
+ strncat(buf, " ", 2);
+
+ len = strlen(silc_cipher_get_name(ch->send_key));
+ strncat(buf, silc_cipher_get_name(ch->send_key),
+ len > 16 ? 16 : len);
+ if (len < 16)
+ for (i = 0; i < 16 - len; i++)
+ strcat(buf, " ");
+ strcat(buf, " ");
+
+ len = strlen(silc_hmac_get_name(ch->hmac));
+ strncat(buf, silc_hmac_get_name(ch->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_dlist_uninit(ckeys);
+ }
+
+ 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) {
+ if (!strcasecmp(argv[5], "UDP"))
+ udp = TRUE;
+ else
+ port = atoi(argv[5]);
+ }
+ if (argc >= 7)
+ udp = TRUE;
+
+ 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) {
+ if (!strcasecmp(argv[5], "UDP"))
+ udp = TRUE;
+ else
+ port = atoi(argv[5]);
+ }
+ if (argc >= 7)
+ udp = TRUE;
+
+ internal = silc_calloc(1, sizeof(*internal));
+ internal->type = type;
+ internal->server = server;
+ }