InputPackage.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #import "InputPackage.h"
  2. #import "ErrorReturn.h"
  3. #import "HelperClass.h"
  4. @implementation InputPackage
  5. - (NSString*) description {
  6. NSString *orig = [super description];
  7. NSMutableDictionary *details = [NSMutableDictionary new];
  8. NSArray *props = [self properties];
  9. [props enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  10. NSString *cv = [self valueForKey:obj];
  11. if (cv){
  12. details[obj] = cv;
  13. }
  14. }];
  15. return [NSString stringWithFormat:@"%@ = %@", orig, details];
  16. }
  17. - (NSString *)listfile {
  18. __block NSMutableArray *outFiles = [NSMutableArray new];
  19. [self.files enumerateObjectsUsingBlock:^(InputPackageFile * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  20. if ([obj.fileType isEqualToString:@"link"]){ //does this need to handle things differently?
  21. [outFiles addObject:obj.path];
  22. } else {
  23. [outFiles addObject:obj.path];
  24. }
  25. }];
  26. return [outFiles componentsJoinedByString:@"\n"];
  27. }
  28. - (void)repackageInCurrentDirectoryWithArch:(NSString *)newArch {
  29. NSString *fakeRoot = [HelperClass singleLineReturnForProcess:@"/usr/bin/which fakeroot"];
  30. NSString *pwd = [HelperClass singleLineReturnForProcess:@"/bin/pwd"];
  31. DLog(@"\nProcessing file: %@\n", self.path);
  32. InputPackage *output = self;
  33. DLog(@"\nFound package: '%@' at version: '%@'...\n", output.packageName, output.version );
  34. NSString *tmpPath = [pwd stringByAppendingPathComponent:output.packageName];
  35. NSString *debian = [tmpPath stringByAppendingPathComponent:@"DEBIAN"];
  36. [FM createDirectoryAtPath:tmpPath withIntermediateDirectories:TRUE attributes:nil error:nil];
  37. DLog(@"\nExtracting package contents for processing...\n");
  38. [HelperClass returnForProcess:[NSString stringWithFormat:@"/usr/local/bin/dpkg -x %@ %@", self.path, tmpPath]];
  39. [FM createDirectoryAtPath:debian withIntermediateDirectories:TRUE attributes:nil error:nil];
  40. DLog(@"\nExtracting DEBIAN files for processing...\n");
  41. [HelperClass returnForProcess:[NSString stringWithFormat:@"/usr/local/bin/dpkg -e %@ %@", self.path, debian]];
  42. if (newArch != nil) {
  43. NSString *controlPath = [debian stringByAppendingPathComponent:@"control"];
  44. NSMutableString *controlFile = [[NSMutableString alloc] initWithContentsOfFile:controlPath encoding:NSUTF8StringEncoding error:nil];
  45. //@"appletvos-arm64"
  46. [controlFile replaceOccurrencesOfString:@"iphoneos-arm" withString:newArch options:NSLiteralSearch range:NSMakeRange(0, [controlFile length])];
  47. [controlFile writeToFile:controlPath atomically:TRUE];
  48. }
  49. //at this point we have the files extracted, time to determine what needs to be changed
  50. NSArray *ignoreFiles = @[@".fauxsu", @".DS_Store"];
  51. NSArray *forbiddenRoots = @[@"etc", @"var", @"tmp"];
  52. //__block NSMutableArray *_overwriteArray = [NSMutableArray new];
  53. [self.files enumerateObjectsUsingBlock:^(InputPackageFile * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  54. if (obj.type == BSPackageFileTypeFile || obj.type == BSPackageFileTypeDirectory){
  55. //DLog(@"processing path: %@", obj.path);
  56. if ([obj.path isEqualToString:@"/private/var/mobile/Applications/"]){
  57. NSString *badPath = [tmpPath stringByAppendingPathComponent:obj.path];
  58. NSString *newPath = [tmpPath stringByAppendingPathComponent:@"Applications"];
  59. DLog(@"\n [INFO] Moving %@ to %@...", badPath, newPath);
  60. [FM moveItemAtPath:badPath toPath:newPath error:nil];
  61. [FM removeItemAtPath:[tmpPath stringByAppendingPathComponent:@"private"] error:nil];
  62. *stop = TRUE;
  63. }
  64. NSString *fullPath = [tmpPath stringByAppendingPathComponent:obj.path];
  65. if ([ignoreFiles containsObject:obj.path.lastPathComponent]){
  66. DLog(@"in ignore file list, purge");
  67. [FM removeItemAtPath:fullPath error:nil];
  68. }
  69. NSArray *pathComponents = [obj.path pathComponents];
  70. if ([pathComponents count] > 1)
  71. {
  72. NSString *rootPath = [pathComponents objectAtIndex:1];
  73. //DLog(@"\n Checking root path: %@ for file %@\n", rootPath, obj.path);
  74. if ([forbiddenRoots containsObject:rootPath])
  75. {
  76. DLog(@"\n [ERROR] package file: '%@' would overwrite symbolic link at '%@'\n\n", obj.path, rootPath);
  77. NSString *privateDir = [tmpPath stringByAppendingPathComponent:@"private"];
  78. if (![FM fileExistsAtPath:privateDir]){
  79. [FM createDirectoryAtPath:privateDir withIntermediateDirectories:TRUE attributes:nil error:nil];
  80. }
  81. //take <package_name>/[rootPath] (could be etc, var, tmp) and move to <package_name>/private/[rootPath]
  82. NSString *badPath = [tmpPath stringByAppendingPathComponent:rootPath];
  83. NSString *newPath = [privateDir stringByAppendingPathComponent:rootPath];
  84. DLog(@"\n [INFO] Moving %@ to %@...", badPath, newPath);
  85. [FM moveItemAtPath:badPath toPath:newPath error:nil];
  86. }
  87. }
  88. }
  89. }];
  90. NSString *depArchiveInfo = [NSString stringWithFormat:@"/usr/local/bin/dpkg -b %@", self.packageName];
  91. if (fakeRoot) {
  92. depArchiveInfo = [NSString stringWithFormat:@"%@ /usr/local/bin/dpkg -b %@", fakeRoot, self.packageName];
  93. }
  94. [[HelperClass returnForProcess:depArchiveInfo] componentsJoinedByString:@"\n"];
  95. //return er;
  96. }
  97. - (ErrorReturn *)errorReturnForBootstrap:(NSString *)bootstrapPath
  98. {
  99. NSArray *ignoreFiles = @[@".fauxsu", @".DS_Store"];
  100. NSArray *forbiddenRoots = @[@"etc", @"var", @"tmp"];
  101. NSFileManager *man = [NSFileManager defaultManager];
  102. __block NSInteger returnValue = 0; //0 = good to go 1 = over write warning, 2 = no go
  103. __block NSMutableArray *_overwriteArray = [NSMutableArray new];
  104. [self.files enumerateObjectsUsingBlock:^(InputPackageFile * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  105. if ([obj.fileType isEqualToString:@"file"]){
  106. NSString *fullPath = [bootstrapPath stringByAppendingPathComponent:obj.path];
  107. if ([man fileExistsAtPath:fullPath] && ![ignoreFiles containsObject:obj.basename]){
  108. //DLog(@"[WARNING] overwriting a file that already exists and isn't DS_Store or .fauxsu: %@", fullPath);
  109. [_overwriteArray addObject:obj.path];
  110. //*stop = TRUE;//return FALSE;
  111. returnValue = 1;
  112. }
  113. NSArray *pathComponents = [obj.path pathComponents];
  114. if ([pathComponents count] > 1)
  115. {
  116. NSString *rootPath = [pathComponents objectAtIndex:1];
  117. if ([forbiddenRoots containsObject:rootPath])
  118. {
  119. DLog(@"\n [ERROR] package file: '%@' would overwrite symbolic link at '%@'! Exiting!\n\n", obj.path, rootPath);
  120. *stop = TRUE;
  121. returnValue = 2;
  122. }
  123. }
  124. }
  125. }];
  126. ErrorReturn *er = [ErrorReturn new];
  127. er.returnStatus = returnValue;
  128. er.overwriteFiles = _overwriteArray;
  129. return er;
  130. }
  131. @end