Skip to content

Commit

Permalink
Fix idownloadd getting started during userspace reboot even if it is …
Browse files Browse the repository at this point in the history
…disabled in settings
  • Loading branch information
opa334 committed Feb 22, 2024
1 parent 0089036 commit 0ed1e6b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Application/Dopamine/Jailbreak/DOEnvironmentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)isTweakInjectionEnabled;
- (void)setTweakInjectionEnabled:(BOOL)enabled;
- (BOOL)isIDownloadEnabled;
- (void)setIDownloadEnabled:(BOOL)enabled;
- (void)setIDownloadEnabled:(BOOL)enabled needsUnsandbox:(BOOL)needsUnsandbox;
- (void)setIDownloadLoaded:(BOOL)loaded needsUnsandbox:(BOOL)needsUnsandbox;
- (BOOL)isJailbreakHidden;
- (void)setJailbreakHidden:(BOOL)hidden;

Expand Down
72 changes: 58 additions & 14 deletions Application/Dopamine/Jailbreak/DOEnvironmentManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -395,26 +395,70 @@ - (void)setTweakInjectionEnabled:(BOOL)enabled

- (BOOL)isIDownloadEnabled
{
return [[NSFileManager defaultManager] fileExistsAtPath:NSJBRootPath(@"/basebin/.idownloadd_enabled")];
__block BOOL isEnabled = NO;
[self runAsRoot:^{
[self runUnsandboxed:^{
NSDictionary *disabledDict = [NSDictionary dictionaryWithContentsOfFile:@"/var/db/com.apple.xpc.launchd/disabled.plist"];
NSNumber *idownloaddDisabledNum = disabledDict[@"com.opa334.Dopamine.idownloadd"];
if (idownloaddDisabledNum) {
isEnabled = ![idownloaddDisabledNum boolValue];
}
else {
isEnabled = NO;
}
}];
}];
return isEnabled;
}

- (void)setIDownloadEnabled:(BOOL)enabled
- (void)setIDownloadEnabled:(BOOL)enabled needsUnsandbox:(BOOL)needsUnsandbox
{
NSString *idownloaddEnabledPath = NSJBRootPath(@"/basebin/.idownloadd_enabled");
if ([self isJailbroken]) {
void (^updateBlock)(void) = ^{
if (enabled) {
exec_cmd_trusted(JBRootPath("/usr/bin/launchctl"), "enable", "system/com.opa334.Dopamine.idownloadd", NULL);
}
else {
exec_cmd_trusted(JBRootPath("/usr/bin/launchctl"), "disable", "system/com.opa334.Dopamine.idownloadd", NULL);
}
};

if (needsUnsandbox) {
[self runAsRoot:^{
[self runUnsandboxed:^{
if (enabled) {
[[NSData data] writeToFile:idownloaddEnabledPath atomically:YES];
exec_cmd(JBRootPath("/usr/bin/launchctl"), "load", JBRootPath("/basebin/LaunchDaemons/com.opa334.Dopamine.idownloadd.plist"), NULL);
}
else {
[[NSFileManager defaultManager] removeItemAtPath:idownloaddEnabledPath error:nil];
exec_cmd(JBRootPath("/usr/bin/launchctl"), "unload", JBRootPath("/basebin/LaunchDaemons/com.opa334.Dopamine.idownloadd.plist"), NULL);
}
}];
[self runUnsandboxed:updateBlock];
}];
}
else {
updateBlock();
}
}

- (void)setIDownloadLoaded:(BOOL)loaded needsUnsandbox:(BOOL)needsUnsandbox
{
if (loaded) {
[self setIDownloadEnabled:loaded needsUnsandbox:needsUnsandbox];
}

void (^updateBlock)(void) = ^{
if (loaded) {
exec_cmd(JBRootPath("/usr/bin/launchctl"), "load", JBRootPath("/basebin/LaunchDaemons/com.opa334.Dopamine.idownloadd.plist"), NULL);
}
else {
exec_cmd(JBRootPath("/usr/bin/launchctl"), "unload", JBRootPath("/basebin/LaunchDaemons/com.opa334.Dopamine.idownloadd.plist"), NULL);
}
};

if (needsUnsandbox) {
[self runAsRoot:^{
[self runUnsandboxed:updateBlock];
}];
}
else {
updateBlock();
}

if (!loaded) {
[self setIDownloadEnabled:loaded needsUnsandbox:needsUnsandbox];
}
}

- (BOOL)isJailbreakHidden
Expand Down
8 changes: 2 additions & 6 deletions Application/Dopamine/Jailbreak/DOJailbreaker.m
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,6 @@ - (void)runWithError:(NSError **)errOut didRemoveJailbreak:(BOOL*)didRemove show
[[NSData data] writeToFile:NSJBRootPath(@"/basebin/.safe_mode") atomically:YES];
}

if (idownloadEnabled) {
printf("Enabling idownloadd\n");
[[NSData data] writeToFile:NSJBRootPath(@"/basebin/.idownloadd_enabled") atomically:YES];
// This file is checked in launchd and determines whether idownloadd gets loaded after a userspace reboot or not
}

[[DOUIManager sharedInstance] sendLog:DOLocalizedString(@"Loading BaseBin TrustCache") debug:NO];
*errOut = [self loadBasebinTrustcache];
if (*errOut) return;
Expand All @@ -502,6 +496,8 @@ - (void)runWithError:(NSError **)errOut didRemoveJailbreak:(BOOL*)didRemove show
*errOut = [self finalizeBootstrapIfNeeded];
if (*errOut) return;

[[DOEnvironmentManager sharedManager] setIDownloadEnabled:idownloadEnabled needsUnsandbox:NO];

[[DOUIManager sharedInstance] sendLog:DOLocalizedString(@"Checking For Duplicate Apps") debug:NO];
*errOut = [self ensureNoDuplicateApps];
if (*errOut) {
Expand Down
2 changes: 1 addition & 1 deletion Application/Dopamine/UI/Settings/DOSettingsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ - (void)setIDownloadEnabled:(id)value specifier:(PSSpecifier *)specifier
[self setPreferenceValue:value specifier:specifier];
DOEnvironmentManager *envManager = [DOEnvironmentManager sharedManager];
if (envManager.isJailbroken) {
[[DOEnvironmentManager sharedManager] setIDownloadEnabled:((NSNumber *)value).boolValue];
[[DOEnvironmentManager sharedManager] setIDownloadLoaded:((NSNumber *)value).boolValue needsUnsandbox:YES];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
<true/>
<key>KeepAlive</key>
<true/>
<key>Disabled</key>
<true/>
</dict>
</plist>
3 changes: 0 additions & 3 deletions BaseBin/launchdhook/src/daemon_hook.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ xpc_object_t xpc_dictionary_get_value_hook(xpc_object_t xdict, const char *key)
if (xpc_get_type(origXvalue) == XPC_TYPE_DICTIONARY) {
for (NSString *daemonPlistName in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSJBRootPath(@"/basebin/LaunchDaemons") error:nil]) {
if ([daemonPlistName.pathExtension isEqualToString:@"plist"]) {
if ([daemonPlistName isEqualToString:@"com.opa334.Dopamine.idownloadd.plist"]) {
if (access(JBRootPath("/basebin/.idownloadd_enabled"), F_OK) != 0) continue;
}
xpc_dictionary_add_launch_daemon_plist_at_path(origXvalue, [NSJBRootPath(@"/basebin/LaunchDaemons") stringByAppendingPathComponent:daemonPlistName].fileSystemRepresentation);
}
}
Expand Down
5 changes: 5 additions & 0 deletions BaseBin/launchdhook/src/update.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,9 @@ void jbupdate_finalize_stage1(const char *prevVersion, const char *newVersion)
void jbupdate_finalize_stage2(const char *prevVersion, const char *newVersion)
{
jbupdate_update_system_info();

// Legacy, this file is no longer used
if (!access(JBRootPath("/basebin/.idownloadd_enabled"), F_OK)) {
remove(JBRootPath("/basebin/.idownloadd_enabled"));
}
}

0 comments on commit 0ed1e6b

Please sign in to comment.