1#include <unistd.h> 2#include <sys/types.h> 3#include <stdio.h> 4#include <stdlib.h> 5#include <string.h> 6#include <errno.h> 7#include <selinux/selinux.h> 8 9int main(int argc, char **argv) 10{ 11 char *buf; 12 security_class_t tclass; 13 const char *objname; 14 int ret; 15 16 if (argc != 4 && argc != 5) { 17 fprintf(stderr, "usage: %s scontext tcontext tclass [objname]\n", 18 argv[0]); 19 exit(1); 20 } 21 22 if (security_check_context(argv[1])) { 23 fprintf(stderr, "%s: invalid source context '%s'\n", argv[0], argv[1]); 24 exit(4); 25 } 26 27 if (security_check_context(argv[2])) { 28 fprintf(stderr, "%s: invalid target context '%s'\n", argv[0], argv[2]); 29 exit(5); 30 } 31 32 tclass = string_to_security_class(argv[3]); 33 if (!tclass) { 34 fprintf(stderr, "%s: invalid class '%s'\n", argv[0], argv[3]); 35 exit(2); 36 } 37 38 objname = (argc == 5) ? argv[4] : NULL; 39 40 ret = security_compute_create_name(argv[1], argv[2], tclass, objname, &buf); 41 if (ret < 0) { 42 fprintf(stderr, "%s: security_compute_create failed: %s\n", 43 argv[0], strerror(errno)); 44 exit(3); 45 } 46 47 printf("%s\n", buf); 48 freecon(buf); 49 exit(EXIT_SUCCESS); 50} 51