如何允许以非 root 用户身份安装 LUKS 卷?

如何允许以非 root 用户身份安装 LUKS 卷?

在图形文件管理器中单击 LUKS 加密卷时,我必须首先输入加密密码,然后输入用户密码才能完成安装。如何允许在窗口管理器中安装 LUKS 卷一次(不是在启动时)而不将权限提升到root?也就是说,无需输入我的登录密码。

答案1

你可以尝试一个帮助程序。就像是:

#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Configuration parameters.  Change to fit your site.
   Some of these must be string constants.  String pasting is used. */
#define UUID "b3b7bd7b-4f00-4848-8af9-33a56f0b8a97"
#define NAME "cryptdisk"
#define MOUNTPOINT "/mnt/1"
/* The next line is optional.  If specified, the user won't even be
   prompted for the encrypted disk password. */
/* #define KEY_FILE "/root/disk_key_file" */


/* Replacement environment, for safety */
char * replacement_environment[] = {
    "PATH=/bin:/usr/bin:/sbin:/usr/sbin",
    "HOME=/root",
    "TERM=dumb",
    0
};

void run(char *exec, ...)
{
    #define MAXARGS 20
    char *args[MAXARGS];
    char *arg;
    int count = 0;
    va_list ap;
    
    args[count++] = exec;
    va_start(ap, exec);
    while ((count < MAXARGS) && (arg = va_arg(ap, char *))) {
        args[count++] = arg;
    }
    if (count >= MAXARGS) {
        fprintf(stderr, "Too many arguments\n");
        exit(127);
    }
    args[count++] = 0;

    pid_t pid = fork();
    switch (pid) {
        case -1:
            perror("fork error");
            exit(127);

        case 0:
            execve(exec, args, replacement_environment);
            perror("exec error");
            exit(127);

        default:
        {
            pid_t wpid;
            int status;
            for (;;) {
                wpid = wait(&status);
                if (wpid == -1) {
                    perror("wait error");
                    exit(127);
                }
                if (pid == wpid) break;
            }
            if (status != 0) {
                if (WIFSIGNALED(status)) 
                    fprintf(stderr, "Child %s terminated with signal %d\n", exec, WTERMSIG(status));
                else if (WIFEXITED(status))
                    fprintf(stderr, "Child %s exited with status %d\n", exec, WEXITSTATUS(status));
                else
                    fprintf(stderr, "Child %s exited with result %d\n", exec, status);
                exit(126);
            }
            break;
        }
    }
}

void do_mount(void)
{
    setreuid(0,0);
#ifdef KEY_FILE
    run("/sbin/cryptsetup", "--key-file", KEY_FILE, "luksOpen", "UUID="UUID, NAME, NULL);
#else
    run("/sbin/cryptsetup", "luksOpen", "UUID="UUID, NAME, NULL);
#endif
    run("/bin/mount", "/dev/mapper/"NAME, MOUNTPOINT, NULL);
}

void do_umount(void)
{
    setreuid(0,0);
    run("/bin/umount", MOUNTPOINT, 0);
    run("/sbin/cryptsetup", "luksClose", NAME, 0);
}

int main(int argc, char **argv)
{
    char const *prog = argv[0];
    char const *last_slash = strrchr(prog, '/');
    if (last_slash) prog = last_slash+1;
    if (prog[0] == 'u') 
        do_umount();
    else
        do_mount();
    exit(0);
}

您将需要更改顶部附近的配置值。如果您需要进一步调整 cryptsetup 参数,那应该是相当明显的。至于如何将其与“ Thunar”联系起来,我无法帮助你。

构建类似的东西:

gcc -o mount_it mount_it.c
sudo chown root mount_it
sudo chmod u+s mount_it
ln -s mount_it umount_it

相关内容