psss / rpms / libselinux

Forked from rpms/libselinux 5 years ago
Clone
aba7ab1
#include <stdio.h>
aba7ab1
#include <stdlib.h>
aba7ab1
#include <string.h>
aba7ab1
#include <selinux/selinux.h>
aba7ab1
#include <selinux/context.h>
aba7ab1
#include <selinux/label.h>
aba7ab1
#include <selinux/restorecon.h>
aba7ab1
aba7ab1
int main(int argc, char **argv) {
aba7ab1
    struct selabel_handle *hndl = NULL;
aba7ab1
    char *path = NULL;
aba7ab1
    unsigned int flags = 0;
aba7ab1
aba7ab1
    if (argc < 3) {
aba7ab1
        fprintf(stderr, "Invalid number of arguments\n");
aba7ab1
        return 1;
aba7ab1
    }
aba7ab1
aba7ab1
    // set restorecon path
aba7ab1
    if (strcmp(argv[2], "EMPTY") == 0) {
aba7ab1
        path = "";
aba7ab1
    }
aba7ab1
    else if (strcmp(argv[2], "NULL") == 0) {
aba7ab1
        path = NULL;
aba7ab1
    }
aba7ab1
    else {
aba7ab1
        path = argv[2];
aba7ab1
    }
aba7ab1
aba7ab1
    // set restorecon flags
aba7ab1
    flags |= SELINUX_RESTORECON_RECURSE;
aba7ab1
    flags |= SELINUX_RESTORECON_IGNORE_DIGEST;
aba7ab1
aba7ab1
    // set sehandle
aba7ab1
    if (strcmp(argv[1], "DEFAULT") == 0) {
aba7ab1
        hndl = selinux_restorecon_default_handle();
aba7ab1
aba7ab1
        if (hndl == NULL) {
aba7ab1
            return 1;
aba7ab1
        }
aba7ab1
    }
aba7ab1
    else if (strcmp(argv[1], "INVALID") == 0) {
aba7ab1
        hndl = (struct selabel_handle *) 1;
aba7ab1
    }
aba7ab1
    else if (strcmp(argv[1], "NULL") == 0) {
aba7ab1
        hndl = NULL;
aba7ab1
    }
aba7ab1
    else if (strcmp(argv[1], "CUSTOM") == 0) {
aba7ab1
        struct selinux_opt options[] = {
aba7ab1
            { SELABEL_OPT_DIGEST, (char *)1 },
aba7ab1
            { SELABEL_OPT_BASEONLY, (char *)1 }
aba7ab1
        };
aba7ab1
aba7ab1
        hndl = selabel_open(SELABEL_CTX_FILE, options, 2);
aba7ab1
    }
aba7ab1
aba7ab1
    printf("Running selinux_restorecon_set_sehandle();\n");
aba7ab1
aba7ab1
    selinux_restorecon_set_sehandle(hndl);
aba7ab1
aba7ab1
    printf("Running selinux_restorecon(\"%s\", %#08x);\n", path, flags);
aba7ab1
aba7ab1
    return selinux_restorecon(path, flags);
aba7ab1
}