Integer type name change.
[silc.git] / apps / silc / clientutil.c
index 321160d49055a73f705825eb99be4f0d331f2449..71b628bc4aacf7d35d8b689140700ef790cd3ce6 100644 (file)
@@ -124,53 +124,6 @@ int silc_get_number_of_emails()
   return num;
 }
 
-/* Returns the username of the user. If the global variable LOGNAME
-   does not exists we will get the name from the password file. */
-
-char *silc_get_username()
-{
-  char *logname = NULL;
-  
-  logname = strdup(getenv("LOGNAME"));
-  if (!logname) {
-    logname = getlogin();
-    if (!logname) {
-      struct passwd *pw;
-
-      pw = getpwuid(getuid());
-      if (!pw) {
-       fprintf(stderr, "silc_get_username: %s\n", strerror(errno));
-       return NULL;
-      }
-      
-      logname = strdup(pw->pw_name);
-    }
-  }
-  
-  return logname;
-}                          
-
-/* Returns the real name of ther user. */
-
-char *silc_get_real_name()
-{
-  char *realname = NULL;
-  struct passwd *pw;
-    
-  pw = getpwuid(getuid());
-  if (!pw) {
-    fprintf(stderr, "silc_get_username: %s\n", strerror(errno));
-    return NULL;
-  }
-
-  if (strchr(pw->pw_gecos, ','))
-    *strchr(pw->pw_gecos, ',') = 0;
-
-  realname = strdup(pw->pw_gecos);
-
-  return realname;
-}
-
 /* Returns time til next minute changes. Used to update the clock when
    needed. */
 
@@ -226,21 +179,27 @@ int silc_client_ask_yes_no(SilcClient client, char *prompt)
 
 void silc_client_list_ciphers()
 {
-
+  char *ciphers = silc_cipher_get_supported();
+  fprintf(stdout, "%s\n", ciphers);
+  silc_free(ciphers);
 }
 
 /* Lists supported (builtin) hash functions */
 
 void silc_client_list_hash_funcs()
 {
-
+  char *hash = silc_hash_get_supported();
+  fprintf(stdout, "%s\n", hash);
+  silc_free(hash);
 }
 
 /* Lists supported PKCS algorithms */
 
 void silc_client_list_pkcs()
 {
-
+  char *pkcs = silc_pkcs_get_supported();
+  fprintf(stdout, "%s\n", pkcs);
+  silc_free(pkcs);
 }
 
 /* Displays input prompt on command line and takes input data from user */
@@ -374,7 +333,7 @@ int silc_client_create_key_pair(char *pkcs_name, int bits,
   SilcPrivateKey prv_key;
   SilcRng rng;
   unsigned char *key;
-  unsigned int key_len;
+  SilcUInt32 key_len;
   char line[256];
   char *pkfile = NULL, *prvfile = NULL;
 
@@ -398,7 +357,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   }
 
   if (!silc_pkcs_is_supported(pkcs_name)) {
-    fprintf(stderr, "Unsupported PKCS `%s'", pkcs_name);
+    fprintf(stderr, "Unknown PKCS `%s'", pkcs_name);
     return FALSE;
   }
 
@@ -435,7 +394,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
 
   rng = silc_rng_alloc();
   silc_rng_init(rng);
-  silc_math_primegen_init();
+  silc_rng_global_init(rng);
 
   if (!public_key) {
     memset(line, 0, sizeof(line));
@@ -482,7 +441,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   if (ret_prv_key)
     *ret_prv_key = prv_key;
 
-  printf("Public key has been save into `%s'.\n", pkfile);
+  printf("Public key has been saved into `%s'.\n", pkfile);
   printf("Private key has been saved into `%s'.\n", prvfile);
   printf("Press <Enter> to continue...\n");
   getchar();
@@ -490,7 +449,6 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   memset(key, 0, sizeof(key_len));
   silc_free(key);
 
-  silc_math_primegen_uninit();
   silc_rng_free(rng);
   silc_pkcs_free(pkcs);
 
@@ -507,7 +465,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
 int silc_client_check_silc_dir()
 {
   char filename[256], file_public_key[256], file_private_key[256];
-  char servfilename[256];
+  char servfilename[256], clientfilename[256];
   char *identifier;
   struct stat st;
   struct passwd *pw;
@@ -532,6 +490,8 @@ int silc_client_check_silc_dir()
   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);
 
   /*
    * Check ~/.silc directory
@@ -597,6 +557,28 @@ int silc_client_check_silc_dir()
     }
   }
   
+  /*
+   * Check ~./silc/clientkeys directory
+   */
+  if ((stat(clientfilename, &st)) == -1) {
+    /* If dir doesn't exist */
+    if (errno == ENOENT) {
+      if (pw->pw_uid == geteuid()) {
+       if ((mkdir(clientfilename, 0755)) == -1) {
+         fprintf(stderr, "Couldn't create `%s' directory\n", clientfilename);
+         return FALSE;
+       }
+      } else {
+       fprintf(stderr, "Couldn't create `%s' directory due to a wrong uid!\n",
+               clientfilename);
+       return FALSE;
+      }
+    } else {
+      fprintf(stderr, "%s\n", strerror(errno));
+      return FALSE;
+    }
+  }
+  
   /*
    * Check Public and Private keys
    */
@@ -730,79 +712,60 @@ int silc_client_load_keys(SilcClient client)
   return TRUE;
 }
 
-/* Parses mode mask and returns the mode as string. */
-
-char *silc_client_chmode(unsigned int mode)
-{
-  char string[20];
-
-  if (!mode)
-    return NULL;
-
-  memset(string, 0, sizeof(string));
-
-  if (mode & SILC_CHANNEL_MODE_PRIVATE)
-    strncat(string, "p", 1);
-
-  if (mode & SILC_CHANNEL_MODE_SECRET)
-    strncat(string, "s", 1);
-
-  if (mode & SILC_CHANNEL_MODE_PRIVKEY)
-    strncat(string, "k", 1);
-
-  if (mode & SILC_CHANNEL_MODE_INVITE)
-    strncat(string, "i", 1);
-
-  if (mode & SILC_CHANNEL_MODE_TOPIC)
-    strncat(string, "t", 1);
-
-  if (mode & SILC_CHANNEL_MODE_ULIMIT)
-    strncat(string, "l", 1);
-
-  if (mode & SILC_CHANNEL_MODE_PASSPHRASE)
-    strncat(string, "a", 1);
-
-  /* Rest of mode is ignored */
-
-  return strdup(string);
-}
-
-/* Parses channel user mode mask and returns te mode as string */
+/* Dumps the public key on screen. Used from the command line option. */
 
-char *silc_client_chumode(unsigned int mode)
+int silc_client_show_key(char *keyfile)
 {
-  char string[4];
-
-  if (!mode)
-    return NULL;
-
-  memset(string, 0, sizeof(string));
-
-  if (mode & SILC_CHANNEL_UMODE_CHANFO)
-    strncat(string, "f", 1);
-
-  if (mode & SILC_CHANNEL_UMODE_CHANOP)
-    strncat(string, "o", 1);
+  SilcPublicKey public_key;
+  SilcPublicKeyIdentifier ident;
+  char *fingerprint;
+  unsigned char *pk;
+  SilcUInt32 pk_len;
+  SilcPKCS pkcs;
+  int key_len = 0;
 
-  return strdup(string);
-}
+  if (silc_pkcs_load_public_key(keyfile, &public_key,
+                               SILC_PKCS_FILE_PEM) == FALSE)
+    if (silc_pkcs_load_public_key(keyfile, &public_key,
+                                 SILC_PKCS_FILE_BIN) == FALSE) {
+      fprintf(stderr, "Could not load public key file `%s'\n", keyfile);
+      return FALSE;
+    }
 
-/* Parses channel user mode and returns it as special mode character. */
+  ident = silc_pkcs_decode_identifier(public_key->identifier);
 
-char *silc_client_chumode_char(unsigned int mode)
-{
-  char string[4];
+  pk = silc_pkcs_public_key_encode(public_key, &pk_len);
+  fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
 
-  if (!mode)
-    return NULL;
+  if (silc_pkcs_alloc(public_key->name, &pkcs)) {
+    key_len = silc_pkcs_public_key_set(pkcs, public_key);
+    silc_pkcs_free(pkcs);
+  }
 
-  memset(string, 0, sizeof(string));
+  printf("Public key file    : %s\n", keyfile);
+  printf("Algorithm          : %s\n", public_key->name);
+  if (key_len)
+    printf("Key length (bits)  : %d\n", key_len);
+  if (ident->realname)
+    printf("Real name          : %s\n", ident->realname);
+  if (ident->username)
+    printf("Username           : %s\n", ident->username);
+  if (ident->host)
+    printf("Hostname           : %s\n", ident->host);
+  if (ident->email)
+    printf("Email              : %s\n", ident->email);
+  if (ident->org)
+    printf("Organization       : %s\n", ident->org);
+  if (ident->country)
+    printf("Country            : %s\n", ident->country);
+  printf("Fingerprint (SHA1) : %s\n", fingerprint); 
 
-  if (mode & SILC_CHANNEL_UMODE_CHANFO)
-    strncat(string, "*", 1);
+  fflush(stdout);
 
-  if (mode & SILC_CHANNEL_UMODE_CHANOP)
-    strncat(string, "@", 1);
+  silc_free(fingerprint);
+  silc_free(pk);
+  silc_pkcs_public_key_free(public_key);
+  silc_pkcs_free_identifier(ident);
 
-  return strdup(string);
+  return TRUE;
 }