HelperClass.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #import "HelperClass.h"
  2. #import "StatusPackageModel.h"
  3. @implementation HelperClass
  4. + (NSArray <StatusPackageModel*>*)statusInstalledPackagesFromFile:(NSString *)statusFile
  5. {
  6. if (![FM fileExistsAtPath:statusFile]) {
  7. return nil;
  8. }
  9. NSString *packageString = [NSString stringWithContentsOfFile:statusFile encoding:NSUTF8StringEncoding error:nil];
  10. NSArray *lineArray = [packageString componentsSeparatedByString:@"\n\n"];
  11. //DDLogInfo(@"lineArray: %@", lineArray);
  12. NSMutableArray *mutableList = [[NSMutableArray alloc] init];
  13. //NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init];
  14. for (id currentItem in lineArray)
  15. {
  16. StatusPackageModel *debModel = [[StatusPackageModel alloc] initWithRawControlString:currentItem];
  17. if (debModel != nil)
  18. [mutableList addObject:debModel];
  19. }
  20. NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES
  21. selector:@selector(localizedCaseInsensitiveCompare:)];
  22. NSSortDescriptor *packageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"package" ascending:YES
  23. selector:@selector(localizedCaseInsensitiveCompare:)];
  24. NSArray *descriptors = [NSArray arrayWithObjects:nameDescriptor, packageDescriptor, nil];
  25. NSArray *sortedArray = [mutableList sortedArrayUsingDescriptors:descriptors];
  26. mutableList = nil;
  27. return sortedArray;
  28. }
  29. + (BOOL)shouldContinueWithError:(NSString *)errorMessage {
  30. NSString *errorString = [NSString stringWithFormat:@"\n%@ Are you sure you want to continue? [y/n]?", errorMessage];
  31. char c;
  32. printf("%s", [errorString UTF8String] );
  33. c=getchar();
  34. while(c!='y' && c!='n')
  35. {
  36. if (c!='\n'){
  37. printf("[y/n]");
  38. }
  39. c=getchar();
  40. }
  41. if (c == 'n')
  42. {
  43. DLog(@"\n smart move... exiting\n\n");
  44. return FALSE;
  45. } else if (c == 'y') {
  46. DLog(@"\n its your funeral....\n\n");
  47. }
  48. return TRUE;
  49. }
  50. + (NSString *)singleLineReturnForProcess:(NSString *)call
  51. {
  52. return [[self returnForProcess:call] componentsJoinedByString:@"\n"];
  53. }
  54. + (NSArray *)returnForProcess:(NSString *)call
  55. {
  56. if (call==nil)
  57. return 0;
  58. char line[200];
  59. DLog(@"\nRunning process: %@\n", call);
  60. FILE* fp = popen([call UTF8String], "r");
  61. NSMutableArray *lines = [[NSMutableArray alloc]init];
  62. if (fp)
  63. {
  64. while (fgets(line, sizeof line, fp))
  65. {
  66. NSString *s = [NSString stringWithCString:line encoding:NSUTF8StringEncoding];
  67. s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  68. [lines addObject:s];
  69. }
  70. }
  71. pclose(fp);
  72. return lines;
  73. }
  74. + (InputPackageFile *)packageFileFromLine:(NSString *)inputLine {
  75. // "-rwxr-xr-x 0 root wheel 69424 Oct 22 03:56 ./Library/MobileSubstrate/DynamicLibraries/beigelist7.dylib\n",
  76. //-rwxr-xr-x root/staff 10860 2011-02-02 03:55 ./Library/Frameworks/CydiaSubstrate.framework/Commands/cycc
  77. inputLine = [inputLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  78. inputLine = [inputLine stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\t"]];
  79. NSMutableString *newString = [[NSMutableString alloc] initWithString:inputLine];
  80. [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
  81. [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
  82. [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
  83. [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
  84. [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
  85. NSArray *lineObjects = [newString componentsSeparatedByString:@" "];
  86. //NSLog(@"lineObjects: %@", lineObjects);
  87. /*
  88. "drwxr-xr-x",
  89. "root/wheel",
  90. 0,
  91. "2018-06-27",
  92. "01:21",
  93. "./"
  94. */
  95. NSString *permissionsAndType = [lineObjects objectAtIndex:0];
  96. NSString *userGroup = [lineObjects objectAtIndex:1];
  97. NSString *size = [lineObjects objectAtIndex:2];
  98. NSString *date = [lineObjects objectAtIndex:3];
  99. NSString *time = [lineObjects objectAtIndex:4];
  100. NSString *path = [lineObjects objectAtIndex:5];
  101. //@"drwxr-xr-x"
  102. NSString *fileTypeChar = [permissionsAndType substringWithRange:NSMakeRange(0, 1)];
  103. NSString *octalPermissions = [self octalFromSymbols:permissionsAndType];
  104. NSString *octalUG = [self octalFromGroupSymbols:userGroup];
  105. NSString *fileName = [path lastPathComponent];
  106. //NSString *fullPath = [NSString stringWithFormat:@"/%@", path];
  107. NSString *fullPath = [path substringFromIndex:1];
  108. InputPackageFile *pf = [InputPackageFile new];
  109. [pf _setFileTypeFromRaw:fileTypeChar];
  110. switch (pf.type) {
  111. case BSPackageFileTypeLink:
  112. {
  113. fullPath = [lineObjects objectAtIndex:7];
  114. NSString *linkDest = [NSString stringWithFormat:@"/%@", path];
  115. pf.permissions = octalPermissions;
  116. pf.owner = octalUG;
  117. pf.size = size;
  118. pf.time = time;
  119. pf.date = date;
  120. pf.path = fullPath;
  121. pf.basename = fileName;
  122. pf.linkDestination = linkDest;
  123. return pf;
  124. }
  125. break;
  126. case BSPackageFileTypeDirectory: //return for now
  127. //DLog(@"we dont want directory entries do we %@", lineObjects);
  128. pf.permissions = octalPermissions;
  129. pf.owner = octalUG;
  130. pf.size = size;
  131. pf.time = time;
  132. pf.date = date;
  133. pf.path = fullPath;
  134. pf.basename = fileName;
  135. return pf;
  136. break;
  137. default:
  138. break;
  139. }
  140. pf.permissions = octalPermissions;
  141. pf.owner = octalUG;
  142. pf.size = size;
  143. pf.time = time;
  144. pf.date = date;
  145. pf.path = fullPath;
  146. pf.basename = fileName;
  147. return pf;
  148. // return [NSDictionary dictionaryWithObjectsAndKeys:fileType, @"fileType",octalPermissions, @"octalPermissions", octalUG, @"octalUG", size, @"size", date, @"date", time, @"time", fileName, @"fileName", fullPath, @"fullPath", nil];
  149. }
  150. + (NSString *)octalFromGroupSymbols:(NSString *)theSymbols
  151. {
  152. NSArray *groupArray = [theSymbols componentsSeparatedByString:@"/"];
  153. NSString *user = [groupArray objectAtIndex:0];
  154. NSString *group = [groupArray objectAtIndex:1];
  155. NSString *octalUser = nil;
  156. NSString *octalGroup = nil;
  157. //uid=0(root) gid=0(wheel) groups=0(wheel),1(daemon),2(kmem),3(sys),4(tty),5(operator),8(procview),9(procmod),20(staff),29(certusers),80(admin)
  158. if ([user isEqualToString:@"root"])
  159. {
  160. octalUser = @"0";
  161. } else if ([user isEqualToString:@"mobile"])
  162. {
  163. octalUser = @"501";
  164. }
  165. //obviously more cases!! FIXME:
  166. if ([group isEqualToString:@"staff"])
  167. {
  168. octalGroup = @"20";
  169. } else if ([group isEqualToString:@"admin"])
  170. {
  171. octalGroup = @"80";
  172. } else if ([group isEqualToString:@"wheel"])
  173. {
  174. octalGroup = @"0";
  175. } else if ([group isEqualToString:@"daemon"])
  176. {
  177. octalGroup = @"1";
  178. } else if ([group isEqualToString:@"kmem"])
  179. {
  180. octalGroup = @"2";
  181. } else if ([group isEqualToString:@"sys"])
  182. {
  183. octalGroup = @"3";
  184. } else if ([group isEqualToString:@"tty"])
  185. {
  186. octalGroup = @"4";
  187. } else if ([group isEqualToString:@"operator"])
  188. {
  189. octalGroup = @"5";
  190. } else if ([group isEqualToString:@"procview"])
  191. {
  192. octalGroup = @"8";
  193. } else if ([group isEqualToString:@"procmod"])
  194. {
  195. octalGroup = @"9";
  196. } else if ([group isEqualToString:@"certusers"])
  197. {
  198. octalGroup = @"29";
  199. } else
  200. {
  201. octalGroup = @"501"; //default to mobile
  202. }
  203. //uid=0(root) gid=0(wheel) groups=0(wheel),1(daemon),2(kmem),3(sys),4(tty),5(operator),8(procview),9(procmod),20(staff),29(certusers),80(admin)
  204. return [NSString stringWithFormat:@"%@:%@", octalUser, octalGroup];
  205. }
  206. + (InputPackage *)packageForDeb:(NSString *)debFile {
  207. NSString *packageName = [self singleLineReturnForProcess:[NSString stringWithFormat:@"/usr/local/bin/dpkg -f %@ Package", debFile]];
  208. NSString *packageVersion = [self singleLineReturnForProcess:[NSString stringWithFormat:@"/usr/local/bin/dpkg -f %@ Version", debFile]];
  209. NSArray <InputPackageFile *> *fileList = [self returnForProcess:[NSString stringWithFormat:@"/usr/local/bin/dpkg -c %@", debFile]];
  210. __block NSMutableArray *finalArray = [NSMutableArray new];
  211. [fileList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  212. InputPackageFile *file = [self packageFileFromLine:obj];
  213. if (file) {
  214. //DLog(@"%@", file);
  215. [finalArray addObject:file];
  216. }
  217. }];
  218. InputPackage *pkg = [InputPackage new];
  219. pkg.files = finalArray;
  220. pkg.path = debFile;
  221. pkg.packageName = packageName;
  222. pkg.version = packageVersion;
  223. return pkg;
  224. }
  225. + (NSString *)octalFromSymbols:(NSString *)theSymbols
  226. {
  227. //NSLog(@"%@ %s", self, _cmd);
  228. NSString *U = [theSymbols substringWithRange:NSMakeRange(1, 3)];
  229. NSString *G = [theSymbols substringWithRange:NSMakeRange(4, 3)];
  230. NSString *O = [theSymbols substringWithRange:NSMakeRange(7, 3)];
  231. //NSLog(@"fileTypeChar: %@", fileTypeChar);
  232. //NSLog(@"U; %@", U);
  233. //NSLog(@"G; %@", G);
  234. //NSLog(@"O; %@", O);
  235. //USER
  236. int sIdBit = 0;
  237. int uOctal = 0;
  238. const char *uArray = [U cStringUsingEncoding:NSASCIIStringEncoding];
  239. int stringLength = [U length];
  240. int x;
  241. for( x=0; x<stringLength; x++ )
  242. {
  243. unsigned int aCharacter = uArray[x];
  244. if (aCharacter == 'r')
  245. {
  246. uOctal += 4;
  247. } else if (aCharacter == 'w')
  248. {
  249. uOctal += 2;
  250. } else if (aCharacter == 'x')
  251. {
  252. uOctal += 1;
  253. } else if (aCharacter == 's')
  254. {
  255. sIdBit += 4;
  256. }
  257. }
  258. //GROUP
  259. int gOctal = 0;
  260. const char *gArray = [G cStringUsingEncoding:NSASCIIStringEncoding];
  261. stringLength = [G length];
  262. int y;
  263. for( y=0; y<stringLength; y++ )
  264. {
  265. unsigned int aCharacter = gArray[y];
  266. if (aCharacter == 'r')
  267. {
  268. gOctal += 4;
  269. } else if (aCharacter == 'w')
  270. {
  271. gOctal += 2;
  272. } else if (aCharacter == 'x')
  273. {
  274. gOctal += 1;
  275. } else if (aCharacter == 's')
  276. {
  277. gOctal += 2;
  278. }
  279. }
  280. //OTHERS
  281. int z;
  282. int oOctal = 0;
  283. const char *oArray = [O cStringUsingEncoding:NSASCIIStringEncoding];
  284. stringLength = [O length];
  285. for( z=0; z<stringLength; z++ )
  286. {
  287. unsigned int aCharacter = oArray[z];
  288. if (aCharacter == 'r')
  289. {
  290. oOctal += 4;
  291. } else if (aCharacter == 'w')
  292. {
  293. oOctal += 2;
  294. } else if (aCharacter == 'x')
  295. {
  296. oOctal += 1;
  297. }
  298. }
  299. return [NSString stringWithFormat:@"%i%i%i%i", sIdBit, uOctal, gOctal, oOctal];
  300. }
  301. @end