X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=apps%2Firssi%2Fsrc%2Fsilc%2Fcore%2Fclientutil.c;h=8f1639329813e77636874510c9b35420ee2eca08;hb=413da0f8686910f5e627393157566ae729ca99c4;hp=1bcc9c8bc3fde0d889130218400c74f69c6c3ed3;hpb=d3143da37731cf38df63a22c4d0c848c15062bd0;p=silc.git diff --git a/apps/irssi/src/silc/core/clientutil.c b/apps/irssi/src/silc/core/clientutil.c index 1bcc9c8b..8f163932 100644 --- a/apps/irssi/src/silc/core/clientutil.c +++ b/apps/irssi/src/silc/core/clientutil.c @@ -2,14 +2,13 @@ client.c - Author: Pekka Riikonen + Author: Pekka Riikonen - Copyright (C) 1997 - 2000 Pekka Riikonen + Copyright (C) 1997 - 2002 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 - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,163 +18,33 @@ */ /* $Id$ */ -#include "clientincludes.h" +#include "module.h" -/* Routine used to print lines to window. This can split the - line neatly if a word would overlap the line. */ +#include "net-nonblock.h" +#include "net-sendbuffer.h" +#include "signals.h" +#include "servers.h" +#include "commands.h" +#include "levels.h" +#include "modules.h" +#include "rawlog.h" +#include "misc.h" +#include "settings.h" -void silc_print_to_window(WINDOW *win, char *message) -{ - int str_len, len; - - str_len = strlen(message); - - if (str_len > COLS - 1) { - /* Split overlapping words to next line */ - /* XXX In principal this is wrong as this modifies the original - string as it replaces the last ' ' with '\n'. This could be done - with little more work so that it would not replace anything. */ - len = COLS - 1; - while (1) { - - while (len && message[len] != ' ') - len--; - - if (!len) - break; - - message[len] = '\n'; - len += COLS - 1; - if (len > str_len) - break; - } - } +#include "channels-setup.h" - wprintw(win, "%s", message); - wrefresh(win); -} +#include "silc-servers.h" +#include "silc-channels.h" +#include "silc-queries.h" +#include "silc-nicklist.h" +#include "window-item-def.h" -/* Prints message to the screen. This is used to print the messages - user is typed and message that came on channels. */ +#include "fe-common/core/printtext.h" +#include "fe-common/core/keyboard.h" -void silc_print(SilcClient client, char *msg, ...) -{ - va_list vp; - char message[2048]; - SilcClientInternal app = client->application; - - memset(message, 0, sizeof(message)); - strncat(message, "\n ", 2); +#include "core.h" - va_start(vp, msg); - vsprintf(message + 1, msg, vp); - va_end(vp); - - /* Print the message */ - silc_print_to_window(app->screen->output_win[0], message); -} - -/* Returns user's mail path */ - -char *silc_get_mail_path() -{ - char pathbuf[MAXPATHLEN]; - char *path; - -#ifndef _PATH_MAILDIR -#define _PATH_MAILDIR "/var/mail" -#endif - - path = getenv("MAIL"); - if (path) { - strncpy(pathbuf, path, strlen(path)); - } else { - strcpy(pathbuf, _PATH_MAILDIR); - strcat(pathbuf, "/"); - strcat(pathbuf, silc_get_username()); - } - - return strdup(pathbuf); -} - -/* gets the number of the user's mails, if possible */ - -int silc_get_number_of_emails() -{ - FILE *tl; - int num = 0; - char *filename; - char data[1024]; - - filename = silc_get_mail_path(); - - tl = fopen(filename, "r"); - if (!tl) { - fprintf(stderr, "Couldn't open mail file (%s).\n", filename); - } else { - while((fscanf(tl, "%s", data)) != EOF) { - if(!strcmp(data, "From:")) - num++; - } - - fclose(tl); - } - - return num; -} - -/* Returns time til next minute changes. Used to update the clock when - needed. */ - -int silc_client_time_til_next_min() -{ - time_t curtime; - struct tm *min; - - curtime = time(0); - min = localtime(&curtime); - - return 60 - min->tm_sec; -} - -/* 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(app->screen); - - /* Print 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(app->screen->input_win, answer, sizeof(answer)); - if (!strncasecmp(answer, "yes", strlen(answer)) || - !strncasecmp(answer, "y", strlen(answer))) { - ret = TRUE; - } else if (!strncasecmp(answer, "no", strlen(answer)) || - !strncasecmp(answer, "n", strlen(answer))) { - ret = FALSE; - } else { - silc_say(client, app->conn, "Type yes or no"); - goto again; - } - noecho(); - - silc_screen_input_reset(app->screen); - - return ret; -} - -/* Lists supported (builtin) ciphers */ +/* Lists supported ciphers */ void silc_client_list_ciphers() { @@ -184,7 +53,7 @@ void silc_client_list_ciphers() silc_free(ciphers); } -/* Lists supported (builtin) hash functions */ +/* Lists supported hash functions */ void silc_client_list_hash_funcs() { @@ -193,6 +62,15 @@ void silc_client_list_hash_funcs() silc_free(hash); } +/* Lists supported hash functions */ + +void silc_client_list_hmacs() +{ + char *hash = silc_hmac_get_supported(); + fprintf(stdout, "%s\n", hash); + silc_free(hash); +} + /* Lists supported PKCS algorithms */ void silc_client_list_pkcs() @@ -202,259 +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); -} - -/* Displays prompt on command line and takes passphrase with echo - off from user. */ - -char *silc_client_get_passphrase(const char *prompt) -{ -#if 0 - char input[2048]; - char *ret; - int fd; - struct termios to; - struct termios to_old; - - fd = open("/dev/tty", O_RDONLY); - if (fd < 0) { - fprintf(stderr, "silc: %s\n", strerror(errno)); - return NULL; - } - - signal(SIGINT, SIG_IGN); - - /* Get terminal info */ - tcgetattr(fd, &to); - to_old = to; - - /* Echo OFF */ - to.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); - tcsetattr(fd, TCSANOW, &to); - - 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) { - tcsetattr(fd, TCSANOW, &to_old); - return NULL; - } - - if (strchr(input, '\n')) - *strchr(input, '\n') = '\0'; - - /* Restore old terminfo */ - tcsetattr(fd, TCSANOW, &to_old); - signal(SIGINT, SIG_DFL); - - ret = silc_calloc(strlen(input), sizeof(char)); - memcpy(ret, input, strlen(input)); - memset(input, 0, sizeof(input)); - return ret; -#else - return NULL; -#endif -} - -/* Returns identifier string for public key generation. */ - -char *silc_client_create_identifier() -{ - char *username = NULL, *realname = NULL; - char hostname[256], email[256]; - - /* Get realname */ - realname = silc_get_real_name(); - - /* Get hostname */ - memset(hostname, 0, sizeof(hostname)); - gethostname(hostname, sizeof(hostname)); - - /* Get username (mandatory) */ - username = silc_get_username(); - if (!username) - return NULL; - - /* Create default email address, whether it is right or not */ - snprintf(email, sizeof(email), "%s@%s", username, hostname); - - return silc_pkcs_encode_identifier(username, hostname, realname, email, - NULL, NULL); -} - -/* Creates new public key and private key pair. This is used only - when user wants to create new key pair from command line. */ - -int silc_client_create_key_pair(char *pkcs_name, int bits, - char *public_key, char *private_key, - char *identifier, - SilcPublicKey *ret_pub_key, - SilcPrivateKey *ret_prv_key) -{ - SilcPKCS pkcs; - SilcPublicKey pub_key; - SilcPrivateKey prv_key; - SilcRng rng; - unsigned char *key; - uint32 key_len; - char line[256]; - char *pkfile = NULL, *prvfile = NULL; - - if (!pkcs_name || !public_key || !private_key) - printf("\ -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]: "); - if (!pkcs_name) - pkcs_name = strdup("rsa"); - - if (*pkcs_name == 'l' || *pkcs_name == 'L') { - silc_client_list_pkcs(); - silc_free(pkcs_name); - goto again_name; - } - } - - if (!silc_pkcs_is_supported(pkcs_name)) { - fprintf(stderr, "Unknown PKCS `%s'", pkcs_name); - return FALSE; - } - - if (!bits) { - char *length = NULL; - length = - silc_client_get_input("Key length in bits [1024]: "); - if (!length) - bits = 1024; - else - bits = atoi(length); - } - - if (!identifier) { - char *def = silc_client_create_identifier(); - - memset(line, 0, sizeof(line)); - if (def) - snprintf(line, sizeof(line), "Identifier [%s]: ", def); - else - 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); - } - - rng = silc_rng_alloc(); - silc_rng_init(rng); - silc_rng_global_init(rng); - - if (!public_key) { - 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) { - 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; - } - - /* Generate keys */ - silc_pkcs_alloc(pkcs_name, &pkcs); - pkcs->pkcs->init(pkcs->context, 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, - 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)); - 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); - - silc_pkcs_save_private_key(prvfile, prv_key, NULL, SILC_PKCS_FILE_BIN); - if (ret_prv_key) - *ret_prv_key = prv_key; - - printf("Public key has been saved into `%s'.\n", pkfile); - printf("Private key has been saved into `%s'.\n", prvfile); - printf("Press to continue...\n"); - getchar(); - - memset(key, 0, sizeof(key_len)); - silc_free(key); - - silc_rng_free(rng); - silc_pkcs_free(pkcs); - - return TRUE; -} - /* This checks stats for various SILC files and directories. First it checks if ~/.silc directory exist and is owned by the correct user. If it doesn't exist, it will create the directory. After that it checks if @@ -465,11 +90,9 @@ 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], clientfilename[256]; - char *identifier; + char servfilename[256], clientfilename[256], friendsfilename[256]; struct stat st; struct passwd *pw; - int firstime = FALSE; time_t curtime, modtime; SILC_LOG_DEBUG(("Checking ~./silc directory")); @@ -484,14 +107,14 @@ int silc_client_check_silc_dir() return FALSE; } - identifier = silc_client_create_identifier(); - /* 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()); + snprintf(friendsfilename, sizeof(friendsfilename) - 1, "%s/friends", + get_irssi_dir()); /* * Check ~/.silc directory @@ -504,9 +127,6 @@ int silc_client_check_silc_dir() fprintf(stderr, "Couldn't create `%s' directory\n", filename); return FALSE; } - - /* Directory was created. First time running SILC */ - firstime = TRUE; } else { fprintf(stderr, "Couldn't create `%s' directory due to a wrong uid!\n", filename); @@ -525,6 +145,7 @@ int silc_client_check_silc_dir() return FALSE; } +#if 0 /* Check the permissions of the dir */ if ((st.st_mode & 0777) != 0755) { if ((chmod(filename, 0755)) == -1) { @@ -533,6 +154,7 @@ int silc_client_check_silc_dir() return FALSE; } } +#endif } /* @@ -579,6 +201,28 @@ int silc_client_check_silc_dir() } } + /* + * Check ~./silc/friends directory + */ + if ((stat(friendsfilename, &st)) == -1) { + /* If dir doesn't exist */ + if (errno == ENOENT) { + if (pw->pw_uid == geteuid()) { + if ((mkdir(friendsfilename, 0755)) == -1) { + fprintf(stderr, "Couldn't create `%s' directory\n", friendsfilename); + return FALSE; + } + } else { + fprintf(stderr, "Couldn't create `%s' directory due to a wrong uid!\n", + friendsfilename); + return FALSE; + } + } else { + fprintf(stderr, "%s\n", strerror(errno)); + return FALSE; + } + } + /* * Check Public and Private keys */ @@ -587,50 +231,44 @@ int silc_client_check_silc_dir() snprintf(file_private_key, sizeof(file_private_key) - 1, "%s%s", filename, SILC_CLIENT_PRIVATE_KEY_NAME); - /* If running SILC first time */ - if (firstime) { - fprintf(stdout, "Running SILC for the first time\n"); - silc_client_create_key_pair(SILC_CLIENT_DEF_PKCS, - SILC_CLIENT_DEF_PKCS_LEN, - file_public_key, file_private_key, - identifier, NULL, NULL); - return TRUE; - } - if ((stat(file_public_key, &st)) == -1) { /* If file doesn't exist */ if (errno == ENOENT) { - fprintf(stdout, "Your public key doesn't exist\n"); - silc_client_create_key_pair(SILC_CLIENT_DEF_PKCS, - SILC_CLIENT_DEF_PKCS_LEN, - file_public_key, - file_private_key, identifier, NULL, NULL); + fprintf(stdout, "Running SILC for the first time\n"); + silc_create_key_pair(SILC_CLIENT_DEF_PKCS, + SILC_CLIENT_DEF_PKCS_LEN, + file_public_key, file_private_key, NULL, + NULL, NULL, NULL, NULL, FALSE); + printf("Press to continue...\n"); + getchar(); } else { fprintf(stderr, "%s\n", strerror(errno)); return FALSE; } } - + + /* 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) { fprintf(stdout, "Your private key doesn't exist\n"); - silc_client_create_key_pair(SILC_CLIENT_DEF_PKCS, - SILC_CLIENT_DEF_PKCS_LEN, - file_public_key, - file_private_key, identifier, NULL, NULL); + silc_create_key_pair(SILC_CLIENT_DEF_PKCS, + SILC_CLIENT_DEF_PKCS_LEN, + file_public_key, file_private_key, NULL, + NULL, NULL, NULL, NULL, FALSE); + printf("Press to continue...\n"); + getchar(); } else { fprintf(stderr, "%s\n", strerror(errno)); return FALSE; } } - /* 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"); @@ -656,23 +294,43 @@ 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); + while (!answer) { + printf("Answer 'y' or 'n' and press Enter\n"); + answer = silc_get_input("Would you like to create a new key pair " + "(y/n)?: ", FALSE); + } + if (answer[0] == 'Y' || answer[0] == 'y') { + silc_create_key_pair(SILC_CLIENT_DEF_PKCS, + SILC_CLIENT_DEF_PKCS_LEN, + file_public_key, file_private_key, NULL, + NULL, NULL, NULL, NULL, FALSE); + printf("Press to continue...\n"); + getchar(); + } 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) - silc_free(identifier); - return TRUE; } @@ -680,8 +338,9 @@ int silc_client_check_silc_dir() int silc_client_load_keys(SilcClient client) { - char filename[256]; + char pub[256], prv[256]; struct passwd *pw; + bool ret; SILC_LOG_DEBUG(("Loading public and private keys")); @@ -689,83 +348,21 @@ int silc_client_load_keys(SilcClient client) if (!pw) return FALSE; - memset(filename, 0, sizeof(filename)); - 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, - 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, - SILC_PKCS_FILE_PEM) == FALSE) - if (silc_pkcs_load_public_key(filename, &client->public_key, - SILC_PKCS_FILE_BIN) == FALSE) - return FALSE; - - return TRUE; -} - -/* Dumps the public key on screen. Used from the command line option. */ + memset(prv, 0, sizeof(prv)); + snprintf(prv, sizeof(prv) - 1, "%s/%s", + get_irssi_dir(), SILC_CLIENT_PRIVATE_KEY_NAME); -int silc_client_show_key(char *keyfile) -{ - SilcPublicKey public_key; - SilcPublicKeyIdentifier ident; - char *fingerprint; - unsigned char *pk; - uint32 pk_len; - SilcPKCS pkcs; - int key_len = 0; - - 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; - } - - ident = silc_pkcs_decode_identifier(public_key->identifier); - - pk = silc_pkcs_public_key_encode(public_key, &pk_len); - fingerprint = silc_hash_fingerprint(NULL, pk, pk_len); - - if (silc_pkcs_alloc(public_key->name, &pkcs)) { - key_len = silc_pkcs_public_key_set(pkcs, public_key); - silc_pkcs_free(pkcs); - } - - 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); - - fflush(stdout); - - silc_free(fingerprint); - silc_free(pk); - silc_pkcs_public_key_free(public_key); - silc_pkcs_free_identifier(ident); - - return TRUE; + memset(pub, 0, sizeof(pub)); + snprintf(pub, sizeof(pub) - 1, "%s/%s", + get_irssi_dir(), SILC_CLIENT_PUBLIC_KEY_NAME); + + /* Try loading first with "" passphrase, for those that didn't set + passphrase for private key, and only if that fails let it prompt + for passphrase. */ + ret = silc_load_key_pair(pub, prv, "", &client->pkcs, &client->public_key, + &client->private_key); + if (!ret) + ret = silc_load_key_pair(pub, prv, NULL, &client->pkcs, + &client->public_key, &client->private_key); + return ret; }