eliminated memory leaks

This commit is contained in:
Nikos Mavrogiannopoulos
2014-01-09 16:56:01 +01:00
parent df1b3ac223
commit 49b4eaa7dd
2 changed files with 47 additions and 28 deletions
+1
View File
@@ -165,6 +165,7 @@ unsigned j;
else \ else \
s_name = 0; \ s_name = 0; \
} \ } \
free(tmp_tf); \
} }
#define READ_NUMERIC(name, s_name) \ #define READ_NUMERIC(name, s_name) \
+46 -28
View File
@@ -371,7 +371,7 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
if (ret == -1) { if (ret == -1) {
e = errno; e = errno;
mslog(s, proc, LOG_ERR, "cannot obtain metadata from command socket: %s", strerror(e)); mslog(s, proc, LOG_ERR, "cannot obtain metadata from command socket: %s", strerror(e));
return -1; return ERR_BAD_COMMAND;
} }
if (ret == 0) { if (ret == 0) {
@@ -381,7 +381,7 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
if (ret < 3) { if (ret < 3) {
mslog(s, proc, LOG_ERR, "command error"); mslog(s, proc, LOG_ERR, "command error");
return -1; return ERR_BAD_COMMAND;
} }
mslog(s, proc, LOG_DEBUG, "main received message %u of %u bytes\n", (unsigned)cmd, (unsigned)length); mslog(s, proc, LOG_DEBUG, "main received message %u of %u bytes\n", (unsigned)cmd, (unsigned)length);
@@ -396,7 +396,8 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
if (raw_len != length) { if (raw_len != length) {
e = errno; e = errno;
mslog(s, proc, LOG_ERR, "cannot obtain data from command socket: %s", strerror(e)); mslog(s, proc, LOG_ERR, "cannot obtain data from command socket: %s", strerror(e));
return -1; ret = ERR_BAD_COMMAND;
goto cleanup;
} }
switch(cmd) { switch(cmd) {
@@ -406,7 +407,8 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
tmsg = tun_mtu_msg__unpack(NULL, raw_len, raw); tmsg = tun_mtu_msg__unpack(NULL, raw_len, raw);
if (tmsg == NULL) { if (tmsg == NULL) {
mslog(s, proc, LOG_ERR, "error unpacking data"); mslog(s, proc, LOG_ERR, "error unpacking data");
return -1; ret = ERR_BAD_COMMAND;
goto cleanup;
} }
set_tun_mtu(s, proc, tmsg->mtu); set_tun_mtu(s, proc, tmsg->mtu);
@@ -422,7 +424,8 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
smsg = session_resume_store_req_msg__unpack(NULL, raw_len, raw); smsg = session_resume_store_req_msg__unpack(NULL, raw_len, raw);
if (smsg == NULL) { if (smsg == NULL) {
mslog(s, proc, LOG_ERR, "error unpacking data"); mslog(s, proc, LOG_ERR, "error unpacking data");
return -1; ret = ERR_BAD_COMMAND;
goto cleanup;
} }
ret = handle_resume_store_req(s, proc, smsg); ret = handle_resume_store_req(s, proc, smsg);
@@ -443,7 +446,8 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
fmsg = session_resume_fetch_msg__unpack(NULL, raw_len, raw); fmsg = session_resume_fetch_msg__unpack(NULL, raw_len, raw);
if (fmsg == NULL) { if (fmsg == NULL) {
mslog(s, proc, LOG_ERR, "error unpacking data"); mslog(s, proc, LOG_ERR, "error unpacking data");
return -1; ret = ERR_BAD_COMMAND;
goto cleanup;
} }
ret = handle_resume_delete_req(s, proc, fmsg); ret = handle_resume_delete_req(s, proc, fmsg);
@@ -464,7 +468,8 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
fmsg = session_resume_fetch_msg__unpack(NULL, raw_len, raw); fmsg = session_resume_fetch_msg__unpack(NULL, raw_len, raw);
if (fmsg == NULL) { if (fmsg == NULL) {
mslog(s, proc, LOG_ERR, "error unpacking data"); mslog(s, proc, LOG_ERR, "error unpacking data");
return -1; ret = ERR_BAD_COMMAND;
goto cleanup;
} }
ret = handle_resume_fetch_req(s, proc, fmsg, &msg); ret = handle_resume_fetch_req(s, proc, fmsg, &msg);
@@ -484,7 +489,8 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
if (ret < 0) { if (ret < 0) {
mslog(s, proc, LOG_ERR, "could not send reply cmd %d.", (unsigned) cmd); mslog(s, proc, LOG_ERR, "could not send reply cmd %d.", (unsigned) cmd);
return ERR_BAD_COMMAND; ret = ERR_BAD_COMMAND;
goto cleanup;
} }
} }
@@ -495,13 +501,15 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
if (proc->auth_status != PS_AUTH_INACTIVE) { if (proc->auth_status != PS_AUTH_INACTIVE) {
mslog(s, proc, LOG_ERR, "received authentication init when complete."); mslog(s, proc, LOG_ERR, "received authentication init when complete.");
return ERR_BAD_COMMAND; ret = ERR_BAD_COMMAND;
goto cleanup;
} }
auth_init = auth_init_msg__unpack(NULL, raw_len, raw); auth_init = auth_init_msg__unpack(NULL, raw_len, raw);
if (auth_init == NULL) { if (auth_init == NULL) {
mslog(s, proc, LOG_ERR, "error unpacking data"); mslog(s, proc, LOG_ERR, "error unpacking data");
return -1; ret = ERR_BAD_COMMAND;
goto cleanup;
} }
ret = handle_auth_init(s, proc, auth_init); ret = handle_auth_init(s, proc, auth_init);
@@ -514,7 +522,7 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
ret = send_auth_reply_msg(s, proc); ret = send_auth_reply_msg(s, proc);
if (ret < 0) { if (ret < 0) {
mslog(s, proc, LOG_ERR, "could not send reply auth cmd."); mslog(s, proc, LOG_ERR, "could not send reply auth cmd.");
return ret; goto cleanup;
} }
break; /* wait for another command */ break; /* wait for another command */
} else if (ret == 0) { } else if (ret == 0) {
@@ -532,22 +540,24 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
break; break;
case AUTH_REQ: case AUTH_REQ:
if (proc->auth_status != PS_AUTH_INIT) { if (proc->auth_status != PS_AUTH_INIT) {
mslog(s, proc, LOG_ERR, "received authentication request when not initialized."); mslog(s, proc, LOG_ERR, "received authentication request when not initialized.");
return ERR_BAD_COMMAND; ret = ERR_BAD_COMMAND;
goto cleanup_auth;
} }
proc->auth_reqs++; proc->auth_reqs++;
if (proc->auth_reqs > MAX_AUTH_REQS) { if (proc->auth_reqs > MAX_AUTH_REQS) {
mslog(s, proc, LOG_ERR, "received too many authentication requests."); mslog(s, proc, LOG_ERR, "received too many authentication requests.");
return ERR_BAD_COMMAND; ret = ERR_BAD_COMMAND;
goto cleanup_auth;
} }
auth_req = auth_request_msg__unpack(NULL, raw_len, raw); auth_req = auth_request_msg__unpack(NULL, raw_len, raw);
if (auth_req == NULL) { if (auth_req == NULL) {
mslog(s, proc, LOG_ERR, "error unpacking data"); mslog(s, proc, LOG_ERR, "error unpacking data");
return -1; ret = ERR_BAD_COMMAND;
goto cleanup_auth;
} }
ret = handle_auth_req(s, proc, auth_req); ret = handle_auth_req(s, proc, auth_req);
@@ -558,32 +568,34 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
ret = send_auth_reply_msg(s, proc); ret = send_auth_reply_msg(s, proc);
if (ret < 0) { if (ret < 0) {
mslog(s, proc, LOG_ERR, "could not send reply auth cmd."); mslog(s, proc, LOG_ERR, "could not send reply auth cmd.");
return ret; goto cleanup_auth;
} }
break; /* wait for another command */ break; /* wait for another command */
} else if (ret < 0) { } else if (ret < 0) {
add_to_ip_ban_list(s, &proc->remote_addr, proc->remote_addr_len); add_to_ip_ban_list(s, &proc->remote_addr, proc->remote_addr_len);
goto cleanup; goto cleanup_auth;
} }
ret = accept_user(s, proc, cmd); ret = accept_user(s, proc, cmd);
if (ret < 0) { if (ret < 0) {
goto cleanup; goto cleanup_auth;
} }
proc->auth_status = PS_AUTH_COMPLETED; proc->auth_status = PS_AUTH_COMPLETED;
goto cleanup; goto cleanup_auth;
case AUTH_COOKIE_REQ: case AUTH_COOKIE_REQ:
if (proc->auth_status != PS_AUTH_INACTIVE) { if (proc->auth_status != PS_AUTH_INACTIVE) {
mslog(s, proc, LOG_ERR, "received unexpected cookie authentication."); mslog(s, proc, LOG_ERR, "received unexpected cookie authentication.");
return ERR_BAD_COMMAND; ret = ERR_BAD_COMMAND;
goto cleanup_auth;
} }
auth_cookie_req = auth_cookie_request_msg__unpack(NULL, raw_len, raw); auth_cookie_req = auth_cookie_request_msg__unpack(NULL, raw_len, raw);
if (auth_cookie_req == NULL) { if (auth_cookie_req == NULL) {
mslog(s, proc, LOG_ERR, "error unpacking data"); mslog(s, proc, LOG_ERR, "error unpacking data");
return -1; ret = ERR_BAD_COMMAND;
goto cleanup_auth;
} }
ret = handle_auth_cookie_req(s, proc, auth_cookie_req); ret = handle_auth_cookie_req(s, proc, auth_cookie_req);
@@ -592,30 +604,36 @@ int handle_commands(main_server_st *s, struct proc_st* proc)
if (ret < 0) { if (ret < 0) {
add_to_ip_ban_list(s, &proc->remote_addr, proc->remote_addr_len); add_to_ip_ban_list(s, &proc->remote_addr, proc->remote_addr_len);
goto cleanup; goto cleanup_auth;
} }
ret = accept_user(s, proc, cmd); ret = accept_user(s, proc, cmd);
if (ret < 0) { if (ret < 0) {
goto cleanup; goto cleanup_auth;
} }
proc->auth_status = PS_AUTH_COMPLETED; proc->auth_status = PS_AUTH_COMPLETED;
cleanup: cleanup_auth:
if (ret == ERR_WAIT_FOR_SCRIPT) if (ret == ERR_WAIT_FOR_SCRIPT)
return 0; ret = 0;
else { else {
/* no script was called. Handle it as a successful script call. */ /* no script was called. Handle it as a successful script call. */
return handle_script_exit(s, proc, ret); ret = handle_script_exit(s, proc, ret);
} }
goto cleanup;
default: default:
mslog(s, proc, LOG_ERR, "unknown CMD 0x%x.", (unsigned)cmd); mslog(s, proc, LOG_ERR, "unknown CMD 0x%x.", (unsigned)cmd);
return ERR_BAD_COMMAND; ret = ERR_BAD_COMMAND;
goto cleanup;
} }
return 0; ret = 0;
cleanup:
free(raw);
return ret;
} }
int check_if_banned(main_server_st* s, struct sockaddr_storage *addr, socklen_t addr_len) int check_if_banned(main_server_st* s, struct sockaddr_storage *addr, socklen_t addr_len)