Merge branch 'master' of git://valera-ext.nynaeve.net/silc into silc.1.1.branch
[silc.git] / apps / irssi / src / silc / core / silc-queries.c
index 9daff3b01c4bbd21e41dc62acd101e1ba6ebac91..ec29a2f32df8ffb85509834b69c010520d23139e 100644 (file)
@@ -1,10 +1,10 @@
 /*
 
-  silc-queries.c 
+  silc-queries.c
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2002 Pekka Riikonen
+  Copyright (C) 2002 - 2008 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
 #include "modules.h"
 #include "commands.h"
 #include "misc.h"
+#include "clientutil.h"
 
 #include "fe-common/core/printtext.h"
 #include "fe-common/core/fe-channels.h"
 #include "fe-common/core/keyboard.h"
 #include "fe-common/silc/module-formats.h"
 
-static void silc_query_attributes_print_final(bool success, void *context);
-static void silc_query_attributes_accept(const char *line, void *context);
+static void silc_query_attributes_print_final(SilcBool success, void *context);
+static void silc_query_attributes_accept(const char *line, void *context,
+               SilcKeyboardPromptStatus reason);
 
 QUERY_REC *silc_query_create(const char *server_tag,
                             const char *nick, int automatic)
@@ -68,7 +70,7 @@ void command_attr(const char *data, SILC_SERVER_REC *server,
   SilcUInt32 argc;
   SilcUInt32 *argv_lens, *argv_types;
   const char *sv;
-  bool allowed;
+  SilcBool allowed;
 
   /* Now parse all arguments */
   tmp = g_strconcat("ATTR", " ", data, NULL);
@@ -247,15 +249,14 @@ void silc_query_attributes_default(SilcClient client,
   const char *sv;
   SilcUInt32 tmp_len, mask;
   SilcAttributeObjService service;
-  SilcAttributeObjMime mime;
+  SilcMime mime;
   SilcAttributeObjGeo geo;
   SilcAttributeObjDevice dev;
   SilcAttributeObjPk pk;
   SilcVCardStruct vcard;
-  bool allowed;
+  SilcBool allowed;
 
   memset(&service, 0, sizeof(service));
-  memset(&mime, 0, sizeof(mime));
   memset(&geo, 0, sizeof(geo));
   memset(&dev, 0, sizeof(dev));
   memset(&pk, 0, sizeof(pk));
@@ -387,12 +388,11 @@ void silc_query_attributes_default(SilcClient client,
                              SILC_ATTRIBUTE_STATUS_MESSAGE, NULL);
     tmp = silc_file_readfile(sv, &tmp_len);
     if (tmp) {
-      tmp[tmp_len] = 0;
-      mime.mime = (const unsigned char *)tmp;
-      mime.mime_len = tmp_len;
-      silc_client_attribute_add(silc_client, conn,
-                               SILC_ATTRIBUTE_STATUS_MESSAGE, &mime,
-                               sizeof(mime));
+      mime = silc_mime_decode(NULL, tmp, tmp_len);
+      if (mime)
+       silc_client_attribute_add(silc_client, conn,
+                                 SILC_ATTRIBUTE_STATUS_MESSAGE, mime,
+                                 sizeof(*mime));
     }
     silc_free(tmp);
   }
@@ -433,6 +433,8 @@ void silc_query_attributes_default(SilcClient client,
        mask |= SILC_ATTRIBUTE_CONTACT_MMS;
       if (!strcasecmp(*entry, "CHAT"))
        mask |= SILC_ATTRIBUTE_CONTACT_CHAT;
+      if (!strcasecmp(*entry, "VIDEO"))
+       mask |= SILC_ATTRIBUTE_CONTACT_VIDEO;
     }
     silc_client_attribute_add(silc_client, conn,
                              SILC_ATTRIBUTE_PREFERRED_CONTACT, (void *)mask,
@@ -553,10 +555,12 @@ typedef struct {
   SILC_SERVER_REC *server;
   char *name;
   SilcAttributeObjPk userpk;
+  SilcPublicKey public_key;
   SilcVCardStruct vcard;
-  SilcAttributeObjMime message;
-  SilcAttributeObjMime extension;
-  bool nopk;
+  SilcMime message;
+  SilcMime extension;
+  SilcBool nopk;
+  SilcBool autoaccept;
 } *AttrVerify;
 
 void silc_query_attributes_print(SILC_SERVER_REC *server,
@@ -664,8 +668,11 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
 
     case SILC_ATTRIBUTE_STATUS_MESSAGE:
       {
-       if (!silc_attribute_get_object(attr, (void *)&verify->message,
-                                      sizeof(verify->message)))
+       verify->message = silc_mime_alloc();
+       if (!verify->message)
+         continue;
+       if (!silc_attribute_get_object(attr, (void *)verify->message,
+                                      sizeof(*verify->message)))
          continue;
        printformat_module("fe-common/silc", server, NULL,
                           MSGLEVEL_CRAP, SILCTXT_ATTR_STATUS_MESSAGE,
@@ -702,6 +709,8 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
          silc_strncat(tmp, sizeof(tmp), "SMS ", strlen(" SMS"));
        if (mask & SILC_ATTRIBUTE_CONTACT_MMS)
          silc_strncat(tmp, sizeof(tmp), "MMS ", strlen(" MMS"));
+       if (mask & SILC_ATTRIBUTE_CONTACT_VIDEO)
+         silc_strncat(tmp, sizeof(tmp), "VIDEO ", strlen(" VIDEO"));
        printformat_module("fe-common/silc", server, NULL,
                           MSGLEVEL_CRAP, SILCTXT_ATTR_PREFERRED_CONTACT, tmp);
       }
@@ -718,8 +727,11 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
 
     case SILC_ATTRIBUTE_EXTENSION:
       {
-       if (!silc_attribute_get_object(attr, (void *)&verify->extension,
-                                      sizeof(verify->extension)))
+       verify->extension = silc_mime_alloc();
+       if (!verify->extension)
+         continue;
+       if (!silc_attribute_get_object(attr, (void *)verify->extension,
+                                      sizeof(*verify->extension)))
          continue;
        printformat_module("fe-common/silc", server, NULL,
                           MSGLEVEL_CRAP, SILCTXT_ATTR_EXTENSION,
@@ -812,72 +824,95 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
 
   /* Handle the signature verifications and public key verifying here */
 
-  if (usersign.data && !strcmp(verify->userpk.type, "silc-rsa")) {
+  if (verify->userpk.data) {
+    SilcPKCSType type = 0;
+
+    if (!strcmp(verify->userpk.type, "silc-rsa"))
+      type = SILC_PKCS_SILC;
+    else if (!strcmp(verify->userpk.type, "ssh-rsa"))
+      type = SILC_PKCS_SSH2;
+    else if (!strcmp(verify->userpk.type, "x509v3-sign-rsa"))
+      type = SILC_PKCS_X509V3;
+    else if (!strcmp(verify->userpk.type, "pgp-sign-rsa"))
+      type = SILC_PKCS_OPENPGP;
+
+    silc_pkcs_public_key_alloc(type, verify->userpk.data,
+                              verify->userpk.data_len,
+                              &verify->public_key);
+  }
+
+  if (usersign.data) {
     /* Verify the signature now */
-    SilcPublicKey public_key;
-    SilcPKCS pkcs;
     unsigned char *verifyd;
     SilcUInt32 verify_len;
 
-    if (silc_pkcs_public_key_decode(verify->userpk.data,
-                                   verify->userpk.data_len,
-                                   &public_key)) {
-      silc_pkcs_alloc("rsa", &pkcs);
+    if (verify->public_key) {
       verifyd = silc_attribute_get_verify_data(attrs, FALSE, &verify_len);
-      if (verifyd && silc_pkcs_public_key_set(pkcs, public_key)){
-       if (silc_pkcs_verify_with_hash(pkcs, client->sha1hash,
-                                      usersign.data,
-                                      usersign.data_len,
-                                      verifyd, verify_len)) {
-         printformat_module("fe-common/silc", server, NULL,
-                            MSGLEVEL_CRAP, SILCTXT_ATTR_USER_SIGN_VERIFIED);
-       } else {
-         printformat_module("fe-common/silc", server, NULL,
-                            MSGLEVEL_CRAP, SILCTXT_ATTR_USER_SIGN_FAILED);
-       }
+      if (verifyd && silc_pkcs_verify(verify->public_key,
+                                     usersign.data,
+                                     usersign.data_len,
+                                     verifyd, verify_len,
+                                     sha1hash)) {
+       printformat_module("fe-common/silc", server, NULL,
+                          MSGLEVEL_CRAP, SILCTXT_ATTR_USER_SIGN_VERIFIED);
+      } else {
+       printformat_module("fe-common/silc", server, NULL,
+                          MSGLEVEL_CRAP, SILCTXT_ATTR_USER_SIGN_FAILED);
       }
 
-      silc_pkcs_public_key_free(public_key);
       silc_free(verifyd);
+    } else {
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_ATTR_USER_SIGN_FAILED);
     }
   } else {
     printformat_module("fe-common/silc", server, NULL,
                       MSGLEVEL_CRAP, SILCTXT_ATTR_USER_SIGN_NOT_PRESENT);
   }
 
-  if (serversign.data && !strcmp(serverpk.type, "silc-rsa")) {
+  if (serversign.data) {
     /* Verify the signature now */
     SilcPublicKey public_key;
-    SilcPKCS pkcs;
+    SilcPKCSType type = 0;
     unsigned char *verifyd;
     SilcUInt32 verify_len;
 
-    if (silc_pkcs_public_key_decode(serverpk.data, serverpk.data_len,
-                                   &public_key)) {
-      silc_pkcs_alloc("rsa", &pkcs);
+    if (!strcmp(serverpk.type, "silc-rsa"))
+      type = SILC_PKCS_SILC;
+    else if (!strcmp(serverpk.type, "ssh-rsa"))
+      type = SILC_PKCS_SSH2;
+    else if (!strcmp(serverpk.type, "x509v3-sign-rsa"))
+      type = SILC_PKCS_X509V3;
+    else if (!strcmp(serverpk.type, "pgp-sign-rsa"))
+      type = SILC_PKCS_OPENPGP;
+
+    if (silc_pkcs_public_key_alloc(type, serverpk.data,
+                                  serverpk.data_len,
+                                  &public_key)) {
       verifyd = silc_attribute_get_verify_data(attrs, TRUE, &verify_len);
-      if (verifyd && silc_pkcs_public_key_set(pkcs, public_key)) {
-       if (silc_pkcs_verify_with_hash(pkcs, client->sha1hash,
-                                      serversign.data,
-                                      serversign.data_len,
-                                      verifyd, verify_len)) {
-         printformat_module("fe-common/silc", server, NULL,
-                            MSGLEVEL_CRAP, SILCTXT_ATTR_SERVER_SIGN_VERIFIED);
-       } else {
-         printformat_module("fe-common/silc", server, NULL,
-                            MSGLEVEL_CRAP, SILCTXT_ATTR_SERVER_SIGN_FAILED);
-       }
+      if (verifyd && silc_pkcs_verify(public_key,
+                                     serversign.data,
+                                     serversign.data_len,
+                                     verifyd, verify_len,
+                                     sha1hash)) {
+       printformat_module("fe-common/silc", server, NULL,
+                          MSGLEVEL_CRAP, SILCTXT_ATTR_SERVER_SIGN_VERIFIED);
+      } else {
+       printformat_module("fe-common/silc", server, NULL,
+                          MSGLEVEL_CRAP, SILCTXT_ATTR_SERVER_SIGN_FAILED);
       }
 
       silc_pkcs_public_key_free(public_key);
       silc_free(verifyd);
+    } else {
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_ATTR_SERVER_SIGN_FAILED);
     }
   }
 
-  if (verify->userpk.data) {
-    silc_verify_public_key(client, conn, SILC_SOCKET_TYPE_CLIENT,
-                          verify->userpk.data, verify->userpk.data_len, 
-                          SILC_SKE_PK_TYPE_SILC,
+  if (verify->public_key) {
+    silc_verify_public_key(client, conn, SILC_CONN_CLIENT,
+                          verify->public_key,
                           silc_query_attributes_print_final, verify);
   } else {
     verify->nopk = TRUE;
@@ -885,15 +920,15 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
   }
 }
 
-static void silc_query_attributes_print_final(bool success, void *context)
+static void silc_query_attributes_print_final(SilcBool success, void *context)
 {
   AttrVerify verify = context;
-  SilcClient client = verify->client;
   SILC_SERVER_REC *server = verify->server;
   char *format = NULL;
   unsigned char filename[256], *fingerprint = NULL, *tmp;
   struct stat st;
   int i;
+  size_t len;
 
   if (!verify->nopk) {
     if (success) {
@@ -911,16 +946,19 @@ static void silc_query_attributes_print_final(bool success, void *context)
                     MSGLEVEL_CRAP, SILCTXT_ATTR_FOOTER);
 
   /* Replace all whitespaces with `_'. */
-  fingerprint = silc_hash_fingerprint(client->sha1hash,
+  fingerprint = silc_hash_fingerprint(sha1hash,
                                      verify->userpk.data,
                                      verify->userpk.data_len);
-  for (i = 0; i < strlen(fingerprint); i++)
+
+  len = strlen(fingerprint);
+
+  for (i = 0; i < len; i++)
     if (fingerprint[i] == ' ')
       fingerprint[i] = '_';
-  
+
   /* Filename for dir */
   tmp = fingerprint + strlen(fingerprint) - 9;
-  snprintf(filename, sizeof(filename) - 1, "%s/friends/%s", 
+  snprintf(filename, sizeof(filename) - 1, "%s/friends/%s",
           get_irssi_dir(), tmp);
   silc_free(fingerprint);
 
@@ -928,28 +966,30 @@ static void silc_query_attributes_print_final(bool success, void *context)
     /* Ask to accept save requested attributes */
     format = format_get_text("fe-common/silc", NULL, NULL, NULL,
                             SILCTXT_ATTR_SAVE);
-    keyboard_entry_redirect((SIGNAL_FUNC)silc_query_attributes_accept,
-                           format, 0, verify);
+    silc_keyboard_entry_redirect(silc_query_attributes_accept,
+                           format, 0, verify, &server->prompt_op);
   } else {
     /* Save new data to existing directory */
-    silc_query_attributes_accept("Y", verify);
+    verify->autoaccept = TRUE; /* Ensure we don't twiddle the async context */
+    silc_query_attributes_accept("Y", verify, KeyboardCompletionSuccess);
   }
 
   g_free(format);
 }
 
-static void silc_query_attributes_accept(const char *line, void *context)
+static void silc_query_attributes_accept(const char *line, void *context,
+               SilcKeyboardPromptStatus reason)
 {
   AttrVerify verify = context;
-  SilcClient client = verify->client;
   SILC_SERVER_REC *server = verify->server;
   struct stat st;
   struct passwd *pw;
   unsigned char filename[256], filename2[256], *fingerprint = NULL, *tmp;
   SilcUInt32 len;
   int i;
+  SilcBool success = (reason == KeyboardCompletionSuccess);
 
-  if (line[0] == 'Y' || line[0] == 'y') {
+  if (success && (line[0] == 'Y' || line[0] == 'y')) {
     /* Save the attributes */
     memset(filename, 0, sizeof(filename));
     memset(filename2, 0, sizeof(filename2));
@@ -959,7 +999,7 @@ static void silc_query_attributes_accept(const char *line, void *context)
       goto out;
 
     /* Replace all whitespaces with `_'. */
-    fingerprint = silc_hash_fingerprint(client->sha1hash,
+    fingerprint = silc_hash_fingerprint(sha1hash,
                                        verify->userpk.data,
                                        verify->userpk.data_len);
     for (i = 0; i < strlen(fingerprint); i++)
@@ -968,7 +1008,7 @@ static void silc_query_attributes_accept(const char *line, void *context)
 
     /* Filename for dir */
     tmp = fingerprint + strlen(fingerprint) - 9;
-    snprintf(filename, sizeof(filename) - 1, "%s/friends/%s", 
+    snprintf(filename, sizeof(filename) - 1, "%s/friends/%s",
             get_irssi_dir(), tmp);
 
     /* Create dir if it doesn't exist */
@@ -1003,29 +1043,32 @@ static void silc_query_attributes_accept(const char *line, void *context)
     }
 
     /* Save public key */
-    memset(filename2, 0, sizeof(filename2));
-    snprintf(filename2, sizeof(filename2) - 1, "%s/clientkey_%s.pub",
-            filename, fingerprint);
-    silc_pkcs_save_public_key_data(filename2, verify->userpk.data,
-                                  verify->userpk.data_len,
-                                  SILC_PKCS_FILE_PEM);
+    if (verify->public_key) {
+      memset(filename2, 0, sizeof(filename2));
+      snprintf(filename2, sizeof(filename2) - 1, "%s/clientkey_%s.pub",
+              filename, fingerprint);
+      silc_pkcs_save_public_key(filename2, verify->public_key,
+                               SILC_PKCS_FILE_BASE64);
+    }
 
     /* Save extension data */
-    if (verify->extension.mime) {
+    if (verify->extension) {
       memset(filename2, 0, sizeof(filename2));
       snprintf(filename2, sizeof(filename2) - 1, "%s/extension.mime",
               filename);
-      silc_file_writefile(filename2, verify->extension.mime,
-                         verify->extension.mime_len);
+      tmp = silc_mime_encode(verify->extension, &len);
+      if (tmp)
+       silc_file_writefile(filename2, tmp, len);
     }
 
     /* Save MIME message data */
-    if (verify->extension.mime) {
+    if (verify->message) {
       memset(filename2, 0, sizeof(filename2));
       snprintf(filename2, sizeof(filename2) - 1, "%s/status_message.mime",
               filename);
-      silc_file_writefile(filename2, verify->message.mime,
-                         verify->message.mime_len);
+      tmp = silc_mime_encode(verify->message, &len);
+      if (tmp)
+       silc_file_writefile(filename2, tmp, len);
     }
 
     printformat_module("fe-common/silc", server, NULL,
@@ -1033,6 +1076,8 @@ static void silc_query_attributes_accept(const char *line, void *context)
   }
 
  out:
+  if((!verify->autoaccept) && (reason != KeyboardCompletionFailed))
+    verify->server->prompt_op = NULL;
   silc_free(fingerprint);
   silc_free(verify->name);
   silc_vcard_free(&verify->vcard);