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/selinux.h> 10 #include <selinux/get_context_list.h> 11 12 static void usage(const char *name, const char *detail, int rc) 13 { 14 fprintf(stderr, "usage: %s [-l level] user [context]\n", name); 15 if (detail) 16 fprintf(stderr, "%s: %s\n", name, detail); 17 exit(rc); 18 } 19 20 int main(int argc, char **argv) 21 { 22 char **list, *usercon = NULL, *cur_context = NULL; 23 char *user = NULL, *level = NULL; 24 int ret, i, opt; 25 26 while ((opt = getopt(argc, argv, "l:")) > 0) { 27 switch (opt) { 28 case 'l': 29 level = strdup(optarg); 30 break; 31 default: 32 usage(argv[0], "invalid option", 1); 33 } 34 } 35 36 if (((argc - optind) < 1) || ((argc - optind) > 2)) 37 usage(argv[0], "invalid number of arguments", 2); 38 39 /* If selinux isn't available, bail out. */ 40 if (!is_selinux_enabled()) { 41 fprintf(stderr, 42 "getconlist may be used only on a SELinux kernel.\n"); 43 return 1; 44 } 45 46 user = argv[optind]; 47 48 /* If a context wasn't passed, use the current context. */ 49 if (((argc - optind) < 2)) { 50 if (getcon(&cur_context) < 0) { 51 fprintf(stderr, "Couldn't get current context.\n"); 52 return 2; 53 } 54 } else 55 cur_context = argv[optind + 1]; 56 57 /* Get the list and print it */ 58 if (level) 59 ret = 60 get_ordered_context_list_with_level(user, level, 61 cur_context, &list); 62 else 63 ret = get_ordered_context_list(user, cur_context, &list); 64 if (ret != -1) { 65 for (i = 0; list[i]; i++) 66 puts(list[i]); 67 freeconary(list); 68 } 69 70 free(usercon); 71 72 return 0; 73 } 74