Eric Paris | 5ef65fd | 2011-06-28 19:40:26 -0400 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <sys/types.h> |
| 3 | #include <fcntl.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <errno.h> |
| 7 | #include <string.h> |
| 8 | #include <ctype.h> |
| 9 | #include <selinux/flask.h> |
| 10 | #include <selinux/selinux.h> |
| 11 | |
Daniel P. Berrange | 5f8ce37 | 2012-01-23 15:41:19 +0000 | [diff] [blame^] | 12 | static void usage(const char *name, const char *detail, int rc) |
Eric Paris | 5ef65fd | 2011-06-28 19:40:26 -0400 | [diff] [blame] | 13 | { |
| 14 | fprintf(stderr, "usage: %s command [ fromcon ]\n", name); |
| 15 | if (detail) |
| 16 | fprintf(stderr, "%s: %s\n", name, detail); |
| 17 | exit(rc); |
| 18 | } |
| 19 | |
| 20 | static security_context_t get_selinux_proc_context(const char *command, security_context_t execcon) { |
| 21 | security_context_t fcon = NULL, newcon = NULL; |
| 22 | |
| 23 | int ret = getfilecon(command, &fcon); |
| 24 | if (ret < 0) goto err; |
| 25 | ret = security_compute_create(execcon, fcon, SECCLASS_PROCESS, &newcon); |
| 26 | if (ret < 0) goto err; |
| 27 | |
| 28 | err: |
| 29 | freecon(fcon); |
| 30 | return newcon; |
| 31 | } |
| 32 | |
| 33 | int main(int argc, char **argv) |
| 34 | { |
| 35 | int ret = -1; |
| 36 | security_context_t proccon = NULL, con = NULL; |
| 37 | if (argc < 2 || argc > 3) |
| 38 | usage(argv[0], "Invalid number of arguments", -1); |
| 39 | |
| 40 | if (argc == 2) { |
| 41 | if (getcon(&con) < 0) { |
| 42 | perror(argv[0]); |
| 43 | return -1; |
| 44 | } |
| 45 | } else { |
| 46 | con = strdup(argv[2]); |
| 47 | } |
| 48 | |
| 49 | proccon = get_selinux_proc_context(argv[1], con); |
| 50 | if (proccon) { |
| 51 | printf("%s\n", proccon); |
| 52 | ret = 0; |
| 53 | } else { |
| 54 | perror(argv[0]); |
| 55 | } |
| 56 | |
| 57 | free(proccon); |
| 58 | free(con); |
| 59 | return ret; |
| 60 | } |