1 #ifdef _WIN32 2 #define __USE_MINGW_ANSI_STDIO 1 3 #endif 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <errno.h> 9 #include <fcntl.h> 10 #ifdef HAS_STDINT_H 11 #include <stdint.h> 12 #endif 13 #ifdef unix 14 #include <unistd.h> 15 #include <sys/types.h> 16 #include <sys/stat.h> 17 #else 18 #include <direct.h> 19 #include <io.h> 20 #endif 21 22 #define MAGIC 0xF97CFF8A 23 24 #define S_IFMT 00170000 25 #define S_IFSOCK 0140000 26 #define S_IFLNK 0120000 27 #define S_IFREG 0100000 28 #define S_IFBLK 0060000 29 #define S_IFDIR 0040000 30 #define S_IFCHR 0020000 31 #define S_IFIFO 0010000 32 #define S_ISUID 0004000 33 #define S_ISGID 0002000 34 #define S_ISVTX 0001000 35 36 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) 37 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) 38 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 39 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) 40 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) 41 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) 42 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) 43 44 char *mode_to_string(unsigned int mode) 45 { 46 if (S_ISBLK(mode)) 47 return "-b"; 48 else if (S_ISCHR(mode)) 49 return "-c"; 50 else if (S_ISDIR(mode)) 51 return "-d"; 52 else if (S_ISFIFO(mode)) 53 return "-p"; 54 else if (S_ISLNK(mode)) 55 return "-l"; 56 else if (S_ISSOCK(mode)) 57 return "-s"; 58 else if (S_ISREG(mode)) 59 return "--"; 60 else 61 return ""; 62 } 63 64 int main(int argc, char *argv[]) 65 { 66 unsigned int selinux_vers; 67 unsigned int nothing; 68 unsigned int i; 69 unsigned int steams; 70 unsigned int num_of_regex; 71 72 char steam[256]; 73 char src[512]; 74 char context[512]; 75 76 FILE *fp = NULL; 77 FILE *fpout = NULL; 78 79 if (argc != 3) { 80 printf("usage: %s BINARY_FILE OUTPUT_TEXT_FILE\n", argv[0]); 81 printf("e.g: %s file_contexts.bin file_contexts\n", argv[0]); 82 return 1; 83 } 84 85 if ((fp = fopen(argv[1], "rb")) == NULL) { 86 printf("Unable to open %s : ", argv[1]); 87 perror(""); 88 return 1; 89 } 90 fread(¬hing, 1, 4, fp); 91 if (nothing != MAGIC) { 92 printf("%s is not a binary!\n", argv[1]); 93 if (fp) fclose(fp); 94 return 1; 95 } 96 97 fread(¬hing, 1, 4, fp); 98 selinux_vers = nothing; 99 fread(¬hing, 1, 4, fp); 100 fseek(fp, (unsigned int)ftell(fp) + nothing, SEEK_SET); 101 102 fread(¬hing, 1, 4, fp); 103 //printf("num of steams: 0x%x\n", nothing); 104 105 for (i=0; i<nothing; ++i) { 106 fread(&steams, 1, 4, fp); 107 fread(steam, 1, steams+1, fp); 108 //printf("%s\n", steam); 109 } 110 111 fread(&num_of_regex, 1, 4, fp); 112 //printf("num of regex: 0x%x\n", num_of_regex); 113 114 if ((fpout = fopen(argv[2], "wb")) == NULL) { 115 printf("unable to open %s for write!\n", argv[2]); 116 if (fp) fclose(fp); 117 return 1; 118 } 119 120 //printf("selinux version: %d\n", selinux_vers); 121 fprintf(fpout, "#context version %d\n", selinux_vers); 122 123 for (i=0; i<num_of_regex; ++i) { 124 fread(¬hing, 1, 4, fp); 125 fread(context, 1, nothing, fp); 126 fread(¬hing, 1, 4, fp); 127 fread(src, 1, nothing, fp); 128 //printf("%s %s\n", src, context); 129 fread(¬hing, 1, 4, fp); 130 fprintf(fpout, "%s\t%s\t%s\n", src, mode_to_string(nothing), context); 131 fseek(fp, (unsigned int)ftell(fp) + 12, SEEK_SET); 132 fread(¬hing, 1, 4, fp); 133 fseek(fp, (unsigned int)ftell(fp) + nothing, SEEK_SET); 134 fread(¬hing, 1 , 4, fp); 135 fread(¬hing, 1 , 4, fp); 136 fseek(fp, (unsigned int)ftell(fp) + nothing - 4, SEEK_SET); 137 } 138 139 if (fp) fclose(fp); 140 if (fpout) fclose(fpout); 141 142 return 0; 143 }