Compare commits

...

7 Commits

9 changed files with 212 additions and 43 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
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
$(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h
$(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h $(SOURCES_DIR)/real_filename.h
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
$(BUILD_DIR)/ui-socket.o: $(SOURCES_DIR)/ui-socket.c $(SOURCES_DIR)/ui-socket.h

View File

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

View File

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

View File

@ -1,41 +1,153 @@
#include "gio/gio.h"
#include "glib-object.h"
#include "glib.h"
#include <adwaita.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) {
// Create the main window
AdwMessageDialog *dialog = ADW_MESSAGE_DIALOG(
adw_message_dialog_new(NULL, "Allow access?", "allow_access?"));
AdwWindow *window = ADW_WINDOW(adw_window_new());
gtk_window_set_application(GTK_WINDOW(window), app);
gtk_window_set_title(GTK_WINDOW(window), "icfs");
// gtk_window_set_default_size(GTK_WINDOW(window), 300, 150);
// Set the dialog as transient for the (non-existent) parent window
gtk_window_set_application(GTK_WINDOW(dialog), app);
AdwStatusPage *content = ADW_STATUS_PAGE(adw_status_page_new());
adw_status_page_set_title(content, "Allow access?");
// Add response buttons
adw_message_dialog_add_response(dialog, "cancel", "Cancel");
adw_message_dialog_add_response(dialog, "ok", "OK");
char *description = NULL;
asprintf(
&description,
"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);
// Set default response
adw_message_dialog_set_default_response(dialog, "ok");
entry_buffer = gtk_entry_buffer_new(
g_object_get_data(G_OBJECT(app), "access_dir"),
strlen(g_object_get_data(G_OBJECT(app), "access_dir")));
// Set close response (when clicking X)
adw_message_dialog_set_close_response(dialog, "cancel");
GtkWidget *entry = gtk_entry_new();
gtk_entry_set_buffer(GTK_ENTRY(entry), entry_buffer);
gtk_entry_set_placeholder_text(GTK_ENTRY(entry), "Enter filename");
gtk_widget_set_hexpand(entry, TRUE);
// Connect response handler
// g_signal_connect(dialog, "response", G_CALLBACK(gtk_window_close), dialog);
// Create a prefix label and box
GtkWidget *entry_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
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
gtk_window_present(GTK_WINDOW(dialog));
adw_window_set_content(window, box);
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) {
// Create a new application
AdwApplication *app = adw_application_new("com.example.zenityclone",
G_APPLICATION_DEFAULT_FLAGS);
G_APPLICATION_HANDLES_COMMAND_LINE);
g_signal_connect(app, "command-line", G_CALLBACK(on_command_line), NULL);
g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
// Run the application
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
return (status == 0) ? exit_code : status;
}

View File

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

7
src/real_filename.h Normal file
View File

@ -0,0 +1,7 @@
#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 <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static struct source_files_handle {
const char *mountpoint;
int root_fd;
} handle;
@ -30,6 +30,14 @@ const char *source_filename_translate(const char *filename) {
}
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);
if (root_fd == -1) {
@ -43,6 +51,32 @@ int source_init(const char *root_path) {
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) {
const char *relative_filename = source_filename_translate(filename);
return mkdirat(handle.root_fd, relative_filename, mode);

View File

@ -19,6 +19,7 @@
* @return 0 on success, -1 on failure.
*/
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
* counterparts. */

View File

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