Skip to content

Commit

Permalink
Move socks4 & socks5 login/password config syntax check to startup
Browse files Browse the repository at this point in the history
  • Loading branch information
darkk committed Apr 3, 2016
1 parent 3c7f635 commit 003765b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
10 changes: 7 additions & 3 deletions socks4.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ const int socks4_status_no_ident = 92;
const int socks4_status_fake_ident = 93;


void socks4_client_init(redsocks_client *client)
static void socks4_instance_init(redsocks_instance *instance)
{
if (client->instance->config.password)
redsocks_log_error(client, LOG_WARNING, "password is ignored for socks4 connections");
if (instance->config.password)
log_error(LOG_WARNING, "password <%s> is ignored for socks4 connections", instance->config.password);
}

static void socks4_client_init(redsocks_client *client)
{
client->state = socks4_new;
}

Expand Down Expand Up @@ -139,6 +142,7 @@ relay_subsys socks4_subsys =
.readcb = socks4_read_cb,
.writecb = socks4_write_cb,
.init = socks4_client_init,
.instance_init = socks4_instance_init,
};


Expand Down
34 changes: 27 additions & 7 deletions socks5.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,47 @@ const char* socks5_status_to_str(int socks5_status)
}
}

int socks5_is_valid_cred(const char *login, const char *password)
bool socks5_is_valid_cred(const char *login, const char *password)
{
if (!login || !password)
return 0;
return false;
if (strlen(login) > 255) {
log_error(LOG_WARNING, "Socks5 login can't be more than 255 chars, <%s> is too long", login);
return 0;
return false;
}
if (strlen(password) > 255) {
log_error(LOG_WARNING, "Socks5 password can't be more than 255 chars, <%s> is too long", password);
return 0;
return false;
}
return 1;
return true;
}

void socks5_client_init(redsocks_client *client)
static void socks5_instance_init(redsocks_instance *instance)
{
redsocks_config *config = &instance->config;
if (config->login || config->password) {
bool deauth = false;
if (config->login && config->password) {
deauth = socks5_is_valid_cred(config->login, config->password);
} else {
log_error(LOG_WARNING, "Socks5 needs either both login and password or none of them");
deauth = true;
}
if (deauth) {
free(config->login);
free(config->password);
config->login = config->password = NULL;
}
}
}

static void socks5_client_init(redsocks_client *client)
{
socks5_client *socks5 = red_payload(client);
const redsocks_config *config = &client->instance->config;

client->state = socks5_new;
socks5->do_password = socks5_is_valid_cred(config->login, config->password);
socks5->do_password = (config->login && config->password) ? 1 : 0;
}

static struct evbuffer *socks5_mkmethods(redsocks_client *client)
Expand Down Expand Up @@ -317,6 +336,7 @@ relay_subsys socks5_subsys =
.readcb = socks5_read_cb,
.writecb = socks5_write_cb,
.init = socks5_client_init,
.instance_init = socks5_instance_init,
};


Expand Down
2 changes: 1 addition & 1 deletion socks5.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static const int socks5_status_Address_type_not_supported = 8;


const char* socks5_status_to_str(int socks5_status);
int socks5_is_valid_cred(const char *login, const char *password);
bool socks5_is_valid_cred(const char *login, const char *password);

struct evbuffer *socks5_mkmethods_plain(int do_password);
struct evbuffer *socks5_mkpassword_plain(const char *login, const char *password);
Expand Down

0 comments on commit 003765b

Please sign in to comment.