система модулей
This commit is contained in:
52
src/auth.c
Normal file
52
src/auth.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "auth.h"
|
||||
#include "config.h"
|
||||
|
||||
int check_folder_password(const char *folder_name, const char *password) {
|
||||
if (!password || !folder_name || password[0] == '\0') return 0;
|
||||
|
||||
FILE *f = fopen(g_config.passwd_file, "r");
|
||||
if (!f) return 1;
|
||||
|
||||
char line[512];
|
||||
while (fgets(line, sizeof(line), f)) {
|
||||
line[strcspn(line, "\r\n")] = 0;
|
||||
char *colon = strchr(line, ':');
|
||||
if (!colon) continue;
|
||||
*colon = '\0';
|
||||
char *folder = line;
|
||||
char *pass = colon + 1;
|
||||
const char *last_slash = strrchr(folder_name, '/');
|
||||
const char *base_name = last_slash ? last_slash + 1 : folder_name;
|
||||
if (strcmp(folder, folder_name) == 0 || strcmp(folder, base_name) == 0) {
|
||||
fclose(f);
|
||||
return strcmp(pass, password) == 0;
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int is_folder_protected(const char *folder_path) {
|
||||
if (!folder_path || folder_path[0] == '\0') return 0;
|
||||
|
||||
FILE *f = fopen(g_config.passwd_file, "r");
|
||||
if (!f) return 0;
|
||||
|
||||
char line[512];
|
||||
while (fgets(line, sizeof(line), f)) {
|
||||
line[strcspn(line, "\r\n")] = 0;
|
||||
char *colon = strchr(line, ':');
|
||||
if (!colon) continue;
|
||||
*colon = '\0';
|
||||
const char *last_slash = strrchr(folder_path, '/');
|
||||
const char *folder_name = last_slash ? last_slash + 1 : folder_path;
|
||||
if (strcmp(line, folder_path) == 0 || strcmp(line, folder_name) == 0) {
|
||||
fclose(f);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user