/*
- silcutil.c
+ silcutil.c
- Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+ Author: Pekka Riikonen <priikone@silcnet.org>
- 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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#include "silcincludes.h"
-/* Reads a file to a buffer. The allocated buffer is returned. Length of
- the file read is returned to the return_len argument. */
-
-char *silc_file_read(const char *filename, uint32 *return_len)
-{
- int fd;
- char *buffer;
- int filelen;
-
- fd = open(filename, O_RDONLY);
- if (fd < 0) {
- if (errno == ENOENT)
- return NULL;
- SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
- return NULL;
- }
-
- filelen = lseek(fd, (off_t)0L, SEEK_END);
- if (filelen < 0) {
- close(fd);
- return NULL;
- }
- if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
- close(fd);
- return NULL;
- }
-
- if (filelen < 0) {
- SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
- close(fd);
- return NULL;
- }
-
- buffer = silc_calloc(filelen + 1, sizeof(char));
-
- if ((read(fd, buffer, filelen)) == -1) {
- memset(buffer, 0, sizeof(buffer));
- close(fd);
- SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
- strerror(errno)));
- return NULL;
- }
-
- close(fd);
- buffer[filelen] = EOF;
-
- if (return_len)
- *return_len = filelen;
-
- return buffer;
-}
-
-/* Writes a buffer to the file. */
-
-int silc_file_write(const char *filename, const char *buffer, uint32 len)
-{
- int fd;
-
- if ((fd = creat(filename, 0644)) == -1) {
- SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
- strerror(errno)));
- return -1;
- }
-
- if ((write(fd, buffer, len)) == -1) {
- SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
- close(fd);
- return -1;
- }
-
- close(fd);
-
- return 0;
-}
-
-/* Writes a buffer to the file. If the file is created specific mode is
- set to the file. */
-
-int silc_file_write_mode(const char *filename, const char *buffer,
- uint32 len, int mode)
-{
- int fd;
-
- if ((fd = creat(filename, mode)) == -1) {
- SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
- strerror(errno)));
- return -1;
- }
-
- if ((write(fd, buffer, len)) == -1) {
- SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
- close(fd);
- return -1;
- }
-
- close(fd);
-
- return 0;
-}
-
/* Gets line from a buffer. Stops reading when a newline or EOF occurs.
This doesn't remove the newline sign from the destination buffer. The
argument begin is returned and should be passed again for the function. */
data string. Note: This is originally public domain code and is
still PD. */
-char *silc_encode_pem(unsigned char *data, uint32 len)
+char *silc_encode_pem(unsigned char *data, SilcUInt32 len)
{
int i, j;
- uint32 bits, c, char_count;
+ SilcUInt32 bits, c, char_count;
char *pem;
char_count = 0;
/* Same as above but puts newline ('\n') every 72 characters. */
-char *silc_encode_pem_file(unsigned char *data, uint32 data_len)
+char *silc_encode_pem_file(unsigned char *data, SilcUInt32 data_len)
{
int i, j;
- uint32 len, cols;
+ SilcUInt32 len, cols;
char *pem, *pem2;
pem = silc_encode_pem(data, data_len);
/* Decodes PEM into data. Returns the decoded data. Note: This is
originally public domain code and is still PD. */
-unsigned char *silc_decode_pem(unsigned char *pem, uint32 pem_len,
- uint32 *ret_len)
+unsigned char *silc_decode_pem(unsigned char *pem, SilcUInt32 pem_len,
+ SilcUInt32 *ret_len)
{
int i, j;
- uint32 len, c, char_count, bits;
+ SilcUInt32 len, c, char_count, bits;
unsigned char *data;
static char ialpha[256], decoder[256];
bool silc_parse_userfqdn(const char *string, char **left, char **right)
{
- uint32 tlen;
+ SilcUInt32 tlen;
if (!string)
return FALSE;
+ if (string[0] == '@') {
+ if (left)
+ *left = strdup(string);
+ return TRUE;
+ }
+
if (strchr(string, '@')) {
tlen = strcspn(string, "@");
void silc_parse_command_line(unsigned char *buffer,
unsigned char ***parsed,
- uint32 **parsed_lens,
- uint32 **parsed_types,
- uint32 *parsed_num,
- uint32 max_args)
+ SilcUInt32 **parsed_lens,
+ SilcUInt32 **parsed_types,
+ SilcUInt32 *parsed_num,
+ SilcUInt32 max_args)
{
int i, len = 0;
int argc = 0;
/* Renders ID to suitable to print for example to log file. */
static char rid[256];
-
-char *silc_id_render(void *id, uint16 type)
+#define _PUT_STRING(d, s) \
+do { \
+ int sp = (sizeof(d) - 1) - strlen(d); \
+ if (sp < strlen(s)) { \
+ if (sp) \
+ strncat(d, s, (sizeof(d) - 1) - strlen(d)); \
+ } else { \
+ strncat(d, s, strlen(s)); \
+ } \
+} while(0)
+
+char *silc_id_render(void *id, SilcUInt16 type)
{
char tmp[100];
unsigned char tmps[2];
+ char *cp;
memset(rid, 0, sizeof(rid));
switch(type) {
case SILC_ID_SERVER:
{
SilcServerID *server_id = (SilcServerID *)id;
- struct in_addr ipv4;
-
if (server_id->ip.data_len > 4) {
-
+#ifdef HAVE_IPV6
+ struct sockaddr_in6 ipv6;
+ memset(&ipv6, 0, sizeof(ipv6));
+ ipv6.sin6_family = AF_INET6;
+ memmove(&ipv6.sin6_addr, server_id->ip.data, sizeof(ipv6.sin6_addr));
+ if (!getnameinfo((struct sockaddr *)&ipv6, sizeof(ipv6),
+ tmp, sizeof(tmp) - 1, NULL, 0, NI_NUMERICHOST))
+ _PUT_STRING(rid, tmp);
+#endif
} else {
- SILC_GET32_MSB(ipv4.s_addr, server_id->ip.data);
- strcat(rid, inet_ntoa(ipv4));
+ struct in_addr ipv4;
+ memmove(&ipv4.s_addr, server_id->ip.data, 4);
+ cp = inet_ntoa(ipv4);
+ if (cp)
+ _PUT_STRING(rid, cp);
}
memset(tmp, 0, sizeof(tmp));
- snprintf(tmp, sizeof(tmp), ",%d,", ntohs(server_id->port));
- strcat(rid, tmp);
+ snprintf(tmp, sizeof(tmp) - 1, ",%d,", ntohs(server_id->port));
+ _PUT_STRING(rid, tmp);
SILC_PUT16_MSB(server_id->rnd, tmps);
memset(tmp, 0, sizeof(tmp));
- snprintf(tmp, sizeof(tmp), "[%02x %02x]", tmps[0], tmps[1]);
- strcat(rid, tmp);
+ snprintf(tmp, sizeof(tmp) - 1, "[%02x %02x]", tmps[0], tmps[1]);
+ _PUT_STRING(rid, tmp);
}
break;
case SILC_ID_CLIENT:
{
SilcClientID *client_id = (SilcClientID *)id;
- struct in_addr ipv4;
-
if (client_id->ip.data_len > 4) {
-
+#ifdef HAVE_IPV6
+ struct sockaddr_in6 ipv6;
+ memset(&ipv6, 0, sizeof(ipv6));
+ ipv6.sin6_family = AF_INET6;
+ memmove(&ipv6.sin6_addr, client_id->ip.data, sizeof(ipv6.sin6_addr));
+ if (!getnameinfo((struct sockaddr *)&ipv6, sizeof(ipv6),
+ tmp, sizeof(tmp) - 1, NULL, 0, NI_NUMERICHOST))
+ _PUT_STRING(rid, tmp);
+#endif
} else {
- SILC_GET32_MSB(ipv4.s_addr, client_id->ip.data);
- strcat(rid, inet_ntoa(ipv4));
+ struct in_addr ipv4;
+ memmove(&ipv4.s_addr, client_id->ip.data, 4);
+ cp = inet_ntoa(ipv4);
+ if (cp)
+ _PUT_STRING(rid, cp);
}
memset(tmp, 0, sizeof(tmp));
- snprintf(tmp, sizeof(tmp), ",%02x,", client_id->rnd);
- strcat(rid, tmp);
+ snprintf(tmp, sizeof(tmp) - 1, ",%02x,", client_id->rnd);
+ _PUT_STRING(rid, tmp);
memset(tmp, 0, sizeof(tmp));
- snprintf(tmp, sizeof(tmp), "[%02x %02x %02x %02x...]",
+ snprintf(tmp, sizeof(tmp) - 1, "[%02x %02x %02x %02x...]",
client_id->hash[0], client_id->hash[1],
client_id->hash[2], client_id->hash[3]);
- strcat(rid, tmp);
+ _PUT_STRING(rid, tmp);
}
break;
case SILC_ID_CHANNEL:
{
SilcChannelID *channel_id = (SilcChannelID *)id;
- struct in_addr ipv4;
-
if (channel_id->ip.data_len > 4) {
-
+#ifdef HAVE_IPV6
+ struct sockaddr_in6 ipv6;
+ memset(&ipv6, 0, sizeof(ipv6));
+ ipv6.sin6_family = AF_INET6;
+ memmove(&ipv6.sin6_addr, channel_id->ip.data, sizeof(ipv6.sin6_addr));
+ if (!getnameinfo((struct sockaddr *)&ipv6, sizeof(ipv6),
+ tmp, sizeof(tmp) - 1, NULL, 0, NI_NUMERICHOST))
+ _PUT_STRING(rid, tmp);
+#endif
} else {
- SILC_GET32_MSB(ipv4.s_addr, channel_id->ip.data);
- strcat(rid, inet_ntoa(ipv4));
+ struct in_addr ipv4;
+ memmove(&ipv4.s_addr, channel_id->ip.data, 4);
+ cp = inet_ntoa(ipv4);
+ if (cp)
+ _PUT_STRING(rid, cp);
}
memset(tmp, 0, sizeof(tmp));
- snprintf(tmp, sizeof(tmp), ",%d,", ntohs(channel_id->port));
- strcat(rid, tmp);
+ snprintf(tmp, sizeof(tmp) - 1, ",%d,", ntohs(channel_id->port));
+ _PUT_STRING(rid, tmp);
SILC_PUT16_MSB(channel_id->rnd, tmps);
memset(tmp, 0, sizeof(tmp));
- snprintf(tmp, sizeof(tmp), "[%02x %02x]", tmps[0], tmps[1]);
- strcat(rid, tmp);
+ snprintf(tmp, sizeof(tmp) - 1, "[%02x %02x]", tmps[0], tmps[1]);
+ _PUT_STRING(rid, tmp);
}
break;
}
int silc_string_compare(char *string1, char *string2)
{
int i;
- int slen1 = strlen(string1);
- int slen2 = strlen(string2);
+ int slen1;
+ int slen2;
char *tmpstr1, *tmpstr2;
if (!string1 || !string2)
return FALSE;
+ slen1 = strlen(string1);
+ slen2 = strlen(string2);
+
/* See if they are same already */
if (!strncmp(string1, string2, strlen(string2)))
return TRUE;
this is used usually with nicknames, channel and server names to provide
case insensitive keys. */
-uint32 silc_hash_string(void *key, void *user_context)
+SilcUInt32 silc_hash_string(void *key, void *user_context)
{
char *s = (char *)key;
- uint32 h = 0, g;
+ SilcUInt32 h = 0, g;
while (*s != '\0') {
h = (h << 4) + tolower(*s);
/* Basic hash function to hash integers. May be used with the SilcHashTable. */
-uint32 silc_hash_uint(void *key, void *user_context)
+SilcUInt32 silc_hash_uint(void *key, void *user_context)
{
- return *(uint32 *)key;
+ return *(SilcUInt32 *)key;
}
/* Basic hash funtion to hash pointers. May be used with the SilcHashTable. */
-uint32 silc_hash_ptr(void *key, void *user_context)
+SilcUInt32 silc_hash_ptr(void *key, void *user_context)
{
- return (uint32)key;
+ return (SilcUInt32)key;
}
/* Hash a ID. The `user_context' is the ID type. */
-uint32 silc_hash_id(void *key, void *user_context)
+SilcUInt32 silc_hash_id(void *key, void *user_context)
{
- SilcIdType id_type = (SilcIdType)(uint32)user_context;
- uint32 h = 0;
+ SilcIdType id_type = (SilcIdType)(SilcUInt32)user_context;
+ SilcUInt32 h = 0;
int i;
switch (id_type) {
case SILC_ID_CLIENT:
{
SilcClientID *id = (SilcClientID *)key;
- uint32 g;
+ SilcUInt32 g;
/* The client ID is hashed by hashing the hash of the ID
(which is a truncated MD5 hash of the nickname) so that we
/* Hash binary data. The `user_context' is the data length. */
-uint32 silc_hash_data(void *key, void *user_context)
+SilcUInt32 silc_hash_data(void *key, void *user_context)
{
- uint32 len = (uint32)user_context, h = 0;
+ SilcUInt32 len = (SilcUInt32)user_context, h = 0;
unsigned char *data = (unsigned char *)key;
int i;
return h;
}
+/* Hashed SILC Public key. */
+
+SilcUInt32 silc_hash_public_key(void *key, void *user_context)
+{
+ SilcPublicKey pk = (SilcPublicKey)key;
+ return (pk->len + silc_hash_string(pk->name, NULL) +
+ silc_hash_string(pk->identifier, NULL) +
+ silc_hash_data(pk->pk, (void *)pk->pk_len));
+}
+
/* Compares two strings. May be used as SilcHashTable comparison function. */
bool silc_hash_string_compare(void *key1, void *key2, void *user_context)
bool silc_hash_id_compare(void *key1, void *key2, void *user_context)
{
- SilcIdType id_type = (SilcIdType)(uint32)user_context;
+ SilcIdType id_type = (SilcIdType)(SilcUInt32)user_context;
return (id_type == SILC_ID_CLIENT ?
SILC_ID_COMPARE_HASH((SilcClientID *)key1, (SilcClientID *)key2) :
SILC_ID_COMPARE_TYPE(key1, key2, id_type));
bool silc_hash_data_compare(void *key1, void *key2, void *user_context)
{
- uint32 len = (uint32)user_context;
+ SilcUInt32 len = (SilcUInt32)user_context;
return !memcmp(key1, key2, len);
}
+/* Compares two SILC Public keys. May be used as SilcHashTable comparison
+ function. */
+
+bool silc_hash_public_key_compare(void *key1, void *key2, void *user_context)
+{
+ return silc_pkcs_public_key_compare(key1, key2);
+}
+
/* Parses mode mask and returns the mode as string. */
-char *silc_client_chmode(uint32 mode, const char *cipher, const char *hmac)
+char *silc_client_chmode(SilcUInt32 mode, const char *cipher, const char *hmac)
{
char string[100];
/* Parses channel user mode mask and returns te mode as string */
-char *silc_client_chumode(uint32 mode)
+char *silc_client_chumode(SilcUInt32 mode)
{
char string[4];
/* Parses channel user mode and returns it as special mode character. */
-char *silc_client_chumode_char(uint32 mode)
+char *silc_client_chumode_char(SilcUInt32 mode)
{
char string[4];
return strdup(string);
}
+
+/* Creates fingerprint from data, usually used with SHA1 digests */
+
+char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len)
+{
+ char fingerprint[64], *cp;
+ int i;
+
+ memset(fingerprint, 0, sizeof(fingerprint));
+ cp = fingerprint;
+ for (i = 0; i < data_len; i++) {
+ snprintf(cp, sizeof(fingerprint), "%02X", data[i]);
+ cp += 2;
+
+ if ((i + 1) % 2 == 0)
+ snprintf(cp++, sizeof(fingerprint), " ");
+
+ if ((i + 1) % 10 == 0)
+ snprintf(cp++, sizeof(fingerprint), " ");
+ }
+ i--;
+ if ((i + 1) % 2 == 0)
+ cp[-2] = 0;
+ if ((i + 1) % 10 == 0)
+ cp[-1] = 0;
+
+ return strdup(fingerprint);
+}
+
+/* Return TRUE if the `data' is ASCII string. */
+
+bool silc_string_is_ascii(const unsigned char *data, SilcUInt32 data_len)
+{
+ int i;
+
+ for (i = 0; i < data_len; i++) {
+ if (!isascii(data[i]))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/* Parses SILC protocol style version string. */
+
+bool silc_parse_version_string(const char *version,
+ SilcUInt32 *protocol_version,
+ char **protocol_version_string,
+ SilcUInt32 *software_version,
+ char **software_version_string,
+ char **vendor_version)
+{
+ char *cp, buf[32];
+ int maj = 0, min = 0;
+
+ if (!strstr(version, "SILC-"))
+ return FALSE;
+
+ cp = (char *)version + 5;
+ if (!cp)
+ return FALSE;
+
+ /* Take protocol version */
+
+ maj = atoi(cp);
+ cp = strchr(cp, '.');
+ if (cp) {
+ min = atoi(cp + 1);
+ cp++;
+ }
+
+ memset(buf, 0, sizeof(buf));
+ snprintf(buf, sizeof(buf) - 1, "%d%d", maj, min);
+ if (protocol_version)
+ *protocol_version = atoi(buf);
+ memset(buf, 0, sizeof(buf));
+ snprintf(buf, sizeof(buf) - 1, "%d.%d", maj, min);
+ if (protocol_version_string)
+ *protocol_version_string = strdup(buf);
+
+ /* Take software version */
+
+ maj = 0;
+ min = 0;
+ cp = strchr(cp, '-');
+ if (!cp)
+ return FALSE;
+
+ maj = atoi(cp + 1);
+ cp = strchr(cp, '.');
+ if (cp)
+ min = atoi(cp + 1);
+
+ memset(buf, 0, sizeof(buf));
+ snprintf(buf, sizeof(buf) - 1, "%d%d", maj, min);
+ if (software_version)
+ *software_version = atoi(buf);
+ memset(buf, 0, sizeof(buf));
+ snprintf(buf, sizeof(buf) - 1, "%d.%d", maj, min);
+ if (software_version_string)
+ *software_version_string = strdup(buf);
+
+ /* Take vendor string */
+
+ cp++;
+ if (cp) {
+ cp = strchr(cp, '.');
+ if (cp && cp + 1 && vendor_version)
+ *vendor_version = strdup(cp + 1);
+ }
+
+ return TRUE;
+}
+
+/* Converts version string x.x into number representation. */
+
+SilcUInt32 silc_version_to_num(const char *version)
+{
+ int maj = 0, min = 0;
+ char *cp, buf[32];
+
+ if (!version)
+ return 0;
+
+ cp = (char *)version;
+ maj = atoi(cp);
+ cp = strchr(cp, '.');
+ if (cp)
+ min = atoi(cp + 1);
+
+ memset(buf, 0, sizeof(buf));
+ snprintf(buf, sizeof(buf) - 1, "%d%d", maj, min);
+ return (SilcUInt32)atoi(buf);
+}
+
+/* Displays input prompt on command line and takes input data from user */
+
+char *silc_get_input(const char *prompt, bool echo_off)
+{
+#ifdef SILC_UNIX
+ if (echo_off) {
+ char *ret = NULL;
+#ifdef HAVE_TERMIOS_H
+ char input[2048];
+ 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));
+#endif /* HAVE_TERMIOS_H */
+ return ret;
+ } else {
+ 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);
+ }
+#else
+ return NULL;
+#endif /* SILC_UNIX */
+}