blob: c39a6b2428ed04f6e01ed15520ae140b5346c80d [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 */
Vojtech Bocek05534202013-09-11 08:11:56 +020038int TWFunc::Exec_Cmd(const 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
Vojtech Bocek05534202013-09-11 08:11:56 +020056int TWFunc::Exec_Cmd(const string& cmd) {
57 pid_t pid;
58 int status;
59 switch(pid = fork())
60 {
61 case -1:
62 LOGERR("Exec_Cmd(): vfork failed!\n");
63 return -1;
64 case 0: // child
65 execl("/sbin/sh", "sh", "-c", cmd.c_str(), NULL);
66 _exit(127);
67 break;
68 default:
69 {
70 if (TWFunc::Wait_For_Child(pid, &status, cmd) != 0)
71 return -1;
72 else
73 return 0;
74 }
75 }
76}
77
Dees_Troy38bd7602012-09-14 13:33:53 -040078// Returns "file.name" from a full /path/to/file.name
79string TWFunc::Get_Filename(string Path) {
80 size_t pos = Path.find_last_of("/");
81 if (pos != string::npos) {
82 string Filename;
83 Filename = Path.substr(pos + 1, Path.size() - pos - 1);
84 return Filename;
85 } else
86 return Path;
87}
88
89// Returns "/path/to/" from a full /path/to/file.name
90string TWFunc::Get_Path(string Path) {
91 size_t pos = Path.find_last_of("/");
92 if (pos != string::npos) {
93 string Pathonly;
94 Pathonly = Path.substr(0, pos + 1);
95 return Pathonly;
96 } else
97 return Path;
98}
99
100// Returns "/path" from a full /path/to/file.name
101string TWFunc::Get_Root_Path(string Path) {
102 string Local_Path = Path;
103
104 // Make sure that we have a leading slash
105 if (Local_Path.substr(0, 1) != "/")
106 Local_Path = "/" + Local_Path;
107
108 // Trim the path to get the root path only
109 size_t position = Local_Path.find("/", 2);
110 if (position != string::npos) {
111 Local_Path.resize(position);
112 }
113 return Local_Path;
114}
115
116void TWFunc::install_htc_dumlock(void) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400117 int need_libs = 0;
118
119 if (!PartitionManager.Mount_By_Path("/system", true))
120 return;
121
122 if (!PartitionManager.Mount_By_Path("/data", true))
123 return;
124
Dees_Troy2673cec2013-04-02 20:22:16 +0000125 gui_print("Installing HTC Dumlock to system...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500126 copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755);
Dees_Troy8170a922012-09-18 15:40:25 -0400127 if (!Path_Exists("/system/bin/flash_image")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000128 gui_print("Installing flash_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500129 copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400130 need_libs = 1;
131 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000132 gui_print("flash_image is already installed, skipping...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400133 if (!Path_Exists("/system/bin/dump_image")) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000134 gui_print("Installing dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500135 copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400136 need_libs = 1;
137 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000138 gui_print("dump_image is already installed, skipping...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400139 if (need_libs) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000140 gui_print("Installing libs needed for flash_image and dump_image...\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500141 copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755);
142 copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755);
143 copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755);
144 copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755);
Dees_Troy38bd7602012-09-14 13:33:53 -0400145 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000146 gui_print("Installing HTC Dumlock app...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400147 mkdir("/data/app", 0777);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500148 unlink("/data/app/com.teamwin.htcdumlock*");
149 copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
Dees_Troy38bd7602012-09-14 13:33:53 -0400150 sync();
Dees_Troy2673cec2013-04-02 20:22:16 +0000151 gui_print("HTC Dumlock is installed.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400152}
153
154void TWFunc::htc_dumlock_restore_original_boot(void) {
155 if (!PartitionManager.Mount_By_Path("/sdcard", true))
156 return;
157
Dees_Troy2673cec2013-04-02 20:22:16 +0000158 gui_print("Restoring original boot...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200159 Exec_Cmd("htcdumlock restore");
Dees_Troy2673cec2013-04-02 20:22:16 +0000160 gui_print("Original boot restored.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400161}
162
163void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
164 if (!PartitionManager.Mount_By_Path("/sdcard", true))
165 return;
Dees_Troy2673cec2013-04-02 20:22:16 +0000166 gui_print("Reflashing recovery to boot...\n");
Vojtech Bocek05534202013-09-11 08:11:56 +0200167 Exec_Cmd("htcdumlock recovery noreboot");
Dees_Troy2673cec2013-04-02 20:22:16 +0000168 gui_print("Recovery is flashed to boot.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400169}
Dees_Troy43d8b002012-09-17 16:00:01 -0400170
171int TWFunc::Recursive_Mkdir(string Path) {
172 string pathCpy = Path;
173 string wholePath;
174 size_t pos = pathCpy.find("/", 2);
175
176 while (pos != string::npos)
177 {
178 wholePath = pathCpy.substr(0, pos);
179 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000180 LOGERR("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno);
Dees_Troy43d8b002012-09-17 16:00:01 -0400181 return false;
182 }
183
184 pos = pathCpy.find("/", pos + 1);
185 }
186 if (mkdir(wholePath.c_str(), 0777) && errno != EEXIST)
187 return false;
188 return true;
189}
190
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100191unsigned long long TWFunc::Get_Folder_Size(const string& Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400192 DIR* d;
193 struct dirent* de;
194 struct stat st;
Dees_Troy43d8b002012-09-17 16:00:01 -0400195 unsigned long long dusize = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500196 unsigned long long dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400197
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100198 d = opendir(Path.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400199 if (d == NULL)
200 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000201 LOGERR("error opening '%s'\n", Path.c_str());
202 LOGERR("error: %s\n", strerror(errno));
Dees_Troy43d8b002012-09-17 16:00:01 -0400203 return 0;
204 }
205
206 while ((de = readdir(d)) != NULL)
207 {
bigbiff bigbiff616afed2013-07-11 18:50:29 -0400208 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 -0400209 {
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100210 dutemp = Get_Folder_Size((Path + "/" + de->d_name), Display_Error);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500211 dusize += dutemp;
212 dutemp = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400213 }
214 else if (de->d_type == DT_REG)
215 {
Vojtech Bocek2e97ec52013-02-02 13:22:42 +0100216 stat((Path + "/" + de->d_name).c_str(), &st);
Dees_Troy43d8b002012-09-17 16:00:01 -0400217 dusize += (unsigned long long)(st.st_size);
218 }
219 }
220 closedir(d);
Dees_Troy43d8b002012-09-17 16:00:01 -0400221 return dusize;
222}
223
224bool TWFunc::Path_Exists(string Path) {
225 // Check to see if the Path exists
Dees_Troy7c2dec82012-09-26 09:49:14 -0400226 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -0400227 if (stat(Path.c_str(), &st) != 0)
Dees_Troy43d8b002012-09-17 16:00:01 -0400228 return false;
229 else
230 return true;
Dees_Troyb46a6842012-09-25 11:06:46 -0400231}
232
233void TWFunc::GUI_Operation_Text(string Read_Value, string Default_Text) {
234 string Display_Text;
235
236 DataManager::GetValue(Read_Value, Display_Text);
237 if (Display_Text.empty())
238 Display_Text = Default_Text;
239
240 DataManager::SetValue("tw_operation", Display_Text);
241 DataManager::SetValue("tw_partition", "");
242}
243
244void TWFunc::GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text) {
245 string Display_Text;
246
247 DataManager::GetValue(Read_Value, Display_Text);
248 if (Display_Text.empty())
249 Display_Text = Default_Text;
250
251 DataManager::SetValue("tw_operation", Display_Text);
252 DataManager::SetValue("tw_partition", Partition_Name);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400253}
254
255unsigned long TWFunc::Get_File_Size(string Path) {
256 struct stat st;
257
258 if (stat(Path.c_str(), &st) != 0)
259 return 0;
260 return st.st_size;
Dees_Troya58bead2012-09-27 09:49:29 -0400261}
262
Dees_Troy2673cec2013-04-02 20:22:16 +0000263void TWFunc::Copy_Log(string Source, string Destination) {
Dees Troy9d7fdf52013-09-19 20:49:25 +0000264 PartitionManager.Mount_By_Path(Destination, false);
Dees_Troy2673cec2013-04-02 20:22:16 +0000265 FILE *destination_log = fopen(Destination.c_str(), "a");
266 if (destination_log == NULL) {
267 LOGERR("TWFunc::Copy_Log -- Can't open destination log file: '%s'\n", Destination.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600268 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000269 FILE *source_log = fopen(Source.c_str(), "r");
270 if (source_log != NULL) {
271 fseek(source_log, Log_Offset, SEEK_SET);
272 char buffer[4096];
273 while (fgets(buffer, sizeof(buffer), source_log))
274 fputs(buffer, destination_log); // Buffered write of log file
275 Log_Offset = ftell(source_log);
276 fflush(source_log);
277 fclose(source_log);
Dees_Troy6ef66352013-02-21 08:26:57 -0600278 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000279 fflush(destination_log);
280 fclose(destination_log);
Dees_Troy6ef66352013-02-21 08:26:57 -0600281 }
Dees_Troya58bead2012-09-27 09:49:29 -0400282}
283
Dees_Troy2673cec2013-04-02 20:22:16 +0000284void TWFunc::Update_Log_File(void) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500285 // Copy logs to cache so the system can find out what happened.
Dees Troy9d7fdf52013-09-19 20:49:25 +0000286 if (PartitionManager.Mount_By_Path("/cache", false)) {
287 if (!TWFunc::Path_Exists("/cache/recovery/.")) {
288 LOGINFO("Recreating /cache/recovery folder.\n");
289 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
290 LOGINFO("Unable to create /cache/recovery folder.\n");
291 }
292 Copy_Log(TMP_LOG_FILE, "/cache/recovery/log");
293 copy_file("/cache/recovery/log", "/cache/recovery/last_log", 600);
294 chown("/cache/recovery/log", 1000, 1000);
295 chmod("/cache/recovery/log", 0600);
296 chmod("/cache/recovery/last_log", 0640);
297 } else {
298 LOGINFO("Failed to mount /cache for TWFunc::Update_Log_File\n");
299 }
Dees_Troya58bead2012-09-27 09:49:29 -0400300
Dees_Troy2673cec2013-04-02 20:22:16 +0000301 // Reset bootloader message
302 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
303 if (Part != NULL) {
304 struct bootloader_message boot;
305 memset(&boot, 0, sizeof(boot));
306 if (Part->Current_File_System == "mtd") {
307 if (set_bootloader_message_mtd_name(&boot, Part->MTD_Name.c_str()) != 0)
308 LOGERR("Unable to set MTD bootloader message.\n");
309 } else if (Part->Current_File_System == "emmc") {
310 if (set_bootloader_message_block_name(&boot, Part->Actual_Block_Device.c_str()) != 0)
311 LOGERR("Unable to set emmc bootloader message.\n");
312 } else {
313 LOGERR("Unknown file system for /misc: '%s'\n", Part->Current_File_System.c_str());
314 }
315 }
Dees_Troya58bead2012-09-27 09:49:29 -0400316
Dees Troy9d7fdf52013-09-19 20:49:25 +0000317 if (PartitionManager.Mount_By_Path("/cache", true)) {
318 if (unlink("/cache/recovery/command") && errno != ENOENT) {
319 LOGINFO("Can't unlink %s\n", "/cache/recovery/command");
320 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500321 }
Dees_Troya58bead2012-09-27 09:49:29 -0400322
Dees_Troy2673cec2013-04-02 20:22:16 +0000323 sync();
324}
325
326void TWFunc::Update_Intent_File(string Intent) {
327 if (PartitionManager.Mount_By_Path("/cache", false) && !Intent.empty()) {
328 TWFunc::write_file("/cache/recovery/intent", Intent);
329 }
Dees_Troya58bead2012-09-27 09:49:29 -0400330}
331
332// reboot: Reboot the system. Return -1 on error, no return on success
333int TWFunc::tw_reboot(RebootCommand command)
334{
335 // Always force a sync before we reboot
Dees_Troy6ef66352013-02-21 08:26:57 -0600336 sync();
Dees_Troya58bead2012-09-27 09:49:29 -0400337
Dees_Troy2673cec2013-04-02 20:22:16 +0000338 switch (command) {
339 case rb_current:
340 case rb_system:
341 Update_Log_File();
342 Update_Intent_File("s");
343 sync();
344 check_and_run_script("/sbin/rebootsystem.sh", "reboot system");
345 return reboot(RB_AUTOBOOT);
346 case rb_recovery:
347 check_and_run_script("/sbin/rebootrecovery.sh", "reboot recovery");
348 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "recovery");
349 case rb_bootloader:
350 check_and_run_script("/sbin/rebootbootloader.sh", "reboot bootloader");
351 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "bootloader");
352 case rb_poweroff:
353 check_and_run_script("/sbin/poweroff.sh", "power off");
Dees_Troya4438782013-02-22 18:44:00 +0000354#ifdef ANDROID_RB_POWEROFF
Dees_Troy2673cec2013-04-02 20:22:16 +0000355 android_reboot(ANDROID_RB_POWEROFF, 0, 0);
Dees_Troya4438782013-02-22 18:44:00 +0000356#endif
Dees_Troy2673cec2013-04-02 20:22:16 +0000357 return reboot(RB_POWER_OFF);
358 case rb_download:
359 check_and_run_script("/sbin/rebootdownload.sh", "reboot download");
360 return __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (void*) "download");
361 default:
362 return -1;
Dees_Troy6ef66352013-02-21 08:26:57 -0600363 }
364 return -1;
Dees_Troya58bead2012-09-27 09:49:29 -0400365}
366
367void TWFunc::check_and_run_script(const char* script_file, const char* display_name)
368{
369 // Check for and run startup script if script exists
370 struct stat st;
371 if (stat(script_file, &st) == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000372 gui_print("Running %s script...\n", display_name);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500373 chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
Vojtech Bocek05534202013-09-11 08:11:56 +0200374 TWFunc::Exec_Cmd(script_file);
Dees_Troy2673cec2013-04-02 20:22:16 +0000375 gui_print("\nFinished running %s script.\n", display_name);
Dees_Troya58bead2012-09-27 09:49:29 -0400376 }
Dees_Troy3477d712012-09-27 15:44:01 -0400377}
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500378
379int TWFunc::removeDir(const string path, bool skipParent) {
Dees_Troyce675462013-01-09 19:48:21 +0000380 DIR *d = opendir(path.c_str());
381 int r = 0;
382 string new_path;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500383
Dees_Troyce675462013-01-09 19:48:21 +0000384 if (d == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000385 LOGERR("Error opening '%s'\n", path.c_str());
Dees_Troyce675462013-01-09 19:48:21 +0000386 return -1;
387 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500388
Dees_Troyce675462013-01-09 19:48:21 +0000389 if (d) {
390 struct dirent *p;
391 while (!r && (p = readdir(d))) {
Dees_Troyce675462013-01-09 19:48:21 +0000392 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
393 continue;
394 new_path = path + "/";
395 new_path.append(p->d_name);
396 if (p->d_type == DT_DIR) {
397 r = removeDir(new_path, true);
398 if (!r) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500399 if (p->d_type == DT_DIR)
400 r = rmdir(new_path.c_str());
401 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000402 LOGINFO("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500403 }
bigbiff bigbiff98f1f902013-01-19 18:46:13 -0500404 } 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 +0000405 r = unlink(new_path.c_str());
Dees_Troye34c1332013-02-06 19:13:00 +0000406 if (r != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000407 LOGINFO("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno));
Dees_Troye34c1332013-02-06 19:13:00 +0000408 }
Dees_Troyce675462013-01-09 19:48:21 +0000409 }
410 }
411 closedir(d);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500412
413 if (!r) {
414 if (skipParent)
415 return 0;
416 else
417 r = rmdir(path.c_str());
418 }
Dees_Troyce675462013-01-09 19:48:21 +0000419 }
420 return r;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500421}
422
423int TWFunc::copy_file(string src, string dst, int mode) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000424 LOGINFO("Copying file %s to %s\n", src.c_str(), dst.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500425 ifstream srcfile(src.c_str(), ios::binary);
426 ofstream dstfile(dst.c_str(), ios::binary);
427 dstfile << srcfile.rdbuf();
428 srcfile.close();
429 dstfile.close();
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500430 if (chmod(dst.c_str(), mode) != 0)
431 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500432 return 0;
433}
Dees_Troy3ee47bc2013-01-25 21:47:37 +0000434
435unsigned int TWFunc::Get_D_Type_From_Stat(string Path) {
436 struct stat st;
437
438 stat(Path.c_str(), &st);
439 if (st.st_mode & S_IFDIR)
440 return DT_DIR;
441 else if (st.st_mode & S_IFBLK)
442 return DT_BLK;
443 else if (st.st_mode & S_IFCHR)
444 return DT_CHR;
445 else if (st.st_mode & S_IFIFO)
446 return DT_FIFO;
447 else if (st.st_mode & S_IFLNK)
448 return DT_LNK;
449 else if (st.st_mode & S_IFREG)
450 return DT_REG;
451 else if (st.st_mode & S_IFSOCK)
452 return DT_SOCK;
453 return DT_UNKNOWN;
Dees_Troye34c1332013-02-06 19:13:00 +0000454}
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500455
456int TWFunc::read_file(string fn, string& results) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200457 ifstream file;
458 file.open(fn.c_str(), ios::in);
459
460 if (file.is_open()) {
461 file >> results;
462 file.close();
463 return 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500464 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200465
466 LOGINFO("Cannot find file %s\n", fn.c_str());
467 return -1;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500468}
469
470int TWFunc::read_file(string fn, vector<string>& results) {
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500471 ifstream file;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500472 string line;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500473 file.open(fn.c_str(), ios::in);
474 if (file.is_open()) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500475 while (getline(file, line))
476 results.push_back(line);
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500477 file.close();
478 return 0;
479 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000480 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500481 return -1;
482}
483
484int TWFunc::write_file(string fn, string& line) {
485 FILE *file;
486 file = fopen(fn.c_str(), "w");
487 if (file != NULL) {
488 fwrite(line.c_str(), line.size(), 1, file);
489 fclose(file);
490 return 0;
491 }
Dees_Troy2673cec2013-04-02 20:22:16 +0000492 LOGINFO("Cannot find file %s\n", fn.c_str());
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500493 return -1;
494}
495
Dees_Troy83bd4832013-05-04 12:39:56 +0000496vector<string> TWFunc::split_string(const string &in, char del, bool skip_empty) {
497 vector<string> res;
498
499 if (in.empty() || del == '\0')
500 return res;
501
502 string field;
503 istringstream f(in);
504 if (del == '\n') {
505 while(getline(f, field)) {
506 if (field.empty() && skip_empty)
507 continue;
508 res.push_back(field);
509 }
510 } else {
511 while(getline(f, field, del)) {
512 if (field.empty() && skip_empty)
513 continue;
514 res.push_back(field);
515 }
516 }
517 return res;
518}
519
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500520timespec TWFunc::timespec_diff(timespec& start, timespec& end)
521{
Dees_Troy6ef66352013-02-21 08:26:57 -0600522 timespec temp;
523 if ((end.tv_nsec-start.tv_nsec)<0) {
524 temp.tv_sec = end.tv_sec-start.tv_sec-1;
525 temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
526 } else {
527 temp.tv_sec = end.tv_sec-start.tv_sec;
528 temp.tv_nsec = end.tv_nsec-start.tv_nsec;
529 }
530 return temp;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500531}
532
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200533int TWFunc::drop_caches(void) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600534 string file = "/proc/sys/vm/drop_caches";
535 string value = "3";
536 if (write_file(file, value) != 0)
537 return -1;
538 return 0;
539}
540
541int TWFunc::Check_su_Perms(void) {
542 struct stat st;
543 int ret = 0;
544
545 if (!PartitionManager.Mount_By_Path("/system", false))
546 return 0;
547
548 // Check to ensure that perms are 6755 for all 3 file locations
549 if (stat("/system/bin/su", &st) == 0) {
550 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) {
551 ret = 1;
552 }
553 }
554 if (stat("/system/xbin/su", &st) == 0) {
555 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) {
556 ret += 2;
557 }
558 }
559 if (stat("/system/bin/.ext/.su", &st) == 0) {
560 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) {
561 ret += 4;
562 }
563 }
564 return ret;
565}
566
567bool TWFunc::Fix_su_Perms(void) {
568 if (!PartitionManager.Mount_By_Path("/system", true))
569 return false;
570
571 string file = "/system/bin/su";
572 if (TWFunc::Path_Exists(file)) {
573 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000574 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600575 return false;
576 }
577 if (tw_chmod(file, "6755") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000578 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600579 return false;
580 }
581 }
582 file = "/system/xbin/su";
583 if (TWFunc::Path_Exists(file)) {
584 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000585 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600586 return false;
587 }
588 if (tw_chmod(file, "6755") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000589 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600590 return false;
591 }
592 }
Dees_Troya7939bb2013-08-29 20:21:12 +0000593 file = "/system/xbin/daemonsu";
594 if (TWFunc::Path_Exists(file)) {
595 if (chown(file.c_str(), 0, 0) != 0) {
596 LOGERR("Failed to chown '%s'\n", file.c_str());
597 return false;
598 }
599 if (tw_chmod(file, "6755") != 0) {
600 LOGERR("Failed to chmod '%s'\n", file.c_str());
601 return false;
602 }
603 }
Dees_Troy6ef66352013-02-21 08:26:57 -0600604 file = "/system/bin/.ext/.su";
605 if (TWFunc::Path_Exists(file)) {
606 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000607 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600608 return false;
609 }
610 if (tw_chmod(file, "6755") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000611 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600612 return false;
613 }
614 }
Dees_Troya7939bb2013-08-29 20:21:12 +0000615 file = "/system/etc/install-recovery.sh";
616 if (TWFunc::Path_Exists(file)) {
617 if (chown(file.c_str(), 0, 0) != 0) {
618 LOGERR("Failed to chown '%s'\n", file.c_str());
619 return false;
620 }
621 if (tw_chmod(file, "0755") != 0) {
622 LOGERR("Failed to chmod '%s'\n", file.c_str());
623 return false;
624 }
625 }
626 file = "/system/etc/init.d/99SuperSUDaemon";
627 if (TWFunc::Path_Exists(file)) {
628 if (chown(file.c_str(), 0, 0) != 0) {
629 LOGERR("Failed to chown '%s'\n", file.c_str());
630 return false;
631 }
632 if (tw_chmod(file, "0755") != 0) {
633 LOGERR("Failed to chmod '%s'\n", file.c_str());
634 return false;
635 }
636 }
Dees_Troy6ef66352013-02-21 08:26:57 -0600637 file = "/system/app/Superuser.apk";
638 if (TWFunc::Path_Exists(file)) {
639 if (chown(file.c_str(), 0, 0) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000640 LOGERR("Failed to chown '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600641 return false;
642 }
643 if (tw_chmod(file, "0644") != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000644 LOGERR("Failed to chmod '%s'\n", file.c_str());
Dees_Troy6ef66352013-02-21 08:26:57 -0600645 return false;
646 }
647 }
648 sync();
649 if (!PartitionManager.UnMount_By_Path("/system", true))
650 return false;
651 return true;
652}
653
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200654int TWFunc::tw_chmod(const string& fn, const string& mode) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600655 long mask = 0;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200656 std::string::size_type n = mode.length();
657 int cls = 0;
Dees_Troy6ef66352013-02-21 08:26:57 -0600658
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200659 if(n == 3)
660 ++cls;
661 else if(n != 4)
662 {
663 LOGERR("TWFunc::tw_chmod used with %u long mode string (should be 3 or 4)!\n", mode.length());
664 return -1;
665 }
666
667 for (n = 0; n < mode.length(); ++n, ++cls) {
668 if (cls == 0) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600669 if (mode[n] == '0')
670 continue;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200671 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600672 mask |= S_ISVTX;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200673 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600674 mask |= S_ISGID;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200675 else if (mode[n] == '4')
Dees_Troy6ef66352013-02-21 08:26:57 -0600676 mask |= S_ISUID;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200677 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600678 mask |= S_ISVTX;
679 mask |= S_ISUID;
680 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200681 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600682 mask |= S_ISGID;
683 mask |= S_ISUID;
684 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200685 else if (mode[n] == '7') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600686 mask |= S_ISVTX;
687 mask |= S_ISGID;
688 mask |= S_ISUID;
689 }
690 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200691 else if (cls == 1) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600692 if (mode[n] == '7') {
693 mask |= S_IRWXU;
694 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200695 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600696 mask |= S_IRUSR;
697 mask |= S_IWUSR;
698 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200699 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600700 mask |= S_IRUSR;
701 mask |= S_IXUSR;
702 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200703 else if (mode[n] == '4')
Dees_Troy6ef66352013-02-21 08:26:57 -0600704 mask |= S_IRUSR;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200705 else if (mode[n] == '3') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600706 mask |= S_IWUSR;
707 mask |= S_IRUSR;
708 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200709 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600710 mask |= S_IWUSR;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200711 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600712 mask |= S_IXUSR;
713 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200714 else if (cls == 2) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600715 if (mode[n] == '7') {
716 mask |= S_IRWXG;
717 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200718 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600719 mask |= S_IRGRP;
720 mask |= S_IWGRP;
721 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200722 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600723 mask |= S_IRGRP;
724 mask |= S_IXGRP;
725 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200726 else if (mode[n] == '4')
Dees_Troy6ef66352013-02-21 08:26:57 -0600727 mask |= S_IRGRP;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200728 else if (mode[n] == '3') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600729 mask |= S_IWGRP;
730 mask |= S_IXGRP;
731 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200732 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600733 mask |= S_IWGRP;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200734 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600735 mask |= S_IXGRP;
736 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200737 else if (cls == 3) {
Dees_Troy6ef66352013-02-21 08:26:57 -0600738 if (mode[n] == '7') {
739 mask |= S_IRWXO;
740 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200741 else if (mode[n] == '6') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600742 mask |= S_IROTH;
743 mask |= S_IWOTH;
744 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200745 else if (mode[n] == '5') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600746 mask |= S_IROTH;
747 mask |= S_IXOTH;
748 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200749 else if (mode[n] == '4')
750 mask |= S_IROTH;
751 else if (mode[n] == '3') {
Dees_Troy6ef66352013-02-21 08:26:57 -0600752 mask |= S_IWOTH;
753 mask |= S_IXOTH;
754 }
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200755 else if (mode[n] == '2')
Dees_Troy6ef66352013-02-21 08:26:57 -0600756 mask |= S_IWOTH;
Vojtech Bocek37aeb8d2013-08-29 22:38:20 +0200757 else if (mode[n] == '1')
Dees_Troy6ef66352013-02-21 08:26:57 -0600758 mask |= S_IXOTH;
759 }
760 }
761
762 if (chmod(fn.c_str(), mask) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000763 LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask);
Dees_Troy6ef66352013-02-21 08:26:57 -0600764 return -1;
765 }
766
767 return 0;
768}
769
770bool TWFunc::Install_SuperSU(void) {
771 if (!PartitionManager.Mount_By_Path("/system", true))
772 return false;
773
Vojtech Bocek05534202013-09-11 08:11:56 +0200774 TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/su");
jt1134113ee732013-02-22 23:26:10 -0600775 if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000776 LOGERR("Failed to copy su binary to /system/bin\n");
Dees_Troy6ef66352013-02-21 08:26:57 -0600777 return false;
778 }
Dees_Troya7939bb2013-08-29 20:21:12 +0000779 if (!Path_Exists("/system/bin/.ext")) {
780 mkdir("/system/bin/.ext", 0777);
781 }
Vojtech Bocek05534202013-09-11 08:11:56 +0200782 TWFunc::Exec_Cmd("/sbin/chattr -i /system/bin/.ext/su");
Dees_Troya7939bb2013-08-29 20:21:12 +0000783 if (copy_file("/supersu/su", "/system/bin/.ext/su", 0755) != 0) {
784 LOGERR("Failed to copy su binary to /system/bin/.ext/su\n");
785 return false;
786 }
Vojtech Bocek05534202013-09-11 08:11:56 +0200787 TWFunc::Exec_Cmd("/sbin/chattr -i /system/xbin/daemonsu");
Dees_Troya7939bb2013-08-29 20:21:12 +0000788 if (copy_file("/supersu/su", "/system/xbin/daemonsu", 0755) != 0) {
789 LOGERR("Failed to copy su binary to /system/xbin/daemonsu\n");
790 return false;
791 }
792 if (Path_Exists("/system/etc/init.d")) {
Vojtech Bocek05534202013-09-11 08:11:56 +0200793 TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/init.d/99SuperSUDaemon");
Dees_Troya7939bb2013-08-29 20:21:12 +0000794 if (copy_file("/supersu/99SuperSUDaemon", "/system/etc/init.d/99SuperSUDaemon", 0755) != 0) {
795 LOGERR("Failed to copy 99SuperSUDaemon to /system/etc/init.d/99SuperSUDaemon\n");
796 return false;
797 }
798 } else {
Vojtech Bocek05534202013-09-11 08:11:56 +0200799 TWFunc::Exec_Cmd("/sbin/chattr -i /system/etc/install-recovery.sh");
Dees_Troya7939bb2013-08-29 20:21:12 +0000800 if (copy_file("/supersu/install-recovery.sh", "/system/etc/install-recovery.sh", 0755) != 0) {
801 LOGERR("Failed to copy install-recovery.sh to /system/etc/install-recovery.sh\n");
802 return false;
803 }
804 }
jt1134113ee732013-02-22 23:26:10 -0600805 if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000806 LOGERR("Failed to copy Superuser app to /system/app\n");
Dees_Troy6ef66352013-02-21 08:26:57 -0600807 return false;
808 }
809 if (!Fix_su_Perms())
810 return false;
811 return true;
bigbiff bigbiff8a68c312013-02-10 14:28:30 -0500812}
Dees_Troy83bd4832013-05-04 12:39:56 +0000813
814int TWFunc::Get_File_Type(string fn) {
815 string::size_type i = 0;
816 int firstbyte = 0, secondbyte = 0;
817 char header[3];
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200818
Dees_Troy83bd4832013-05-04 12:39:56 +0000819 ifstream f;
820 f.open(fn.c_str(), ios::in | ios::binary);
821 f.get(header, 3);
822 f.close();
823 firstbyte = header[i] & 0xff;
824 secondbyte = header[++i] & 0xff;
825
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200826 if (firstbyte == 0x1f && secondbyte == 0x8b)
Dees_Troy83bd4832013-05-04 12:39:56 +0000827 return 1; // Compressed
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200828 else if (firstbyte == 0x4f && secondbyte == 0x41)
Dees_Troy83bd4832013-05-04 12:39:56 +0000829 return 2; // Encrypted
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200830 else
Dees_Troy83bd4832013-05-04 12:39:56 +0000831 return 0; // Unknown
Dees_Troy83bd4832013-05-04 12:39:56 +0000832
833 return 0;
834}
835
836int TWFunc::Try_Decrypting_File(string fn, string password) {
837#ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
838 OAES_CTX * ctx = NULL;
839 uint8_t _key_data[32] = "";
840 FILE *f;
841 uint8_t buffer[4096];
842 uint8_t *buffer_out = NULL;
843 uint8_t *ptr = NULL;
844 size_t read_len = 0, out_len = 0;
845 int firstbyte = 0, secondbyte = 0, key_len;
846 size_t _j = 0;
847 size_t _key_data_len = 0;
848
849 // mostly kanged from OpenAES oaes.c
850 for( _j = 0; _j < 32; _j++ )
851 _key_data[_j] = _j + 1;
852 _key_data_len = password.size();
853 if( 16 >= _key_data_len )
854 _key_data_len = 16;
855 else if( 24 >= _key_data_len )
856 _key_data_len = 24;
857 else
858 _key_data_len = 32;
859 memcpy(_key_data, password.c_str(), password.size());
860
861 ctx = oaes_alloc();
862 if (ctx == NULL) {
863 LOGERR("Failed to allocate OAES\n");
864 return -1;
865 }
866
867 oaes_key_import_data(ctx, _key_data, _key_data_len);
868
869 f = fopen(fn.c_str(), "rb");
870 if (f == NULL) {
871 LOGERR("Failed to open '%s' to try decrypt\n", fn.c_str());
872 return -1;
873 }
874 read_len = fread(buffer, sizeof(uint8_t), 4096, f);
875 if (read_len <= 0) {
876 LOGERR("Read size during try decrypt failed\n");
877 fclose(f);
878 return -1;
879 }
880 if (oaes_decrypt(ctx, buffer, read_len, NULL, &out_len) != OAES_RET_SUCCESS) {
881 LOGERR("Error: Failed to retrieve required buffer size for trying decryption.\n");
882 fclose(f);
883 return -1;
884 }
885 buffer_out = (uint8_t *) calloc(out_len, sizeof(char));
886 if (buffer_out == NULL) {
887 LOGERR("Failed to allocate output buffer for try decrypt.\n");
888 fclose(f);
889 return -1;
890 }
891 if (oaes_decrypt(ctx, buffer, read_len, buffer_out, &out_len) != OAES_RET_SUCCESS) {
892 LOGERR("Failed to decrypt file '%s'\n", fn.c_str());
893 fclose(f);
894 free(buffer_out);
895 return 0;
896 }
897 fclose(f);
898 if (out_len < 2) {
899 LOGINFO("Successfully decrypted '%s' but read length %i too small.\n", fn.c_str(), out_len);
900 free(buffer_out);
901 return 1; // Decrypted successfully
902 }
903 ptr = buffer_out;
904 firstbyte = *ptr & 0xff;
905 ptr++;
906 secondbyte = *ptr & 0xff;
907 if (firstbyte == 0x1f && secondbyte == 0x8b) {
908 LOGINFO("Successfully decrypted '%s' and file is compressed.\n", fn.c_str());
909 free(buffer_out);
910 return 3; // Compressed
911 }
912 if (out_len >= 262) {
913 ptr = buffer_out + 257;
914 if (strncmp((char*)ptr, "ustar", 5) == 0) {
915 LOGINFO("Successfully decrypted '%s' and file is tar format.\n", fn.c_str());
916 free(buffer_out);
917 return 2; // Tar
918 }
919 }
920 free(buffer_out);
921 LOGINFO("No errors decrypting '%s' but no known file format.\n", fn.c_str());
922 return 1; // Decrypted successfully
923#else
924 LOGERR("Encrypted backup support not included.\n");
925 return -1;
926#endif
927}
928
929bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) {
930 DIR* d;
931
932 string Filename;
933 Restore_Path += "/";
934 d = opendir(Restore_Path.c_str());
935 if (d == NULL) {
936 LOGERR("Error opening '%s'\n", Restore_Path.c_str());
937 return false;
938 }
939
940 struct dirent* de;
941 while ((de = readdir(d)) != NULL) {
942 Filename = Restore_Path;
943 Filename += de->d_name;
944 if (TWFunc::Get_File_Type(Filename) == 2) {
945 if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) {
946 DataManager::SetValue("tw_restore_password", ""); // Clear the bad password
947 DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask
948 closedir(d);
949 return false;
950 }
951 }
952 }
953 closedir(d);
954 return true;
955}
956
957int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name) {
958 pid_t rc_pid;
959
960 rc_pid = waitpid(pid, status, 0);
961 if (rc_pid > 0) {
962 if (WEXITSTATUS(*status) == 0)
963 LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success
964 else if (WIFSIGNALED(*status)) {
965 LOGINFO("%s process ended with signal: %d\n", Child_Name.c_str(), WTERMSIG(*status)); // Seg fault or some other non-graceful termination
966 return -1;
967 } else if (WEXITSTATUS(*status) != 0) {
968 LOGINFO("%s process ended with ERROR=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Graceful exit, but there was an error
969 return -1;
970 }
971 } else { // no PID returned
972 if (errno == ECHILD)
973 LOGINFO("%s no child process exist\n", Child_Name.c_str());
974 else {
975 LOGINFO("%s Unexpected error\n", Child_Name.c_str());
976 return -1;
977 }
978 }
979 return 0;
980}
Dees Troyb21cc642013-09-10 17:36:41 +0000981
982string TWFunc::Get_Current_Date() {
983 string Current_Date;
984 time_t seconds = time(0);
985 struct tm *t = localtime(&seconds);
986 char timestamp[255];
987 sprintf(timestamp,"%04d-%02d-%02d--%02d-%02d-%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
988 Current_Date = timestamp;
989 return Current_Date;
990}
991
992void TWFunc::Auto_Generate_Backup_Name() {
993 bool mount_state = PartitionManager.Is_Mounted_By_Path("/system");
994 std::vector<string> buildprop;
995 if (!PartitionManager.Mount_By_Path("/system", true)) {
996 DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
997 return;
998 }
999 if (TWFunc::read_file("/system/build.prop", buildprop) != 0) {
1000 LOGINFO("Unable to open /system/build.prop for getting backup name.\n");
Vojtech Boceka2e70162013-09-17 17:05:10 +02001001 DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
Dees Troyb21cc642013-09-10 17:36:41 +00001002 if (!mount_state)
1003 PartitionManager.UnMount_By_Path("/system", false);
1004 return;
1005 }
1006 int line_count = buildprop.size();
1007 int index;
1008 size_t start_pos = 0, end_pos;
1009 string propname, propvalue;
1010 for (index = 0; index < line_count; index++) {
1011 end_pos = buildprop.at(index).find("=", start_pos);
1012 propname = buildprop.at(index).substr(start_pos, end_pos);
1013 if (propname == "ro.build.display.id") {
1014 propvalue = buildprop.at(index).substr(end_pos + 1, buildprop.at(index).size());
1015 string Backup_Name = Get_Current_Date();
1016 Backup_Name += " " + propvalue;
1017 if (Backup_Name.size() > MAX_BACKUP_NAME_LEN)
1018 Backup_Name.resize(MAX_BACKUP_NAME_LEN);
1019 DataManager::SetValue(TW_BACKUP_NAME, Backup_Name);
1020 break;
1021 }
1022 }
1023 if (propvalue.empty()) {
1024 LOGINFO("ro.build.display.id not found in build.prop\n");
Vojtech Boceka2e70162013-09-17 17:05:10 +02001025 DataManager::SetValue(TW_BACKUP_NAME, Get_Current_Date());
Dees Troyb21cc642013-09-10 17:36:41 +00001026 }
1027 if (!mount_state)
1028 PartitionManager.UnMount_By_Path("/system", false);
Vojtech Bocek05534202013-09-11 08:11:56 +02001029}