blob: 48a9552a559ce065a4b055d0039a9c1884f3e22c [file] [log] [blame]
Dees_Troy38bd7602012-09-14 13:33:53 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
Dees_Troy38bd7602012-09-14 13:33:53 -04004#include <unistd.h>
5#include <vector>
6#include <dirent.h>
7#include <time.h>
Dees_Troy43d8b002012-09-17 16:00:01 -04008#include <errno.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -05009#include <fcntl.h>
10#include <sys/mount.h>
Dees_Troya58bead2012-09-27 09:49:29 -040011#include <sys/reboot.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050012#include <sys/sendfile.h>
13#include <sys/stat.h>
14#include <sys/vfs.h>
Dees_Troy83bd4832013-05-04 12:39:56 +000015#include <sys/types.h>
16#include <sys/wait.h>
Dees_Troya4438782013-02-22 18:44:00 +000017#ifdef ANDROID_RB_POWEROFF
18 #include "cutils/android_reboot.h"
19#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -050020#include <iostream>
21#include <fstream>
Dees_Troy83bd4832013-05-04 12:39:56 +000022#include <sstream>
Dees_Troy38bd7602012-09-14 13:33:53 -040023#include "twrp-functions.hpp"
24#include "partitions.hpp"
Dees_Troy2673cec2013-04-02 20:22:16 +000025#include "twcommon.h"
Dees_Troyb46a6842012-09-25 11:06:46 -040026#include "data.hpp"
Dees_Troy3477d712012-09-27 15:44:01 -040027#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000028#include "bootloader.h"
Dees_Troy83bd4832013-05-04 12:39:56 +000029#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
30 #include "openaes/inc/oaes_lib.h"
31#endif
Dees_Troy38bd7602012-09-14 13:33:53 -040032
Dees_Troyb05ddee2013-01-28 20:24:50 +000033extern "C" {
34 #include "libcrecovery/common.h"
35}
36
bigbiff bigbiff9c754052013-01-09 09:09:08 -050037/* Execute a command */
bigbiff bigbiff9c754052013-01-09 09:09:08 -050038int TWFunc::Exec_Cmd(string cmd, string &result) {
Dees_Troy29a06352013-08-24 12:06:47 +000039 FILE* exec;
40 char buffer[130];
41 int ret = 0;
42 exec = __popen(cmd.c_str(), "r");
43 if (!exec) return -1;
44 while(!feof(exec)) {
45 memset(&buffer, 0, sizeof(buffer));
46 if (fgets(buffer, 128, exec) != NULL) {
47 buffer[128] = '\n';
48 buffer[129] = NULL;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050049 result += buffer;
Dees_Troyb05ddee2013-01-28 20:24:50 +000050 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -050051 }
Dees_Troy29a06352013-08-24 12:06:47 +000052 ret = __pclose(exec);
53 return ret;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050054}
55
Dees_Troy38bd7602012-09-14 13:33:53 -040056// Returns "file.name" from a full /path/to/file.name
57string TWFunc::Get_Filename(string Path) {
58 size_t pos = Path.find_last_of("/");
59 if (pos != string::npos) {
60 string Filename;
61 Filename = Path.substr(pos + 1, Path.size() - pos - 1);
62 return Filename;
63 } else
64 return Path;
65}
66
67// Returns "/path/to/" from a full /path/to/file.name
68string TWFunc::Get_Path(string Path) {
69 size_t pos = Path.find_last_of("/");
70 if (pos != string::npos) {
71 string Pathonly;
72 Pathonly = Path.substr(0, pos + 1);
73 return Pathonly;
74 } else
75 return Path;
76}
77
78// Returns "/path" from a full /path/to/file.name
79string TWFunc::Get_Root_Path(string Path) {
80 string Local_Path = Path;
81
82 // Make sure that we have a leading slash
83 if (Local_Path.substr(0, 1) != "/")
84 Local_Path = "/" + Local_Path;
85
86 // Trim the path to get the root path only
87 size_t position = Local_Path.find("/", 2);
88 if (position != string::npos) {
89 Local_Path.resize(position);
90 }
91 return Local_Path;
92}
93
94void TWFunc::install_htc_dumlock(void) {
Dees_Troy38bd7602012-09-14 13:33:53 -040095 int need_libs = 0;
96
97 if (!PartitionManager.Mount_By_Path("/system", true))
98 return;
99
100 if (!PartitionManager.Mount_By_Path("/data", true))
101 return;
102
Dees_Troy2673cec2013-04-02 20:22:16 +0000103 gui_print("Installing HTC Dumlock to system...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500104 copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755);
Dees_Troy8170a922012-09-18 15:40:25 -0400105 if (!Path_Exists("/system/bin/flash_image")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000106 gui_print("Installing flash_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500107 copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400108 need_libs = 1;
109 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000110 gui_print("flash_image is already installed, skipping...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400111 if (!Path_Exists("/system/bin/dump_image")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000112 gui_print("Installing dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500113 copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400114 need_libs = 1;
115 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000116 gui_print("dump_image is already installed, skipping...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400117 if (need_libs) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000118 gui_print("Installing libs needed for flash_image and dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500119 copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755);
120 copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755);
121 copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755);
122 copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400123 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000124 gui_print("Installing HTC Dumlock app...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400125 mkdir("/data/app", 0777);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500126 unlink("/data/app/com.teamwin.htcdumlock*");
127 copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
Dees_Troy38bd7602012-09-14 13:33:53 -0400128 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000129 gui_print("HTC Dumlock is installed.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400130}
131
132void TWFunc::htc_dumlock_restore_original_boot(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500133 string status;
Dees_Troy38bd7602012-09-14 13:33:53 -0400134 if (!PartitionManager.Mount_By_Path("/sdcard", true))
135 return;
136
Dees_Troy2673cec2013-04-02 20:22:16 +0000137 gui_print("Restoring original boot...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500138 Exec_Cmd("htcdumlock restore", status);
Dees_Troy2673cec2013-04-02 20:22:16 +0000139 gui_print("Original boot restored.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400140}
141
142void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500143 string status;
Dees_Troy38bd7602012-09-14 13:33:53 -0400144 if (!PartitionManager.Mount_By_Path("/sdcard", true))
145 return;
Dees_Troy2673cec2013-04-02 20:22:16 +0000146 gui_print("Reflashing recovery to boot...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500147 Exec_Cmd("htcdumlock recovery noreboot", status);
Dees_Troy2673cec2013-04-02 20:22:16 +0000148 gui_print("Recovery is flashed to boot.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400149}
Dees_Troy43d8b002012-09-17 16:00:01 -0400150
151int TWFunc::Recursive_Mkdir(string Path) {
152 string pathCpy = Path;
153 string wholePath;
154 size_t pos = pathCpy.find("/", 2);
155
156 while (pos != string::npos)
157 {
158 wholePath = pathCpy.substr(0, pos);
159 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000160 LOGERR("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno);
Dees_Troy43d8b002012-09-17 16:00:01 -0400161 return false;
162 }
163
164 pos = pathCpy.find("/", pos + 1);
165 }
166 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST)
167 return false;
168 return true;
169}
170
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100171unsigned long long TWFunc::Get_Folder_Size(const string& Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400172 DIR* d;
173 struct dirent* de;
174 struct stat st;
Dees_Troy43d8b002012-09-17 16:00:01 -0400175 unsigned long long dusize = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500176 unsigned long long dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400177
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100178 d = opendir(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400179 if (d == NULL)
180 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000181 LOGERR("error opening '%s'\n", Path.c_str());
182 LOGERR("error: %s\n", strerror(errno));
Dees_Troy43d8b002012-09-17 16:00:01 -0400183 return 0;
184 }
185
186 while ((de = readdir(d)) != NULL)
187 {
bigbiff bigbiff616afed2013-07-11 18:50:29 -0400188 if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0 && strcmp(de->d_name, "lost+found") != 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400189 {
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100190 dutemp = Get_Folder_Size((Path + "/" + de->d_name), Display_Error);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500191 dusize += dutemp;
192 dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400193 }
194 else if (de->d_type == DT_REG)
195 {
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100196 stat((Path + "/" + de->d_name).c_str(), &st);
Dees_Troy43d8b002012-09-17 16:00:01 -0400197 dusize += (unsigned long long)(st.st_size);
198 }
199 }
200 closedir(d);
Dees_Troy43d8b002012-09-17 16:00:01 -0400201 return dusize;
202}
203
204bool TWFunc::Path_Exists(string Path) {
205 // Check to see if the Path exists
Dees_Troy7c2dec82012-09-26 09:49:14 -0400206 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -0400207 if (stat(Path.c_str(), &st) != 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400208 return false;
209 else
210 return true;
Dees_Troyb46a6842012-09-25 11:06:46 -0400211}
212
213void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) {
214 string Display_Text;
215
216 DataManager::GetValue(Read_Value, Display_Text);
217 if (Display_Text.empty())
218 Display_Text = Default_Text;
219
220 DataManager::SetValue("tw_operation", Display_Text);
221 DataManager::SetValue("tw_partition", "");
222}
223
224void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) {
225 string Display_Text;
226
227 DataManager::GetValue(Read_Value, Display_Text);
228 if (Display_Text.empty())
229 Display_Text = Default_Text;
230
231 DataManager::SetValue("tw_operation", Display_Text);
232 DataManager::SetValue("tw_partition", Partition_Name);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400233}
234
235unsigned long TWFunc::Get_File_Size(string Path) {
236 struct stat st;
237
238 if (stat(Path.c_str(), &st) != 0)
239 return 0;
240 return st.st_size;
Dees_Troya58bead2012-09-27 09:49:29 -0400241}
242
Dees_Troy2673cec2013-04-02 20:22:16 +0000243void TWFunc::Copy_Log(string Source, string Destination) {
244 FILE *destination_log = fopen(Destination.c_str(), "a");
245 if (destination_log == NULL) {
246 LOGERR("TWFunc::Copy_Log -- Can't open destination log file: '%s'\n", Destination.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600247 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000248 FILE *source_log = fopen(Source.c_str(), "r");
249 if (source_log != NULL) {
250 fseek(source_log, Log_Offset, SEEK_SET);
251 char buffer[4096];
252 while (fgets(buffer, sizeof(buffer), source_log))
253 fputs(buffer, destination_log); // Buffered write of log file
254 Log_Offset = ftell(source_log);
255 fflush(source_log);
256 fclose(source_log);
Dees_Troy6ef66352013-02-21 08:26:57 -0600257 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000258 fflush(destination_log);
259 fclose(destination_log);
Dees_Troy6ef66352013-02-21 08:26:57 -0600260 }
Dees_Troya58bead2012-09-27 09:49:29 -0400261}
262
Dees_Troy2673cec2013-04-02 20:22:16 +0000263void TWFunc::Update_Log_File(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500264 // Copy logs to cache so the system can find out what happened.
Dees_Troy2673cec2013-04-02 20:22:16 +0000265 Copy_Log(TMP_LOG_FILE, "/cache/recovery/log");
266 copy_file("/cache/recovery/log", "/cache/recovery/last_log", 600);
267 chown("/cache/recovery/log", 1000, 1000);
268 chmod("/cache/recovery/log", 0600);
269 chmod("/cache/recovery/last_log", 0640);
Dees_Troya58bead2012-09-27 09:49:29 -0400270
Dees_Troy2673cec2013-04-02 20:22:16 +0000271 // Reset bootloader message
272 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
273 if (Part != NULL) {
274 struct bootloader_message boot;
275 memset(&boot, 0, sizeof(boot));
276 if (Part->Current_File_System == "mtd") {
277 if (set_bootloader_message_mtd_name(&boot, Part->MTD_Name.c_str()) != 0)
278 LOGERR("Unable to set MTD bootloader message.\n");
279 } else if (Part->Current_File_System == "emmc") {
280 if (set_bootloader_message_block_name(&boot, Part->Actual_Block_Device.c_str()) != 0)
281 LOGERR("Unable to set emmc bootloader message.\n");
282 } else {
283 LOGERR("Unknown file system for /misc: '%s'\n", Part->Current_File_System.c_str());
284 }
285 }
Dees_Troya58bead2012-09-27 09:49:29 -0400286
Dees_Troy2673cec2013-04-02 20:22:16 +0000287 if (!PartitionManager.Mount_By_Path("/cache", true) || (unlink("/cache/recovery/command") && errno != ENOENT)) {
288 LOGINFO("Can't unlink %s\n", "/cache/recovery/command");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500289 }
Dees_Troya58bead2012-09-27 09:49:29 -0400290
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500291 PartitionManager.UnMount_By_Path("/cache", true);
Dees_Troy2673cec2013-04-02 20:22:16 +0000292 sync();
293}
294
295void TWFunc::Update_Intent_File(string Intent) {
296 if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) {
297 TWFunc::write_file("/cache/recovery/intent", Intent);
298 }
Dees_Troya58bead2012-09-27 09:49:29 -0400299}
300
301// reboot: Reboot the system. Return -1 on error, no return on success
302int TWFunc::tw_reboot(RebootCommand command)
303{
304 // Always force a sync before we reboot
Dees_Troy6ef66352013-02-21 08:26:57 -0600305 sync();
Dees_Troya58bead2012-09-27 09:49:29 -0400306
Dees_Troy2673cec2013-04-02 20:22:16 +0000307 switch (command) {
308 case rb_current:
309 case rb_system:
310 Update_Log_File();
311 Update_Intent_File("s");
312 sync();
313 check_and_run_script("/sbin/rebootsystem.sh", "reboot system");
314 return reboot(RB_AUTOBOOT);
315 case rb_recovery:
316 check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery");
317 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery");
318 case rb_bootloader:
319 check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader");
320 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader");
321 case rb_poweroff:
322 check_and_run_script("/sbin/poweroff.sh", "power off");
Dees_Troya4438782013-02-22 18:44:00 +0000323#ifdef ANDROID_RB_POWEROFF
Dees_Troy2673cec2013-04-02 20:22:16 +0000324 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
Dees_Troya4438782013-02-22 18:44:00 +0000325#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000326 return reboot(RB_POWER_OFF);
327 case rb_download:
328 check_and_run_script("/sbin/rebootdownload.sh", "reboot download");
329 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download");
330 default:
331 return -1;
Dees_Troy6ef66352013-02-21 08:26:57 -0600332 }
333 return -1;
Dees_Troya58bead2012-09-27 09:49:29 -0400334}
335
336void TWFunc::check_and_run_script(const char* script_file, const char* display_name)
337{
338 // Check for and run startup script if script exists
339 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500340 string result;
Dees_Troya58bead2012-09-27 09:49:29 -0400341 if (stat(script_file, &st) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000342 gui_print("Running %s script...\n", display_name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500343 chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
344 TWFunc::Exec_Cmd(script_file, result);
Dees_Troy2673cec2013-04-02 20:22:16 +0000345 gui_print("\nFinished running %s script.\n", display_name);
Dees_Troya58bead2012-09-27 09:49:29 -0400346 }
Dees_Troy3477d712012-09-27 15:44:01 -0400347}
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500348
349int TWFunc::removeDir(const string path, bool skipParent) {
Dees_Troyce675462013-01-09 19:48:21 +0000350 DIR *d = opendir(path.c_str());
351 int r = 0;
352 string new_path;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500353
Dees_Troyce675462013-01-09 19:48:21 +0000354 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000355 LOGERR("Error opening '%s'\n", path.c_str());
Dees_Troyce675462013-01-09 19:48:21 +0000356 return -1;
357 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500358
Dees_Troyce675462013-01-09 19:48:21 +0000359 if (d) {
360 struct dirent *p;
361 while (!r && (p = readdir(d))) {
Dees_Troyce675462013-01-09 19:48:21 +0000362 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
363 continue;
364 new_path = path + "/";
365 new_path.append(p->d_name);
366 if (p->d_type == DT_DIR) {
367 r = removeDir(new_path, true);
368 if (!r) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500369 if (p->d_type == DT_DIR)
370 r = rmdir(new_path.c_str());
371 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000372 LOGINFO("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500373 }
bigbiff bigbiff98f1f902013-01-19 18:46:13 -0500374 } else if (p->d_type == DT_REG || p->d_type == DT_LNK || p->d_type == DT_FIFO || p->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +0000375 r = unlink(new_path.c_str());
Dees_Troye34c1332013-02-06 19:13:00 +0000376 if (r != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000377 LOGINFO("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno));
Dees_Troye34c1332013-02-06 19:13:00 +0000378 }
Dees_Troyce675462013-01-09 19:48:21 +0000379 }
380 }
381 closedir(d);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500382
383 if (!r) {
384 if (skipParent)
385 return 0;
386 else
387 r = rmdir(path.c_str());
388 }
Dees_Troyce675462013-01-09 19:48:21 +0000389 }
390 return r;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500391}
392
393int TWFunc::copy_file(string src, string dst, int mode) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000394 LOGINFO("Copying file %s to %s\n", src.c_str(), dst.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500395 ifstream srcfile(src.c_str(), ios::binary);
396 ofstream dstfile(dst.c_str(), ios::binary);
397 dstfile << srcfile.rdbuf();
398 srcfile.close();
399 dstfile.close();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500400 if (chmod(dst.c_str(), mode) != 0)
401 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500402 return 0;
403}
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000404
405unsigned int TWFunc::Get_D_Type_From_Stat(string Path) {
406 struct stat st;
407
408 stat(Path.c_str(), &st);
409 if (st.st_mode & S_IFDIR)
410 return DT_DIR;
411 else if (st.st_mode & S_IFBLK)
412 return DT_BLK;
413 else if (st.st_mode & S_IFCHR)
414 return DT_CHR;
415 else if (st.st_mode & S_IFIFO)
416 return DT_FIFO;
417 else if (st.st_mode & S_IFLNK)
418 return DT_LNK;
419 else if (st.st_mode & S_IFREG)
420 return DT_REG;
421 else if (st.st_mode & S_IFSOCK)
422 return DT_SOCK;
423 return DT_UNKNOWN;
Dees_Troye34c1332013-02-06 19:13:00 +0000424}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500425
426int TWFunc::read_file(string fn, string& results) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200427 ifstream file;
428 file.open(fn.c_str(), ios::in);
429
430 if (file.is_open()) {
431 file >> results;
432 file.close();
433 return 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500434 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200435
436 LOGINFO("Cannot find file %s\n", fn.c_str());
437 return -1;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500438}
439
440int TWFunc::read_file(string fn, vector<string>& results) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500441 ifstream file;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500442 string line;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500443 file.open(fn.c_str(), ios::in);
444 if (file.is_open()) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500445 while (getline(file, line))
446 results.push_back(line);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500447 file.close();
448 return 0;
449 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000450 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500451 return -1;
452}
453
454int TWFunc::write_file(string fn, string& line) {
455 FILE *file;
456 file = fopen(fn.c_str(), "w");
457 if (file != NULL) {
458 fwrite(line.c_str(), line.size(), 1, file);
459 fclose(file);
460 return 0;
461 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000462 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500463 return -1;
464}
465
Dees_Troy83bd4832013-05-04 12:39:56 +0000466vector<string> TWFunc::split_string(const string &in, char del, bool skip_empty) {
467 vector<string> res;
468
469 if (in.empty() || del == '\0')
470 return res;
471
472 string field;
473 istringstream f(in);
474 if (del == '\n') {
475 while(getline(f, field)) {
476 if (field.empty() && skip_empty)
477 continue;
478 res.push_back(field);
479 }
480 } else {
481 while(getline(f, field, del)) {
482 if (field.empty() && skip_empty)
483 continue;
484 res.push_back(field);
485 }
486 }
487 return res;
488}
489
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500490timespec TWFunc::timespec_diff(timespec& start, timespec& end)
491{
Dees_Troy6ef66352013-02-21 08:26:57 -0600492 timespec temp;
493 if ((end.tv_nsec-start.tv_nsec)<0) {
494 temp.tv_sec = end.tv_sec-start.tv_sec-1;
495 temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
496 } else {
497 temp.tv_sec = end.tv_sec-start.tv_sec;
498 temp.tv_nsec = end.tv_nsec-start.tv_nsec;
499 }
500 return temp;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500501}
502
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200503int TWFunc::drop_caches(void) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600504 string file = "/proc/sys/vm/drop_caches";
505 string value = "3";
506 if (write_file(file, value) != 0)
507 return -1;
508 return 0;
509}
510
511int TWFunc::Check_su_Perms(void) {
512 struct stat st;
513 int ret = 0;
514
515 if (!PartitionManager.Mount_By_Path("/system", false))
516 return 0;
517
518 // Check to ensure that perms are 6755 for all 3 file locations
519 if (stat("/system/bin/su", &st) == 0) {
520 if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) {
521 ret = 1;
522 }
523 }
524 if (stat("/system/xbin/su", &st) == 0) {
525 if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) {
526 ret += 2;
527 }
528 }
529 if (stat("/system/bin/.ext/.su", &st) == 0) {
530 if ((st.st_mode & (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) != (S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) || st.st_uid != 0 || st.st_gid != 0) {
531 ret += 4;
532 }
533 }
534 return ret;
535}
536
537bool TWFunc::Fix_su_Perms(void) {
538 if (!PartitionManager.Mount_By_Path("/system", true))
539 return false;
540
541 string file = "/system/bin/su";
542 if (TWFunc::Path_Exists(file)) {
543 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000544 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600545 return false;
546 }
547 if (tw_chmod(file, "6755") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000548 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600549 return false;
550 }
551 }
552 file = "/system/xbin/su";
553 if (TWFunc::Path_Exists(file)) {
554 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000555 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600556 return false;
557 }
558 if (tw_chmod(file, "6755") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000559 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600560 return false;
561 }
562 }
Dees_Troya7939bb2013-08-29 20:21:12 +0000563 file = "/system/xbin/daemonsu";
564 if (TWFunc::Path_Exists(file)) {
565 if (chown(file.c_str(), 0, 0) != 0) {
566 LOGERR("Failed to chown '%s'\n", file.c_str());
567 return false;
568 }
569 if (tw_chmod(file, "6755") != 0) {
570 LOGERR("Failed to chmod '%s'\n", file.c_str());
571 return false;
572 }
573 }
Dees_Troy6ef66352013-02-21 08:26:57 -0600574 file = "/system/bin/.ext/.su";
575 if (TWFunc::Path_Exists(file)) {
576 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000577 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600578 return false;
579 }
580 if (tw_chmod(file, "6755") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000581 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600582 return false;
583 }
584 }
Dees_Troya7939bb2013-08-29 20:21:12 +0000585 file = "/system/etc/install-recovery.sh";
586 if (TWFunc::Path_Exists(file)) {
587 if (chown(file.c_str(), 0, 0) != 0) {
588 LOGERR("Failed to chown '%s'\n", file.c_str());
589 return false;
590 }
591 if (tw_chmod(file, "0755") != 0) {
592 LOGERR("Failed to chmod '%s'\n", file.c_str());
593 return false;
594 }
595 }
596 file = "/system/etc/init.d/99SuperSUDaemon";
597 if (TWFunc::Path_Exists(file)) {
598 if (chown(file.c_str(), 0, 0) != 0) {
599 LOGERR("Failed to chown '%s'\n", file.c_str());
600 return false;
601 }
602 if (tw_chmod(file, "0755") != 0) {
603 LOGERR("Failed to chmod '%s'\n", file.c_str());
604 return false;
605 }
606 }
Dees_Troy6ef66352013-02-21 08:26:57 -0600607 file = "/system/app/Superuser.apk";
608 if (TWFunc::Path_Exists(file)) {
609 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000610 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600611 return false;
612 }
613 if (tw_chmod(file, "0644") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000614 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600615 return false;
616 }
617 }
618 sync();
619 if (!PartitionManager.UnMount_By_Path("/system", true))
620 return false;
621 return true;
622}
623
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200624int TWFunc::tw_chmod(const string& fn, const string& mode) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600625 long mask = 0;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200626 std::string::size_type n = mode.length();
627 int cls = 0;
Dees_Troy6ef66352013-02-21 08:26:57 -0600628
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200629 if(n == 3)
630 ++cls;
631 else if(n != 4)
632 {
633 LOGERR("TWFunc::tw_chmod used with %u long mode string (should be 3 or 4)!\n", mode.length());
634 return -1;
635 }
636
637 for (n = 0; n < mode.length(); ++n, ++cls) {
638 if (cls == 0) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600639 if (mode[n] == '0')
640 continue;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200641 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600642 mask |= S_ISVTX;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200643 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600644 mask |= S_ISGID;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200645 else if (mode[n] == '4')
Dees_Troy6ef66352013-02-21 08:26:57 -0600646 mask |= S_ISUID;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200647 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600648 mask |= S_ISVTX;
649 mask |= S_ISUID;
650 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200651 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600652 mask |= S_ISGID;
653 mask |= S_ISUID;
654 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200655 else if (mode[n] == '7') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600656 mask |= S_ISVTX;
657 mask |= S_ISGID;
658 mask |= S_ISUID;
659 }
660 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200661 else if (cls == 1) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600662 if (mode[n] == '7') {
663 mask |= S_IRWXU;
664 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200665 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600666 mask |= S_IRUSR;
667 mask |= S_IWUSR;
668 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200669 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600670 mask |= S_IRUSR;
671 mask |= S_IXUSR;
672 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200673 else if (mode[n] == '4')
Dees_Troy6ef66352013-02-21 08:26:57 -0600674 mask |= S_IRUSR;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200675 else if (mode[n] == '3') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600676 mask |= S_IWUSR;
677 mask |= S_IRUSR;
678 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200679 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600680 mask |= S_IWUSR;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200681 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600682 mask |= S_IXUSR;
683 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200684 else if (cls == 2) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600685 if (mode[n] == '7') {
686 mask |= S_IRWXG;
687 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200688 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600689 mask |= S_IRGRP;
690 mask |= S_IWGRP;
691 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200692 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600693 mask |= S_IRGRP;
694 mask |= S_IXGRP;
695 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200696 else if (mode[n] == '4')
Dees_Troy6ef66352013-02-21 08:26:57 -0600697 mask |= S_IRGRP;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200698 else if (mode[n] == '3') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600699 mask |= S_IWGRP;
700 mask |= S_IXGRP;
701 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200702 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600703 mask |= S_IWGRP;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200704 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600705 mask |= S_IXGRP;
706 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200707 else if (cls == 3) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600708 if (mode[n] == '7') {
709 mask |= S_IRWXO;
710 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200711 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600712 mask |= S_IROTH;
713 mask |= S_IWOTH;
714 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200715 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600716 mask |= S_IROTH;
717 mask |= S_IXOTH;
718 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200719 else if (mode[n] == '4')
720 mask |= S_IROTH;
721 else if (mode[n] == '3') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600722 mask |= S_IWOTH;
723 mask |= S_IXOTH;
724 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200725 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600726 mask |= S_IWOTH;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200727 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600728 mask |= S_IXOTH;
729 }
730 }
731
732 if (chmod(fn.c_str(), mask) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000733 LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask);
Dees_Troy6ef66352013-02-21 08:26:57 -0600734 return -1;
735 }
736
737 return 0;
738}
739
740bool TWFunc::Install_SuperSU(void) {
Dees_Troya7939bb2013-08-29 20:21:12 +0000741 string status;
742
Dees_Troy6ef66352013-02-21 08:26:57 -0600743 if (!PartitionManager.Mount_By_Path("/system", true))
744 return false;
745
Dees_Troya7939bb2013-08-29 20:21:12 +0000746 TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/su", status);
jt1134113ee732013-02-22 23:26:10 -0600747 if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000748 LOGERR("Failed to copy su binary to /system/bin\n");
Dees_Troy6ef66352013-02-21 08:26:57 -0600749 return false;
750 }
Dees_Troya7939bb2013-08-29 20:21:12 +0000751 if (!Path_Exists("/system/bin/.ext")) {
752 mkdir("/system/bin/.ext", 0777);
753 }
754 TWFunc::Exec_Cmd("/sbin/chattr -i /system/bin/.ext/su", status);
755 if (copy_file("/supersu/su", "/system/bin/.ext/su", 0755) != 0) {
756 LOGERR("Failed to copy su binary to /system/bin/.ext/su\n");
757 return false;
758 }
759 TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/daemonsu", status);
760 if (copy_file("/supersu/su", "/system/xbin/daemonsu", 0755) != 0) {
761 LOGERR("Failed to copy su binary to /system/xbin/daemonsu\n");
762 return false;
763 }
764 if (Path_Exists("/system/etc/init.d")) {
765 TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/init.d/99SuperSUDaemon", status);
766 if (copy_file("/supersu/99SuperSUDaemon", "/system/etc/init.d/99SuperSUDaemon", 0755) != 0) {
767 LOGERR("Failed to copy 99SuperSUDaemon to /system/etc/init.d/99SuperSUDaemon\n");
768 return false;
769 }
770 } else {
771 TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/install-recovery.sh", status);
772 if (copy_file("/supersu/install-recovery.sh", "/system/etc/install-recovery.sh", 0755) != 0) {
773 LOGERR("Failed to copy install-recovery.sh to /system/etc/install-recovery.sh\n");
774 return false;
775 }
776 }
jt1134113ee732013-02-22 23:26:10 -0600777 if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000778 LOGERR("Failed to copy Superuser app to /system/app\n");
Dees_Troy6ef66352013-02-21 08:26:57 -0600779 return false;
780 }
781 if (!Fix_su_Perms())
782 return false;
783 return true;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500784}
Dees_Troy83bd4832013-05-04 12:39:56 +0000785
786int TWFunc::Get_File_Type(string fn) {
787 string::size_type i = 0;
788 int firstbyte = 0, secondbyte = 0;
789 char header[3];
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200790
Dees_Troy83bd4832013-05-04 12:39:56 +0000791 ifstream f;
792 f.open(fn.c_str(), ios::in | ios::binary);
793 f.get(header, 3);
794 f.close();
795 firstbyte = header[i] & 0xff;
796 secondbyte = header[++i] & 0xff;
797
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200798 if (firstbyte == 0x1f && secondbyte == 0x8b)
Dees_Troy83bd4832013-05-04 12:39:56 +0000799 return 1; // Compressed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200800 else if (firstbyte == 0x4f && secondbyte == 0x41)
Dees_Troy83bd4832013-05-04 12:39:56 +0000801 return 2; // Encrypted
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200802 else
Dees_Troy83bd4832013-05-04 12:39:56 +0000803 return 0; // Unknown
Dees_Troy83bd4832013-05-04 12:39:56 +0000804
805 return 0;
806}
807
808int TWFunc::Try_Decrypting_File(string fn, string password) {
809#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
810 OAES_CTX * ctx = NULL;
811 uint8_t _key_data[32] = "";
812 FILE *f;
813 uint8_t buffer[4096];
814 uint8_t *buffer_out = NULL;
815 uint8_t *ptr = NULL;
816 size_t read_len = 0, out_len = 0;
817 int firstbyte = 0, secondbyte = 0, key_len;
818 size_t _j = 0;
819 size_t _key_data_len = 0;
820
821 // mostly kanged from OpenAES oaes.c
822 for( _j = 0; _j < 32; _j++ )
823 _key_data[_j] = _j + 1;
824 _key_data_len = password.size();
825 if( 16 >= _key_data_len )
826 _key_data_len = 16;
827 else if( 24 >= _key_data_len )
828 _key_data_len = 24;
829 else
830 _key_data_len = 32;
831 memcpy(_key_data, password.c_str(), password.size());
832
833 ctx = oaes_alloc();
834 if (ctx == NULL) {
835 LOGERR("Failed to allocate OAES\n");
836 return -1;
837 }
838
839 oaes_key_import_data(ctx, _key_data, _key_data_len);
840
841 f = fopen(fn.c_str(), "rb");
842 if (f == NULL) {
843 LOGERR("Failed to open '%s' to try decrypt\n", fn.c_str());
844 return -1;
845 }
846 read_len = fread(buffer, sizeof(uint8_t), 4096, f);
847 if (read_len <= 0) {
848 LOGERR("Read size during try decrypt failed\n");
849 fclose(f);
850 return -1;
851 }
852 if (oaes_decrypt(ctx, buffer, read_len, NULL, &out_len) != OAES_RET_SUCCESS) {
853 LOGERR("Error: Failed to retrieve required buffer size for trying decryption.\n");
854 fclose(f);
855 return -1;
856 }
857 buffer_out = (uint8_t *) calloc(out_len, sizeof(char));
858 if (buffer_out == NULL) {
859 LOGERR("Failed to allocate output buffer for try decrypt.\n");
860 fclose(f);
861 return -1;
862 }
863 if (oaes_decrypt(ctx, buffer, read_len, buffer_out, &out_len) != OAES_RET_SUCCESS) {
864 LOGERR("Failed to decrypt file '%s'\n", fn.c_str());
865 fclose(f);
866 free(buffer_out);
867 return 0;
868 }
869 fclose(f);
870 if (out_len < 2) {
871 LOGINFO("Successfully decrypted '%s' but read length %i too small.\n", fn.c_str(), out_len);
872 free(buffer_out);
873 return 1; // Decrypted successfully
874 }
875 ptr = buffer_out;
876 firstbyte = *ptr & 0xff;
877 ptr++;
878 secondbyte = *ptr & 0xff;
879 if (firstbyte == 0x1f && secondbyte == 0x8b) {
880 LOGINFO("Successfully decrypted '%s' and file is compressed.\n", fn.c_str());
881 free(buffer_out);
882 return 3; // Compressed
883 }
884 if (out_len >= 262) {
885 ptr = buffer_out + 257;
886 if (strncmp((char*)ptr, "ustar", 5) == 0) {
887 LOGINFO("Successfully decrypted '%s' and file is tar format.\n", fn.c_str());
888 free(buffer_out);
889 return 2; // Tar
890 }
891 }
892 free(buffer_out);
893 LOGINFO("No errors decrypting '%s' but no known file format.\n", fn.c_str());
894 return 1; // Decrypted successfully
895#else
896 LOGERR("Encrypted backup support not included.\n");
897 return -1;
898#endif
899}
900
901bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) {
902 DIR* d;
903
904 string Filename;
905 Restore_Path += "/";
906 d = opendir(Restore_Path.c_str());
907 if (d == NULL) {
908 LOGERR("Error opening '%s'\n", Restore_Path.c_str());
909 return false;
910 }
911
912 struct dirent* de;
913 while ((de = readdir(d)) != NULL) {
914 Filename = Restore_Path;
915 Filename += de->d_name;
916 if (TWFunc::Get_File_Type(Filename) == 2) {
917 if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) {
918 DataManager::SetValue("tw_restore_password", ""); // Clear the bad password
919 DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask
920 closedir(d);
921 return false;
922 }
923 }
924 }
925 closedir(d);
926 return true;
927}
928
929int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name) {
930 pid_t rc_pid;
931
932 rc_pid = waitpid(pid, status, 0);
933 if (rc_pid > 0) {
934 if (WEXITSTATUS(*status) == 0)
935 LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success
936 else if (WIFSIGNALED(*status)) {
937 LOGINFO("%s process ended with signal: %d\n", Child_Name.c_str(), WTERMSIG(*status)); // Seg fault or some other non-graceful termination
938 return -1;
939 } else if (WEXITSTATUS(*status) != 0) {
940 LOGINFO("%s process ended with ERROR=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Graceful exit, but there was an error
941 return -1;
942 }
943 } else { // no PID returned
944 if (errno == ECHILD)
945 LOGINFO("%s no child process exist\n", Child_Name.c_str());
946 else {
947 LOGINFO("%s Unexpected error\n", Child_Name.c_str());
948 return -1;
949 }
950 }
951 return 0;
952}