aba7ab
#include <stdio.h>
aba7ab
#include <stdlib.h>
aba7ab
#include <string.h>
aba7ab
#include <stdint.h>
aba7ab
#include <errno.h>
aba7ab
#include <selinux/selinux.h>
aba7ab
#include <selinux/label.h>
aba7ab
aba7ab
int main (int argc, char **argv)
aba7ab
{
aba7ab
    struct selabel_handle *hnd = NULL;
aba7ab
    unsigned int backend = 0;
aba7ab
aba7ab
    struct selinux_opt selabel_option [] = {
aba7ab
        { SELABEL_OPT_PATH, NULL },
aba7ab
        { SELABEL_OPT_VALIDATE, (char *) 1 },
aba7ab
        { SELABEL_OPT_DIGEST, (char *) 1 }
aba7ab
    };
aba7ab
aba7ab
    if (argc < 5) {
aba7ab
        fprintf(stderr, "Invalid number of arguments\n");
aba7ab
        return 255;
aba7ab
    }
aba7ab
aba7ab
    // set backend
aba7ab
    if (strcmp(argv[1], "CTX_FILE") == 0)
aba7ab
        backend = SELABEL_CTX_FILE;
aba7ab
    else if (strcmp(argv[1], "CTX_MEDIA") == 0)
aba7ab
        backend = SELABEL_CTX_MEDIA;
aba7ab
    else if (strcmp(argv[1], "CTX_X") == 0)
aba7ab
        backend = SELABEL_CTX_X;
aba7ab
    else if (strcmp(argv[1], "CTX_DB") == 0)
aba7ab
        backend = SELABEL_CTX_DB;
aba7ab
#ifndef RHEL6
aba7ab
    else if (strcmp(argv[1], "CTX_ANDROID_PROP") == 0)
aba7ab
        backend = SELABEL_CTX_ANDROID_PROP;
aba7ab
#endif
aba7ab
    else
aba7ab
        backend = strtoul(argv[1], NULL, 10);
aba7ab
aba7ab
aba7ab
    if ((argc == 6) && (strcmp(argv[5], "nohandle") == 0)) {
aba7ab
        hnd = NULL;
aba7ab
    }
aba7ab
    else {
aba7ab
        // set file contexts path
aba7ab
        if (strcmp(argv[2], "NULL") == 0) {
aba7ab
            selabel_option[0].value = NULL;
aba7ab
        }
aba7ab
        else {
aba7ab
            selabel_option[0].value = argv[2];
aba7ab
        }
aba7ab
aba7ab
        // set validate
aba7ab
        if (strcmp(argv[3], "0") == 0) {
aba7ab
            selabel_option[1].value = NULL;
aba7ab
        }
aba7ab
        else {
aba7ab
            selabel_option[1].value = (char *) 1;
aba7ab
        }
aba7ab
aba7ab
        // set digest
aba7ab
        if (strcmp(argv[4], "0") == 0) {
aba7ab
            selabel_option[2].value = NULL;
aba7ab
        }
aba7ab
        else {
aba7ab
            selabel_option[2].value = (char *) 1;
aba7ab
        }
aba7ab
aba7ab
        printf("selabel_options: "); 
aba7ab
        printf("SELABEL_OPT_PATH = %s, ", selabel_option[0].value);
aba7ab
        printf("SELABEL_OPT_VALIDATE = %ld, ", (long int)(intptr_t) selabel_option[1].value);
aba7ab
        printf("SELABEL_OPT_DIGEST = %ld, ", (long int)(intptr_t) selabel_option[2].value);
aba7ab
aba7ab
        printf("Executing: selabel_open(SELABEL_%s, &selabel_option, 3)\n", argv[1]);
aba7ab
aba7ab
        errno = 0;
aba7ab
aba7ab
        if ((hnd = selabel_open(backend, selabel_option, 3)) == NULL) {
aba7ab
            perror("selabel_open - ERROR");
aba7ab
            return 255;
aba7ab
        }
aba7ab
    }
aba7ab
aba7ab
    unsigned char *digest;
aba7ab
    size_t digest_len;
aba7ab
    char **specfiles;
aba7ab
    size_t num_specfiles;
aba7ab
    int result, e = 0;
aba7ab
aba7ab
    printf("Executing: selabel_digest(hnd, digest, digest_len, specfiles, num_specfiles)\n");
aba7ab
    
aba7ab
    errno = 0;
aba7ab
    
aba7ab
    if ((result = selabel_digest(hnd, &digest, &digest_len, &specfiles, &num_specfiles)) == -1) {
aba7ab
        e = errno;
aba7ab
        perror("selabel_digest - ERROR");
aba7ab
    }
aba7ab
    else {
aba7ab
        printf("digest_len: %lu\n", digest_len);
aba7ab
aba7ab
        printf("digest: ");
aba7ab
aba7ab
        for (size_t i = 0; i < digest_len; i++)
aba7ab
            printf("%2x", digest[i]);
aba7ab
aba7ab
        printf("\n");
aba7ab
aba7ab
        printf("num_specfiles: %lu\n", num_specfiles);
aba7ab
aba7ab
        printf("specfiles:\n");
aba7ab
aba7ab
        for (size_t i = 0; i < num_specfiles; i++)
aba7ab
            printf("specfile: %s\n", specfiles[i]);
aba7ab
    }
aba7ab
aba7ab
    if (hnd != NULL)
aba7ab
        selabel_close(hnd);
aba7ab
aba7ab
    return e;
aba7ab
}