Use silc_pkcs_get_name. SilcPKCS cannot be accessed directly.
[silc.git] / apps / irssi / src / silc / core / clientutil.c
index 93b575fe9a5fdc14a38da05166f101a169c7ce1d..e09e514bb378f6b4304257d278f9e3fe73cb29a4 100644 (file)
@@ -42,6 +42,8 @@
 #include "fe-common/core/printtext.h"
 #include "fe-common/core/keyboard.h"
 
+#include "core.h"
+
 /* Lists supported ciphers */
 
 void silc_client_list_ciphers()
@@ -78,38 +80,6 @@ void silc_client_list_pkcs()
   silc_free(pkcs);
 }
 
-/* Displays input prompt on command line and takes input data from user */
-
-char *silc_client_get_input(const char *prompt)
-{
-  char input[2048];
-  int fd;
-
-  fd = open("/dev/tty", O_RDONLY);
-  if (fd < 0) {
-    fprintf(stderr, "silc: %s\n", strerror(errno));
-    return NULL;
-  }
-
-  memset(input, 0, sizeof(input));
-
-  printf("%s", prompt);
-  fflush(stdout);
-
-  if ((read(fd, input, sizeof(input))) < 0) {
-    fprintf(stderr, "silc: %s\n", strerror(errno));
-    return NULL;
-  }
-
-  if (strlen(input) <= 1)
-    return NULL;
-
-  if (strchr(input, '\n'))
-    *strchr(input, '\n') = '\0';
-
-  return strdup(input);
-}
-
 /* Returns identifier string for public key generation. */
 
 char *silc_client_create_identifier()
@@ -169,8 +139,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
 
   if (!pkcs_name) {
   again_name:
-    pkcs_name = 
-      silc_client_get_input("PKCS name (l to list names) [rsa]: ");
+    pkcs_name = silc_get_input("PKCS name (l to list names) [rsa]: ", FALSE);
     if (!pkcs_name)
       pkcs_name = strdup("rsa");
 
@@ -188,8 +157,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
 
   if (!bits) {
     char *length = NULL;
-    length = 
-      silc_client_get_input("Key length in bits [1024]: ");
+    length = silc_get_input("Key length in bits [1024]: ", FALSE);
     if (!length)
       bits = 1024;
     else
@@ -208,7 +176,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
               "RN=Jon Johnson, E=jon@dummy.com): ");
 
     while (!identifier) {
-      identifier = silc_client_get_input(line);
+      identifier = silc_get_input(line, FALSE);
       if (!identifier && def)
        identifier = strdup(def);
     }
@@ -225,7 +193,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
     memset(line, 0, sizeof(line));
     snprintf(line, sizeof(line), "Public key filename [%s] ", 
             SILC_CLIENT_PUBLIC_KEY_NAME);
-    pkfile = silc_client_get_input(line);
+    pkfile = silc_get_input(line, FALSE);
     if (!pkfile)
       pkfile = SILC_CLIENT_PUBLIC_KEY_NAME;
   } else {
@@ -236,7 +204,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
     memset(line, 0, sizeof(line));
     snprintf(line, sizeof(line), "Public key filename [%s] ", 
             SILC_CLIENT_PRIVATE_KEY_NAME);
-    prvfile = silc_client_get_input(line);
+    prvfile = silc_get_input(line, FALSE);
     if (!prvfile)
       prvfile = SILC_CLIENT_PRIVATE_KEY_NAME;
   } else {
@@ -245,23 +213,23 @@ New pair of keys will be created.  Please, answer to following questions.\n\
 
   /* Generate keys */
   silc_pkcs_alloc(pkcs_name, &pkcs);
-  pkcs->pkcs->init(pkcs->context, bits, rng);
+  silc_pkcs_generate_key(pkcs, bits, rng);
 
   /* Save public key into file */
   key = silc_pkcs_get_public_key(pkcs, &key_len);
-  pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,
+  pub_key = silc_pkcs_public_key_alloc(silc_pkcs_get_name(pkcs), identifier,
                                       key, key_len);
   silc_pkcs_save_public_key(pkfile, pub_key, SILC_PKCS_FILE_PEM);
   if (ret_pub_key)
     *ret_pub_key = pub_key;
 
-  memset(key, 0, sizeof(key_len));
+  memset(key, 0, key_len);
   silc_free(key);
 
   /* Save private key into file */
   key = silc_pkcs_get_private_key(pkcs, &key_len);
-  prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);
-
+  prv_key = silc_pkcs_private_key_alloc(silc_pkcs_get_name(pkcs),
+                                       key, key_len);
   silc_pkcs_save_private_key(prvfile, prv_key, NULL, SILC_PKCS_FILE_BIN);
   if (ret_prv_key)
     *ret_prv_key = prv_key;
@@ -271,7 +239,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   printf("Press <Enter> to continue...\n");
   getchar();
 
-  memset(key, 0, sizeof(key_len));
+  memset(key, 0, key_len);
   silc_free(key);
 
   silc_rng_free(rng);
@@ -314,11 +282,11 @@ int silc_client_check_silc_dir()
   }
 
   /* We'll take home path from /etc/passwd file to be sure. */
-  snprintf(filename, sizeof(filename) - 1, "%s/.silc/", pw->pw_dir);
-  snprintf(servfilename, sizeof(servfilename) - 1, "%s/.silc/serverkeys", 
-          pw->pw_dir);
-  snprintf(clientfilename, sizeof(clientfilename) - 1, "%s/.silc/clientkeys", 
-          pw->pw_dir);
+  snprintf(filename, sizeof(filename) - 1, "%s/", get_irssi_dir());
+  snprintf(servfilename, sizeof(servfilename) - 1, "%s/serverkeys", 
+          get_irssi_dir());
+  snprintf(clientfilename, sizeof(clientfilename) - 1, "%s/clientkeys", 
+          get_irssi_dir());
 
   /*
    * Check ~/.silc directory
@@ -440,6 +408,12 @@ int silc_client_check_silc_dir()
     }
   }
 
+  /* Check the owner of the public key */
+  if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { 
+    fprintf(stderr, "You don't seem to own your public key!?\n");
+    return FALSE;
+  }
+  
   if ((stat(file_private_key, &st)) == -1) {
     /* If file doesn't exist */
     if (errno == ENOENT) {
@@ -454,12 +428,6 @@ int silc_client_check_silc_dir()
     }
   }
     
-  /* Check the owner of the public key */
-  if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { 
-    fprintf(stderr, "You don't seem to own your public key!?\n");
-    return FALSE;
-  }
-  
   /* Check the owner of the private key */
   if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { 
     fprintf(stderr, "You don't seem to own your private key!?\n");
@@ -485,18 +453,34 @@ int silc_client_check_silc_dir()
     
   /* 86400 is seconds in a day. */
   if (curtime >= (86400 * SILC_CLIENT_KEY_EXPIRES)) {
+    char *answer;
+
     fprintf(stdout, 
-           "--------------------------------------------------\n"
+           "----------------------------------------------------\n"
            "Your private key has expired and needs to be\n" 
-           "recreated.  This will be done automatically now.\n"
-           "Your new key will expire in %d days from today.\n"
-           "--------------------------------------------------\n",
-           SILC_CLIENT_KEY_EXPIRES);
-
-    silc_client_create_key_pair(SILC_CLIENT_DEF_PKCS, 
-                               SILC_CLIENT_DEF_PKCS_LEN,
-                               file_public_key, 
-                               file_private_key, identifier, NULL, NULL);
+           "recreated.  Would you like to create a new key pair\n"
+           "now?  If you answer Yes, the new key will expire in\n"
+           "%d days from today.  If you answer No, the old key\n"
+           "will expire again in %d days from today.\n"
+           "----------------------------------------------------\n",
+           SILC_CLIENT_KEY_EXPIRES, SILC_CLIENT_KEY_EXPIRES);
+
+    answer = silc_get_input("Would you like to create a new key pair "
+                           "([y]/n)?: ", FALSE);
+    if (!answer || answer[0] == 'Y' || answer[0] == 'y') {
+      silc_client_create_key_pair(SILC_CLIENT_DEF_PKCS, 
+                                 SILC_CLIENT_DEF_PKCS_LEN,
+                                 file_public_key, 
+                                 file_private_key, identifier, NULL, NULL);
+    } else {
+#ifdef HAVE_UTIME
+      struct utimbuf utim;
+      utim.actime = time(NULL);
+      utim.modtime = time(NULL);
+      utime(file_private_key, &utim);
+#endif
+    }
+    silc_free(answer);
   }
   
   if (identifier)
@@ -519,8 +503,8 @@ int silc_client_load_keys(SilcClient client)
     return FALSE;
 
   memset(filename, 0, sizeof(filename));
-  snprintf(filename, sizeof(filename) - 1, "%s/.silc/%s", 
-          pw->pw_dir, SILC_CLIENT_PRIVATE_KEY_NAME);
+  snprintf(filename, sizeof(filename) - 1, "%s/%s", 
+          get_irssi_dir(), SILC_CLIENT_PRIVATE_KEY_NAME);
 
   if (silc_pkcs_load_private_key(filename, &client->private_key,
                                 SILC_PKCS_FILE_BIN) == FALSE)
@@ -529,8 +513,8 @@ int silc_client_load_keys(SilcClient client)
       return FALSE;
 
   memset(filename, 0, sizeof(filename));
-  snprintf(filename, sizeof(filename) - 1, "%s/.silc/%s", 
-          pw->pw_dir, SILC_CLIENT_PUBLIC_KEY_NAME);
+  snprintf(filename, sizeof(filename) - 1, "%s/%s", 
+          get_irssi_dir(), SILC_CLIENT_PUBLIC_KEY_NAME);
 
   if (silc_pkcs_load_public_key(filename, &client->public_key,
                                SILC_PKCS_FILE_PEM) == FALSE)