131 lines
5.8 KiB
C
131 lines
5.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <locale.h>
|
|
#include "config.h"
|
|
#include "auth.h"
|
|
#include "fs.h"
|
|
#include "render.h"
|
|
#include "utils.h"
|
|
|
|
int main() {
|
|
setlocale(LC_ALL, "en_US.UTF-8");
|
|
setlocale(LC_CTYPE, "en_US.UTF-8");
|
|
|
|
if (load_config("/home/romkazvo/www/cgi-bin/config.ini") != 0) {
|
|
set_default_config();
|
|
}
|
|
|
|
strcpy(g_params.sort_by, "name");
|
|
g_params.sort_order = 1;
|
|
g_params.search[0] = '\0';
|
|
g_params.recursive = 0;
|
|
|
|
char *query_string = getenv("QUERY_STRING");
|
|
char display_path[1024] = "";
|
|
char safe_display_path[1024] = "";
|
|
char base_path[1024];
|
|
strcpy(base_path, g_config.base_path);
|
|
|
|
if (query_string) {
|
|
char *path_start = strstr(query_string, "path=");
|
|
if (path_start) {
|
|
path_start += 5;
|
|
char *path_end = strchr(path_start, '&');
|
|
int path_len = path_end ? (int)(path_end - path_start) : (int)strlen(path_start);
|
|
if (path_len > 0 && path_len < (int)sizeof(display_path) - 1) {
|
|
char encoded_path[1024];
|
|
strncpy(encoded_path, path_start, path_len);
|
|
encoded_path[path_len] = '\0';
|
|
url_decode_enhanced(encoded_path, display_path, sizeof(display_path));
|
|
if (!is_safe_path(display_path)) {
|
|
display_path[0] = '\0';
|
|
strcpy(base_path, g_config.base_path);
|
|
} else {
|
|
strncpy(safe_display_path, display_path, sizeof(safe_display_path) - 1);
|
|
safe_display_path[sizeof(safe_display_path) - 1] = '\0';
|
|
safe_path_join(base_path, sizeof(base_path), g_config.base_path, safe_display_path);
|
|
}
|
|
}
|
|
}
|
|
|
|
char *sort_start = strstr(query_string, "sort=");
|
|
if (sort_start) {
|
|
sort_start += 5;
|
|
char *sort_end = strchr(sort_start, '&');
|
|
int sort_len = sort_end ? (int)(sort_end - sort_start) : (int)strlen(sort_start);
|
|
if (sort_len > 0 && sort_len < (int)sizeof(g_params.sort_by)) {
|
|
strncpy(g_params.sort_by, sort_start, sort_len);
|
|
g_params.sort_by[sort_len] = '\0';
|
|
}
|
|
}
|
|
|
|
char *search_start = strstr(query_string, "search=");
|
|
if (search_start) {
|
|
search_start += 7;
|
|
char *search_end = strchr(search_start, '&');
|
|
int search_len = search_end ? (int)(search_end - search_start) : (int)strlen(search_start);
|
|
if (search_len > 0 && search_len < (int)sizeof(g_params.search) - 1) {
|
|
char encoded_search[256];
|
|
strncpy(encoded_search, search_start, search_len);
|
|
encoded_search[search_len] = '\0';
|
|
url_decode_enhanced(encoded_search, g_params.search, sizeof(g_params.search));
|
|
if (g_params.search[0] != '\0') {
|
|
g_params.recursive = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (strstr(query_string, "password=")) {
|
|
char *error = strstr(query_string, "error=1");
|
|
if (!error) {
|
|
char folder_name[256] = "";
|
|
if (display_path[0]) {
|
|
char *last_slash = strrchr(display_path, '/');
|
|
if (last_slash) strcpy(folder_name, last_slash + 1);
|
|
else strcpy(folder_name, display_path);
|
|
if (is_folder_protected(folder_name)) {
|
|
char *pass_start = strstr(query_string, "password=");
|
|
if (pass_start) {
|
|
pass_start += 9;
|
|
char *pass_end = strchr(pass_start, '&');
|
|
int pass_len = pass_end ? (int)(pass_end - pass_start) : (int)strlen(pass_start);
|
|
if (pass_len > 0 && pass_len < 256) {
|
|
char pass_buf[256];
|
|
strncpy(pass_buf, pass_start, pass_len);
|
|
pass_buf[pass_len] = '\0';
|
|
char decoded_pass[256];
|
|
url_decode_enhanced(pass_buf, decoded_pass, sizeof(decoded_pass));
|
|
if (decoded_pass[0] == '\0') {
|
|
char encoded_path[1024];
|
|
url_encode(display_path, encoded_path, sizeof(encoded_path));
|
|
printf("Status: 302 Found\r\n");
|
|
printf("Location: /cgi-bin/index.cgi?path=%s&error=1\r\n\r\n", encoded_path);
|
|
return 0;
|
|
}
|
|
if (!check_folder_password(folder_name, decoded_pass)) {
|
|
char encoded_path[1024];
|
|
url_encode(display_path, encoded_path, sizeof(encoded_path));
|
|
printf("Status: 302 Found\r\n");
|
|
printf("Location: /cgi-bin/index.cgi?path=%s&error=1\r\n\r\n", encoded_path);
|
|
return 0;
|
|
}
|
|
} else {
|
|
char encoded_path[1024];
|
|
url_encode(display_path, encoded_path, sizeof(encoded_path));
|
|
printf("Status: 302 Found\r\n");
|
|
printf("Location: /cgi-bin/index.cgi?path=%s&error=1\r\n\r\n", encoded_path);
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
printf("Content-type: text/html; charset=utf-8\n\n");
|
|
print_template(base_path, display_path);
|
|
return 0;
|
|
}
|