1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-16 23:03:01 +01:00

implement toxdns3 username lookups

This commit is contained in:
Jfreegman 2014-06-16 21:22:26 -04:00
parent 53cb4b0248
commit 1456cef991
10 changed files with 319 additions and 33 deletions

View File

@ -34,13 +34,16 @@ toxic_SOURCES = $(top_srcdir)/src/toxic.c \
$(top_srcdir)/src/line_info.c \ $(top_srcdir)/src/line_info.c \
$(top_srcdir)/src/line_info.h \ $(top_srcdir)/src/line_info.h \
$(top_srcdir)/src/settings.c \ $(top_srcdir)/src/settings.c \
$(top_srcdir)/src/settings.h $(top_srcdir)/src/settings.h \
$(top_srcdir)/src/dns.c \
$(top_srcdir)/src/dns.h
toxic_CFLAGS = -I$(top_srcdir) \ toxic_CFLAGS = -I$(top_srcdir) \
$(NCURSES_CFLAGS) \ $(NCURSES_CFLAGS) \
$(LIBSODIUM_CFLAGS) \ $(LIBSODIUM_CFLAGS) \
$(LIBTOXCORE_CFLAGS) \ $(LIBTOXCORE_CFLAGS) \
$(PTHREAD_CFLAGS) $(PTHREAD_CFLAGS) \
$(LRESOLV_CFLAGS)
toxic_CPPFLAGS = '-DTOXICVER="$(TOXIC_VERSION)"' toxic_CPPFLAGS = '-DTOXICVER="$(TOXIC_VERSION)"'
@ -50,7 +53,8 @@ toxic_LDADD = $(LIBTOXCORE_LDFLAGS) \
$(LIBTOXCORE_LIBS) \ $(LIBTOXCORE_LIBS) \
$(LIBSODIUM_LIBS) \ $(LIBSODIUM_LIBS) \
$(WINSOCK2_LIBS) \ $(WINSOCK2_LIBS) \
$(PTHREAD_LIBS) $(PTHREAD_LIBS) \
$(LRESOLV_LIBS)
# For audio support # For audio support

View File

@ -387,6 +387,19 @@ if test "x$LIBTOXCORE_FOUND" = "xno"; then
AC_SUBST(LIBTOXCORE_LDFLAGS) AC_SUBST(LIBTOXCORE_LDFLAGS)
fi fi
PKG_CHECK_MODULES([LRESOLV], [lresolv],
[],
[
AC_CHECK_HEADER([resolv.h],
[
LRESOLV_CFLAGS="-lresolv"
LRESOLV_LIBS="-lresolv"
AC_SUBST(LRESOLV_CFLAGS)
AC_SUBST(LRESOLV_LIBS)
],
[]
)
])
#### ####
#### A/V Stuff #### A/V Stuff

237
src/dns.c Normal file
View File

@ -0,0 +1,237 @@
/* dns.c
*
*
* Copyright (C) 2014 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic 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 3 of the License, or
* (at your option) any later version.
*
* Toxic 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <tox/toxdns.h>
#include "toxic.h"
#include "windows.h"
#include "line_info.h"
#include "dns.h"
#define MAX_DNS_REQST_SIZE 256
#define NUM_DNS3_SERVERS 1 /* must correspond to number of items in dns3_servers array */
#define TOX_DNS3_TXT_PREFIX "v=tox3;id="
#define DNS3_KEY_SZ 32
static struct dns3_server {
uint8_t *name;
uint8_t key[DNS3_KEY_SZ];
} dns3_servers[] = {
{
"utox.org",
{
0xD3, 0x15, 0x4F, 0x65, 0xD2, 0x8A, 0x5B, 0x41, 0xA0, 0x5D, 0x4A, 0xC7, 0xE4, 0xB3, 0x9C, 0x6B,
0x1C, 0x23, 0x3C, 0xC8, 0x57, 0xFB, 0x36, 0x5C, 0x56, 0xE8, 0x39, 0x27, 0x37, 0x46, 0x2A, 0x12
}
},
};
static int dns_error(ToxWindow *self, uint8_t *errmsg, void *dns_obj)
{
uint8_t msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "DNS lookup failed: %s", errmsg);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
if (dns_obj)
tox_dns3_kill(dns_obj);
return -1;
}
/* puts TXT from dns response in buf */
static int parse_dns_response(ToxWindow *self, u_char *answer, int ans_len, uint8_t *buf)
{
uint8_t *ans_pt = answer + sizeof(HEADER);
uint8_t *ans_end = answer + ans_len;
uint8_t exp_ans[PACKETSZ];
int len = dn_expand(answer, ans_end, ans_pt, exp_ans, sizeof(exp_ans));
if (len == -1)
return dns_error(self, "dn_expand failed.", NULL);
ans_pt += len;
if (ans_pt > ans_end - 4)
return dns_error(self, "Reply was too short.", NULL);
int type;
GETSHORT(type, ans_pt);
if (type != T_TXT)
return dns_error(self, "Broken reply.", NULL);
ans_pt += INT16SZ; /* class */
uint32_t size = 0;
/* recurse through CNAME rr's */
do {
ans_pt += size;
len = dn_expand(answer, ans_end, ans_pt, exp_ans, sizeof(exp_ans));
if (len == -1)
return dns_error(self, "Second dn_expand failed.", NULL);
ans_pt += len;
if (ans_pt > ans_end - 10)
return dns_error(self, "Reply was too short.", NULL);
GETSHORT(type, ans_pt);
ans_pt += INT16SZ;
ans_pt += 4;
GETSHORT(size, ans_pt);
if (ans_pt + size < answer || ans_pt + size > ans_end)
return dns_error(self, "RR overflow.", NULL);
} while (type == T_CNAME);
if (type != T_TXT)
return dns_error(self, "Not a TXT record.", NULL);
uint32_t txt_len = *ans_pt;
if (!size || txt_len >= size || !txt_len) {
uint8_t errmsg[MAX_STR_SIZE];
snprintf(errmsg, sizeof(errmsg), "No record found.", txt_len, size);
return dns_error(self, errmsg, NULL);
}
ans_pt++;
ans_pt[txt_len] = '\0';
memcpy(buf, ans_pt, txt_len + 1);
return 0;
}
/* Takes address addr in the form "username@domain", puts the username in namebuf,
and the domain in dombuf.
return length of username on success, -1 on failure */
static int parse_addr(uint8_t *addr, uint8_t *namebuf, uint8_t *dombuf)
{
uint8_t tmpaddr[MAX_STR_SIZE];
uint8_t *tmpname, *tmpdom;
strcpy(tmpaddr, addr);
tmpname = strtok(tmpaddr, "@");
tmpdom = strtok(NULL, "");
if (tmpname == NULL || tmpdom == NULL)
return -1;
strcpy(namebuf, tmpname);
strcpy(dombuf, tmpdom);
return strlen(namebuf);
}
/* Does DNS lookup for addr and puts resulting tox id in id_bin.
Return 0 on success, -1 on failure. */
int dns3_lookup(ToxWindow *self, uint8_t *id_bin, uint8_t *addr, uint16_t addr_len)
{
if (addr_len >= MAX_STR_SIZE)
return dns_error(self, "Address too long.", NULL);
uint8_t domain[MAX_STR_SIZE];
uint8_t name[MAX_STR_SIZE];
int namelen = parse_addr(addr, name, domain);
if (namelen == -1)
return dns_error(self, "Address must be in the form username@domain", NULL);
/* get domain name/pub key */
uint8_t *DNS_pubkey, *domname = NULL;
int i;
for (i = 0; i < NUM_DNS3_SERVERS; ++i) {
if (strcmp(dns3_servers[i].name, domain) == 0) {
DNS_pubkey = dns3_servers[i].key;
domname = dns3_servers[i].name;
break;
}
}
if (domname == NULL)
return dns_error(self, "Domain not found.", NULL);
void *dns_obj = tox_dns3_new(DNS_pubkey);
if (dns_obj == NULL)
return dns_error(self, "Failed to create DNS object.", NULL);
uint8_t string[MAX_DNS_REQST_SIZE];
uint32_t request_id;
int str_len = tox_generate_dns3_string(dns_obj, string, sizeof(string), &request_id, name, namelen);
if (str_len == -1)
return dns_error(self, "Failed to generate string.", dns_obj);
string[str_len] = '\0';
u_char answer[PACKETSZ];
uint8_t d_string[MAX_DNS_REQST_SIZE];
/* format string and create dns query */
snprintf(d_string, sizeof(d_string), "_%s._tox.%s", string, domname);
int ans_len = res_query(d_string, C_IN, T_TXT, answer, sizeof(answer));
if (ans_len <= 0)
return dns_error(self, "Query failed.", dns_obj);
uint8_t ans_id[MAX_DNS_REQST_SIZE];
/* extract TXT from DNS response */
if (parse_dns_response(self, answer, ans_len, ans_id) == -1) {
tox_dns3_kill(dns_obj);
return -1;
}
uint8_t encrypted_id[MAX_DNS_REQST_SIZE];
int prfx_len = strlen(TOX_DNS3_TXT_PREFIX);
/* extract the encrypted ID from TXT response */
if (strncmp(ans_id, TOX_DNS3_TXT_PREFIX, prfx_len) != 0)
return dns_error(self, "Bad dns3 TXT response.", dns_obj);
memcpy(encrypted_id, ans_id + prfx_len, ans_len - prfx_len);
if (tox_decrypt_dns3_TXT(dns_obj, id_bin, encrypted_id, strlen(encrypted_id), request_id) == -1)
return dns_error(self, "Error decrypting response.", dns_obj);
tox_dns3_kill(dns_obj);
return 0;
}

25
src/dns.h Normal file
View File

@ -0,0 +1,25 @@
/* dns.c
*
*
* Copyright (C) 2014 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic 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 3 of the License, or
* (at your option) any later version.
*
* Toxic 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* Does DNS lookup for addr and puts resulting tox id in id_bin.
Return 0 on success, -1 on failure. */
int dns3_lookup(ToxWindow *self, uint8_t *id_bin, uint8_t *name, uint16_t id_len);

View File

@ -55,6 +55,7 @@ static struct cmd_func global_commands[] = {
{ "/q", cmd_quit }, { "/q", cmd_quit },
{ "/quit", cmd_quit }, { "/quit", cmd_quit },
{ "/status", cmd_status }, { "/status", cmd_status },
#ifdef _SUPPORT_AUDIO #ifdef _SUPPORT_AUDIO
{ "/lsdev", cmd_list_devices }, { "/lsdev", cmd_list_devices },
{ "/sdev", cmd_change_device }, { "/sdev", cmd_change_device },

View File

@ -33,6 +33,7 @@
#include "friendlist.h" #include "friendlist.h"
#include "log.h" #include "log.h"
#include "line_info.h" #include "line_info.h"
#include "dns.h"
extern char *DATA_FILE; extern char *DATA_FILE;
extern ToxWindow *prompt; extern ToxWindow *prompt;
@ -120,33 +121,31 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
snprintf(msg, sizeof(msg), "Hello, my name is %s. Care to Tox?", selfname); snprintf(msg, sizeof(msg), "Hello, my name is %s. Care to Tox?", selfname);
} }
if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) { uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE] = {0};
errmsg = "Invalid ID length."; uint16_t id_len = strlen(id);
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
return;
}
size_t i; /* try to add tox ID */
char xx[3]; if (id_len == 2 * TOX_FRIEND_ADDRESS_SIZE) {
uint32_t x; size_t i;
uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE]; char xx[3];
uint32_t x;
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) { for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++i) {
xx[0] = id[2 * i]; xx[0] = id[2 * i];
xx[1] = id[2 * i + 1]; xx[1] = id[2 * i + 1];
xx[2] = '\0'; xx[2] = '\0';
if (sscanf(xx, "%02x", &x) != 1) { if (sscanf(xx, "%02x", &x) != 1) {
errmsg = "Invalid ID."; errmsg = "Invalid ID.";
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
return; return;
}
id_bin[i] = x;
} }
} else { /* assume id is a username@domain address and do DNS lookup */
id_bin[i] = x; if (dns3_lookup(self, id_bin, id, id_len) == -1)
} return;
for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
id[i] = toupper(id[i]);
} }
int32_t f_num = tox_add_friend(m, id_bin, msg, strlen(msg)); int32_t f_num = tox_add_friend(m, id_bin, msg, strlen(msg));
@ -177,7 +176,7 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
break; break;
case TOX_FAERR_SETNEWNOSPAM: case TOX_FAERR_SETNEWNOSPAM:
errmsg = "Nospam was different (is this contact already added?"; errmsg = "Nospam was different.";
break; break;
default: default:

View File

@ -28,6 +28,7 @@
void cmd_accept(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_accept(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_add(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_add(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_addkey(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_clear(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_clear(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_connect(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_connect(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_groupchat(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);

View File

@ -25,8 +25,13 @@
#include "windows.h" #include "windows.h"
#include "toxic.h" #include "toxic.h"
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
#ifndef MAX
#define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif
/* convert a hex string to binary */ /* convert a hex string to binary */
char *hex_string_to_bin(const char *hex_string); char *hex_string_to_bin(const char *hex_string);

BIN
src/toxic

Binary file not shown.

View File

@ -84,6 +84,11 @@ struct arg_opts {
struct _Winthread Winthread; struct _Winthread Winthread;
struct user_settings *user_settings = NULL; struct user_settings *user_settings = NULL;
static void ignore_SIGINT(int sig)
{
return;
}
static void init_term(void) static void init_term(void)
{ {
signal(SIGWINCH, on_window_resize); signal(SIGWINCH, on_window_resize);
@ -439,11 +444,6 @@ void exit_toxic(Tox *m)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
void ignore_SIGINT(int sig)
{
return;
}
static void do_toxic(Tox *m, ToxWindow *prompt) static void do_toxic(Tox *m, ToxWindow *prompt)
{ {
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
@ -538,6 +538,8 @@ int main(int argc, char *argv[])
/* Make sure all written files are read/writeable only by the current user. */ /* Make sure all written files are read/writeable only by the current user. */
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
signal(SIGINT, ignore_SIGINT);
config_err = create_user_config_dir(user_config_dir); config_err = create_user_config_dir(user_config_dir);
if (DATA_FILE == NULL ) { if (DATA_FILE == NULL ) {
@ -633,7 +635,6 @@ int main(int argc, char *argv[])
sort_friendlist_index(); sort_friendlist_index();
prompt_init_statusbar(prompt, m); prompt_init_statusbar(prompt, m);
signal(SIGINT, ignore_SIGINT);
while (true) { while (true) {
update_unix_time(); update_unix_time();