diff --git a/.SRCINFO b/.SRCINFO index 67ae45f..dc271cb 100644 --- a/.SRCINFO +++ b/.SRCINFO @@ -1,6 +1,6 @@ pkgbase = coolerdash pkgdesc = Extends CoolerControl with a polished LCD dashboard - pkgver = 1.83 + pkgver = 1.84 pkgrel = 1 url = https://github.com/damachine/coolerdash install = coolerdash.install diff --git a/VERSION b/VERSION index 74c280f..40671b9 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.83 +1.84 diff --git a/src/config.c b/src/config.c index 2b80f7c..4af5091 100644 --- a/src/config.c +++ b/src/config.c @@ -24,12 +24,24 @@ #include #include #include +#include +#include // cppcheck-suppress-end missingIncludeSystem // Include project headers #include "config.h" #include "coolercontrol.h" +// Define O_NOFOLLOW if not defined (for portability) +#ifndef O_NOFOLLOW +#define O_NOFOLLOW 0 +#endif + +// Ensure fdopen is declared if not available in the environment +#ifndef HAVE_DECL_FDOPEN +FILE *fdopen(int fd, const char *mode); +#endif + /** * @brief Global logging implementation for all modules except main.c * @details Provides unified log output for info, status, warning and error messages. @@ -735,12 +747,25 @@ int load_config(const char *path, Config *config) // Initialize config struct with zeros to ensure fallbacks work memset(config, 0, sizeof(Config)); - // Check if file exists and is readable - FILE *file = fopen(path, "r"); + // Sicheres Öffnen der Konfigurationsdatei + int cfd = open(path, O_RDONLY | O_NOFOLLOW); + FILE *file = NULL; + if (cfd != -1) + { + struct stat cst; + if (fstat(cfd, &cst) == 0 && S_ISREG(cst.st_mode)) + { + file = fdopen(cfd, "r"); + } + else + { + close(cfd); + } + } if (!file) { // File doesn't exist - use fallbacks only - log_message(LOG_INFO, "Config file '%s' not found, using fallback values", path); + log_message(LOG_INFO, "Config file '%s' not found oder nicht regulär, using fallback values", path); get_config_defaults(config); return 0; // Return success, fallbacks are valid } diff --git a/src/main.c b/src/main.c index 94305f6..7acb3d3 100644 --- a/src/main.c +++ b/src/main.c @@ -87,14 +87,38 @@ static const char *read_version_from_file(void) return version_buffer[0] ? version_buffer : DEFAULT_VERSION; } - // Try to read from VERSION file - FILE *fp = fopen("VERSION", "r"); + // Try to read from VERSION file sicher + int vfd = open("VERSION", O_RDONLY | O_NOFOLLOW); + FILE *fp = NULL; + if (vfd != -1) + { + struct stat vst; + if (fstat(vfd, &vst) == 0 && S_ISREG(vst.st_mode)) + { + fp = fdopen(vfd, "r"); + } + else + { + close(vfd); + } + } if (!fp) { // Try alternative path for installed version - fp = fopen("/opt/coolerdash/VERSION", "r"); + vfd = open("/opt/coolerdash/VERSION", O_RDONLY | O_NOFOLLOW); + if (vfd != -1) + { + struct stat vst2; + if (fstat(vfd, &vst2) == 0 && S_ISREG(vst2.st_mode)) + { + fp = fdopen(vfd, "r"); + } + else + { + close(vfd); + } + } } - if (!fp) { log_message(LOG_WARNING, "Could not open VERSION file, using default version"); @@ -480,21 +504,32 @@ static void send_shutdown_image_if_needed(void) return; } - // Check if shutdown image file exists - FILE *image_file = fopen(shutdown_image_path, "r"); - if (image_file) + // Check if shutdown image file exists and is a regular file + int img_fd = open(shutdown_image_path, O_RDONLY | O_NOFOLLOW); + if (img_fd != -1) { - // Image exists, send it normally - fclose(image_file); - send_image_to_lcd(g_config_ptr, shutdown_image_path, device_uid); - send_image_to_lcd(g_config_ptr, shutdown_image_path, device_uid); // Send twice for better reliability + struct stat img_st; + if (fstat(img_fd, &img_st) == 0 && S_ISREG(img_st.st_mode)) + { + // Send shutdown image to LCD + close(img_fd); + send_image_to_lcd(g_config_ptr, shutdown_image_path, device_uid); + send_image_to_lcd(g_config_ptr, shutdown_image_path, device_uid); // Send twice for better reliability + } + else + { + // Not a regular file + close(img_fd); + log_message(LOG_WARNING, "Shutdown image '%s' ist kein reguläres File oder nicht lesbar", shutdown_image_path); + goto shutdown_image_missing; + } } else { - // Image doesn't exist, create temporary config with brightness 0 to turn off LCD - log_message(LOG_WARNING, "Shutdown image '%s' not found, turning off LCD display", shutdown_image_path); - - // Create a temporary config copy with brightness set to 0 + // File doesn't exist or can't be opened + log_message(LOG_WARNING, "Shutdown image '%s' nicht gefunden oder nicht lesbar, turning off LCD display", shutdown_image_path); + // Fallback to turning off the display + shutdown_image_missing: Config temp_config = *g_config_ptr; temp_config.lcd_brightness = 0;