InputPackage.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 returnForProcess:@"/usr/bin/which fakeroot"] componentsJoinedByString:@"\n"];
  30. NSString *pwd = [[HelperClass returnForProcess:@"/bin/pwd"] componentsJoinedByString:@"\n"];
  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. NSString *fullPath = [tmpPath stringByAppendingPathComponent:obj.path];
  56. if ([ignoreFiles containsObject:obj.path.lastPathComponent]){
  57. DLog(@"in ignore file list, purge");
  58. [FM removeItemAtPath:fullPath error:nil];
  59. }
  60. NSArray *pathComponents = [obj.path pathComponents];
  61. if ([pathComponents count] > 1)
  62. {
  63. NSString *rootPath = [pathComponents objectAtIndex:1];
  64. DLog(@"\n Checking root path: %@ for file %@\n", rootPath, obj.path);
  65. if ([forbiddenRoots containsObject:rootPath])
  66. {
  67. DLog(@"\n [ERROR] package file: '%@' would overwrite symbolic link at '%@'\n\n", obj.path, rootPath);
  68. NSString *privateDir = [tmpPath stringByAppendingPathComponent:@"private"];
  69. if (![FM fileExistsAtPath:privateDir]){
  70. [FM createDirectoryAtPath:privateDir withIntermediateDirectories:TRUE attributes:nil error:nil];
  71. }
  72. //take <package_name>/[rootPath] (could be etc, var, tmp) and move to <package_name>/private/[rootPath]
  73. NSString *badPath = [tmpPath stringByAppendingPathComponent:rootPath];
  74. NSString *newPath = [privateDir stringByAppendingPathComponent:rootPath];
  75. DLog(@"\n [INFO] Moving %@ to %@...", badPath, newPath);
  76. [FM moveItemAtPath:badPath toPath:newPath error:nil];
  77. }
  78. }
  79. }
  80. }];
  81. NSString *depArchiveInfo = [NSString stringWithFormat:@"/usr/local/bin/dpkg -b %@", self.packageName];
  82. if (fakeRoot) {
  83. depArchiveInfo = [NSString stringWithFormat:@"%@ /usr/local/bin/dpkg -b %@", fakeRoot, self.packageName];
  84. }
  85. [[HelperClass returnForProcess:depArchiveInfo] componentsJoinedByString:@"\n"];
  86. //return er;
  87. }
  88. - (ErrorReturn *)errorReturnForBootstrap:(NSString *)bootstrapPath
  89. {
  90. NSArray *ignoreFiles = @[@".fauxsu", @".DS_Store"];
  91. NSArray *forbiddenRoots = @[@"etc", @"var", @"tmp"];
  92. NSFileManager *man = [NSFileManager defaultManager];
  93. __block NSInteger returnValue = 0; //0 = good to go 1 = over write warning, 2 = no go
  94. __block NSMutableArray *_overwriteArray = [NSMutableArray new];
  95. [self.files enumerateObjectsUsingBlock:^(InputPackageFile * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  96. if ([obj.fileType isEqualToString:@"file"]){
  97. NSString *fullPath = [bootstrapPath stringByAppendingPathComponent:obj.path];
  98. if ([man fileExistsAtPath:fullPath] && ![ignoreFiles containsObject:obj.basename]){
  99. //DLog(@"[WARNING] overwriting a file that already exists and isn't DS_Store or .fauxsu: %@", fullPath);
  100. [_overwriteArray addObject:obj.path];
  101. //*stop = TRUE;//return FALSE;
  102. returnValue = 1;
  103. }
  104. NSArray *pathComponents = [obj.path pathComponents];
  105. if ([pathComponents count] > 1)
  106. {
  107. NSString *rootPath = [pathComponents objectAtIndex:1];
  108. if ([forbiddenRoots containsObject:rootPath])
  109. {
  110. DLog(@"\n [ERROR] package file: '%@' would overwrite symbolic link at '%@'! Exiting!\n\n", obj.path, rootPath);
  111. *stop = TRUE;
  112. returnValue = 2;
  113. }
  114. }
  115. }
  116. }];
  117. ErrorReturn *er = [ErrorReturn new];
  118. er.returnStatus = returnValue;
  119. er.overwriteFiles = _overwriteArray;
  120. return er;
  121. }
  122. @end