16cd6a6acSopenharmony_ci#include <unistd.h>
26cd6a6acSopenharmony_ci#include <sys/types.h>
36cd6a6acSopenharmony_ci#include <stdio.h>
46cd6a6acSopenharmony_ci#include <stdlib.h>
56cd6a6acSopenharmony_ci#include <string.h>
66cd6a6acSopenharmony_ci#include <errno.h>
76cd6a6acSopenharmony_ci#include <selinux/selinux.h>
86cd6a6acSopenharmony_ci
96cd6a6acSopenharmony_ciint main(int argc, char **argv)
106cd6a6acSopenharmony_ci{
116cd6a6acSopenharmony_ci	char *buf;
126cd6a6acSopenharmony_ci	security_class_t tclass;
136cd6a6acSopenharmony_ci	const char *objname;
146cd6a6acSopenharmony_ci	int ret;
156cd6a6acSopenharmony_ci
166cd6a6acSopenharmony_ci	if (argc != 4 && argc != 5) {
176cd6a6acSopenharmony_ci		fprintf(stderr, "usage:  %s scontext tcontext tclass [objname]\n",
186cd6a6acSopenharmony_ci			argv[0]);
196cd6a6acSopenharmony_ci		exit(1);
206cd6a6acSopenharmony_ci	}
216cd6a6acSopenharmony_ci
226cd6a6acSopenharmony_ci	if (security_check_context(argv[1])) {
236cd6a6acSopenharmony_ci		fprintf(stderr, "%s:  invalid source context '%s'\n", argv[0], argv[1]);
246cd6a6acSopenharmony_ci		exit(4);
256cd6a6acSopenharmony_ci	}
266cd6a6acSopenharmony_ci
276cd6a6acSopenharmony_ci	if (security_check_context(argv[2])) {
286cd6a6acSopenharmony_ci		fprintf(stderr, "%s:  invalid target context '%s'\n", argv[0], argv[2]);
296cd6a6acSopenharmony_ci		exit(5);
306cd6a6acSopenharmony_ci	}
316cd6a6acSopenharmony_ci
326cd6a6acSopenharmony_ci	tclass = string_to_security_class(argv[3]);
336cd6a6acSopenharmony_ci	if (!tclass) {
346cd6a6acSopenharmony_ci		fprintf(stderr, "%s:  invalid class '%s'\n", argv[0], argv[3]);
356cd6a6acSopenharmony_ci		exit(2);
366cd6a6acSopenharmony_ci	}
376cd6a6acSopenharmony_ci
386cd6a6acSopenharmony_ci	objname = (argc == 5) ? argv[4] : NULL;
396cd6a6acSopenharmony_ci
406cd6a6acSopenharmony_ci	ret = security_compute_create_name(argv[1], argv[2], tclass, objname, &buf);
416cd6a6acSopenharmony_ci	if (ret < 0) {
426cd6a6acSopenharmony_ci		fprintf(stderr, "%s:  security_compute_create failed:  %s\n",
436cd6a6acSopenharmony_ci			argv[0], strerror(errno));
446cd6a6acSopenharmony_ci		exit(3);
456cd6a6acSopenharmony_ci	}
466cd6a6acSopenharmony_ci
476cd6a6acSopenharmony_ci	printf("%s\n", buf);
486cd6a6acSopenharmony_ci	freecon(buf);
496cd6a6acSopenharmony_ci	exit(EXIT_SUCCESS);
506cd6a6acSopenharmony_ci}
51