Moved silc_client_ch[u]mode[_char] to client library from silc/.
[silc.git] / apps / silc / clientutil.c
index 8fd39a0d9fb0e6164860ec443e5dab448839001a..210f50b02c5d2307f6a8bb4bf12a211bb97e6c38 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.3  2000/07/07 06:53:45  priikone
- *     Added support for server public key verification.
- *
- * Revision 1.2  2000/07/05 06:11:00  priikone
- *     Added ~./silc directory checking, autoloading of keys and
- *     tweaked the key pair generation function.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "clientincludes.h"
 
-/* Internal routine used to print lines to window. This can split the
+/* Routine used to print lines to window. This can split the
    line neatly if a word would overlap the line. */
 
-static void silc_print_to_window(WINDOW *win, char *message)
+void silc_print_to_window(WINDOW *win, char *message)
 {
   int str_len, len;
 
@@ -69,36 +55,14 @@ static void silc_print_to_window(WINDOW *win, char *message)
   wrefresh(win);
 }
 
-/* Prints a message with three star (*) sign before the actual message
-   on the current output window. This is used to print command outputs
-   and error messages. */
-/* XXX Change to accept SilcClientWindow and use output window 
-   from there (the pointer to the output window must be added to the
-   SilcClientWindow object. */
-
-void silc_say(SilcClient client, char *msg, ...)
-{
-  va_list vp;
-  char message[1024];
-  
-  memset(message, 0, sizeof(message));
-  strncat(message, "\n***  ", 5);
-
-  va_start(vp, msg);
-  vsprintf(message + 5, msg, vp);
-  va_end(vp);
-  
-  /* Print the message */
-  silc_print_to_window(client->screen->output_win[0], message);
-}
-
 /* Prints message to the screen. This is used to print the messages
    user is typed and message that came on channels. */
 
 void silc_print(SilcClient client, char *msg, ...)
 {
   va_list vp;
-  char message[1024];
+  char message[2048];
+  SilcClientInternal app = client->application;
   
   memset(message, 0, sizeof(message));
   strncat(message, "\n ", 2);
@@ -108,7 +72,7 @@ void silc_print(SilcClient client, char *msg, ...)
   va_end(vp);
   
   /* Print the message */
-  silc_print_to_window(client->screen->output_win[0], message);
+  silc_print_to_window(app->screen->output_win[0], message);
 }
 
 /* Returns user's mail path */
@@ -117,8 +81,13 @@ char *silc_get_mail_path()
 {
   char pathbuf[MAXPATHLEN];
   char *path;
+
+#ifndef _PATH_MAILDIR
+#define _PATH_MAILDIR "/var/mail"
+#endif
   
-  if ((path = (char *)getenv("MAIL")) != 0) {
+  path = getenv("MAIL");
+  if (path) {
     strncpy(pathbuf, path, strlen(path));
   } else {
     strcpy(pathbuf, _PATH_MAILDIR);
@@ -145,7 +114,7 @@ int silc_get_number_of_emails()
     fprintf(stderr, "Couldn't open mail file (%s).\n", filename);
   } else {
     while((fscanf(tl, "%s", data)) != EOF) { 
-      if(!strcmp(data, "Subject:"))
+      if(!strcmp(data, "From:"))
        num++;
     }
     
@@ -216,71 +185,26 @@ int silc_client_time_til_next_min()
   return 60 - min->tm_sec;
 }
 
-/* Asks passphrase from user on the input line. */
-
-char *silc_client_ask_passphrase(SilcClient client)
-{
-  char pass1[256], pass2[256];
-  char *ret;
-  int try = 3;
-
-  while(try) {
-
-    /* Print prompt */
-    wattroff(client->screen->input_win, A_INVIS);
-    silc_screen_input_print_prompt(client->screen, "Passphrase: ");
-    wattron(client->screen->input_win, A_INVIS);
-    
-    /* Get string */
-    memset(pass1, 0, sizeof(pass1));
-    wgetnstr(client->screen->input_win, pass1, sizeof(pass1));
-    
-    /* Print retype prompt */
-    wattroff(client->screen->input_win, A_INVIS);
-    silc_screen_input_print_prompt(client->screen, "Retype passphrase: ");
-    wattron(client->screen->input_win, A_INVIS);
-    
-    /* Get string */
-    memset(pass2, 0, sizeof(pass2));
-    wgetnstr(client->screen->input_win, pass2, sizeof(pass2));
-
-    if (!strncmp(pass1, pass2, strlen(pass2)))
-      break;
-
-    try--;
-  }
-
-  ret = silc_calloc(strlen(pass1), sizeof(char));
-  memcpy(ret, pass1, strlen(pass1));
-
-  memset(pass1, 0, sizeof(pass1));
-  memset(pass2, 0, sizeof(pass2));
-
-  wattroff(client->screen->input_win, A_INVIS);
-  silc_screen_input_reset(client->screen);
-
-  return ret;
-}
-
 /* Asks yes/no from user on the input line. Returns TRUE on "yes" and
    FALSE on "no". */
 
 int silc_client_ask_yes_no(SilcClient client, char *prompt)
 {
+  SilcClientInternal app = (SilcClientInternal)client->application;
   char answer[4];
   int ret;
 
  again:
-  silc_screen_input_reset(client->screen);
+  silc_screen_input_reset(app->screen);
 
   /* Print prompt */
-  wattroff(client->screen->input_win, A_INVIS);
-  silc_screen_input_print_prompt(client->screen, prompt);
+  wattroff(app->screen->input_win, A_INVIS);
+  silc_screen_input_print_prompt(app->screen, prompt);
 
   /* Get string */
   memset(answer, 0, sizeof(answer));
   echo();
-  wgetnstr(client->screen->input_win, answer, sizeof(answer));
+  wgetnstr(app->screen->input_win, answer, sizeof(answer));
   if (!strncasecmp(answer, "yes", strlen(answer)) ||
       !strncasecmp(answer, "y", strlen(answer))) {
     ret = TRUE;
@@ -288,12 +212,12 @@ int silc_client_ask_yes_no(SilcClient client, char *prompt)
             !strncasecmp(answer, "n", strlen(answer))) {
     ret = FALSE;
   } else {
-    silc_say(client, "Type yes or no");
+    silc_say(client, app->conn, "Type yes or no");
     goto again;
   }
   noecho();
 
-  silc_screen_input_reset(client->screen);
+  silc_screen_input_reset(app->screen);
 
   return ret;
 }
@@ -451,6 +375,7 @@ int silc_client_create_key_pair(char *pkcs_name, int bits,
   SilcRng rng;
   unsigned char *key;
   unsigned int key_len;
+  char line[256];
   char *pkfile = NULL, *prvfile = NULL;
 
   if (!pkcs_name || !public_key || !private_key)
@@ -490,17 +415,19 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   if (!identifier) {
     char *def = silc_client_create_identifier();
 
-    if (identifier)
-      snprintf(def, sizeof(def), "Public key identifier [%s]: ", def);
+    memset(line, 0, sizeof(line));
+    if (def)
+      snprintf(line, sizeof(line), "Identifier [%s]: ", def);
     else
-      snprintf(def, sizeof(def),
-              "Public key identifier (eg. UN=priikone, HN=poseidon.pspt.fi, "
-              "RN=Pekka Riikonen, E=priikone@poseidon.pspt.fi): ");
-
-  again_ident:
-    identifier = silc_client_get_input(def);
-    if (!identifier)
-      goto again_ident;
+      snprintf(line, sizeof(line),
+              "Identifier (eg. UN=jon, HN=jon.dummy.com, "
+              "RN=Jon Johnson, E=jon@dummy.com): ");
+
+    while (!identifier) {
+      identifier = silc_client_get_input(line);
+      if (!identifier && def)
+       identifier = strdup(def);
+    }
 
     if (def)
       silc_free(def);
@@ -511,23 +438,23 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   silc_math_primegen_init();
 
   if (!public_key) {
-  again_pk:
-    pkfile = silc_client_get_input("Public key filename: ");
-    if (!pkfile) {
-      printf("Public key filename must be defined\n");
-      goto again_pk;
-    }
+    memset(line, 0, sizeof(line));
+    snprintf(line, sizeof(line), "Public key filename [%s] ", 
+            SILC_CLIENT_PUBLIC_KEY_NAME);
+    pkfile = silc_client_get_input(line);
+    if (!pkfile)
+      pkfile = SILC_CLIENT_PUBLIC_KEY_NAME;
   } else {
     pkfile = public_key;
   }
 
   if (!private_key) {
-  again_prv:
-    prvfile = silc_client_get_input("Private key filename: ");
-    if (!prvfile) {
-      printf("Private key filename must be defined\n");
-      goto again_prv;
-    }
+    memset(line, 0, sizeof(line));
+    snprintf(line, sizeof(line), "Public key filename [%s] ", 
+            SILC_CLIENT_PRIVATE_KEY_NAME);
+    prvfile = silc_client_get_input(line);
+    if (!prvfile)
+      prvfile = SILC_CLIENT_PRIVATE_KEY_NAME;
   } else {
     prvfile = private_key;
   }
@@ -540,7 +467,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   key = silc_pkcs_get_public_key(pkcs, &key_len);
   pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,
                                       key, key_len);
-  silc_pkcs_save_public_key(pkfile, pub_key);
+  silc_pkcs_save_public_key(pkfile, pub_key, SILC_PKCS_FILE_PEM);
   if (ret_pub_key)
     *ret_pub_key = pub_key;
 
@@ -551,7 +478,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   key = silc_pkcs_get_private_key(pkcs, &key_len);
   prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);
 
-  silc_pkcs_save_private_key(prvfile, prv_key, NULL);
+  silc_pkcs_save_private_key(prvfile, prv_key, NULL, SILC_PKCS_FILE_BIN);
   if (ret_prv_key)
     *ret_prv_key = prv_key;
 
@@ -629,22 +556,23 @@ int silc_client_check_silc_dir()
       fprintf(stderr, "%s\n", strerror(errno));
       return FALSE;
     }
-  }
+  } else {
     
-  /* Check the owner of the dir */
-  if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { 
-    fprintf(stderr, "You don't seem to own `%s' directory\n",
-           filename);
-    return FALSE;
-  }
-  
-  /* Check the permissions of the dir */
-  if ((st.st_mode & 0777) != 0755) {
-    if ((chmod(filename, 0755)) == -1) {
-      fprintf(stderr, "Permissions for `%s' directory must be 0755\n", 
+    /* Check the owner of the dir */
+    if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { 
+      fprintf(stderr, "You don't seem to own `%s' directory\n",
              filename);
       return FALSE;
     }
+    
+    /* Check the permissions of the dir */
+    if ((st.st_mode & 0777) != 0755) {
+      if ((chmod(filename, 0755)) == -1) {
+       fprintf(stderr, "Permissions for `%s' directory must be 0755\n", 
+               filename);
+       return FALSE;
+      }
+    }
   }
 
   /*
@@ -783,133 +711,21 @@ int silc_client_load_keys(SilcClient client)
   snprintf(filename, sizeof(filename) - 1, "%s/.silc/%s", 
           pw->pw_dir, SILC_CLIENT_PRIVATE_KEY_NAME);
 
-  if (silc_pkcs_load_private_key(filename, &client->private_key) == FALSE)
-    return FALSE;
+  if (silc_pkcs_load_private_key(filename, &client->private_key,
+                                SILC_PKCS_FILE_BIN) == FALSE)
+    if (silc_pkcs_load_private_key(filename, &client->private_key,
+                                  SILC_PKCS_FILE_PEM) == FALSE)
+      return FALSE;
 
   memset(filename, 0, sizeof(filename));
   snprintf(filename, sizeof(filename) - 1, "%s/.silc/%s", 
           pw->pw_dir, SILC_CLIENT_PUBLIC_KEY_NAME);
 
-  if (silc_pkcs_load_public_key(filename, &client->public_key) == FALSE)
-    return FALSE;
-
-  return TRUE;
-}
-
-/* Verifies received public key. If user decides to trust the key it is
-   saved as trusted server key for later use. If user does not trust the
-   key this returns FALSE. */
-
-int silc_client_verify_server_key(SilcClient client, 
-                                 SilcSocketConnection sock,
-                                 unsigned char *pk, unsigned int pk_len,
-                                 SilcSKEPKType pk_type)
-{
-  char filename[256];
-  char file[256];
-  char *hostname;
-  struct passwd *pw;
-  struct stat st;
-
-  hostname = sock->hostname ? sock->hostname : sock->ip;
-
-  if (pk_type != SILC_SKE_PK_TYPE_SILC) {
-    silc_say(client, "We don't support server %s key type", hostname);
-    return FALSE;
-  }
-
-  pw = getpwuid(getuid());
-  if (!pw)
-    return FALSE;
-
-  memset(filename, 0, sizeof(filename));
-  memset(file, 0, sizeof(file));
-  snprintf(file, sizeof(file) - 1, "serverkey_%s_%d.pub", hostname,
-          sock->port);
-  snprintf(filename, sizeof(filename) - 1, "%s/.silc/serverkeys/%s", 
-          pw->pw_dir, file);
-
-  /* Check wheter this key already exists */
-  if (stat(filename, &st) < 0) {
-
-    silc_say(client, "Received server %s public key", hostname);
-    /* XXX print fingerprint of the key */
-
-    /* Ask user to verify the key and save it */
-    if (silc_client_ask_yes_no(client, 
-       "Would you like to accept the server key (y/n)? "))
-      {
-       /* Save the key for future checking */
-       silc_pkcs_save_public_key_data(filename, pk, pk_len);
-       return TRUE;
-      }
-  } else {
-    /* The key already exists, verify it. */
-    SilcPublicKey public_key;
-    unsigned char *encpk;
-    unsigned int encpk_len;
-
-    /* Load the key file */
-    if (!silc_pkcs_load_public_key(filename, &public_key)) {
-      silc_say(client, "Received server %s public key", hostname);
-      silc_say(client, "Could not load your local copy of the server %s key",
-              hostname);
-      if (silc_client_ask_yes_no(client, 
-         "Would you like to accept the server key anyway (y/n)? "))
-       {
-         /* Save the key for future checking */
-         unlink(filename);
-         silc_pkcs_save_public_key_data(filename, pk, pk_len);
-         return TRUE;
-       }
-
-      return FALSE;
-    }
-
-    /* Encode the key data */
-    encpk = silc_pkcs_public_key_encode(public_key, &encpk_len);
-    if (!encpk) {
-      silc_say(client, "Received server %s public key", hostname);
-      silc_say(client, "Your local copy of the server %s key is malformed",
-              hostname);
-      if (silc_client_ask_yes_no(client, 
-         "Would you like to accept the server key anyway (y/n)? "))
-       {
-         /* Save the key for future checking */
-         unlink(filename);
-         silc_pkcs_save_public_key_data(filename, pk, pk_len);
-         return TRUE;
-       }
-
+  if (silc_pkcs_load_public_key(filename, &client->public_key,
+                               SILC_PKCS_FILE_PEM) == FALSE)
+    if (silc_pkcs_load_public_key(filename, &client->public_key,
+                                 SILC_PKCS_FILE_BIN) == FALSE)
       return FALSE;
-    }
-
-    if (memcmp(encpk, pk, encpk_len)) {
-      silc_say(client, "Received server %s public key", hostname);
-      silc_say(client, "Server %s key does not match with your local copy",
-              hostname);
-      silc_say(client, "It is possible that the key has expired or changed");
-      silc_say(client, "It is also possible that some one is performing "
-                      "man-in-the-middle attack");
-      
-      /* Ask user to verify the key and save it */
-      if (silc_client_ask_yes_no(client, 
-         "Would you like to accept the server key anyway (y/n)? "))
-       {
-         /* Save the key for future checking */
-         unlink(filename);
-         silc_pkcs_save_public_key_data(filename, pk, pk_len);
-         return TRUE;
-       }
 
-      silc_say(client, "Will not accept server %s key", hostname);
-      return FALSE;
-    }
-
-    /* Local copy matched */
-    return TRUE;
-  }
-
-  silc_say(client, "Will not accept server %s key", hostname);
-  return FALSE;
+  return TRUE;
 }