Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | #include <vector> |
| 6 | #include <dirent.h> |
| 7 | #include <time.h> |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 8 | #include <errno.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | #include <fcntl.h> |
| 10 | #include <sys/mount.h> |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 11 | #include <sys/reboot.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 12 | #include <sys/sendfile.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <sys/vfs.h> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | #include <sys/wait.h> |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 17 | #ifdef ANDROID_RB_POWEROFF |
| 18 | #include "cutils/android_reboot.h" |
| 19 | #endif |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 20 | #include <iostream> |
| 21 | #include <fstream> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 22 | #include <sstream> |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 23 | #include "twrp-functions.hpp" |
| 24 | #include "partitions.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 25 | #include "twcommon.h" |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 26 | #include "data.hpp" |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 27 | #include "variables.h" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 28 | #include "bootloader.h" |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 29 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 30 | #include "openaes/inc/oaes_lib.h" |
| 31 | #endif |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 32 | |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 33 | extern "C" { |
| 34 | #include "libcrecovery/common.h" |
| 35 | } |
| 36 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 37 | /* Execute a command */ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 38 | int TWFunc::Exec_Cmd(string cmd, string &result) { |
| 39 | FILE* exec; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 40 | char buffer[130]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 41 | int ret = 0; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 42 | exec = __popen(cmd.c_str(), "r"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 43 | if (!exec) return -1; |
| 44 | while(!feof(exec)) { |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 45 | memset(&buffer, 0, sizeof(buffer)); |
| 46 | if (fgets(buffer, 128, exec) != NULL) { |
| 47 | buffer[128] = '\n'; |
| 48 | buffer[129] = NULL; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 49 | result += buffer; |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 50 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 51 | } |
Dees_Troy | b05ddee | 2013-01-28 20:24:50 +0000 | [diff] [blame] | 52 | ret = __pclose(exec); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 53 | return ret; |
| 54 | } |
| 55 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 56 | // Returns "file.name" from a full /path/to/file.name |
| 57 | string 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 |
| 68 | string 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 |
| 79 | string 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 | |
| 94 | void TWFunc::install_htc_dumlock(void) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 95 | 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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 103 | gui_print("Installing HTC Dumlock to system...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 104 | copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 105 | if (!Path_Exists("/system/bin/flash_image")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 106 | gui_print("Installing flash_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 107 | copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 108 | need_libs = 1; |
| 109 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 110 | gui_print("flash_image is already installed, skipping...\n"); |
Dees_Troy | 8170a92 | 2012-09-18 15:40:25 -0400 | [diff] [blame] | 111 | if (!Path_Exists("/system/bin/dump_image")) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 112 | gui_print("Installing dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 113 | copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 114 | need_libs = 1; |
| 115 | } else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 116 | gui_print("dump_image is already installed, skipping...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 117 | if (need_libs) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 118 | gui_print("Installing libs needed for flash_image and dump_image...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 119 | 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_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 123 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 124 | gui_print("Installing HTC Dumlock app...\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 125 | mkdir("/data/app", 0777); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 126 | unlink("/data/app/com.teamwin.htcdumlock*"); |
| 127 | copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 128 | sync(); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 129 | gui_print("HTC Dumlock is installed.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void TWFunc::htc_dumlock_restore_original_boot(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 133 | string status; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 134 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 135 | return; |
| 136 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 137 | gui_print("Restoring original boot...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 138 | Exec_Cmd("htcdumlock restore", status); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 139 | gui_print("Original boot restored.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 143 | string status; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 144 | if (!PartitionManager.Mount_By_Path("/sdcard", true)) |
| 145 | return; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 146 | gui_print("Reflashing recovery to boot...\n"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 147 | Exec_Cmd("htcdumlock recovery noreboot", status); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 148 | gui_print("Recovery is flashed to boot.\n"); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame] | 149 | } |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 150 | |
| 151 | int 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_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 160 | LOGERR("Unable to create folder: %s (errno=%d)\n", wholePath.c_str(), errno); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 161 | 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 Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 171 | unsigned long long TWFunc::Get_Folder_Size(const string& Path, bool Display_Error) { |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 172 | DIR* d; |
| 173 | struct dirent* de; |
| 174 | struct stat st; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 175 | unsigned long long dusize = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 176 | unsigned long long dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 177 | |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 178 | d = opendir(Path.c_str()); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 179 | if (d == NULL) |
| 180 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 181 | LOGERR("error opening '%s'\n", Path.c_str()); |
| 182 | LOGERR("error: %s\n", strerror(errno)); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | while ((de = readdir(d)) != NULL) |
| 187 | { |
| 188 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) |
| 189 | { |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 190 | dutemp = Get_Folder_Size((Path + "/" + de->d_name), Display_Error); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 191 | dusize += dutemp; |
| 192 | dutemp = 0; |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 193 | } |
| 194 | else if (de->d_type == DT_REG) |
| 195 | { |
Vojtech Bocek | 2e97ec5 | 2013-02-02 13:22:42 +0100 | [diff] [blame] | 196 | stat((Path + "/" + de->d_name).c_str(), &st); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 197 | dusize += (unsigned long long)(st.st_size); |
| 198 | } |
| 199 | } |
| 200 | closedir(d); |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 201 | return dusize; |
| 202 | } |
| 203 | |
| 204 | bool TWFunc::Path_Exists(string Path) { |
| 205 | // Check to see if the Path exists |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 206 | struct stat st; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 207 | if (stat(Path.c_str(), &st) != 0) |
Dees_Troy | 43d8b00 | 2012-09-17 16:00:01 -0400 | [diff] [blame] | 208 | return false; |
| 209 | else |
| 210 | return true; |
Dees_Troy | b46a684 | 2012-09-25 11:06:46 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void 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 | |
| 224 | void 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_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | unsigned 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_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 241 | } |
| 242 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 243 | void 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 247 | } else { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 248 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 257 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 258 | fflush(destination_log); |
| 259 | fclose(destination_log); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 260 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 263 | void TWFunc::Update_Log_File(void) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 264 | // Copy logs to cache so the system can find out what happened. |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 265 | 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_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 270 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 271 | // 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_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 286 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 287 | if (!PartitionManager.Mount_By_Path("/cache", true) || (unlink("/cache/recovery/command") && errno != ENOENT)) { |
| 288 | LOGINFO("Can't unlink %s\n", "/cache/recovery/command"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 289 | } |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 290 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 291 | PartitionManager.UnMount_By_Path("/cache", true); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 292 | sync(); |
| 293 | } |
| 294 | |
| 295 | void 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_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // reboot: Reboot the system. Return -1 on error, no return on success |
| 302 | int TWFunc::tw_reboot(RebootCommand command) |
| 303 | { |
| 304 | // Always force a sync before we reboot |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 305 | sync(); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 306 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 307 | 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_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 323 | #ifdef ANDROID_RB_POWEROFF |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 324 | android_reboot(ANDROID_RB_POWEROFF, 0, 0); |
Dees_Troy | a443878 | 2013-02-22 18:44:00 +0000 | [diff] [blame] | 325 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 326 | 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_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 332 | } |
| 333 | return -1; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 340 | string result; |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 341 | if (stat(script_file, &st) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 342 | gui_print("Running %s script...\n", display_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 343 | chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
| 344 | TWFunc::Exec_Cmd(script_file, result); |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 345 | gui_print("\nFinished running %s script.\n", display_name); |
Dees_Troy | a58bead | 2012-09-27 09:49:29 -0400 | [diff] [blame] | 346 | } |
Dees_Troy | 3477d71 | 2012-09-27 15:44:01 -0400 | [diff] [blame] | 347 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 348 | |
| 349 | int TWFunc::removeDir(const string path, bool skipParent) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 350 | DIR *d = opendir(path.c_str()); |
| 351 | int r = 0; |
| 352 | string new_path; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 353 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 354 | if (d == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 355 | LOGERR("Error opening '%s'\n", path.c_str()); |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 356 | return -1; |
| 357 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 358 | |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 359 | if (d) { |
| 360 | struct dirent *p; |
| 361 | while (!r && (p = readdir(d))) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 362 | 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 bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 369 | if (p->d_type == DT_DIR) |
| 370 | r = rmdir(new_path.c_str()); |
| 371 | else |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 372 | LOGINFO("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 373 | } |
bigbiff bigbiff | 98f1f90 | 2013-01-19 18:46:13 -0500 | [diff] [blame] | 374 | } else if (p->d_type == DT_REG || p->d_type == DT_LNK || p->d_type == DT_FIFO || p->d_type == DT_SOCK) { |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 375 | r = unlink(new_path.c_str()); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 376 | if (r != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 377 | LOGINFO("Unable to unlink '%s: %s'\n", new_path.c_str(), strerror(errno)); |
Dees_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 378 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | closedir(d); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 382 | |
| 383 | if (!r) { |
| 384 | if (skipParent) |
| 385 | return 0; |
| 386 | else |
| 387 | r = rmdir(path.c_str()); |
| 388 | } |
Dees_Troy | ce67546 | 2013-01-09 19:48:21 +0000 | [diff] [blame] | 389 | } |
| 390 | return r; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | int TWFunc::copy_file(string src, string dst, int mode) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 394 | LOGINFO("Copying file %s to %s\n", src.c_str(), dst.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 395 | 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 bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 400 | if (chmod(dst.c_str(), mode) != 0) |
| 401 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 402 | return 0; |
| 403 | } |
Dees_Troy | 3ee47bc | 2013-01-25 21:47:37 +0000 | [diff] [blame] | 404 | |
| 405 | unsigned 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_Troy | e34c133 | 2013-02-06 19:13:00 +0000 | [diff] [blame] | 424 | } |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 425 | |
| 426 | int TWFunc::read_file(string fn, string& results) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 427 | ifstream file; |
| 428 | file.open(fn.c_str(), ios::in); |
| 429 | if (file.is_open()) { |
| 430 | file >> results; |
| 431 | file.close(); |
| 432 | return 0; |
| 433 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 434 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | int TWFunc::read_file(string fn, vector<string>& results) { |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 439 | ifstream file; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 440 | string line; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 441 | file.open(fn.c_str(), ios::in); |
| 442 | if (file.is_open()) { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 443 | while (getline(file, line)) |
| 444 | results.push_back(line); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 445 | file.close(); |
| 446 | return 0; |
| 447 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 448 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | int TWFunc::write_file(string fn, string& line) { |
| 453 | FILE *file; |
| 454 | file = fopen(fn.c_str(), "w"); |
| 455 | if (file != NULL) { |
| 456 | fwrite(line.c_str(), line.size(), 1, file); |
| 457 | fclose(file); |
| 458 | return 0; |
| 459 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 460 | LOGINFO("Cannot find file %s\n", fn.c_str()); |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 461 | return -1; |
| 462 | } |
| 463 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 464 | vector<string> TWFunc::split_string(const string &in, char del, bool skip_empty) { |
| 465 | vector<string> res; |
| 466 | |
| 467 | if (in.empty() || del == '\0') |
| 468 | return res; |
| 469 | |
| 470 | string field; |
| 471 | istringstream f(in); |
| 472 | if (del == '\n') { |
| 473 | while(getline(f, field)) { |
| 474 | if (field.empty() && skip_empty) |
| 475 | continue; |
| 476 | res.push_back(field); |
| 477 | } |
| 478 | } else { |
| 479 | while(getline(f, field, del)) { |
| 480 | if (field.empty() && skip_empty) |
| 481 | continue; |
| 482 | res.push_back(field); |
| 483 | } |
| 484 | } |
| 485 | return res; |
| 486 | } |
| 487 | |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 488 | timespec TWFunc::timespec_diff(timespec& start, timespec& end) |
| 489 | { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 490 | timespec temp; |
| 491 | if ((end.tv_nsec-start.tv_nsec)<0) { |
| 492 | temp.tv_sec = end.tv_sec-start.tv_sec-1; |
| 493 | temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; |
| 494 | } else { |
| 495 | temp.tv_sec = end.tv_sec-start.tv_sec; |
| 496 | temp.tv_nsec = end.tv_nsec-start.tv_nsec; |
| 497 | } |
| 498 | return temp; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | int TWFunc::drop_caches(void) { |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 502 | string file = "/proc/sys/vm/drop_caches"; |
| 503 | string value = "3"; |
| 504 | if (write_file(file, value) != 0) |
| 505 | return -1; |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | int TWFunc::Check_su_Perms(void) { |
| 510 | struct stat st; |
| 511 | int ret = 0; |
| 512 | |
| 513 | if (!PartitionManager.Mount_By_Path("/system", false)) |
| 514 | return 0; |
| 515 | |
| 516 | // Check to ensure that perms are 6755 for all 3 file locations |
| 517 | if (stat("/system/bin/su", &st) == 0) { |
| 518 | 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) { |
| 519 | ret = 1; |
| 520 | } |
| 521 | } |
| 522 | if (stat("/system/xbin/su", &st) == 0) { |
| 523 | 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) { |
| 524 | ret += 2; |
| 525 | } |
| 526 | } |
| 527 | if (stat("/system/bin/.ext/.su", &st) == 0) { |
| 528 | 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) { |
| 529 | ret += 4; |
| 530 | } |
| 531 | } |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | bool TWFunc::Fix_su_Perms(void) { |
| 536 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 537 | return false; |
| 538 | |
| 539 | string file = "/system/bin/su"; |
| 540 | if (TWFunc::Path_Exists(file)) { |
| 541 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 542 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 543 | return false; |
| 544 | } |
| 545 | if (tw_chmod(file, "6755") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 546 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 547 | return false; |
| 548 | } |
| 549 | } |
| 550 | file = "/system/xbin/su"; |
| 551 | if (TWFunc::Path_Exists(file)) { |
| 552 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 553 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 554 | return false; |
| 555 | } |
| 556 | if (tw_chmod(file, "6755") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 557 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | } |
| 561 | file = "/system/bin/.ext/.su"; |
| 562 | if (TWFunc::Path_Exists(file)) { |
| 563 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 564 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 565 | return false; |
| 566 | } |
| 567 | if (tw_chmod(file, "6755") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 568 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | } |
| 572 | file = "/system/app/Superuser.apk"; |
| 573 | if (TWFunc::Path_Exists(file)) { |
| 574 | if (chown(file.c_str(), 0, 0) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 575 | LOGERR("Failed to chown '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | if (tw_chmod(file, "0644") != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 579 | LOGERR("Failed to chmod '%s'\n", file.c_str()); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 580 | return false; |
| 581 | } |
| 582 | } |
| 583 | sync(); |
| 584 | if (!PartitionManager.UnMount_By_Path("/system", true)) |
| 585 | return false; |
| 586 | return true; |
| 587 | } |
| 588 | |
| 589 | int TWFunc::tw_chmod(string fn, string mode) { |
| 590 | long mask = 0; |
| 591 | |
| 592 | for ( std::string::size_type n = 0; n < mode.length(); ++n) { |
| 593 | if (n == 0) { |
| 594 | if (mode[n] == '0') |
| 595 | continue; |
| 596 | if (mode[n] == '1') |
| 597 | mask |= S_ISVTX; |
| 598 | if (mode[n] == '2') |
| 599 | mask |= S_ISGID; |
| 600 | if (mode[n] == '4') |
| 601 | mask |= S_ISUID; |
| 602 | if (mode[n] == '5') { |
| 603 | mask |= S_ISVTX; |
| 604 | mask |= S_ISUID; |
| 605 | } |
| 606 | if (mode[n] == '6') { |
| 607 | mask |= S_ISGID; |
| 608 | mask |= S_ISUID; |
| 609 | } |
| 610 | if (mode[n] == '7') { |
| 611 | mask |= S_ISVTX; |
| 612 | mask |= S_ISGID; |
| 613 | mask |= S_ISUID; |
| 614 | } |
| 615 | } |
| 616 | else if (n == 1) { |
| 617 | if (mode[n] == '7') { |
| 618 | mask |= S_IRWXU; |
| 619 | } |
| 620 | if (mode[n] == '6') { |
| 621 | mask |= S_IRUSR; |
| 622 | mask |= S_IWUSR; |
| 623 | } |
| 624 | if (mode[n] == '5') { |
| 625 | mask |= S_IRUSR; |
| 626 | mask |= S_IXUSR; |
| 627 | } |
| 628 | if (mode[n] == '4') |
| 629 | mask |= S_IRUSR; |
| 630 | if (mode[n] == '3') { |
| 631 | mask |= S_IWUSR; |
| 632 | mask |= S_IRUSR; |
| 633 | } |
| 634 | if (mode[n] == '2') |
| 635 | mask |= S_IWUSR; |
| 636 | if (mode[n] == '1') |
| 637 | mask |= S_IXUSR; |
| 638 | } |
| 639 | else if (n == 2) { |
| 640 | if (mode[n] == '7') { |
| 641 | mask |= S_IRWXG; |
| 642 | } |
| 643 | if (mode[n] == '6') { |
| 644 | mask |= S_IRGRP; |
| 645 | mask |= S_IWGRP; |
| 646 | } |
| 647 | if (mode[n] == '5') { |
| 648 | mask |= S_IRGRP; |
| 649 | mask |= S_IXGRP; |
| 650 | } |
| 651 | if (mode[n] == '4') |
| 652 | mask |= S_IRGRP; |
| 653 | if (mode[n] == '3') { |
| 654 | mask |= S_IWGRP; |
| 655 | mask |= S_IXGRP; |
| 656 | } |
| 657 | if (mode[n] == '2') |
| 658 | mask |= S_IWGRP; |
| 659 | if (mode[n] == '1') |
| 660 | mask |= S_IXGRP; |
| 661 | } |
| 662 | else if (n == 3) { |
| 663 | if (mode[n] == '7') { |
| 664 | mask |= S_IRWXO; |
| 665 | } |
| 666 | if (mode[n] == '6') { |
| 667 | mask |= S_IROTH; |
| 668 | mask |= S_IWOTH; |
| 669 | } |
| 670 | if (mode[n] == '5') { |
| 671 | mask |= S_IROTH; |
| 672 | mask |= S_IXOTH; |
| 673 | } |
| 674 | if (mode[n] == '4') |
| 675 | mask |= S_IROTH; |
| 676 | if (mode[n] == '3') { |
| 677 | mask |= S_IWOTH; |
| 678 | mask |= S_IXOTH; |
| 679 | } |
| 680 | if (mode[n] == '2') |
| 681 | mask |= S_IWOTH; |
| 682 | if (mode[n] == '1') |
| 683 | mask |= S_IXOTH; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (chmod(fn.c_str(), mask) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 688 | LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 689 | return -1; |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | bool TWFunc::Install_SuperSU(void) { |
| 696 | if (!PartitionManager.Mount_By_Path("/system", true)) |
| 697 | return false; |
| 698 | |
jt1134 | 113ee73 | 2013-02-22 23:26:10 -0600 | [diff] [blame] | 699 | if (copy_file("/supersu/su", "/system/xbin/su", 0755) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 700 | LOGERR("Failed to copy su binary to /system/bin\n"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 701 | return false; |
| 702 | } |
jt1134 | 113ee73 | 2013-02-22 23:26:10 -0600 | [diff] [blame] | 703 | if (copy_file("/supersu/Superuser.apk", "/system/app/Superuser.apk", 0644) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 704 | LOGERR("Failed to copy Superuser app to /system/app\n"); |
Dees_Troy | 6ef6635 | 2013-02-21 08:26:57 -0600 | [diff] [blame] | 705 | return false; |
| 706 | } |
| 707 | if (!Fix_su_Perms()) |
| 708 | return false; |
| 709 | return true; |
bigbiff bigbiff | 8a68c31 | 2013-02-10 14:28:30 -0500 | [diff] [blame] | 710 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 711 | |
| 712 | int TWFunc::Get_File_Type(string fn) { |
| 713 | string::size_type i = 0; |
| 714 | int firstbyte = 0, secondbyte = 0; |
| 715 | char header[3]; |
| 716 | |
| 717 | ifstream f; |
| 718 | f.open(fn.c_str(), ios::in | ios::binary); |
| 719 | f.get(header, 3); |
| 720 | f.close(); |
| 721 | firstbyte = header[i] & 0xff; |
| 722 | secondbyte = header[++i] & 0xff; |
| 723 | |
| 724 | if (firstbyte == 0x1f && secondbyte == 0x8b) { |
| 725 | return 1; // Compressed |
| 726 | } else if (firstbyte == 0x4f && secondbyte == 0x41) { |
| 727 | return 2; // Encrypted |
| 728 | } else { |
| 729 | return 0; // Unknown |
| 730 | } |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | int TWFunc::Try_Decrypting_File(string fn, string password) { |
| 736 | #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS |
| 737 | OAES_CTX * ctx = NULL; |
| 738 | uint8_t _key_data[32] = ""; |
| 739 | FILE *f; |
| 740 | uint8_t buffer[4096]; |
| 741 | uint8_t *buffer_out = NULL; |
| 742 | uint8_t *ptr = NULL; |
| 743 | size_t read_len = 0, out_len = 0; |
| 744 | int firstbyte = 0, secondbyte = 0, key_len; |
| 745 | size_t _j = 0; |
| 746 | size_t _key_data_len = 0; |
| 747 | |
| 748 | // mostly kanged from OpenAES oaes.c |
| 749 | for( _j = 0; _j < 32; _j++ ) |
| 750 | _key_data[_j] = _j + 1; |
| 751 | _key_data_len = password.size(); |
| 752 | if( 16 >= _key_data_len ) |
| 753 | _key_data_len = 16; |
| 754 | else if( 24 >= _key_data_len ) |
| 755 | _key_data_len = 24; |
| 756 | else |
| 757 | _key_data_len = 32; |
| 758 | memcpy(_key_data, password.c_str(), password.size()); |
| 759 | |
| 760 | ctx = oaes_alloc(); |
| 761 | if (ctx == NULL) { |
| 762 | LOGERR("Failed to allocate OAES\n"); |
| 763 | return -1; |
| 764 | } |
| 765 | |
| 766 | oaes_key_import_data(ctx, _key_data, _key_data_len); |
| 767 | |
| 768 | f = fopen(fn.c_str(), "rb"); |
| 769 | if (f == NULL) { |
| 770 | LOGERR("Failed to open '%s' to try decrypt\n", fn.c_str()); |
| 771 | return -1; |
| 772 | } |
| 773 | read_len = fread(buffer, sizeof(uint8_t), 4096, f); |
| 774 | if (read_len <= 0) { |
| 775 | LOGERR("Read size during try decrypt failed\n"); |
| 776 | fclose(f); |
| 777 | return -1; |
| 778 | } |
| 779 | if (oaes_decrypt(ctx, buffer, read_len, NULL, &out_len) != OAES_RET_SUCCESS) { |
| 780 | LOGERR("Error: Failed to retrieve required buffer size for trying decryption.\n"); |
| 781 | fclose(f); |
| 782 | return -1; |
| 783 | } |
| 784 | buffer_out = (uint8_t *) calloc(out_len, sizeof(char)); |
| 785 | if (buffer_out == NULL) { |
| 786 | LOGERR("Failed to allocate output buffer for try decrypt.\n"); |
| 787 | fclose(f); |
| 788 | return -1; |
| 789 | } |
| 790 | if (oaes_decrypt(ctx, buffer, read_len, buffer_out, &out_len) != OAES_RET_SUCCESS) { |
| 791 | LOGERR("Failed to decrypt file '%s'\n", fn.c_str()); |
| 792 | fclose(f); |
| 793 | free(buffer_out); |
| 794 | return 0; |
| 795 | } |
| 796 | fclose(f); |
| 797 | if (out_len < 2) { |
| 798 | LOGINFO("Successfully decrypted '%s' but read length %i too small.\n", fn.c_str(), out_len); |
| 799 | free(buffer_out); |
| 800 | return 1; // Decrypted successfully |
| 801 | } |
| 802 | ptr = buffer_out; |
| 803 | firstbyte = *ptr & 0xff; |
| 804 | ptr++; |
| 805 | secondbyte = *ptr & 0xff; |
| 806 | if (firstbyte == 0x1f && secondbyte == 0x8b) { |
| 807 | LOGINFO("Successfully decrypted '%s' and file is compressed.\n", fn.c_str()); |
| 808 | free(buffer_out); |
| 809 | return 3; // Compressed |
| 810 | } |
| 811 | if (out_len >= 262) { |
| 812 | ptr = buffer_out + 257; |
| 813 | if (strncmp((char*)ptr, "ustar", 5) == 0) { |
| 814 | LOGINFO("Successfully decrypted '%s' and file is tar format.\n", fn.c_str()); |
| 815 | free(buffer_out); |
| 816 | return 2; // Tar |
| 817 | } |
| 818 | } |
| 819 | free(buffer_out); |
| 820 | LOGINFO("No errors decrypting '%s' but no known file format.\n", fn.c_str()); |
| 821 | return 1; // Decrypted successfully |
| 822 | #else |
| 823 | LOGERR("Encrypted backup support not included.\n"); |
| 824 | return -1; |
| 825 | #endif |
| 826 | } |
| 827 | |
| 828 | bool TWFunc::Try_Decrypting_Backup(string Restore_Path, string Password) { |
| 829 | DIR* d; |
| 830 | |
| 831 | string Filename; |
| 832 | Restore_Path += "/"; |
| 833 | d = opendir(Restore_Path.c_str()); |
| 834 | if (d == NULL) { |
| 835 | LOGERR("Error opening '%s'\n", Restore_Path.c_str()); |
| 836 | return false; |
| 837 | } |
| 838 | |
| 839 | struct dirent* de; |
| 840 | while ((de = readdir(d)) != NULL) { |
| 841 | Filename = Restore_Path; |
| 842 | Filename += de->d_name; |
| 843 | if (TWFunc::Get_File_Type(Filename) == 2) { |
| 844 | if (TWFunc::Try_Decrypting_File(Filename, Password) < 2) { |
| 845 | DataManager::SetValue("tw_restore_password", ""); // Clear the bad password |
| 846 | DataManager::SetValue("tw_restore_display", ""); // Also clear the display mask |
| 847 | closedir(d); |
| 848 | return false; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | closedir(d); |
| 853 | return true; |
| 854 | } |
| 855 | |
| 856 | int TWFunc::Wait_For_Child(pid_t pid, int *status, string Child_Name) { |
| 857 | pid_t rc_pid; |
| 858 | |
| 859 | rc_pid = waitpid(pid, status, 0); |
| 860 | if (rc_pid > 0) { |
| 861 | if (WEXITSTATUS(*status) == 0) |
| 862 | LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success |
| 863 | else if (WIFSIGNALED(*status)) { |
| 864 | LOGINFO("%s process ended with signal: %d\n", Child_Name.c_str(), WTERMSIG(*status)); // Seg fault or some other non-graceful termination |
| 865 | return -1; |
| 866 | } else if (WEXITSTATUS(*status) != 0) { |
| 867 | LOGINFO("%s process ended with ERROR=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Graceful exit, but there was an error |
| 868 | return -1; |
| 869 | } |
| 870 | } else { // no PID returned |
| 871 | if (errno == ECHILD) |
| 872 | LOGINFO("%s no child process exist\n", Child_Name.c_str()); |
| 873 | else { |
| 874 | LOGINFO("%s Unexpected error\n", Child_Name.c_str()); |
| 875 | return -1; |
| 876 | } |
| 877 | } |
| 878 | return 0; |
| 879 | } |