Fix crash on expired keyboard prompts
[silc.git] / apps / irssi / src / silc / core / silc-queries.c
index 4a0516f74609b4b034774d21ce1004753f605762..0da01d6b6918a9c6eec30a4a08c5ab4d395dbc82 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2002 - 2006 Pekka Riikonen
+  Copyright (C) 2002 - 2007 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
@@ -26,6 +26,7 @@
 #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"
@@ -33,7 +34,8 @@
 #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_accept(const char *line, void *context,
+               SilcKeyboardPromptStatus reason);
 
 QUERY_REC *silc_query_create(const char *server_tag,
                             const char *nick, int automatic)
@@ -558,6 +560,7 @@ typedef struct {
   SilcMime message;
   SilcMime extension;
   bool nopk;
+  bool autoaccept;
 } *AttrVerify;
 
 void silc_query_attributes_print(SILC_SERVER_REC *server,
@@ -821,12 +824,8 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
 
   /* Handle the signature verifications and public key verifying here */
 
-  if (usersign.data) {
-    /* Verify the signature now */
-    SilcPublicKey public_key;
+  if (verify->userpk.data) {
     SilcPKCSType type = 0;
-    unsigned char *verifyd;
-    SilcUInt32 verify_len;
 
     if (!strcmp(verify->userpk.type, "silc-rsa"))
       type = SILC_PKCS_SILC;
@@ -837,9 +836,17 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
     else if (!strcmp(verify->userpk.type, "pgp-sign-rsa"))
       type = SILC_PKCS_OPENPGP;
 
-    if (silc_pkcs_public_key_alloc(type, verify->userpk.data,
-                                  verify->userpk.data_len,
-                                  &verify->public_key)) {
+    silc_pkcs_public_key_alloc(type, verify->userpk.data,
+                              verify->userpk.data_len,
+                              &verify->public_key);
+  }
+
+  if (usersign.data) {
+    /* Verify the signature now */
+    unsigned char *verifyd;
+    SilcUInt32 verify_len;
+
+    if (verify->public_key) {
       verifyd = silc_attribute_get_verify_data(attrs, FALSE, &verify_len);
       if (verifyd && silc_pkcs_verify(verify->public_key,
                                      usersign.data,
@@ -870,13 +877,13 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
     unsigned char *verifyd;
     SilcUInt32 verify_len;
 
-    if (!strcmp(verify->userpk.type, "silc-rsa"))
+    if (!strcmp(serverpk.type, "silc-rsa"))
       type = SILC_PKCS_SILC;
-    else if (!strcmp(verify->userpk.type, "ssh-rsa"))
+    else if (!strcmp(serverpk.type, "ssh-rsa"))
       type = SILC_PKCS_SSH2;
-    else if (!strcmp(verify->userpk.type, "x509v3-sign-rsa"))
+    else if (!strcmp(serverpk.type, "x509v3-sign-rsa"))
       type = SILC_PKCS_X509V3;
-    else if (!strcmp(verify->userpk.type, "pgp-sign-rsa"))
+    else if (!strcmp(serverpk.type, "pgp-sign-rsa"))
       type = SILC_PKCS_OPENPGP;
 
     if (silc_pkcs_public_key_alloc(type, serverpk.data,
@@ -903,7 +910,7 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
     }
   }
 
-  if (verify->userpk.data) {
+  if (verify->public_key) {
     silc_verify_public_key(client, conn, SILC_CONN_CLIENT,
                           verify->public_key,
                           silc_query_attributes_print_final, verify);
@@ -916,12 +923,12 @@ void silc_query_attributes_print(SILC_SERVER_REC *server,
 static void silc_query_attributes_print_final(bool 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) {
@@ -942,7 +949,10 @@ static void silc_query_attributes_print_final(bool success, void *context)
   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] = '_';
 
@@ -956,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;
+  bool 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));
@@ -1031,11 +1043,13 @@ 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(filename2, verify->public_key,
-                             SILC_PKCS_FILE_BASE64);
+    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) {
@@ -1062,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);