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
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
    int mode;
aba7ab1
    
aba7ab1
    if (strcmp(argv[7], "X_PROP") == 0)
aba7ab1
        mode = SELABEL_X_PROP;
aba7ab1
    else if (strcmp(argv[7], "X_SELN") == 0)
aba7ab1
        mode = SELABEL_X_SELN;
aba7ab1
    else if (strcmp(argv[7], "X_EXT") == 0)
aba7ab1
        mode = SELABEL_X_EXT;
aba7ab1
    else if (strcmp(argv[7], "X_EVENT") == 0)
aba7ab1
        mode = SELABEL_X_EVENT;
aba7ab1
    else if (strcmp(argv[7], "X_CLIENT") == 0)
aba7ab1
        mode = SELABEL_X_CLIENT;
aba7ab1
    else if (strcmp(argv[7], "X_POLYPROP") == 0)
aba7ab1
        mode = SELABEL_X_POLYPROP;
aba7ab1
    else if (strcmp(argv[7], "X_POLYSELN") == 0)
aba7ab1
        mode = SELABEL_X_POLYSELN;
aba7ab1
    else if (strcmp(argv[7], "DB_DATABASE") == 0)
aba7ab1
        mode = SELABEL_DB_DATABASE;
aba7ab1
    else if (strcmp(argv[7], "DB_SCHEMA") == 0)
aba7ab1
        mode = SELABEL_DB_SCHEMA;
aba7ab1
    else if (strcmp(argv[7], "DB_VIEW") == 0)
aba7ab1
        mode = SELABEL_DB_VIEW;
aba7ab1
    else if (strcmp(argv[7], "DB_TABLE") == 0)
aba7ab1
        mode = SELABEL_DB_TABLE;
aba7ab1
    else if (strcmp(argv[7], "DB_COLUMN") == 0)
aba7ab1
        mode = SELABEL_DB_COLUMN;
aba7ab1
    else if (strcmp(argv[7], "DB_TUPLE") == 0)
aba7ab1
        mode = SELABEL_DB_TUPLE;
aba7ab1
    else if (strcmp(argv[7], "DB_PROCEDURE") == 0)
aba7ab1
        mode = SELABEL_DB_PROCEDURE;
aba7ab1
    else if (strcmp(argv[7], "DB_SEQUENCE") == 0)
aba7ab1
        mode = SELABEL_DB_SEQUENCE;
aba7ab1
    else if (strcmp(argv[7], "DB_BLOB") == 0)
aba7ab1
        mode = SELABEL_DB_BLOB;
aba7ab1
#ifndef RHEL6
aba7ab1
    else if (strcmp(argv[7], "DB_LANGUAGE") == 0)
aba7ab1
        mode = SELABEL_DB_LANGUAGE;
aba7ab1
    else if (strcmp(argv[7], "DB_EXCEPTION") == 0)
aba7ab1
        mode = SELABEL_DB_EXCEPTION;
aba7ab1
    else if (strcmp(argv[7], "DB_DATATYPE") == 0)
aba7ab1
        mode = SELABEL_DB_DATATYPE;
aba7ab1
#endif
aba7ab1
    else
aba7ab1
        // notice the base 8
aba7ab1
        mode = strtol(argv[7], NULL, 8);
aba7ab1
    
aba7ab1
    int e1 = 0, e2 = 0;
aba7ab1
aba7ab1
    if (strcmp(argv[6], "NULL") == 0) {
aba7ab1
        path = NULL;
aba7ab1
    }
aba7ab1
    else if (strcmp(argv[6], "'*'") == 0) {
aba7ab1
        path = "*";
aba7ab1
    }
aba7ab1
    else {
aba7ab1
        path = argv[6];
aba7ab1
    }
aba7ab1
aba7ab1
    printf("Executing: selabel_lookup(hnd, &selabel_context, %s, %d)\n", path, mode);
aba7ab1
aba7ab1
    errno = 0;
aba7ab1
    
aba7ab1
    if ((result = selabel_lookup(hnd, &selabel_context, path, mode)) == -1) {
aba7ab1
        e1 = errno;
aba7ab1
        perror("selabel_lookup - ERROR");
aba7ab1
    }
aba7ab1
    else {
aba7ab1
        printf("selabel_lookup context: %s\n", selabel_context);
aba7ab1
        freecon(selabel_context);
aba7ab1
    }
aba7ab1
aba7ab1
    printf("Executing: selabel_lookup_raw(hnd, &selabel_context, %s, %d)\n", path, mode);
aba7ab1
aba7ab1
    errno = 0;
aba7ab1
    
aba7ab1
    if ((result = selabel_lookup_raw(hnd, &selabel_context, path, mode)) == -1) {
aba7ab1
        e2 = errno;
aba7ab1
        perror("selabel_lookup_raw - ERROR");
aba7ab1
    }
aba7ab1
    else {
aba7ab1
        printf("selabel_lookup_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
}