bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <iostream> |
| 20 | #include <fstream> |
| 21 | #include <sstream> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | #include <string.h> |
| 25 | #include <libgen.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <dirent.h> |
| 29 | #include "gui/rapidxml.hpp" |
| 30 | #include "fixPermissions.hpp" |
| 31 | #include "twrp-functions.hpp" |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 32 | #include "twcommon.h" |
bigbiff bigbiff | 872a3b9 | 2013-10-18 20:50:25 -0400 | [diff] [blame^] | 33 | #ifdef HAVE_SELINUX |
| 34 | #include "selinux/selinux.h" |
| 35 | #include "selinux/label.h" |
| 36 | #include "selinux/android.h" |
| 37 | #include "selinux/label.h" |
| 38 | #endif |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 39 | |
| 40 | using namespace std; |
| 41 | using namespace rapidxml; |
| 42 | |
bigbiff bigbiff | 872a3b9 | 2013-10-18 20:50:25 -0400 | [diff] [blame^] | 43 | #ifdef HAVE_SELINUX |
| 44 | int fixPermissions::restorecon(string entry, struct stat *sb) { |
| 45 | char *oldcontext, *newcontext; |
| 46 | struct selabel_handle *sehandle; |
| 47 | |
| 48 | sehandle = selinux_android_file_context_handle(); |
| 49 | if (lgetfilecon(entry.c_str(), &oldcontext) < 0) { |
| 50 | LOGINFO("Couldn't get selinux context for %s\n", entry.c_str()); |
| 51 | return -1; |
| 52 | } |
| 53 | if (selabel_lookup(sehandle, &newcontext, entry.c_str(), sb->st_mode) < 0) { |
| 54 | LOGINFO("Couldn't lookup selinux context for %s\n", entry.c_str()); |
| 55 | return -1; |
| 56 | } |
| 57 | LOGINFO("Relabeling %s from %s to %s\n", entry.c_str(), oldcontext, newcontext); |
| 58 | if (lsetfilecon(entry.c_str(), newcontext) < 0) { |
| 59 | LOGINFO("Couldn't label %s with %s: %s\n", entry.c_str(), newcontext, strerror(errno)); |
| 60 | } |
| 61 | freecon(oldcontext); |
| 62 | freecon(newcontext); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | int fixPermissions::fixDataDataContexts(void) { |
| 67 | DIR *d; |
| 68 | struct dirent *de; |
| 69 | struct stat sb; |
| 70 | struct selabel_handle *selinux_handle; |
| 71 | struct selinux_opt selinux_options[] = { |
| 72 | { SELABEL_OPT_PATH, "/file_contexts" } |
| 73 | }; |
| 74 | selinux_handle = selabel_open(SELABEL_CTX_FILE, selinux_options, 1); |
| 75 | if (!selinux_handle) |
| 76 | printf("No file contexts for SELinux\n"); |
| 77 | else |
| 78 | printf("SELinux contexts loaded from /file_contexts\n"); |
| 79 | d = opendir("/data/data"); |
| 80 | while (( de = readdir(d)) != NULL) { |
| 81 | stat(de->d_name, &sb); |
| 82 | string f = "/data/data/"; |
| 83 | f = f + de->d_name; |
| 84 | restorecon(f, &sb); |
| 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | #endif |
| 89 | |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 90 | int fixPermissions::fixPerms(bool enable_debug, bool remove_data_for_missing_apps) { |
| 91 | packageFile = "/data/system/packages.xml"; |
| 92 | debug = enable_debug; |
| 93 | remove_data = remove_data_for_missing_apps; |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 94 | multi_user = TWFunc::Path_Exists("/data/user"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 95 | |
| 96 | if (!(TWFunc::Path_Exists(packageFile))) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 97 | gui_print("Can't check permissions\n"); |
| 98 | gui_print("after Factory Reset.\n"); |
| 99 | gui_print("Please boot rom and try\n"); |
| 100 | gui_print("again after you reboot into\n"); |
| 101 | gui_print("recovery.\n"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 102 | return -1; |
| 103 | } |
| 104 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 105 | gui_print("Fixing permissions...\nLoading packages...\n"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 106 | if ((getPackages()) != 0) { |
| 107 | return -1; |
| 108 | } |
Dees_Troy | 32f2ca8 | 2013-02-02 17:03:12 +0000 | [diff] [blame] | 109 | |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 110 | gui_print("Fixing /system/app permissions...\n"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 111 | if ((fixSystemApps()) != 0) { |
| 112 | return -1; |
| 113 | } |
| 114 | |
grimsrud | cbec59f | 2013-07-29 15:46:22 +0200 | [diff] [blame] | 115 | gui_print("Fixing /data/app permissions...\n"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 116 | if ((fixDataApps()) != 0) { |
| 117 | return -1; |
| 118 | } |
| 119 | |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 120 | if (multi_user) { |
| 121 | DIR *d = opendir("/data/user"); |
| 122 | string new_path, user_id; |
| 123 | |
| 124 | if (d == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 125 | LOGERR("Error opening '/data/user'\n"); |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | if (d) { |
| 130 | struct dirent *p; |
| 131 | while ((p = readdir(d))) { |
| 132 | if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) |
| 133 | continue; |
| 134 | |
| 135 | new_path = "/data/user/"; |
| 136 | new_path.append(p->d_name); |
| 137 | user_id = "u"; |
| 138 | user_id += p->d_name; |
| 139 | user_id += "_"; |
| 140 | if (p->d_type == DT_LNK) { |
| 141 | char link[512], realPath[512]; |
| 142 | strcpy(link, new_path.c_str()); |
| 143 | memset(realPath, 0, sizeof(realPath)); |
| 144 | while (readlink(link, realPath, sizeof(realPath)) > 0) { |
| 145 | strcpy(link, realPath); |
| 146 | memset(realPath, 0, sizeof(realPath)); |
| 147 | } |
| 148 | new_path = link; |
| 149 | } else if (p->d_type != DT_DIR) { |
| 150 | continue; |
| 151 | } else { |
| 152 | new_path.append("/"); |
| 153 | // We're probably going to need to fix permissions on multi user but |
| 154 | // it will have to wait for another time. Need to figure out where |
| 155 | // the uid and gid is stored for other users. |
| 156 | continue; |
| 157 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 158 | gui_print("Fixing %s permissions...\n", new_path.c_str()); |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 159 | if ((fixDataData(new_path)) != 0) { |
| 160 | closedir(d); |
| 161 | return -1; |
| 162 | } |
| 163 | } |
| 164 | closedir(d); |
| 165 | } |
| 166 | } else { |
grimsrud | cbec59f | 2013-07-29 15:46:22 +0200 | [diff] [blame] | 167 | gui_print("Fixing /data/data permissions...\n"); |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 168 | if ((fixDataData("/data/data/")) != 0) { |
| 169 | return -1; |
| 170 | } |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 171 | } |
bigbiff bigbiff | 872a3b9 | 2013-10-18 20:50:25 -0400 | [diff] [blame^] | 172 | #ifdef HAVE_SELINUX |
| 173 | gui_print("Fixing /data/data contexts.\n"); |
| 174 | fixDataDataContexts(); |
| 175 | #endif |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 176 | gui_print("Done fixing permissions.\n"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | int fixPermissions::pchown(string fn, int puid, int pgid) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 181 | LOGINFO("Fixing %s, uid: %d, gid: %d\n", fn.c_str(), puid, pgid); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 182 | if (chown(fn.c_str(), puid, pgid) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 183 | LOGERR("Unable to chown '%s' %i %i\n", fn.c_str(), puid, pgid); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 184 | return -1; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | int fixPermissions::pchmod(string fn, string mode) { |
| 190 | long mask = 0; |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 191 | LOGINFO("Fixing %s, mode: %s\n", fn.c_str(), mode.c_str()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 192 | for ( std::string::size_type n = 0; n < mode.length(); ++n) { |
| 193 | if (n == 0) { |
| 194 | if (mode[n] == '0') |
| 195 | continue; |
| 196 | else if (mode[n] == '1') |
| 197 | mask = S_ISVTX; |
| 198 | else if (mode[n] == '2') |
| 199 | mask = S_ISGID; |
| 200 | } |
| 201 | else if (n == 1) { |
| 202 | if (mode[n] == '7') { |
| 203 | mask |= S_IRWXU; |
| 204 | } |
| 205 | if (mode[n] == '6') { |
| 206 | mask |= S_IRUSR; |
| 207 | mask |= S_IWUSR; |
| 208 | } |
| 209 | if (mode[n] == '5') { |
| 210 | mask |= S_IRUSR; |
| 211 | mask |= S_IXUSR; |
| 212 | } |
| 213 | if (mode[n] == '4') |
| 214 | mask |= S_IRUSR; |
| 215 | if (mode[n] == '3') { |
| 216 | mask |= S_IWUSR; |
| 217 | mask |= S_IRUSR; |
| 218 | } |
| 219 | if (mode[n] == '2') |
| 220 | mask |= S_IWUSR; |
| 221 | if (mode[n] == '1') |
| 222 | mask |= S_IXUSR; |
| 223 | } |
| 224 | else if (n == 2) { |
| 225 | if (mode[n] == '7') { |
| 226 | mask |= S_IRWXG; |
| 227 | } |
| 228 | if (mode[n] == '6') { |
| 229 | mask |= S_IRGRP; |
| 230 | mask |= S_IWGRP; |
| 231 | } |
| 232 | if (mode[n] == '5') { |
| 233 | mask |= S_IRGRP; |
| 234 | mask |= S_IXGRP; |
| 235 | } |
| 236 | if (mode[n] == '4') |
| 237 | mask |= S_IRGRP; |
| 238 | if (mode[n] == '3') { |
| 239 | mask |= S_IWGRP; |
| 240 | mask |= S_IXGRP; |
| 241 | } |
| 242 | if (mode[n] == '2') |
| 243 | mask |= S_IWGRP; |
| 244 | if (mode[n] == '1') |
| 245 | mask |= S_IXGRP; |
| 246 | } |
| 247 | else if (n == 3) { |
| 248 | if (mode[n] == '7') { |
| 249 | mask |= S_IRWXO; |
| 250 | } |
| 251 | if (mode[n] == '6') { |
| 252 | mask |= S_IROTH; |
| 253 | mask |= S_IWOTH; |
| 254 | } |
| 255 | if (mode[n] == '5') { |
| 256 | mask |= S_IROTH; |
| 257 | mask |= S_IXOTH; |
| 258 | } |
| 259 | if (mode[n] == '4') |
| 260 | mask |= S_IROTH; |
| 261 | if (mode[n] == '3') { |
| 262 | mask |= S_IWOTH; |
| 263 | mask |= S_IXOTH; |
| 264 | } |
| 265 | if (mode[n] == '2') |
Dees_Troy | 6480ce0 | 2012-10-10 10:26:54 -0400 | [diff] [blame] | 266 | mask |= S_IWOTH; |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 267 | if (mode[n] == '1') |
Dees_Troy | 6480ce0 | 2012-10-10 10:26:54 -0400 | [diff] [blame] | 268 | mask |= S_IXOTH; |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | if (chmod(fn.c_str(), mask) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 273 | LOGERR("Unable to chmod '%s' %l\n", fn.c_str(), mask); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 280 | int fixPermissions::fixSystemApps() { |
| 281 | temp = head; |
| 282 | while (temp != NULL) { |
| 283 | if (TWFunc::Path_Exists(temp->codePath)) { |
| 284 | if (temp->appDir.compare("/system/app") == 0) { |
bigbiff bigbiff | 872a3b9 | 2013-10-18 20:50:25 -0400 | [diff] [blame^] | 285 | if (debug) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 286 | LOGINFO("Looking at '%s'\n", temp->codePath.c_str()); |
| 287 | LOGINFO("Fixing permissions on '%s'\n", temp->pkgName.c_str()); |
| 288 | LOGINFO("Directory: '%s'\n", temp->appDir.c_str()); |
| 289 | LOGINFO("Original package owner: %d, group: %d\n", temp->uid, temp->gid); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 290 | } |
| 291 | if (pchown(temp->codePath, 0, 0) != 0) |
| 292 | return -1; |
| 293 | if (pchmod(temp->codePath, "0644") != 0) |
| 294 | return -1; |
| 295 | } |
| 296 | } else { |
| 297 | //Remove data directory since app isn't installed |
| 298 | if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") { |
| 299 | if (debug) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 300 | LOGINFO("Looking at '%s', removing data dir: '%s', appDir: '%s'", temp->codePath.c_str(), temp->dDir.c_str(), temp->appDir.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 301 | if (TWFunc::removeDir(temp->dDir, false) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 302 | LOGINFO("Unable to removeDir '%s'\n", temp->dDir.c_str()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 303 | return -1; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | temp = temp->next; |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int fixPermissions::fixDataApps() { |
| 313 | bool fix = false; |
| 314 | int new_gid = 0; |
| 315 | string perms = "0000"; |
| 316 | |
| 317 | temp = head; |
| 318 | while (temp != NULL) { |
| 319 | if (TWFunc::Path_Exists(temp->codePath)) { |
| 320 | if (temp->appDir.compare("/data/app") == 0 || temp->appDir.compare("/sd-ext/app") == 0) { |
| 321 | fix = true; |
| 322 | new_gid = 1000; |
| 323 | perms = "0644"; |
| 324 | } else if (temp->appDir.compare("/data/app-private") == 0 || temp->appDir.compare("/sd-ext/app-private") == 0) { |
| 325 | fix = true; |
| 326 | new_gid = temp->gid; |
| 327 | perms = "0640"; |
| 328 | } else |
| 329 | fix = false; |
| 330 | if (fix) { |
| 331 | if (debug) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 332 | LOGINFO("Looking at '%s'\n", temp->codePath.c_str()); |
| 333 | LOGINFO("Fixing permissions on '%s'\n", temp->pkgName.c_str()); |
| 334 | LOGINFO("Directory: '%s'\n", temp->appDir.c_str()); |
| 335 | LOGINFO("Original package owner: %d, group: %d\n", temp->uid, temp->gid); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 336 | } |
| 337 | if (pchown(temp->codePath, 1000, new_gid) != 0) |
| 338 | return -1; |
| 339 | if (pchmod(temp->codePath, perms) != 0) |
| 340 | return -1; |
| 341 | } |
| 342 | } else { |
| 343 | //Remove data directory since app isn't installed |
| 344 | if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") { |
| 345 | if (debug) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 346 | LOGINFO("Looking at '%s', removing data dir: '%s', appDir: '%s'", temp->codePath.c_str(), temp->dDir.c_str(), temp->appDir.c_str()); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 347 | if (TWFunc::removeDir(temp->dDir, false) != 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 348 | LOGINFO("Unable to removeDir '%s'\n", temp->dDir.c_str()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 349 | return -1; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | temp = temp->next; |
| 354 | } |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | int fixPermissions::fixAllFiles(string directory, int gid, int uid, string file_perms) { |
| 359 | vector <string> files; |
| 360 | string file; |
| 361 | |
| 362 | files = listAllFiles(directory); |
| 363 | for (unsigned i = 0; i < files.size(); ++i) { |
| 364 | file = directory + "/"; |
| 365 | file.append(files.at(i)); |
| 366 | if (debug) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 367 | LOGINFO("Looking at file '%s'\n", file.c_str()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 368 | if (pchmod(file, file_perms) != 0) |
| 369 | return -1; |
| 370 | if (pchown(file, uid, gid) != 0) |
| 371 | return -1; |
| 372 | } |
| 373 | return 0; |
| 374 | } |
| 375 | |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 376 | int fixPermissions::fixDataData(string dataDir) { |
| 377 | string directory, dir; |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 378 | |
| 379 | temp = head; |
| 380 | while (temp != NULL) { |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 381 | dir = dataDir + temp->dDir; |
| 382 | if (TWFunc::Path_Exists(dir)) { |
| 383 | vector <string> dataDataDirs = listAllDirectories(dir); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 384 | for (unsigned n = 0; n < dataDataDirs.size(); ++n) { |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 385 | directory = dir + "/"; |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 386 | directory.append(dataDataDirs.at(n)); |
| 387 | if (debug) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 388 | LOGINFO("Looking at data directory: '%s'\n", directory.c_str()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 389 | if (dataDataDirs.at(n) == ".") { |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 390 | if (pchmod(directory, "0755") != 0) |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 391 | return -1; |
| 392 | if (pchown(directory.c_str(), temp->uid, temp->gid) != 0) |
| 393 | return -1; |
| 394 | if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0) |
| 395 | return -1; |
| 396 | } |
| 397 | else if (dataDataDirs.at(n) == "..") { |
| 398 | if (debug) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 399 | LOGINFO("Skipping ..\n"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 400 | continue; |
| 401 | } |
| 402 | else if (dataDataDirs.at(n) == "lib") { |
| 403 | if (pchmod(directory.c_str(), "0755") != 0) |
| 404 | return -1; |
| 405 | if (pchown(directory.c_str(), 1000, 1000) != 0) |
| 406 | return -1; |
| 407 | if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0) |
| 408 | return -1; |
| 409 | } |
| 410 | else if (dataDataDirs.at(n) == "shared_prefs") { |
| 411 | if (pchmod(directory.c_str(), "0771") != 0) |
| 412 | return -1; |
| 413 | if (pchown(directory.c_str(), temp->uid, temp->gid) != 0) |
| 414 | return -1; |
| 415 | if (fixAllFiles(directory, temp->uid, temp->gid, "0660") != 0) |
| 416 | return -1; |
| 417 | } |
| 418 | else if (dataDataDirs.at(n) == "databases") { |
| 419 | if (pchmod(directory.c_str(), "0771") != 0) |
| 420 | return -1; |
| 421 | if (pchown(directory.c_str(), temp->uid, temp->gid) != 0) |
| 422 | return -1; |
| 423 | if (fixAllFiles(directory, temp->uid, temp->gid, "0660") != 0) |
| 424 | return -1; |
| 425 | } |
| 426 | else if (dataDataDirs.at(n) == "cache") { |
| 427 | if (pchmod(directory.c_str(), "0771") != 0) |
| 428 | return -1; |
| 429 | if (pchown(directory.c_str(), temp->uid, temp->gid) != 0) |
| 430 | return -1; |
| 431 | if (fixAllFiles(directory, temp->uid, temp->gid, "0600") != 0) |
| 432 | return -1; |
| 433 | } |
| 434 | else { |
| 435 | if (pchmod(directory.c_str(), "0771") != 0) |
| 436 | return -1; |
| 437 | if (pchown(directory.c_str(), temp->uid, temp->gid) != 0) |
| 438 | return -1; |
| 439 | if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0) |
| 440 | return -1; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | temp = temp->next; |
| 445 | } |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | vector <string> fixPermissions::listAllDirectories(string path) { |
| 450 | DIR *dir = opendir(path.c_str()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 451 | vector <string> dirs; |
| 452 | |
| 453 | if (dir == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 454 | LOGERR("Error opening '%s'\n", path.c_str()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 455 | return dirs; |
| 456 | } |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 457 | struct dirent *entry = readdir(dir); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 458 | while (entry != NULL) { |
| 459 | if (entry->d_type == DT_DIR) |
| 460 | dirs.push_back(entry->d_name); |
| 461 | entry = readdir(dir); |
| 462 | } |
| 463 | closedir(dir); |
| 464 | return dirs; |
| 465 | } |
| 466 | |
| 467 | vector <string> fixPermissions::listAllFiles(string path) { |
| 468 | DIR *dir = opendir(path.c_str()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 469 | vector <string> files; |
| 470 | |
| 471 | if (dir == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 472 | LOGERR("Error opening '%s'\n", path.c_str()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 473 | return files; |
| 474 | } |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 475 | struct dirent *entry = readdir(dir); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 476 | while (entry != NULL) { |
| 477 | if (entry->d_type == DT_REG) |
| 478 | files.push_back(entry->d_name); |
| 479 | entry = readdir(dir); |
| 480 | } |
| 481 | closedir(dir); |
| 482 | return files; |
| 483 | } |
| 484 | |
| 485 | int fixPermissions::getPackages() { |
| 486 | int len = 0; |
| 487 | bool skiploop = false; |
| 488 | vector <string> skip; |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 489 | string name; |
| 490 | head = NULL; |
| 491 | |
| 492 | skip.push_back("/system/framework/framework-res.apk"); |
| 493 | skip.push_back("/system/framework/com.htc.resources.apk"); |
| 494 | |
| 495 | ifstream xmlFile(packageFile.c_str()); |
| 496 | xmlFile.seekg(0, ios::end); |
| 497 | len = (int) xmlFile.tellg(); |
| 498 | xmlFile.seekg(0, ios::beg); |
| 499 | char xmlBuf[len + 1]; |
| 500 | xmlFile.read(&xmlBuf[0], len); |
| 501 | xmlBuf[len] = '\0'; |
| 502 | xml_document<> pkgDoc; |
Dees_Troy | 34614eb | 2013-04-05 12:02:14 -0500 | [diff] [blame] | 503 | LOGINFO("parsing package, %i...\n", len); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 504 | pkgDoc.parse<parse_full>(&xmlBuf[0]); |
| 505 | |
| 506 | xml_node<> * pkgNode = pkgDoc.first_node("packages"); |
Dees_Troy | 34614eb | 2013-04-05 12:02:14 -0500 | [diff] [blame] | 507 | if (pkgNode == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 508 | LOGERR("No packages found to fix.\n"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 509 | return -1; |
| 510 | } |
Dees_Troy | 34614eb | 2013-04-05 12:02:14 -0500 | [diff] [blame] | 511 | xml_node <> * next = pkgNode->first_node("package"); |
| 512 | if (next == NULL) { |
| 513 | LOGERR("No package found to fix.\n"); |
| 514 | return -1; |
| 515 | } |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 516 | |
| 517 | //Get packages |
| 518 | while (next->first_attribute("name") != NULL) { |
| 519 | package* temp = new package; |
| 520 | for (unsigned n = 0; n < skip.size(); ++n) { |
| 521 | if (skip.at(n).compare(next->first_attribute("codePath")->value()) == 0) { |
| 522 | skiploop = true; |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (skiploop == true) { |
| 528 | if (debug) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 529 | LOGINFO("Skipping package %s\n", next->first_attribute("codePath")->value()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 530 | free(temp); |
| 531 | next = next->next_sibling(); |
| 532 | skiploop = false; |
| 533 | continue; |
| 534 | } |
| 535 | name.append((next->first_attribute("name")->value())); |
| 536 | temp->pkgName = next->first_attribute("name")->value(); |
| 537 | if (debug) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 538 | LOGINFO("Loading pkg: %s\n", next->first_attribute("name")->value()); |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 539 | if (next->first_attribute("codePath") == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 540 | LOGINFO("Problem with codePath on %s\n", next->first_attribute("name")->value()); |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 541 | } else { |
| 542 | temp->codePath = next->first_attribute("codePath")->value(); |
| 543 | temp->app = basename(next->first_attribute("codePath")->value()); |
| 544 | temp->appDir = dirname(next->first_attribute("codePath")->value()); |
| 545 | } |
| 546 | temp->dDir = name; |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 547 | if ( next->first_attribute("sharedUserId") != NULL) { |
| 548 | temp->uid = atoi(next->first_attribute("sharedUserId")->value()); |
| 549 | temp->gid = atoi(next->first_attribute("sharedUserId")->value()); |
| 550 | } |
| 551 | else { |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 552 | if (next->first_attribute("userId") == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 553 | LOGINFO("Problem with userID on %s\n", next->first_attribute("name")->value()); |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 554 | } else { |
| 555 | temp->uid = atoi(next->first_attribute("userId")->value()); |
| 556 | temp->gid = atoi(next->first_attribute("userId")->value()); |
| 557 | } |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 558 | } |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 559 | temp->next = head; |
| 560 | head = temp; |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 561 | if (next->next_sibling("package") == NULL) |
| 562 | break; |
| 563 | name.clear(); |
| 564 | next = next->next_sibling("package"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 565 | } |
| 566 | //Get updated packages |
| 567 | next = pkgNode->first_node("updated-package"); |
| 568 | if (next != NULL) { |
| 569 | while (next->first_attribute("name") != NULL) { |
| 570 | package* temp = new package; |
| 571 | for (unsigned n = 0; n < skip.size(); ++n) { |
| 572 | if (skip.at(n).compare(next->first_attribute("codePath")->value()) == 0) { |
| 573 | skiploop = true; |
| 574 | break; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | if (skiploop == true) { |
| 579 | if (debug) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 580 | LOGINFO("Skipping package %s\n", next->first_attribute("codePath")->value()); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 581 | free(temp); |
| 582 | next = next->next_sibling(); |
| 583 | skiploop = false; |
| 584 | continue; |
| 585 | } |
| 586 | name.append((next->first_attribute("name")->value())); |
| 587 | temp->pkgName = next->first_attribute("name")->value(); |
| 588 | if (debug) |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 589 | LOGINFO("Loading pkg: %s\n", next->first_attribute("name")->value()); |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 590 | if (next->first_attribute("codePath") == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 591 | LOGINFO("Problem with codePath on %s\n", next->first_attribute("name")->value()); |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 592 | } else { |
| 593 | temp->codePath = next->first_attribute("codePath")->value(); |
| 594 | temp->app = basename(next->first_attribute("codePath")->value()); |
| 595 | temp->appDir = dirname(next->first_attribute("codePath")->value()); |
| 596 | } |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 597 | |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 598 | temp->dDir = name; |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 599 | if ( next->first_attribute("sharedUserId") != NULL) { |
| 600 | temp->uid = atoi(next->first_attribute("sharedUserId")->value()); |
| 601 | temp->gid = atoi(next->first_attribute("sharedUserId")->value()); |
| 602 | } |
| 603 | else { |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 604 | if (next->first_attribute("userId") == NULL) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 605 | LOGINFO("Problem with userID on %s\n", next->first_attribute("name")->value()); |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 606 | } else { |
| 607 | temp->uid = atoi(next->first_attribute("userId")->value()); |
| 608 | temp->gid = atoi(next->first_attribute("userId")->value()); |
| 609 | } |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 610 | } |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 611 | temp->next = head; |
| 612 | head = temp; |
Dees_Troy | 201d76b | 2012-11-16 17:12:02 +0000 | [diff] [blame] | 613 | if (next->next_sibling("package") == NULL) |
| 614 | break; |
| 615 | name.clear(); |
| 616 | next = next->next_sibling("package"); |
bigbiff bigbiff | a0f8a59 | 2012-10-09 21:01:03 -0400 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | return 0; |
| 620 | } |