Linux Permission Denied

so i clone the target directory and create 4 users on Linux with usernames according to the contents of the folder in the target directory.

User Folder
andi andi
budi budi
cony cony
deni deni
Folder ownership table

the task is to make it so that each user can’t add, change, or delete folders or files in folders they don’t own. All the files inside of a folder will be encoded if accessed by a user who is not the owner of that folder. (using Base64 encoding). On the contrary, all files will be decoded if they are accessed by the user who owns that folder.

so my code is like this

#define FUSE_USE_VERSION 28
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/time.h>
#include <pwd.h>
#include <stdlib.h>
#include <stddef.h>

static const char *target_dir = "/mnt/d/task-2/target";
static const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

char *get_current_user() {
    uid_t uid = geteuid();
    struct passwd *pw = getpwuid(uid);
    if (pw) {
        return pw->pw_name;
    }
    return NULL;
}

void base64_encode(const unsigned char *src, size_t len, char *out) {
    int i, j;
    for (i = 0, j = 0; i < len;) {
        uint32_t octet_a = i < len ? (unsigned char)src[i++] : 0;
        uint32_t octet_b = i < len ? (unsigned char)src[i++] : 0;
        uint32_t octet_c = i < len ? (unsigned char)src[i++] : 0;

        uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

        out[j++] = base64_table[(triple >> 3 * 6) & 0x3F];
        out[j++] = base64_table[(triple >> 2 * 6) & 0x3F];
        out[j++] = base64_table[(triple >> 1 * 6) & 0x3F];
        out[j++] = base64_table[(triple >> 0 * 6) & 0x3F];
    }
    for (int pad = len % 3; pad && pad < 3; pad++) {
        out[--j] = '=';
    }
    out[j] = '';
}

int base64_decode(const char *src, unsigned char *out, size_t *out_len) {
    size_t len = strlen(src);
    if (len % 4) return -1;

    size_t output_len = len / 4 * 3;
    if (src[len - 1] == '=') output_len--;
    if (src[len - 2] == '=') output_len--;

    *out_len = output_len;
    for (size_t i = 0, j = 0; i < len;) {
        uint32_t sextet_a = src[i] == '=' ? 0 & i++ : strchr(base64_table, src[i++]) - base64_table;
        uint32_t sextet_b = src[i] == '=' ? 0 & i++ : strchr(base64_table, src[i++]) - base64_table;
        uint32_t sextet_c = src[i] == '=' ? 0 & i++ : strchr(base64_table, src[i++]) - base64_table;
        uint32_t sextet_d = src[i] == '=' ? 0 & i++ : strchr(base64_table, src[i++]) - base64_table;

        uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);

        if (j < output_len) out[j++] = (triple >> 2 * 8) & 0xFF;
        if (j < output_len) out[j++] = (triple >> 1 * 8) & 0xFF;
        if (j < output_len) out[j++] = (triple >> 0 * 8) & 0xFF;
    }
    return 0;
}

int check_ownership(const char *path, const char *current_user) {
    const char *last_slash = strrchr(path, '/');
    if (last_slash == NULL) {
        return 0;
    }
    const char *subfolder_name = last_slash + 1;

    if (strcmp(subfolder_name, current_user) == 0) {
        return 1;
    } else {
        return 0;
    }
}



static int xmp_getattr(const char *path, struct stat *stbuf) {
    int res;
    char fpath[1000];
    sprintf(fpath, "%s%s", target_dir, path);
    res = lstat(fpath, stbuf);
    if (res == -1) return -errno;
    return 0;
}

// Membaca direktori
static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
    char fpath[1000];
    if (strcmp(path, "/") == 0) {
        path = target_dir;
        sprintf(fpath, "%s", path);
    } else {
        sprintf(fpath, "%s%s", target_dir, path);
    }

    int res = 0;
    DIR *dp;
    struct dirent *de;
    (void) offset;
    (void) fi;
    dp = opendir(fpath);
    if (dp == NULL) return -errno;
    while ((de = readdir(dp)) != NULL) {
        struct stat st;
        memset(&st, 0, sizeof(st));
        st.st_ino = de->d_ino;
        st.st_mode = de->d_type << 12;
        res = (filler(buf, de->d_name, &st, 0));
        if (res != 0) break;
    }
    closedir(dp);
    return 0;
}

static int xmp_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
    char fpath[1000];
    sprintf(fpath, "%s%s", target_dir, path);

    int fd;
    int res;
    (void) fi;

    fd = open(fpath, O_RDONLY);
    if (fd == -1) return -errno;

    res = pread(fd, buf, size, offset);
    if (res == -1) res = -errno;
    close(fd);

    if (!check_ownership(path, get_current_user())) {
        char encoded_buf[4 * ((res + 2) / 3) + 1];
        base64_encode((unsigned char *)buf, res, encoded_buf);
        memcpy(buf, encoded_buf, strlen(encoded_buf) + 1);
        res = strlen(buf);
    }
    return res;
}

static int xmp_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
    char fpath[1000];
    sprintf(fpath, "%s%s", target_dir, path);

    if (!check_ownership(path, get_current_user())) {
        return -EACCES;
    }

    int fd;
    int res;
    (void) fi;
    fd = open(fpath, O_WRONLY);
    if (fd == -1) return -errno;

    res = pwrite(fd, buf, size, offset);
    if (res == -1) res = -errno;
    close(fd);
    return res;
}

// Membuat file node
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
    char fpath[1000];
    sprintf(fpath, "%s%s", target_dir, path);

    // Jika bukan pemilik folder, tolak akses
    if (!check_ownership(path, get_current_user())) {
        return -EACCES;
    }

    int res;
    if (S_ISREG(mode)) {
        res = open(fpath, O_CREAT | O_EXCL | O_WRONLY, mode);
        if (res >= 0) res = close(res);
    } else if (S_ISFIFO(mode)) {
        res = mkfifo(fpath, mode);
    } else {
        res = mknod(fpath, mode, rdev);
    }
    if (res == -1) return -errno;
    return 0;
}

// Membuat direktori
static int xmp_mkdir(const char *path, mode_t mode) {
    char fpath[1000];
    sprintf(fpath, "%s%s", target_dir, path);

    // Jika bukan pemilik folder, tolak akses
    if (!check_ownership(path, get_current_user())) {
        return -EACCES;
    }

    int res = mkdir(fpath, mode);
    if (res == -1) return -errno;
    return 0;
}

// Menghapus file
static int xmp_unlink(const char *path) {
    char fpath[1000];
    sprintf(fpath, "%s%s", target_dir, path);

    // Jika bukan pemilik folder, tolak akses
    if (!check_ownership(path, get_current_user())) {
        return -EACCES;
    }

    int res = unlink(fpath);
    if (res == -1) return -errno;
    return 0;
}

// Menghapus direktori
static int xmp_rmdir(const char *path) {
    char fpath[1000];
    sprintf(fpath, "%s%s", target_dir, path);

    // Jika bukan pemilik folder, tolak akses
    if (!check_ownership(path, get_current_user())) {
        return -EACCES;
    }

    int res = rmdir(fpath);
    if (res == -1) return -errno;
    return 0;
}

static int xmp_rename(const char *from, const char *to) {
    char ffrom[1000], fto[1000];
    sprintf(ffrom, "%s%s", target_dir, from);
    sprintf(fto, "%s%s", target_dir, to);

    // Jika bukan pemilik folder, tolak akses
    if (!check_ownership(from, get_current_user()) || !check_ownership(to, get_current_user())) {
        return -EACCES;
    }

    int res = rename(ffrom, fto);
    if (res == -1) return -errno;
    return 0;
}

static struct fuse_operations xmp_oper = {
    .getattr    = xmp_getattr,
    .readdir    = xmp_readdir,
    .read       = xmp_read,
    .write      = xmp_write,
    .mknod      = xmp_mknod,
    .mkdir      = xmp_mkdir,
    .unlink     = xmp_unlink,
    .rmdir      = xmp_rmdir,
    .rename     = xmp_rename,
};

int main(int argc, char *argv[]) {
    umask(0);
    return fuse_main(argc, argv, &xmp_oper, NULL);
}

then i run with
./fuse -o allow_other /mnt/d/task-2/mount
i run that in task-2 directory because the fuse.c is in my task-2 directory

then i do cd mount and i do su andi, and i do cd andi,
when i do mkdir test

it says
mkdir: cannot create directory ‘test’: Permission denied
why? according to the check_ownership() the subfolder_name is andi because right now im in directory called andi and the user i login is now andi too, it should given me access to mkdir on andi directory

New contributor

Ryos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật