Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Coverity defects #1268

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct vpn_config {
int password_set;
char otp[OTP_SIZE + 1];
char *cookie;
int saml_port;
uint16_t saml_port;
char saml_session_id[MAX_SAML_SESSION_ID_LENGTH + 1];
char *otp_prompt;
unsigned int otp_delay;
Expand Down
31 changes: 18 additions & 13 deletions src/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ static void send_status_response(int socket, const char *userMessage)

// Using two separate writes here to make the code not more complicated assembling
// the buffers.
write(socket, replyHeaderBuffer, strlen(replyHeaderBuffer));
write(socket, replyBodyBuffer, strlen(replyBodyBuffer));
if (write(socket, replyHeaderBuffer, strlen(replyHeaderBuffer)) < 0)
log_warn("Failed to write: %s\n", strerror(errno));
if (write(socket, replyBodyBuffer, strlen(replyBodyBuffer)) < 0)
log_warn("Failed to write: %s\n", strerror(errno));

end:
free(replyBodyBuffer);
Expand All @@ -140,7 +142,10 @@ static int process_request(int new_socket, char *id)

int flag = 1;

setsockopt(new_socket, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
if (setsockopt(new_socket, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag))) {
log_error("Failed to set socket options: %s\n", strerror(errno));
return -1;
}

// Read the request
char request[1024];
Expand All @@ -152,7 +157,7 @@ static int process_request(int new_socket, char *id)
// If the received request from the server is larger than the buffer,
// the result will not be null-terminated causing strlen to behave wrong.
if (read_result < 0) {
log_error("Bad request\n");
log_error("Bad request: %s\n", strerror(errno));
send_status_response(new_socket, "Invalid redirect response from Fortinet server. VPN could not be established.");
return -1;
}
Expand Down Expand Up @@ -227,44 +232,43 @@ int wait_for_http_request(struct vpn_config *config)
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
long saml_port = config->saml_port;

// Creating socket file descriptor
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == 0) {
log_error("Failed to create socket\n");
if (server_fd < 0) {
log_error("Failed to create socket: %s\n", strerror(errno));
return -1;
}

// Forcefully attaching socket to the port
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
close(server_fd);
log_error("Failed to set socket options\n");
log_error("Failed to set socket options: %s\n", strerror(errno));
return -1;
}

address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
address.sin_port = htons(saml_port);
address.sin_port = htons(config->saml_port);

// Forcefully attaching socket to the port
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
close(server_fd);
log_error("Failed to bind socket to port %d\n", saml_port);
log_error("Failed to bind socket to port %u\n", config->saml_port);
return -1;
}

if (listen(server_fd, 3) < 0) {
close(server_fd);
log_error("Failed to listen on socket\n");
log_error("Failed to listen on socket: %s\n", strerror(errno));
return -1;
}

int max_tries = 5;
fd_set readfds;
struct timeval tv;

log_info("Listening for SAML login on port %d\n", saml_port);
log_info("Listening for SAML login on port %u\n", config->saml_port);
print_url(config);

while (max_tries > 0) {
Expand All @@ -286,7 +290,8 @@ int wait_for_http_request(struct vpn_config *config)
(struct sockaddr *)&address,
(socklen_t *)&addrlen);
if (new_socket < 0) {
log_error("Failed to accept connection\n");
log_error("Failed to accept connection: %s\n",
strerror(errno));
continue;
}
} else {
Expand Down
Loading