Add --debug-web option for comprehensive web interface debugging

- Added debug_web flag to wssshd_config_t structure
- Added --debug-web command line option parsing
- Added comprehensive debug output for web requests including:
  * Request method, path, query parameters, and content
  * Session validation and authentication status
  * Login attempts with username/password logging
  * HTTP response details (status, headers, content type, length)
- Updated help text and configuration display
- Fixed compiler warning in debug output
parent b5f41d6b
...@@ -38,6 +38,7 @@ static void set_default_config(wssshd_config_t *config) { ...@@ -38,6 +38,7 @@ static void set_default_config(wssshd_config_t *config) {
config->web_port = 0; config->web_port = 0;
config->web_https = false; config->web_https = false;
config->debug = false; config->debug = false;
config->debug_web = false;
} }
static void load_config_file(wssshd_config_t *config, const char *config_file) { static void load_config_file(wssshd_config_t *config, const char *config_file) {
...@@ -148,6 +149,7 @@ wssshd_config_t *load_config(int argc, char *argv[]) { ...@@ -148,6 +149,7 @@ wssshd_config_t *load_config(int argc, char *argv[]) {
{"web-port", required_argument, 0, 'W'}, {"web-port", required_argument, 0, 'W'},
{"web-https", no_argument, 0, 's'}, {"web-https", no_argument, 0, 's'},
{"debug", no_argument, 0, 'D'}, {"debug", no_argument, 0, 'D'},
{"debug-web", no_argument, 0, 'E'},
{"help", no_argument, 0, '?'}, {"help", no_argument, 0, '?'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
...@@ -155,7 +157,7 @@ wssshd_config_t *load_config(int argc, char *argv[]) { ...@@ -155,7 +157,7 @@ wssshd_config_t *load_config(int argc, char *argv[]) {
int opt; int opt;
int option_index = 0; int option_index = 0;
while ((opt = getopt_long(argc, argv, "c:h:p:d:P:w:W:sD?", long_options, &option_index)) != -1) { while ((opt = getopt_long(argc, argv, "c:h:p:d:P:w:W:sDE?", long_options, &option_index)) != -1) {
switch (opt) { switch (opt) {
case 'c': case 'c':
if (config->config_file) free(config->config_file); if (config->config_file) free(config->config_file);
...@@ -189,6 +191,9 @@ wssshd_config_t *load_config(int argc, char *argv[]) { ...@@ -189,6 +191,9 @@ wssshd_config_t *load_config(int argc, char *argv[]) {
case 'D': case 'D':
config->debug = true; config->debug = true;
break; break;
case 'E':
config->debug_web = true;
break;
case '?': case '?':
printf("Usage: %s [OPTIONS]\n", argv[0]); printf("Usage: %s [OPTIONS]\n", argv[0]);
printf("Options:\n"); printf("Options:\n");
...@@ -201,6 +206,7 @@ wssshd_config_t *load_config(int argc, char *argv[]) { ...@@ -201,6 +206,7 @@ wssshd_config_t *load_config(int argc, char *argv[]) {
printf(" --web-port PORT Web interface port\n"); printf(" --web-port PORT Web interface port\n");
printf(" --web-https Enable HTTPS for web interface\n"); printf(" --web-https Enable HTTPS for web interface\n");
printf(" --debug Enable debug output\n"); printf(" --debug Enable debug output\n");
printf(" --debug-web Enable comprehensive web interface debug output\n");
printf(" --help Show this help\n"); printf(" --help Show this help\n");
free_config(config); free_config(config);
exit(0); exit(0);
...@@ -273,4 +279,5 @@ void print_config(const wssshd_config_t *config) { ...@@ -273,4 +279,5 @@ void print_config(const wssshd_config_t *config) {
printf(" Web port: %d\n", config->web_port); printf(" Web port: %d\n", config->web_port);
printf(" Web HTTPS: %s\n", config->web_https ? "yes" : "no"); printf(" Web HTTPS: %s\n", config->web_https ? "yes" : "no");
printf(" Debug: %s\n", config->debug ? "yes" : "no"); printf(" Debug: %s\n", config->debug ? "yes" : "no");
printf(" Debug Web: %s\n", config->debug_web ? "yes" : "no");
} }
\ No newline at end of file
...@@ -33,6 +33,7 @@ typedef struct { ...@@ -33,6 +33,7 @@ typedef struct {
int web_port; int web_port;
bool web_https; bool web_https;
bool debug; bool debug;
bool debug_web;
} wssshd_config_t; } wssshd_config_t;
// Function declarations // Function declarations
......
...@@ -274,6 +274,17 @@ static const char *get_cookie(const char *headers, const char *name) { ...@@ -274,6 +274,17 @@ static const char *get_cookie(const char *headers, const char *name) {
static void send_response(int client_fd, int status_code, const char *status_text, static void send_response(int client_fd, int status_code, const char *status_text,
const char *content_type, const char *body, size_t body_len, const char *content_type, const char *body, size_t body_len,
const char *set_cookie, const char *extra_header) { const char *set_cookie, const char *extra_header) {
if (global_config && global_config->debug_web) {
printf("[WEB-DEBUG] Sending response: %d %s, Content-Type: %s, Length: %zu\n",
status_code, status_text, content_type, body_len);
if (set_cookie) {
printf("[WEB-DEBUG] Set-Cookie: %s\n", set_cookie);
}
if (extra_header) {
printf("[WEB-DEBUG] Extra header: %s\n", extra_header);
}
}
char response[8192]; char response[8192];
char date[64]; char date[64];
time_t now = time(NULL); time_t now = time(NULL);
...@@ -354,10 +365,24 @@ static char *generate_index_html(const char *username, int is_admin) { ...@@ -354,10 +365,24 @@ static char *generate_index_html(const char *username, int is_admin) {
// Handle HTTP requests // Handle HTTP requests
static void handle_request(int client_fd, const http_request_t *req) { static void handle_request(int client_fd, const http_request_t *req) {
if (global_config && global_config->debug_web) {
printf("[WEB-DEBUG] Received %s request for %s\n", req->method, req->path);
if (req->query[0]) {
printf("[WEB-DEBUG] Query: %s\n", req->query);
}
if (req->content_length > 0) {
printf("[WEB-DEBUG] Content-Length: %d\n", req->content_length);
}
}
const char *session_id = get_cookie(req->headers, "session_id"); const char *session_id = get_cookie(req->headers, "session_id");
const char *username = NULL; const char *username = NULL;
int is_admin = 0; int is_admin = 0;
if (global_config && global_config->debug_web) {
printf("[WEB-DEBUG] Session ID: %s\n", session_id ? session_id : "none");
}
if (session_id) { if (session_id) {
username = validate_session(session_id); username = validate_session(session_id);
if (username) { if (username) {
...@@ -366,6 +391,11 @@ static void handle_request(int client_fd, const http_request_t *req) { ...@@ -366,6 +391,11 @@ static void handle_request(int client_fd, const http_request_t *req) {
} }
} }
if (global_config && global_config->debug_web) {
printf("[WEB-DEBUG] Authenticated user: %s (admin: %s)\n",
username ? username : "none", is_admin ? "yes" : "no");
}
// Route handling // Route handling
if (strcmp(req->method, "GET") == 0) { if (strcmp(req->method, "GET") == 0) {
if (strcmp(req->path, "/") == 0 || strcmp(req->path, "/index.html") == 0) { if (strcmp(req->path, "/") == 0 || strcmp(req->path, "/index.html") == 0) {
...@@ -695,16 +725,33 @@ static void handle_request(int client_fd, const http_request_t *req) { ...@@ -695,16 +725,33 @@ static void handle_request(int client_fd, const http_request_t *req) {
char form_password[50] = ""; char form_password[50] = "";
parse_form_data(req->body, form_username, form_password, sizeof(form_username)); parse_form_data(req->body, form_username, form_password, sizeof(form_username));
if (global_config && global_config->debug_web) {
printf("[WEB-DEBUG] Login attempt: username='%s', password='%s'\n",
form_username, form_password[0] ? "***" : "(empty)");
}
web_user_t *user = find_user(form_username); web_user_t *user = find_user(form_username);
if (user) { if (user) {
char hashed[100]; char hashed[100];
simple_hash(form_password, hashed, sizeof(hashed)); simple_hash(form_password, hashed, sizeof(hashed));
if (strcmp(user->password_hash, hashed) == 0) { if (strcmp(user->password_hash, hashed) == 0) {
const char *new_session = create_session(form_username); const char *new_session = create_session(form_username);
if (global_config && global_config->debug_web) {
printf("[WEB-DEBUG] Login successful for user '%s', session: %s\n",
form_username, new_session);
}
char cookie[256]; char cookie[256];
snprintf(cookie, sizeof(cookie), "session_id=%s; Path=/; HttpOnly", new_session); snprintf(cookie, sizeof(cookie), "session_id=%s; Path=/; HttpOnly", new_session);
send_response(client_fd, 302, "Found", "text/html", NULL, 0, cookie, NULL); send_response(client_fd, 302, "Found", "text/html", NULL, 0, cookie, NULL);
return; return;
} else {
if (global_config && global_config->debug_web) {
printf("[WEB-DEBUG] Login failed: invalid password for user '%s'\n", form_username);
}
}
} else {
if (global_config && global_config->debug_web) {
printf("[WEB-DEBUG] Login failed: user '%s' not found\n", form_username);
} }
} }
const char *asset = get_embedded_asset("/login", NULL); const char *asset = get_embedded_asset("/login", NULL);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment