Skip to content

Instantly share code, notes, and snippets.

@mikhailnov
Last active February 13, 2020 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikhailnov/7c55e5481527933e5ba850813121d157 to your computer and use it in GitHub Desktop.
Save mikhailnov/7c55e5481527933e5ba850813121d157 to your computer and use it in GitHub Desktop.
Get default selinux user context
// gcc -lselinux st.c -o st.bin
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <selinux/selinux.h>
#include <selinux/get_context_list.h>
int main(){
security_context_t *contextlist = NULL;
int rc = -1;
int count = 0;
char *linuxuser = NULL;
char *selinuxuser = NULL;
char *level = NULL;
char *newcon = NULL;
// get current user from env USER
linuxuser = getenv("USER");
printf("Got: Linux user name: %s\n", linuxuser);
count = ++count;
// get selinux user by linux user name
// int getseuserbyname(const char *linuxuser, char **selinuxuser, char **level);
rc = getseuserbyname(linuxuser, &selinuxuser, &level);
if (rc != 0)
goto exit;
printf("Got: SELinux user: %s\n", selinuxuser);
count = ++count;
printf("Got: SELinux level: %s\n", level);
count = ++count;
// Get default SELinux context for the curerent user
// or the user specified in $USER env
// int get_default_context(const char *user, char *fromcon, char **newcon);
// fromcon = NULL -> current context should be used
rc = get_default_context(selinuxuser, NULL, &newcon);
if (rc != 0)
goto exit;
printf("Got: default context for the SELinux user: %s\n", newcon);
count = ++count;
exit:
// TODO: free memory (?)
printf("Number of executed tests: %d out of 4\n", count);
printf("st finished, exit code is: %d\n", rc);
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment