Compare commits

..

No commits in common. "5452c3d1d7ecc810eaac7a66184e34324dfa84f8" and "ccb449ae57786ee27cfe499ffa717a59f7d6422a" have entirely different histories.

9 changed files with 43 additions and 212 deletions

View File

@ -71,7 +71,7 @@ $(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c
$(BUILD_DIR)/fuse_operations.o: $(SOURCES_DIR)/fuse_operations.c $(SOURCES_DIR)/fuse_operations.h $(BUILD_DIR)/fuse_operations.o: $(SOURCES_DIR)/fuse_operations.c $(SOURCES_DIR)/fuse_operations.h
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@ $(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
$(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h $(SOURCES_DIR)/real_filename.h $(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@ $(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
$(BUILD_DIR)/ui-socket.o: $(SOURCES_DIR)/ui-socket.c $(SOURCES_DIR)/ui-socket.h $(BUILD_DIR)/ui-socket.o: $(SOURCES_DIR)/ui-socket.c $(SOURCES_DIR)/ui-socket.h

View File

@ -11,8 +11,6 @@
See the file LICENSE. See the file LICENSE.
*/ */
#include "real_filename.h"
#include <assert.h>
#include <stddef.h> #include <stddef.h>
#define FUSE_USE_VERSION 31 #define FUSE_USE_VERSION 31
@ -96,6 +94,9 @@ const char *get_process_name_by_pid(const int pid) {
*/ */
} }
// TODO: move this somewhere else
const char *real_filename(const char *filename) { return filename; }
static void *xmp_init(struct fuse_conn_info *conn, struct fuse_config *cfg) { static void *xmp_init(struct fuse_conn_info *conn, struct fuse_config *cfg) {
(void)conn; (void)conn;
cfg->use_ino = 1; cfg->use_ino = 1;
@ -119,7 +120,6 @@ static void *xmp_init(struct fuse_conn_info *conn, struct fuse_config *cfg) {
cfg->attr_timeout = 0; cfg->attr_timeout = 0;
cfg->negative_timeout = 0; cfg->negative_timeout = 0;
fprintf(stderr, "%d\n", getpid()); fprintf(stderr, "%d\n", getpid());
assert(get_mountpoint() != NULL);
return NULL; return NULL;
} }
@ -157,7 +157,7 @@ static int xmp_access(const char *path, int mask) {
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi)); // fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (!interactive_access(path, proc_info, 0)) { if (!interactive_access(real_filename(path), proc_info, 0)) {
free((void *)proc_info.name); free((void *)proc_info.name);
return -EACCES; return -EACCES;
} }
@ -323,7 +323,7 @@ static int xmp_unlink(const char *path) {
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi)); // fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (!interactive_access(path, pi, 0)) { if (!interactive_access(real_filename(path), pi, 0)) {
free(pi.name); free(pi.name);
return -EACCES; return -EACCES;
} }
@ -371,14 +371,15 @@ static int xmp_rename(const char *from, const char *to, unsigned int flags) {
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi)); // fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (!interactive_access(from, pi, 0)) { if (!interactive_access(real_filename(from), pi, 0)) {
free(pi.name); free(pi.name);
return -EACCES; return -EACCES;
} }
// the "to" file may exist and the process needs to get persmission to modify // the "to" file may exist and the process needs to get persmission to modify
// it // it
if (source_access(to, F_OK) == 0 && !interactive_access(to, pi, 0)) { if (source_access(to, F_OK) == 0 &&
!interactive_access(real_filename(to), pi, 0)) {
free(pi.name); free(pi.name);
return -EACCES; return -EACCES;
} }
@ -401,7 +402,7 @@ static int xmp_link(const char *from, const char *to) {
pi.name = get_process_name_by_pid(pi.PID); pi.name = get_process_name_by_pid(pi.PID);
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi)); // fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (!interactive_access(from, pi, 0)) { if (!interactive_access(real_filename(from), pi, 0)) {
free(pi.name); free(pi.name);
return -EACCES; return -EACCES;
} }
@ -426,7 +427,7 @@ static int xmp_chmod(const char *path, mode_t mode, struct fuse_file_info *fi) {
pi.name = get_process_name_by_pid(pi.PID); pi.name = get_process_name_by_pid(pi.PID);
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi)); // fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (!interactive_access(path, pi, 0)) { if (!interactive_access(real_filename(path), pi, 0)) {
free(pi.name); free(pi.name);
return -EACCES; return -EACCES;
} }
@ -457,7 +458,7 @@ static int xmp_chown(const char *path, uid_t uid, gid_t gid,
pi.name = get_process_name_by_pid(pi.PID); pi.name = get_process_name_by_pid(pi.PID);
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi)); // fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (!interactive_access(path, pi, 0)) { if (!interactive_access(real_filename(path), pi, 0)) {
free(pi.name); free(pi.name);
return -EACCES; return -EACCES;
} }
@ -517,7 +518,7 @@ static int xmp_create(const char *path, mode_t mode,
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi)); // fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (!interactive_access(path, pi, GRANT_PERM)) { if (!interactive_access(real_filename(path), pi, GRANT_PERM)) {
free(pi.name); free(pi.name);
return -EACCES; return -EACCES;
} }
@ -541,7 +542,7 @@ static int xmp_open(const char *path, struct fuse_file_info *fi) {
pi.name = get_process_name_by_pid(pi.PID); pi.name = get_process_name_by_pid(pi.PID);
// fprintf(stderr, "%s, %d\n", path, ask_access(path, pi)); // fprintf(stderr, "%s, %d\n", path, ask_access(path, pi));
if (!interactive_access(path, pi, 0)) { if (!interactive_access(real_filename(path), pi, 0)) {
free(pi.name); free(pi.name);
return -EACCES; return -EACCES;
} }

View File

@ -46,7 +46,7 @@ endif
TARGETS := $(BUILD_DIR)/zenity TARGETS := $(BUILD_DIR)/zenity
ifeq ($(TEST), 1) ifeq ($(TEST), 1)
TARGETS += zenity_test #TARGETS += icfs_test
endif endif
@ -54,10 +54,7 @@ endif
default: $(TARGETS) default: $(TARGETS)
.PHONY: clean zenity_test .PHONY: clean
zenity_test: $(BUILD_DIR)/zenity
./zenity 666 cat /home/fedir Downloads
$(BUILD_DIR)/zenity: $(BUILD_DIR)/zenity.o $(BUILD_DIR)/zenity: $(BUILD_DIR)/zenity.o
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $(BUILD_DIR)/zenity $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $(BUILD_DIR)/zenity

View File

@ -1,153 +1,41 @@
#include "gio/gio.h" #include "gio/gio.h"
#include "glib-object.h"
#include "glib.h"
#include <adwaita.h> #include <adwaita.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define YES 0
#define NO 1
#define PERM 2
int exit_code = 0;
gboolean is_permanent = false;
GtkEntryBuffer *entry_buffer = NULL;
GtkWidget *checkbox = NULL;
static void negative_response(GtkWindow *window) {
fprintf(stdout, "%s", gtk_entry_buffer_get_text(entry_buffer));
exit_code = (gtk_check_button_get_active(GTK_CHECK_BUTTON(checkbox)))
? YES | PERM
: YES;
gtk_window_close(window);
}
static void positive_response(GtkWindow *window) {
fprintf(stdout, "%s", gtk_entry_buffer_get_text(entry_buffer));
exit_code = (gtk_check_button_get_active(GTK_CHECK_BUTTON(checkbox)))
? NO | PERM
: NO;
gtk_window_close(window);
}
static void on_check_button_toggled(GtkToggleButton *button,
gpointer user_data) {
gboolean active = gtk_toggle_button_get_active(button);
}
static void on_activate(GtkApplication *app, gpointer user_data) { static void on_activate(GtkApplication *app, gpointer user_data) {
// Create the main window // Create the main window
AdwWindow *window = ADW_WINDOW(adw_window_new()); AdwMessageDialog *dialog = ADW_MESSAGE_DIALOG(
gtk_window_set_application(GTK_WINDOW(window), app); adw_message_dialog_new(NULL, "Allow access?", "allow_access?"));
gtk_window_set_title(GTK_WINDOW(window), "icfs");
// gtk_window_set_default_size(GTK_WINDOW(window), 300, 150);
AdwStatusPage *content = ADW_STATUS_PAGE(adw_status_page_new()); // Set the dialog as transient for the (non-existent) parent window
adw_status_page_set_title(content, "Allow access?"); gtk_window_set_application(GTK_WINDOW(dialog), app);
char *description = NULL; // Add response buttons
asprintf( adw_message_dialog_add_response(dialog, "cancel", "Cancel");
&description, adw_message_dialog_add_response(dialog, "ok", "OK");
"Allow process <tt>%s</tt> with PID <tt>%s</tt> to access <tt>%s</tt>",
g_object_get_data(G_OBJECT(app), "accessing_name"),
g_object_get_data(G_OBJECT(app), "accessing_pid"),
g_object_get_data(G_OBJECT(app), "access_dir"));
adw_status_page_set_description(content, description);
free(description);
entry_buffer = gtk_entry_buffer_new( // Set default response
g_object_get_data(G_OBJECT(app), "access_dir"), adw_message_dialog_set_default_response(dialog, "ok");
strlen(g_object_get_data(G_OBJECT(app), "access_dir")));
GtkWidget *entry = gtk_entry_new(); // Set close response (when clicking X)
gtk_entry_set_buffer(GTK_ENTRY(entry), entry_buffer); adw_message_dialog_set_close_response(dialog, "cancel");
gtk_entry_set_placeholder_text(GTK_ENTRY(entry), "Enter filename");
gtk_widget_set_hexpand(entry, TRUE);
// Create a prefix label and box // Connect response handler
GtkWidget *entry_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); // g_signal_connect(dialog, "response", G_CALLBACK(gtk_window_close), dialog);
GtkWidget *prefix_label =
gtk_label_new(g_object_get_data(G_OBJECT(app), "root_folder"));
gtk_box_append(GTK_BOX(entry_box), prefix_label);
gtk_box_append(GTK_BOX(entry_box), entry);
checkbox = gtk_check_button_new_with_label("Permanent");
gtk_check_button_set_active(GTK_CHECK_BUTTON(checkbox), false);
// gtk_widget_set_halign(checkbox, GTK_ALIGN_CENTER);
GtkWidget *yes_button = gtk_button_new_with_label("Yes");
gtk_widget_set_hexpand(yes_button, TRUE);
g_signal_connect_swapped(yes_button, "clicked", G_CALLBACK(positive_response),
window);
GtkWidget *no_button = gtk_button_new_with_label("No");
gtk_widget_set_hexpand(no_button, TRUE);
g_signal_connect_swapped(no_button, "clicked", G_CALLBACK(negative_response),
window);
GtkWidget *button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 6);
gtk_box_append(GTK_BOX(button_box), yes_button);
gtk_box_append(GTK_BOX(button_box), no_button);
gtk_widget_set_halign(button_box, GTK_ALIGN_FILL);
// Combine everything in a box
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
gtk_box_append(GTK_BOX(box), GTK_WIDGET(content));
gtk_box_append(GTK_BOX(box), entry_box);
gtk_box_append(GTK_BOX(box), checkbox);
gtk_box_append(GTK_BOX(box), button_box);
gtk_widget_set_margin_top(GTK_WIDGET(box), 12);
gtk_widget_set_margin_bottom(GTK_WIDGET(box), 12);
gtk_widget_set_margin_start(GTK_WIDGET(box), 12);
gtk_widget_set_margin_end(GTK_WIDGET(box), 12);
// g_signal_connect(window, "response", G_CALLBACK(gtk_window_close), window);
// Show the dialog // Show the dialog
adw_window_set_content(window, box); gtk_window_present(GTK_WINDOW(dialog));
gtk_window_present(GTK_WINDOW(window));
}
static int on_command_line(GApplication *app, GApplicationCommandLine *cmdline,
gpointer user_data) {
gchar **argv;
gint argc;
argv = g_application_command_line_get_arguments(cmdline, &argc);
// Handle your arguments here
if (argc >= 4) {
fprintf(stderr, "%s\n", argv[1]);
g_object_set_data_full(G_OBJECT(app), "accessing_pid", g_strdup(argv[1]),
g_free);
g_object_set_data_full(G_OBJECT(app), "accessing_name", g_strdup(argv[2]),
g_free);
g_object_set_data_full(G_OBJECT(app), "root_folder", g_strdup(argv[3]),
g_free);
g_object_set_data_full(G_OBJECT(app), "access_dir", g_strdup(argv[4]),
g_free);
}
g_strfreev(argv);
// Activate the application
g_application_activate(app);
return 0;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
// Create a new application // Create a new application
AdwApplication *app = adw_application_new("com.example.zenityclone", AdwApplication *app = adw_application_new("com.example.zenityclone",
G_APPLICATION_HANDLES_COMMAND_LINE); G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "command-line", G_CALLBACK(on_command_line), NULL);
g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL); g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
// Run the application // Run the application
int status = g_application_run(G_APPLICATION(app), argc, argv); int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app); g_object_unref(app);
return (status == 0) ? exit_code : status; return status;
} }

View File

@ -56,7 +56,6 @@ int main(int argc, char *argv[]) {
ret = fuse_main(argc - 1, argv, get_fuse_operations(), NULL); ret = fuse_main(argc - 1, argv, get_fuse_operations(), NULL);
free((void *)mountpoint); free((void *)mountpoint);
source_destroy();
destroy_ui_socket(); destroy_ui_socket();
return ret; return ret;
} }

View File

@ -1,7 +0,0 @@
#ifndef REAL_FILENAME_H
#define REAL_FILENAME_H
const char *real_filename(const char *filename);
const char *get_mountpoint(void);
#endif // !REAL_FILENAME_H

View File

@ -10,14 +10,14 @@
#include "sourcefs.h" #include "sourcefs.h"
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
static struct source_files_handle { static struct source_files_handle {
const char *mountpoint;
int root_fd; int root_fd;
} handle; } handle;
@ -30,14 +30,6 @@ const char *source_filename_translate(const char *filename) {
} }
int source_init(const char *root_path) { int source_init(const char *root_path) {
handle.mountpoint = malloc(strlen(root_path) + 1);
if (handle.mountpoint == NULL) {
perror("Malloc failed");
return -1;
}
strcpy(handle.mountpoint, root_path);
int root_fd = open(root_path, O_PATH); int root_fd = open(root_path, O_PATH);
if (root_fd == -1) { if (root_fd == -1) {
@ -51,32 +43,6 @@ int source_init(const char *root_path) {
return 0; return 0;
} }
void source_destroy(void) { free(handle.mountpoint); }
const char *get_mountpoint(void) { return handle.mountpoint; }
const char *real_filename(const char *filename) {
const char *mountpoint = get_mountpoint();
// Calculate required length
size_t len1 = strlen(mountpoint);
size_t len2 = strlen(filename);
size_t total_len = len1 + len2;
// Allocate memory (+1 for null terminator)
char *result = malloc(total_len + 1);
if (result == NULL) {
fprintf(stderr, "Memory allocation failed");
perror("");
return NULL;
}
// Copy strings
strcpy(result, mountpoint);
strcat(result, filename);
return result;
}
int source_mkdir(const char *filename, mode_t mode) { int source_mkdir(const char *filename, mode_t mode) {
const char *relative_filename = source_filename_translate(filename); const char *relative_filename = source_filename_translate(filename);
return mkdirat(handle.root_fd, relative_filename, mode); return mkdirat(handle.root_fd, relative_filename, mode);

View File

@ -19,7 +19,6 @@
* @return 0 on success, -1 on failure. * @return 0 on success, -1 on failure.
*/ */
int source_init(const char *root_path); int source_init(const char *root_path);
void source_destroy(void);
/* All of the functions below are designed to behave exactly as their non-source /* All of the functions below are designed to behave exactly as their non-source
* counterparts. */ * counterparts. */

View File

@ -12,7 +12,6 @@
#include <time.h> #include <time.h>
#define _GNU_SOURCE #define _GNU_SOURCE
#include "perm_permissions_table.h" #include "perm_permissions_table.h"
#include "real_filename.h"
#include "temp_permissions_table.h" #include "temp_permissions_table.h"
#include "ui-socket.h" #include "ui-socket.h"
#include <pthread.h> #include <pthread.h>
@ -71,7 +70,7 @@ access_t ask_access(const char *filename, struct process_info proc_info) {
"zenity --question --extra-button \"Allow this time\" --title " "zenity --question --extra-button \"Allow this time\" --title "
"\"Allow Access?\" --text \"Allow process " "\"Allow Access?\" --text \"Allow process "
"<tt>%s</tt> with PID <tt>%d</tt> to access <tt>%s</tt>\"", "<tt>%s</tt> with PID <tt>%d</tt> to access <tt>%s</tt>\"",
proc_info.name, proc_info.PID, filename, get_mountpoint()); proc_info.name, proc_info.PID, filename);
if (ret < 0) { if (ret < 0) {
// If asprintf fails, the contents of command are undefined (see man // If asprintf fails, the contents of command are undefined (see man
@ -127,25 +126,20 @@ access_t ask_access(const char *filename, struct process_info proc_info) {
*/ */
int interactive_access(const char *filename, struct process_info proc_info, int interactive_access(const char *filename, struct process_info proc_info,
int opts) { int opts) {
char *real_path = real_filename(filename);
access_t access = check_temp_access(real_path, proc_info); access_t access = check_temp_access(filename, proc_info);
if (access == ALLOW) { if (access == ALLOW) {
free(real_path);
return 1; return 1;
} }
if (access == DENY) { if (access == DENY) {
free(real_path);
return 0; return 0;
} }
access = check_perm_access(real_path, proc_info); access = check_perm_access(filename, proc_info);
if (access == ALLOW) { if (access == ALLOW) {
free(real_path);
return 1; return 1;
} }
if (access == DENY) { if (access == DENY) {
free(real_path);
return 0; return 0;
} }
@ -153,36 +147,30 @@ int interactive_access(const char *filename, struct process_info proc_info,
// permissions are granted // permissions are granted
if (opts & GRANT_PERM) { if (opts & GRANT_PERM) {
give_perm_access(real_path, proc_info); give_perm_access(filename, proc_info);
free(real_path);
return 1; return 1;
} }
if (opts & GRANT_TEMP) { if (opts & GRANT_TEMP) {
set_temp_access(real_path, proc_info, SET_ALLOW); set_temp_access(filename, proc_info, SET_ALLOW);
free(real_path);
return 1; return 1;
} }
access_t user_response = ask_access(real_path, proc_info); access_t user_response = ask_access(filename, proc_info);
if (user_response == ALLOW) { if (user_response == ALLOW) {
give_perm_access(real_path, proc_info); give_perm_access(filename, proc_info);
free(real_path);
return 1; return 1;
} }
if (user_response == ALLOW_TEMP) { if (user_response == ALLOW_TEMP) {
set_temp_access(real_path, proc_info, SET_ALLOW); set_temp_access(filename, proc_info, SET_ALLOW);
free(real_path);
return 1; return 1;
} }
if (user_response == DENY) { if (user_response == DENY) {
set_temp_access(real_path, proc_info, SET_DENY); set_temp_access(filename, proc_info, SET_DENY);
free(real_path);
return 0; return 0;
} }
free(real_path);
// deny on unknown options. // deny on unknown options.
return 0; return 0;
} }