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