7 Commits

Author SHA1 Message Date
e0b69cfea1 Improved ui socket and made open and create send requests. 2024-12-25 17:07:52 +01:00
3cbe520916 Edited Makefile to compite ui socket. 2024-12-25 17:06:56 +01:00
e2014f03f1 Basic socket communication 2024-12-25 11:00:35 +01:00
dadcc6476b Added the ui-socket.h.
Two new issues to solve:

* Should the ui communication component also be the one that manages
  permissions?
* The format of data sent (protocol) needs definition.
2024-12-20 08:48:21 +01:00
1646b2fe3f Resolved merge conflict in favor of basic-passthrough 2024-12-17 10:29:56 +01:00
bfc22c79e0 Implemented the passthrough.
Passthrough is usable now. There have been issues with the `access`
operation: it's unclear what it must return, since the answer isn't
known at the time when it is called. If it always returns "denied", many
applications would finish without trying to open a file, thinking the
access would not be granted after `access` call. Although always
returning "permitted" seems like a better choice, it still might cause unexpected
behaviour. Perhaps one way to solve this, is actually asking user
whether to allow access. In any case, this issue needs to be looked
into.
2024-12-17 10:11:59 +01:00
ff6a8713d3 Removed useless comments from main.c. 2024-11-20 10:35:58 +01:00
6 changed files with 258 additions and 20 deletions

View File

@@ -22,7 +22,7 @@ endif
SOURCES_DIR := ./sources
BUILD_DIR := ./build
build: $(BUILD_DIR)/main.o $(BUILD_DIR)/sourcefs.o
build: $(BUILD_DIR)/main.o $(BUILD_DIR)/sourcefs.o $(BUILD_DIR)/ui-socket.o
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $(BUILD_DIR)/icfs
$(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c
@@ -31,5 +31,8 @@ $(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c
$(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
$(BUILD_DIR)/ui-socket.o: $(SOURCES_DIR)/ui-socket.c $(SOURCES_DIR)/ui-socket.h
$(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@
clean:
rm $(BUILD_DIR)/*

View File

@@ -49,6 +49,7 @@
#include <sys/file.h> /* flock(2) */
#include "sourcefs.h"
#include "ui-socket.h"
const char *mountpoint = NULL;
@@ -217,6 +218,7 @@ static int xmp_releasedir(const char *path, struct fuse_file_info *fi) {
return 0;
}
/*
// TODO: make this work
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
int res;
@@ -230,6 +232,7 @@ static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
return 0;
}
*/
static int xmp_mkdir(const char *path, mode_t mode) {
int res;
@@ -288,7 +291,7 @@ static int xmp_rename(const char *from, const char *to, unsigned int flags) {
static int xmp_link(const char *from, const char *to) {
int res;
res = link(from, to);
res = source_link(from, to);
if (res == -1)
return -errno;
@@ -301,7 +304,7 @@ static int xmp_chmod(const char *path, mode_t mode, struct fuse_file_info *fi) {
if (fi)
res = fchmod(fi->fh, mode);
else
res = chmod(path, mode);
res = source_chmod(path, mode);
if (res == -1)
return -errno;
@@ -315,7 +318,7 @@ static int xmp_chown(const char *path, uid_t uid, gid_t gid,
if (fi)
res = fchown(fi->fh, uid, gid);
else
res = lchown(path, uid, gid);
res = source_chown(path, uid, gid);
if (res == -1)
return -errno;
@@ -329,7 +332,7 @@ static int xmp_truncate(const char *path, off_t size,
if (fi)
res = ftruncate(fi->fh, size);
else
res = truncate(path, size);
res = source_truncate(path, size);
if (res == -1)
return -errno;
@@ -358,7 +361,17 @@ static int xmp_create(const char *path, mode_t mode,
struct fuse_file_info *fi) {
int fd;
fd = open(path, fi->flags, mode);
struct process_info pi;
struct fuse_context *fc = fuse_get_context();
pi.PID = fc->pid;
pi.UID = fc->uid;
pi.name = "";
printf("%d", ask_access(path, pi));
fd = source_create(path, fi->flags, mode);
if (fd == -1)
return -errno;
@@ -369,7 +382,17 @@ static int xmp_create(const char *path, mode_t mode,
static int xmp_open(const char *path, struct fuse_file_info *fi) {
int fd;
fd = open(path, fi->flags);
struct process_info pi;
struct fuse_context *fc = fuse_get_context();
pi.PID = fc->pid;
pi.UID = fc->uid;
pi.name = "";
printf("%d", ask_access(path, pi));
fd = source_open(path, fi->flags);
if (fd == -1)
return -errno;
@@ -386,6 +409,7 @@ static int xmp_open(const char *path, struct fuse_file_info *fi) {
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {
int res;
@@ -398,6 +422,7 @@ static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
return res;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_read_buf(const char *path, struct fuse_bufvec **bufp,
size_t size, off_t offset, struct fuse_file_info *fi) {
struct fuse_bufvec *src;
@@ -419,6 +444,7 @@ static int xmp_read_buf(const char *path, struct fuse_bufvec **bufp,
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi) {
int res;
@@ -431,6 +457,7 @@ static int xmp_write(const char *path, const char *buf, size_t size,
return res;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_write_buf(const char *path, struct fuse_bufvec *buf,
off_t offset, struct fuse_file_info *fi) {
struct fuse_bufvec dst = FUSE_BUFVEC_INIT(fuse_buf_size(buf));
@@ -454,6 +481,7 @@ static int xmp_statfs(const char *path, struct statvfs *stbuf) {
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_flush(const char *path, struct fuse_file_info *fi) {
int res;
@@ -470,6 +498,7 @@ static int xmp_flush(const char *path, struct fuse_file_info *fi) {
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_release(const char *path, struct fuse_file_info *fi) {
(void)path;
close(fi->fh);
@@ -477,6 +506,7 @@ static int xmp_release(const char *path, struct fuse_file_info *fi) {
return 0;
}
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi) {
int res;
@@ -551,6 +581,7 @@ static int xmp_lock(const char *path, struct fuse_file_info *fi, int cmd,
}
#endif
/* Complete copy of the example method(no need to modify anything so far) */
static int xmp_flock(const char *path, struct fuse_file_info *fi, int op) {
int res;
(void)path;
@@ -580,6 +611,7 @@ static ssize_t xmp_copy_file_range(const char *path_in,
}
#endif
/* Complete copy of the example method(no need to modify anything so far) */
static off_t xmp_lseek(const char *path, off_t off, int whence,
struct fuse_file_info *fi) {
off_t res;
@@ -592,15 +624,16 @@ static off_t xmp_lseek(const char *path, off_t off, int whence,
return res;
}
// TODO: look trough "optional"(commented out) operations.
static const struct fuse_operations xmp_oper = {
.init = xmp_init,
.getattr = xmp_getattr,
.access = xmp_access,
// .access = xmp_access,
.readlink = xmp_readlink,
.opendir = xmp_opendir,
.readdir = xmp_readdir,
.releasedir = xmp_releasedir,
.mknod = xmp_mknod,
// .mknod = xmp_mknod,
.mkdir = xmp_mkdir,
.symlink = xmp_symlink,
.unlink = xmp_unlink,
@@ -611,7 +644,7 @@ static const struct fuse_operations xmp_oper = {
.chown = xmp_chown,
.truncate = xmp_truncate,
#ifdef HAVE_UTIMENSAT
.utimens = xmp_utimens,
// .utimens = xmp_utimens,
#endif
.create = xmp_create,
.open = xmp_open,
@@ -624,20 +657,20 @@ static const struct fuse_operations xmp_oper = {
.release = xmp_release,
.fsync = xmp_fsync,
#ifdef HAVE_POSIX_FALLOCATE
.fallocate = xmp_fallocate,
// .fallocate = xmp_fallocate,
#endif
#ifdef HAVE_SETXATTR
.setxattr = xmp_setxattr,
.getxattr = xmp_getxattr,
.listxattr = xmp_listxattr,
.removexattr = xmp_removexattr,
// .setxattr = xmp_setxattr,
// .getxattr = xmp_getxattr,
// .listxattr = xmp_listxattr,
// .removexattr = xmp_removexattr,
#endif
#ifdef HAVE_LIBULOCKMGR
.lock = xmp_lock,
// .lock = xmp_lock,
#endif
.flock = xmp_flock,
#ifdef HAVE_COPY_FILE_RANGE
.copy_file_range = xmp_copy_file_range,
// .copy_file_range = xmp_copy_file_range,
#endif
.lseek = xmp_lseek,
};
@@ -651,7 +684,13 @@ int main(int argc, char *argv[]) {
int ret = source_init(mountpoint);
if (ret != 0) {
perror("Failed to initialize filesystem.");
perror("source_init");
exit(EXIT_FAILURE);
}
ret = init_ui_socket("/home/fedir/.icfs-sock");
if (ret != 0) {
perror("init_ui_socket");
exit(EXIT_FAILURE);
}

View File

@@ -70,6 +70,47 @@ DIR *source_opendir(const char *filename) {
}
int source_rename(const char *oldpath, const char *newpath) {
printf("{\"%s\", \"%s\"}\n", oldpath, newpath);
return -1;
const char *relative_oldpath = source_fname_translate(oldpath);
const char *relative_newpath = source_fname_translate(newpath);
return renameat(handle.root_fd, relative_oldpath, handle.root_fd,
relative_newpath);
}
int source_link(const char *oldpath, const char *newpath) {
const char *relative_oldpath = source_fname_translate(oldpath);
const char *relative_newpath = source_fname_translate(newpath);
return linkat(handle.root_fd, relative_oldpath, handle.root_fd,
relative_newpath, 0);
// NOTE: perhaps the flags here need to be reevaluated.
}
int source_chmod(const char *filename, mode_t mode) {
const char *relative_filename = source_fname_translate(filename);
return fchmodat(handle.root_fd, relative_filename, mode, 0);
// NOTE: perhaps the flags here need to be reevaluated.
}
int source_chown(const char *filename, uid_t owner, gid_t group) {
const char *relative_filename = source_fname_translate(filename);
return fchownat(handle.root_fd, filename, owner, group, AT_SYMLINK_NOFOLLOW);
}
int source_truncate(const char *filename, off_t length) {
const char *relative_filename = source_fname_translate(filename);
int fd = openat(handle.root_fd, relative_filename, NULL);
if (fd < 0) {
perror("Openat failed");
return -1;
}
return ftruncate(fd, length);
}
int source_open(const char *filename, int flags) {
const char *relative_filename = source_fname_translate(filename);
return openat(handle.root_fd, relative_filename, flags);
}
int source_create(const char *filename, int flags, mode_t mode) {
const char *relative_filename = source_fname_translate(filename);
return openat(handle.root_fd, relative_filename, flags, mode);
}

View File

@@ -33,4 +33,19 @@ int source_symlink(const char *target, const char *linkpath);
int source_rename(const char *oldpath, const char *newpath);
int source_link(const char *oldpath, const char *newpath);
int source_chmod(const char *filename, mode_t mode);
int source_chown(const char *filename, uid_t owner, gid_t group);
int source_truncate(const char *filename, off_t length);
/* `open` and `create` are designed to correspond to fuse operations, not the
* libc's `open(2)`. Both of them actually call `openat`. */
int source_open(const char *filename, int flags);
int source_create(const char *filename, int flags, mode_t mode);
#endif // !SOURCEFS_H

116
sources/ui-socket.c Normal file
View File

@@ -0,0 +1,116 @@
#define _GNU_SOURCE
#include "ui-socket.h"
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define MAX_MESSAGE_SIZE 1024
// Mutex for thread safety
static pthread_mutex_t socket_mutex = PTHREAD_MUTEX_INITIALIZER;
static int ui_socket_fd = -1; // Global socket file descriptor
int init_ui_socket(const char *filename) {
if (!filename) {
return -1;
}
// Create the socket
ui_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (ui_socket_fd == -1) {
perror("socket");
return -1;
}
// Remove the socket file if it already exists
if (unlink(filename) == -1 &&
errno != ENOENT) { // ENOENT means the file does not exist, which is fine
perror("unlink");
close(ui_socket_fd);
ui_socket_fd = -1;
return -1;
}
// Set up the socket address structure
struct sockaddr_un addr;
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1);
// Bind the socket
if (bind(ui_socket_fd, (struct sockaddr *)&addr,
sizeof(struct sockaddr_un)) == -1) {
perror("bind");
close(ui_socket_fd);
ui_socket_fd = -1;
return -1;
}
// Listen for incoming connections
if (listen(ui_socket_fd, 5) == -1) {
perror("listen");
close(ui_socket_fd);
ui_socket_fd = -1;
return -1;
}
return 0;
}
int ask_access(const char *filename, struct process_info pi) {
if (!filename || ui_socket_fd == -1) {
return -1;
}
// Lock the mutex for thread safety
pthread_mutex_lock(&socket_mutex);
int client_fd = accept(ui_socket_fd, NULL, NULL);
if (client_fd == -1) {
perror("accept");
pthread_mutex_unlock(&socket_mutex);
return -1;
}
// Prepare the message to send to the GUI
char message[MAX_MESSAGE_SIZE];
int len_filename = strlen(filename);
int len_name = strlen(pi.name);
snprintf(message, sizeof(message), "r%04d%s%04d%04d%s%04d", len_filename,
filename, pi.PID, len_name, pi.name, pi.UID);
// Send the message to the GUI
if (send(client_fd, message, strlen(message), 0) == -1) {
perror("send");
close(client_fd);
pthread_mutex_unlock(&socket_mutex);
return -1;
}
// Receive the response from the GUI
char response[2];
if (recv(client_fd, response, sizeof(response), 0) == -1) {
perror("recv");
close(client_fd);
pthread_mutex_unlock(&socket_mutex);
return -1;
}
close(client_fd);
pthread_mutex_unlock(&socket_mutex);
// Process the response
if (response[0] == 'a' && response[1] == 'y') {
return 0; // Access granted
} else if (response[0] == 'a' && response[1] == 'n') {
return 1; // Access denied
}
return -1; // Invalid response
}

24
sources/ui-socket.h Normal file
View File

@@ -0,0 +1,24 @@
/*
* Interface for controlling communication with the UI.
*/
#ifndef UI_SOCKET_H
#define UI_SOCKET_H
#include <sys/types.h>
struct process_info {
pid_t PID;
const char *name;
uid_t UID;
};
// For default socket location, set socket_path = NULL.
int init_ui_socket(const char *socket_path);
// TODO: design an interface for asking user for permission.
int ask_access(const char *filename, struct process_info pi);
#endif // !UI_SOCKET_H