5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
23 * Revision 1.1.1.1 2000/06/27 11:36:56 priikone
24 * Importet from internal CVS/Added Log headers.
29 #include "clientincludes.h"
31 /* Internal routine used to print lines to window. This can split the
32 line neatly if a word would overlap the line. */
34 static void silc_print_to_window(WINDOW *win, char *message)
38 str_len = strlen(message);
40 if (str_len > COLS - 1) {
41 /* Split overlapping words to next line */
42 /* XXX In principal this is wrong as this modifies the original
43 string as it replaces the last ' ' with '\n'. This could be done
44 with little more work so that it would not replace anything. */
48 while (len && message[len] != ' ')
61 wprintw(win, "%s", message);
65 /* Prints a message with three star (*) sign before the actual message
66 on the current output window. This is used to print command outputs
67 and error messages. */
68 /* XXX Change to accept SilcClientWindow and use output window
69 from there (the pointer to the output window must be added to the
70 SilcClientWindow object. */
72 void silc_say(SilcClient client, char *msg, ...)
77 memset(message, 0, sizeof(message));
78 strncat(message, "\n*** ", 5);
81 vsprintf(message + 5, msg, vp);
84 /* Print the message */
85 silc_print_to_window(client->screen->output_win[0], message);
88 /* Prints message to the screen. This is used to print the messages
89 user is typed and message that came on channels. */
91 void silc_print(SilcClient client, char *msg, ...)
96 memset(message, 0, sizeof(message));
97 strncat(message, "\n ", 2);
100 vsprintf(message + 1, msg, vp);
103 /* Print the message */
104 silc_print_to_window(client->screen->output_win[0], message);
107 /* Returns user's mail path */
109 char *silc_get_mail_path()
111 char pathbuf[MAXPATHLEN];
114 if ((path = (char *)getenv("MAIL")) != 0) {
115 strncpy(pathbuf, path, strlen(path));
117 strcpy(pathbuf, _PATH_MAILDIR);
118 strcat(pathbuf, "/");
119 strcat(pathbuf, silc_get_username());
122 return strdup(pathbuf);
125 /* gets the number of the user's mails, if possible */
127 int silc_get_number_of_emails()
134 filename = silc_get_mail_path();
136 tl = fopen(filename, "r");
138 fprintf(stderr, "Couldn't open mail file (%s).\n", filename);
140 while((fscanf(tl, "%s", data)) != EOF) {
141 if(!strcmp(data, "Subject:"))
151 /* Returns the username of the user. If the global variable LOGNAME
152 does not exists we will get the name from the password file. */
154 char *silc_get_username()
156 char *logname = NULL;
158 logname = strdup(getenv("LOGNAME"));
160 logname = getlogin();
164 pw = getpwuid(getuid());
166 fprintf(stderr, "silc_get_username: %s\n", strerror(errno));
170 logname = strdup(pw->pw_name);
177 /* Returns the real name of ther user. */
179 char *silc_get_real_name()
181 char *realname = NULL;
184 pw = getpwuid(getuid());
186 fprintf(stderr, "silc_get_username: %s\n", strerror(errno));
190 if (strchr(pw->pw_gecos, ','))
191 *strchr(pw->pw_gecos, ',') = 0;
193 realname = strdup(pw->pw_gecos);
198 /* Returns time til next minute changes. Used to update the clock when
201 int silc_client_time_til_next_min()
207 min = localtime(&curtime);
209 return 60 - min->tm_sec;
212 /* Asks passphrase from user on the input line. */
214 char *silc_client_ask_passphrase(SilcClient client)
216 char pass1[256], pass2[256];
223 wattroff(client->screen->input_win, A_INVIS);
224 silc_screen_input_print_prompt(client->screen, "Passphrase: ");
225 wattron(client->screen->input_win, A_INVIS);
228 memset(pass1, 0, sizeof(pass1));
229 wgetnstr(client->screen->input_win, pass1, sizeof(pass1));
231 /* Print retype prompt */
232 wattroff(client->screen->input_win, A_INVIS);
233 silc_screen_input_print_prompt(client->screen, "Retype passphrase: ");
234 wattron(client->screen->input_win, A_INVIS);
237 memset(pass2, 0, sizeof(pass2));
238 wgetnstr(client->screen->input_win, pass2, sizeof(pass2));
240 if (!strncmp(pass1, pass2, strlen(pass2)))
246 ret = silc_calloc(strlen(pass1), sizeof(char));
247 memcpy(ret, pass1, strlen(pass1));
249 memset(pass1, 0, sizeof(pass1));
250 memset(pass2, 0, sizeof(pass2));
252 wattroff(client->screen->input_win, A_INVIS);
253 silc_screen_input_reset(client->screen);
258 /* Lists supported (builtin) ciphers */
260 void silc_client_list_ciphers()
265 /* Lists supported (builtin) hash functions */
267 void silc_client_list_hash_funcs()
272 /* Lists supported PKCS algorithms */
274 void silc_client_list_pkcs()
279 /* Displays input prompt on command line and takes input data from user */
281 char *silc_client_get_input(const char *prompt)
286 fd = open("/dev/tty", O_RDONLY);
288 fprintf(stderr, "silc: %s\n", strerror(errno));
292 memset(input, 0, sizeof(input));
294 printf("%s", prompt);
297 if ((read(fd, input, sizeof(input))) < 0) {
298 fprintf(stderr, "silc: %s\n", strerror(errno));
302 if (strlen(input) <= 1)
305 if (strchr(input, '\n'))
306 *strchr(input, '\n') = '\0';
308 return strdup(input);
311 /* Displays prompt on command line and takes passphrase with echo
314 char *silc_client_get_passphrase(const char *prompt)
321 struct termios to_old;
323 fd = open("/dev/tty", O_RDONLY);
325 fprintf(stderr, "silc: %s\n", strerror(errno));
329 signal(SIGINT, SIG_IGN);
331 /* Get terminal info */
336 to.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
337 tcsetattr(fd, TCSANOW, &to);
339 memset(input, 0, sizeof(input));
341 printf("%s", prompt);
344 if ((read(fd, input, sizeof(input))) < 0) {
345 fprintf(stderr, "silc: %s\n", strerror(errno));
349 if (strlen(input) <= 1) {
350 tcsetattr(fd, TCSANOW, &to_old);
354 if (strchr(input, '\n'))
355 *strchr(input, '\n') = '\0';
357 /* Restore old terminfo */
358 tcsetattr(fd, TCSANOW, &to_old);
359 signal(SIGINT, SIG_DFL);
361 ret = silc_calloc(strlen(input), sizeof(char));
362 memcpy(ret, input, strlen(input));
363 memset(input, 0, sizeof(input));
370 /* Creates new public key and private key pair. This is used only
371 when user wants to create new key pair from command line. */
373 void silc_client_create_key_pair(char *pkcs_name, int bits)
378 unsigned int key_len;
379 char *pkfile = NULL, *prvfile = NULL;
382 New pair of keys will be created. Please, answer to following questions.\n\
388 silc_client_get_input("PKCS name (l to list names) [rsa]: ");
390 pkcs_name = strdup("rsa");
392 if (*pkcs_name == 'l' || *pkcs_name == 'L') {
393 silc_client_list_pkcs();
394 silc_free(pkcs_name);
402 silc_client_get_input("Key length in bits [1024]: ");
409 rng = silc_rng_alloc();
411 silc_math_primegen_init();
414 pkfile = silc_client_get_input("Public key filename: ");
416 printf("Public key filename must be defined\n");
421 prvfile = silc_client_get_input("Private key filename: ");
423 printf("Private key filename must be defined\n");
428 silc_pkcs_alloc(pkcs_name, &pkcs);
429 pkcs->pkcs->init(pkcs->context, bits, rng);
431 /* Save keys into file */
432 key = silc_pkcs_get_public_key(pkcs, &key_len);
433 silc_pkcs_save_public_key(pkcs, pkfile, key, key_len);
434 memset(key, 0, sizeof(key_len));
436 key = silc_pkcs_get_private_key(pkcs, &key_len);
437 silc_pkcs_save_private_key(pkcs, prvfile, key, key_len, "");
438 memset(key, 0, sizeof(key_len));
441 silc_math_primegen_uninit();
443 silc_pkcs_free(pkcs);