Fix more bugs

Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
Santiago Lo Coco 2022-06-21 07:57:03 -03:00
parent 7de6cbae60
commit 9561b7cac9
7 changed files with 64 additions and 39 deletions

6
.gitignore vendored
View File

@ -21,3 +21,9 @@ a.out
PVS-Studio.log PVS-Studio.log
report.tasks report.tasks
strace_out strace_out
## Docs
pruebas.txt
rfc1928.txt
rfc1929.txt
enunciado.txt

View File

@ -6,8 +6,10 @@ BProxy (Bottler Proxy)
- [Requisitos <a name="requisitos"></a>](#requisitos-) - [Requisitos <a name="requisitos"></a>](#requisitos-)
- [Compilación <a name="compilación"></a>](#compilación-) - [Compilación <a name="compilación"></a>](#compilación-)
- [Ejecución <a name="ejecución"></a>](#ejecución-) - [Ejecución <a name="ejecución"></a>](#ejecución-)
- [Casos de uso <a name="casos-de-uso"></a>](#casos-de-uso-)
- [Testeos <a name="tests"></a>](#testeos-) - [Testeos <a name="tests"></a>](#testeos-)
- [Limpieza <a name="limpieza"></a>](#limpieza-) - [Limpieza <a name="limpieza"></a>](#limpieza-)
- [Documentación <a name="documentación"></a>](#documentación-)
## Requisitos <a name="requisitos"></a> ## Requisitos <a name="requisitos"></a>
@ -92,6 +94,9 @@ Si desea borrar los archivos creados luego de la compilación (y los testeos si
make clean make clean
``` ```
## Documentación <a name="documentación"></a>
Debe notar que el informe y el rfc del protocolo BCP se encuentran en la carpeta `docs/`.
# Autores # Autores
### Bottler: ### Bottler:
- Barmasch, Juan Martín (61033) - Barmasch, Juan Martín (61033)

View File

@ -14,7 +14,8 @@ Note:
field. When the size of a field is a range, it indicates that the field. When the size of a field is a range, it indicates that the
corresponding field has a variable length defined either by an corresponding field has a variable length defined either by an
associated (one or two octet) length field. associated (one or two octet) length field.
Fields taking more than one octet are in network octet order.
Also, fields taking more than one octet are in network octet order.
2. Requests 2. Requests

View File

@ -63,7 +63,26 @@ static void usage(const char *progname) {
exit(1); exit(1);
} }
static void print_logo() {
printf("\033[0;36m");
printf("$$\\\n"
"$$ |\n"
"$$ |\n"
"$$$$$$$\\ $$$$$$\\ $$$$$$\\ $$$$$$\\ $$\\ $$\\ $$\\ $$\\ \n"
"$$ __$$\\ $$ __$$\\ $$ __$$\\ $$ __$$\\ \\$$\\ $$ |$$ | $$ |\n"
"$$ | $$ |$$ / $$ |$$ | \\__|$$ / $$ | \\$$$$ / $$ | $$ |\n"
"$$ | $$ |$$ | $$ |$$ | $$ | $$ | $$ $$< $$ | $$ |\n"
"$$$$$$$ |$$$$$$$ |$$ | \\$$$$$$ |$$ /\\$$\\ \\$$$$$$$ |\n"
"\\_______/ $$ ____/ \\__| \\______/ \\__/ \\__| \\____$$ |\n"
" $$ | $$\\ $$ |\n"
" $$ | \\$$$$$$ |\n"
" \\__| \\______/\n");
printf("\033[0m\n");
}
void parse_args(const int argc, char ** argv, struct socks5args * args) { void parse_args(const int argc, char ** argv, struct socks5args * args) {
print_logo();
memset(args, 0, sizeof(*args)); memset(args, 0, sizeof(*args));
args->socks_addr = NULL; args->socks_addr = NULL;

View File

@ -431,7 +431,7 @@ int main(int argc, char **argv) {
if (address != NULL) { if (address != NULL) {
if (inet_pton(AF_INET6, address, buf)) { if (inet_pton(AF_INET6, address, buf)) {
domain = AF_INET6; domain = AF_INET6;
} else if (!inet_pton(AF_INET, address, buf)) { } else if (inet_pton(AF_INET, address, buf) <= 0) {
err_msg = "Incorrect network address"; err_msg = "Incorrect network address";
goto finally; goto finally;
} }
@ -527,7 +527,11 @@ finally:
free(recv_buffer); free(recv_buffer);
} }
if (err_msg) { if (err_msg) {
if (errno == 0) {
fprintf(stderr, "%s\n", err_msg);
} else {
perror(err_msg); perror(err_msg);
}
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
} }

View File

@ -40,27 +40,12 @@ int main(int argc, char **argv) {
config_token = strtoul(token, NULL, 16); config_token = strtoul(token, NULL, 16);
} }
else { else {
fprintf(stderr, "No token defined\n"); fprintf(stderr, "No token found. Please see README.md\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
args = malloc(sizeof(struct socks5args)); args = malloc(sizeof(struct socks5args));
parse_args(argc, argv, args); parse_args(argc, argv, args);
printf("\033[0;36m");
printf("$$\\\n"
"$$ |\n"
"$$ |\n"
"$$$$$$$\\ $$$$$$\\ $$$$$$\\ $$$$$$\\ $$\\ $$\\ $$\\ $$\\ \n"
"$$ __$$\\ $$ __$$\\ $$ __$$\\ $$ __$$\\ \\$$\\ $$ |$$ | $$ |\n"
"$$ | $$ |$$ / $$ |$$ | \\__|$$ / $$ | \\$$$$ / $$ | $$ |\n"
"$$ | $$ |$$ | $$ |$$ | $$ | $$ | $$ $$< $$ | $$ |\n"
"$$$$$$$ |$$$$$$$ |$$ | \\$$$$$$ |$$ /\\$$\\ \\$$$$$$$ |\n"
"\\_______/ $$ ____/ \\__| \\______/ \\__/ \\__| \\____$$ |\n"
" $$ | $$\\ $$ |\n"
" $$ | \\$$$$$$ |\n"
" \\__| \\______/\n\n");
printf("\033[0m");
close(STDIN_FILENO); close(STDIN_FILENO);
const char *err_msg = NULL; const char *err_msg = NULL;
@ -68,10 +53,10 @@ int main(int argc, char **argv) {
fd_selector selector = NULL; fd_selector selector = NULL;
struct config * config_ret = NULL; struct config * config_ret = NULL;
struct config * config_ret_ipv6 = NULL; struct config * config_ret_ipv6 = NULL;
int server = -1;
// ------ int server_ipv6 = -1;
// TCP int udp_server = -1;
// ------ int udp_server_ipv6 = -1;
unsigned char buf[sizeof(struct in6_addr)]; unsigned char buf[sizeof(struct in6_addr)];
int domain = -1; int domain = -1;
@ -80,11 +65,13 @@ int main(int argc, char **argv) {
domain = AF_INET; domain = AF_INET;
} else if (inet_pton(AF_INET6, args->socks_addr, buf)) { } else if (inet_pton(AF_INET6, args->socks_addr, buf)) {
domain = AF_INET6; domain = AF_INET6;
} else {
err_msg = "Incorrect network address";
goto finally;
} }
} }
struct sockaddr_in addr; struct sockaddr_in addr;
int server;
if (domain == AF_INET || domain == -1) { if (domain == AF_INET || domain == -1) {
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@ -123,7 +110,6 @@ int main(int argc, char **argv) {
} }
struct sockaddr_in6 tcp_addr_ipv6; struct sockaddr_in6 tcp_addr_ipv6;
int server_ipv6;
if (domain == AF_INET6 || domain == -1) { if (domain == AF_INET6 || domain == -1) {
memset(&tcp_addr_ipv6, 0, sizeof(tcp_addr_ipv6)); memset(&tcp_addr_ipv6, 0, sizeof(tcp_addr_ipv6));
tcp_addr_ipv6.sin6_family = AF_INET6; tcp_addr_ipv6.sin6_family = AF_INET6;
@ -162,21 +148,20 @@ int main(int argc, char **argv) {
} }
} }
// ------
// UDP
// ------
int domain_udp = -1; int domain_udp = -1;
memset(&buf, 0, sizeof(buf));
if (args->mng_addr != NULL) { if (args->mng_addr != NULL) {
if (inet_pton(AF_INET, args->mng_addr, buf)) { if (inet_pton(AF_INET, args->mng_addr, buf)) {
domain_udp = AF_INET; domain_udp = AF_INET;
} else if (inet_pton(AF_INET6, args->mng_addr, buf)) { } else if (inet_pton(AF_INET6, args->mng_addr, buf)) {
domain_udp = AF_INET6; domain_udp = AF_INET6;
} else {
err_msg = "Incorrect network address";
goto finally;
} }
} }
struct sockaddr_in udp_addr; struct sockaddr_in udp_addr;
int udp_server;
if (domain_udp == AF_INET || domain_udp == -1) { if (domain_udp == AF_INET || domain_udp == -1) {
memset(&udp_addr, 0, sizeof(udp_addr)); memset(&udp_addr, 0, sizeof(udp_addr));
udp_addr.sin_family = AF_INET; udp_addr.sin_family = AF_INET;
@ -224,7 +209,6 @@ int main(int argc, char **argv) {
} }
struct sockaddr_in6 udp_addr_ipv6; struct sockaddr_in6 udp_addr_ipv6;
int udp_server_ipv6;
if (domain_udp == AF_INET6 || domain_udp == -1) { if (domain_udp == AF_INET6 || domain_udp == -1) {
memset(&udp_addr_ipv6, 0, sizeof(udp_addr_ipv6)); memset(&udp_addr_ipv6, 0, sizeof(udp_addr_ipv6));
udp_addr_ipv6.sin6_family = AF_INET6; udp_addr_ipv6.sin6_family = AF_INET6;
@ -267,7 +251,7 @@ int main(int argc, char **argv) {
err_msg = "Malloc failed"; err_msg = "Malloc failed";
goto finally; goto finally;
} }
memset(config_ret_ipv6, 0x00, sizeof(*config_ret)); memset(config_ret_ipv6, 0x00, sizeof(*config_ret_ipv6));
buffer_init(&config_ret_ipv6->read_buffer, N(config_ret_ipv6->raw_buff_a), config_ret_ipv6->raw_buff_a); buffer_init(&config_ret_ipv6->read_buffer, N(config_ret_ipv6->raw_buff_a), config_ret_ipv6->raw_buff_a);
buffer_init(&config_ret_ipv6->write_buffer, N(config_ret_ipv6->raw_buff_b), config_ret_ipv6->raw_buff_b); buffer_init(&config_ret_ipv6->write_buffer, N(config_ret_ipv6->raw_buff_b), config_ret_ipv6->raw_buff_b);
} }
@ -383,7 +367,11 @@ finally:
fprintf(stderr, "%s: %s\n", (err_msg == NULL) ? "": err_msg, ss == SELECTOR_IO ? strerror(errno) : selector_error(ss)); fprintf(stderr, "%s: %s\n", (err_msg == NULL) ? "": err_msg, ss == SELECTOR_IO ? strerror(errno) : selector_error(ss));
ret = 2; ret = 2;
} else if (err_msg) { } else if (err_msg) {
if (errno == 0) {
fprintf(stderr, "%s\n", err_msg);
} else {
perror(err_msg); perror(err_msg);
}
ret = 1; ret = 1;
} }
if (selector != NULL) { if (selector != NULL) {
@ -399,10 +387,10 @@ finally:
if (server_ipv6 >= 0) { if (server_ipv6 >= 0) {
close(server_ipv6); close(server_ipv6);
} }
if (udp_server > 0) { if (udp_server >= 0) {
close(udp_server); close(udp_server);
} }
if (udp_server_ipv6 > 0) { if (udp_server_ipv6 >= 0) {
close(udp_server); close(udp_server);
} }
return ret; return ret;

View File

@ -276,6 +276,7 @@ socks5_destroy_(struct socks5 * s) {
} }
if (s->username != NULL) { if (s->username != NULL) {
free(s->username); free(s->username);
s->username = 0;
} }
free(s); free(s);
} }
@ -291,6 +292,7 @@ socks5_destroy(struct socks5 * s) {
parsers_destroy(s); parsers_destroy(s);
if (s->username != NULL) { if (s->username != NULL) {
free(s->username); free(s->username);
s->username = 0;
} }
if(s->origin_resolution != NULL) { if(s->origin_resolution != NULL) {
freeaddrinfo(s->origin_resolution); freeaddrinfo(s->origin_resolution);
@ -934,7 +936,7 @@ void log_request_access(enum socks_response_status status, const struct sockaddr
if (username != NULL) { if (username != NULL) {
fprintf(file, "register=A user=%s %s status=%d\n", username, cbuff, status); fprintf(file, "register=A user=%s %s status=%d\n", username, cbuff, status);
} else { } else {
fprintf(file, "register=A %s status=%d\n", cbuff, status); fprintf(file, "register=A anon %s status=%d\n", cbuff, status);
} }
fclose(file); fclose(file);
} }
@ -958,7 +960,7 @@ void log_request_password(const struct sockaddr * origin_addr, char * username,
break; break;
} }
char * protocol; char * protocol;
if (port == 110) { if (ntohs(port) == 110) {
protocol = "POP3"; protocol = "POP3";
} }
else { else {