aba7ab1
#include <stdio.h>
aba7ab1
#include <stdlib.h>
aba7ab1
#include <string.h>
aba7ab1
#include <stdint.h>
aba7ab1
#include <errno.h>
aba7ab1
#include <selinux/selinux.h>
aba7ab1
#include <selinux/label.h>
aba7ab1
aba7ab1
int main (int argc, char **argv)
aba7ab1
{
aba7ab1
    struct selabel_handle *hnd = NULL;
aba7ab1
    unsigned int backend = 0;
aba7ab1
aba7ab1
    struct selinux_opt selabel_option [] = {
aba7ab1
        { SELABEL_OPT_PATH, NULL },
aba7ab1
        { SELABEL_OPT_SUBSET, NULL },
aba7ab1
        { SELABEL_OPT_VALIDATE, (char *) 1 },
aba7ab1
        { SELABEL_OPT_BASEONLY, (char *) 1 }
aba7ab1
    };
aba7ab1
aba7ab1
    if (argc < 8) {
aba7ab1
        fprintf(stderr, "Invalid number of arguments\n");
aba7ab1
        return 255;
aba7ab1
    }
aba7ab1
aba7ab1
    // set backend
aba7ab1
    if (strcmp(argv[1], "CTX_FILE") == 0)
aba7ab1
        backend = SELABEL_CTX_FILE;
aba7ab1
    else if (strcmp(argv[1], "CTX_MEDIA") == 0)
aba7ab1
        backend = SELABEL_CTX_MEDIA;
aba7ab1
    else if (strcmp(argv[1], "CTX_X") == 0)
aba7ab1
        backend = SELABEL_CTX_X;
aba7ab1
    else if (strcmp(argv[1], "CTX_DB") == 0)
aba7ab1
        backend = SELABEL_CTX_DB;
aba7ab1
#ifndef RHEL6
aba7ab1
    else if (strcmp(argv[1], "CTX_ANDROID_PROP") == 0)
aba7ab1
        backend = SELABEL_CTX_ANDROID_PROP;
aba7ab1
#endif
aba7ab1
    else
aba7ab1
        backend = strtoul(argv[1], NULL, 10);
aba7ab1
aba7ab1
    if ((argc == 9) && (strcmp(argv[8], "nohandle") == 0)) {
aba7ab1
        hnd = NULL;
aba7ab1
    }
aba7ab1
    else {
aba7ab1
        // set file contexts path
aba7ab1
        if (strcmp(argv[2], "NULL") == 0) {
aba7ab1
            selabel_option[0].value = NULL;
aba7ab1
        }
aba7ab1
        else {
aba7ab1
            selabel_option[0].value = argv[2];
aba7ab1
        }
aba7ab1
aba7ab1
        // set subset
aba7ab1
        if (strcmp(argv[3], "NULL") == 0) {
aba7ab1
            selabel_option[1].value = NULL;
aba7ab1
        }
aba7ab1
        else {
aba7ab1
            selabel_option[1].value = argv[3];
aba7ab1
        }
aba7ab1
aba7ab1
        // set validate
aba7ab1
        if (strcmp(argv[4], "0") == 0) {
aba7ab1
            selabel_option[2].value = NULL;
aba7ab1
        }
aba7ab1
        else {
aba7ab1
            selabel_option[2].value = (char *) 1;
aba7ab1
        }
aba7ab1
aba7ab1
        // set baseonly
aba7ab1
        if (strcmp(argv[5], "0") == 0) {
aba7ab1
            selabel_option[3].value = NULL;
aba7ab1
        }
aba7ab1
        else {
aba7ab1
            selabel_option[3].value = (char *) 1;
aba7ab1
        }
aba7ab1
aba7ab1
        printf("selabel_options: "); 
aba7ab1
        printf("SELABEL_OPT_PATH = %s, ", selabel_option[0].value);
aba7ab1
        printf("SELABEL_OPT_SUBSET = %s, ", selabel_option[1].value);
aba7ab1
        printf("SELABEL_OPT_VALIDATE = %ld, ", (long int)(intptr_t) selabel_option[2].value);
aba7ab1
        printf("SELABEL_OPT_BASEONLY = %ld\n", (long int)(intptr_t) selabel_option[3].value);
aba7ab1
aba7ab1
        printf("Executing: selabel_open(SELABEL_%s, &selabel_option, 4)\n", argv[1]);
aba7ab1
aba7ab1
        errno = 0;
aba7ab1
aba7ab1
        if ((hnd = selabel_open(backend, selabel_option, 4)) == NULL) {
aba7ab1
            perror("selabel_open - ERROR");
aba7ab1
            return 255;
aba7ab1
        }
aba7ab1
    }
aba7ab1
aba7ab1
    int result;
aba7ab1
    security_context_t selabel_context;
aba7ab1
    char *path;
aba7ab1
aba7ab1
    if (strcmp(argv[6], "NULL") == 0) {
aba7ab1
        path = NULL;
aba7ab1
    }
aba7ab1
    else {
aba7ab1
        path = argv[6];
aba7ab1
    }
aba7ab1
aba7ab1
    // notice the base 8
aba7ab1
    int mode = strtol(argv[7], NULL, 8);
aba7ab1
aba7ab1
    int alias_cnt = argc-8;
aba7ab1
    const char **aliases = malloc((alias_cnt + 1)*sizeof(const char *));
aba7ab1
aba7ab1
    if (aliases == NULL)
aba7ab1
        return 255;
aba7ab1
aba7ab1
    printf("aliases:");
aba7ab1
aba7ab1
    for (int i = 0; i < alias_cnt; i++) {
aba7ab1
        aliases[i] = argv[8 + i];
aba7ab1
        printf(" %s", argv[8 + i]);
aba7ab1
    }
aba7ab1
aba7ab1
    printf("\n");
aba7ab1
aba7ab1
    aliases[alias_cnt] = NULL;
aba7ab1
aba7ab1
    printf("Executing: selabel_lookup_best_match(hnd, &selabel_context, %s, aliases, %d)\n", path, mode);
aba7ab1
aba7ab1
    errno = 0;
aba7ab1
    int e1 = 0, e2 = 0;
aba7ab1
aba7ab1
    if ((result = selabel_lookup_best_match(hnd, &selabel_context, path, aliases, mode)) == -1) {
aba7ab1
        e1 = errno;
aba7ab1
        perror("selabel_lookup_best_match - ERROR");
aba7ab1
    }
aba7ab1
    else {
aba7ab1
        printf("selabel_lookup_best_match context: %s\n", selabel_context);
aba7ab1
        freecon(selabel_context);
aba7ab1
    }
aba7ab1
aba7ab1
    printf("Executing: selabel_lookup_best_match_raw(hnd, &selabel_context, %s, aliases, %d)\n", path, mode);
aba7ab1
aba7ab1
    errno = 0;
aba7ab1
aba7ab1
    if ((result = selabel_lookup_best_match_raw(hnd, &selabel_context, path, aliases, mode)) == -1) {
aba7ab1
        e2 = errno;
aba7ab1
        perror("selabel_lookup_best_match_raw - ERROR");
aba7ab1
    }
aba7ab1
    else {
aba7ab1
        printf("selabel_lookup_best_match_raw context: %s\n", selabel_context);
aba7ab1
        freecon(selabel_context);
aba7ab1
    }
aba7ab1
aba7ab1
    if (hnd != NULL)
aba7ab1
        selabel_close(hnd);
aba7ab1
aba7ab1
    if (e1 == e2)
aba7ab1
        return e1;
aba7ab1
    else
aba7ab1
        return 255;
aba7ab1
}