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