selinux/libsemanage/example/test_fcontext.c
Joshua Brindle e319cd8538 Author: Daniel J Walsh
Email: dwalsh@redhat.com
Subject: libsemage patch to not compile modules for seusers and fcontext
Date: Wed, 10 Sep 2008 10:30:08 -0400

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ivan Gyurdiev wrote:
>
>>> I'm a little unclear on what this is doing - can you clarify?
>>>
>> This is clearing the existing seusers.final file, otherwise delete was
>> not working.
>>
> I think the previous code was doing more - it was merging the local file
> with the shipped base package file, like this:
>
>    data = extract_file_from_policy_package( )
>    write_file ( "seusers.final", data )
>    if ( data != null ) {
>        seusers.clear_cache()  // thereby forcing reload from
> seusers.final when cache() is called again (in merge_components)
>    } else {
>        seusers.clear()
>    }
>
> It's also doing this three times (once for fcontexts, once for seusers,
> once for seusers_extra).
> The problem is that you're skipping the link_sandbox call, which builds
> the base package, containing this information.
>
> Ivan
>
>
Ok I found some problems with the previous patch and did some code
reuse.  I added a function that only read base.pp in order to handle the
 base user_extra and seusers problem.

Signed-off-by: Joshua Brindle <method@manicmethod.com>
2008-09-15 09:25:33 -04:00

73 lines
1.8 KiB
C

#include <semanage/fcontext_record.h>
#include <semanage/semanage.h>
#include <semanage/fcontexts_local.h>
#include <sepol/sepol.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(const int argc, const char **argv) {
semanage_handle_t *sh = NULL;
semanage_fcontext_t *fcontext;
semanage_context_t *con;
semanage_fcontext_key_t *k;
int exist = 0;
sh = semanage_handle_create();
if (sh == NULL) {
perror("Can't create semanage handle\n");
return -1;
}
if (semanage_access_check(sh) < 0) {
perror("Semanage access check failed\n");
return -1;
}
if (semanage_connect(sh) < 0) {
perror("Semanage connect failed\n");
return -1;
}
if (semanage_fcontext_key_create(sh, argv[2], SEMANAGE_FCONTEXT_REG, &k) < 0) {
fprintf(stderr, "Could not create key for %s", argv[2]);
return -1;
}
if(semanage_fcontext_exists(sh, k, &exist) < 0) {
fprintf(stderr,"Could not check if key exists for %s", argv[2]);
return -1;
}
if (exist) {
fprintf(stderr,"Could create %s mapping already exists", argv[2]);
return -1;
}
if (semanage_fcontext_create(sh, &fcontext) < 0) {
fprintf(stderr,"Could not create file context for %s", argv[2]);
return -1;
}
semanage_fcontext_set_expr(sh, fcontext, argv[2]);
if (semanage_context_from_string(sh, argv[1], &con)) {
fprintf(stderr,"Could not create context using %s for file context %s", argv[1], argv[2]);
return -1;
}
if (semanage_fcontext_set_con(sh, fcontext, con) < 0) {
fprintf(stderr,"Could not set file context for %s", argv[2]);
return -1;
}
semanage_fcontext_set_type(fcontext, SEMANAGE_FCONTEXT_REG);
if(semanage_fcontext_modify_local(sh, k, fcontext) < 0) {
fprintf(stderr,"Could not add file context for %s", argv[2]);
return -1;
}
semanage_fcontext_key_free(k);
semanage_fcontext_free(fcontext);
return 0;
}