1 Star 0 Fork 1

10808012/TWRP_CN

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
fixPermissions.cpp 15.86 KB
一键复制 编辑 原始数据 按行查看 历史
syhost 提交于 2012-11-26 23:54 . first commit
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
/*
Copyright 2012 bigbiff/Dees_Troy TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TWRP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include "gui/rapidxml.hpp"
#include "fixPermissions.hpp"
#include "twrp-functions.hpp"
#include "common.h"
using namespace std;
using namespace rapidxml;
int fixPermissions::fixPerms(bool enable_debug, bool remove_data_for_missing_apps) {
packageFile = "/data/system/packages.xml";
debug = enable_debug;
remove_data = remove_data_for_missing_apps;
multi_user = TWFunc::Path_Exists("/data/user");
if (!(TWFunc::Path_Exists(packageFile))) {
ui_print("Can't check permissions\n");
ui_print("after Factory Reset.\n");
ui_print("Please boot rom and try\n");
ui_print("again after you reboot into\n");
ui_print("recovery.\n");
return -1;
}
ui_print("Fixing permissions...\nLoading packages...\n");
if ((getPackages()) != 0) {
return -1;
}
ui_print("Fixing /system/app perissions...\n");
if ((fixSystemApps()) != 0) {
return -1;
}
ui_print("Fixing /data/app permisions...\n");
if ((fixDataApps()) != 0) {
return -1;
}
if (multi_user) {
DIR *d = opendir("/data/user");
string new_path, user_id;
if (d == NULL) {
LOGE("Error opening '/data/user'\n");
return -1;
}
if (d) {
struct dirent *p;
while ((p = readdir(d))) {
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
continue;
new_path = "/data/user/";
new_path.append(p->d_name);
user_id = "u";
user_id += p->d_name;
user_id += "_";
if (p->d_type == DT_LNK) {
char link[512], realPath[512];
strcpy(link, new_path.c_str());
memset(realPath, 0, sizeof(realPath));
while (readlink(link, realPath, sizeof(realPath)) > 0) {
strcpy(link, realPath);
memset(realPath, 0, sizeof(realPath));
}
new_path = link;
} else if (p->d_type != DT_DIR) {
continue;
} else {
new_path.append("/");
// We're probably going to need to fix permissions on multi user but
// it will have to wait for another time. Need to figure out where
// the uid and gid is stored for other users.
continue;
}
ui_print("Fixing %s permissions...\n", new_path.c_str());
if ((fixDataData(new_path)) != 0) {
closedir(d);
return -1;
}
}
closedir(d);
}
} else {
ui_print("Fixing /data/data permisions...\n");
if ((fixDataData("/data/data/")) != 0) {
return -1;
}
}
ui_print("Done fixing permissions.\n");
return 0;
}
int fixPermissions::pchown(string fn, int puid, int pgid) {
LOGI("Fixing %s, uid: %d, gid: %d\n", fn.c_str(), puid, pgid);
if (chown(fn.c_str(), puid, pgid) != 0) {
LOGE("Unable to chown '%s' %i %i\n", fn.c_str(), puid, pgid);
return -1;
}
return 0;
}
int fixPermissions::pchmod(string fn, string mode) {
long mask = 0;
LOGI("Fixing %s, mode: %s\n", fn.c_str(), mode.c_str());
for ( std::string::size_type n = 0; n < mode.length(); ++n) {
if (n == 0) {
if (mode[n] == '0')
continue;
else if (mode[n] == '1')
mask = S_ISVTX;
else if (mode[n] == '2')
mask = S_ISGID;
}
else if (n == 1) {
if (mode[n] == '7') {
mask |= S_IRWXU;
}
if (mode[n] == '6') {
mask |= S_IRUSR;
mask |= S_IWUSR;
}
if (mode[n] == '5') {
mask |= S_IRUSR;
mask |= S_IXUSR;
}
if (mode[n] == '4')
mask |= S_IRUSR;
if (mode[n] == '3') {
mask |= S_IWUSR;
mask |= S_IRUSR;
}
if (mode[n] == '2')
mask |= S_IWUSR;
if (mode[n] == '1')
mask |= S_IXUSR;
}
else if (n == 2) {
if (mode[n] == '7') {
mask |= S_IRWXG;
}
if (mode[n] == '6') {
mask |= S_IRGRP;
mask |= S_IWGRP;
}
if (mode[n] == '5') {
mask |= S_IRGRP;
mask |= S_IXGRP;
}
if (mode[n] == '4')
mask |= S_IRGRP;
if (mode[n] == '3') {
mask |= S_IWGRP;
mask |= S_IXGRP;
}
if (mode[n] == '2')
mask |= S_IWGRP;
if (mode[n] == '1')
mask |= S_IXGRP;
}
else if (n == 3) {
if (mode[n] == '7') {
mask |= S_IRWXO;
}
if (mode[n] == '6') {
mask |= S_IROTH;
mask |= S_IWOTH;
}
if (mode[n] == '5') {
mask |= S_IROTH;
mask |= S_IXOTH;
}
if (mode[n] == '4')
mask |= S_IROTH;
if (mode[n] == '3') {
mask |= S_IWOTH;
mask |= S_IXOTH;
}
if (mode[n] == '2')
mask |= S_IWOTH;
if (mode[n] == '1')
mask |= S_IXOTH;
}
}
if (chmod(fn.c_str(), mask) != 0) {
LOGE("Unable to chmod '%s' %l\n", fn.c_str(), mask);
return -1;
}
return 0;
}
int fixPermissions::removeDir(const string path) {
DIR *d = opendir(path.c_str());
int r = 0;
string new_path;
if (d == NULL) {
LOGE("Error opening '%s'\n", path.c_str());
return -1;
}
if (d) {
struct dirent *p;
while (!r && (p = readdir(d))) {
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
continue;
new_path = path + "/";
new_path.append(p->d_name);
if (p->d_type == DT_DIR) {
r = removeDir(new_path);
if (!r)
LOGI("Unable to removeDir '%s'\n", new_path.c_str());
} else {
r = unlink(new_path.c_str());
if (!r)
LOGI("Unable to unlink '%s'\n", new_path.c_str());
}
}
closedir(d);
}
if (!r)
r = rmdir(path.c_str());
if (!r)
LOGI("Unable to rmdir '%s'\n", path.c_str());
return r;
}
int fixPermissions::fixSystemApps() {
temp = head;
while (temp != NULL) {
if (TWFunc::Path_Exists(temp->codePath)) {
if (temp->appDir.compare("/system/app") == 0) {
if (debug) {
LOGI("Looking at '%s'\n", temp->codePath.c_str());
LOGI("Fixing permissions on '%s'\n", temp->pkgName.c_str());
LOGI("Directory: '%s'\n", temp->appDir.c_str());
LOGI("Original package owner: %d, group: %d\n", temp->uid, temp->gid);
}
if (pchown(temp->codePath, 0, 0) != 0)
return -1;
if (pchmod(temp->codePath, "0644") != 0)
return -1;
}
} else {
//Remove data directory since app isn't installed
if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") {
if (debug)
LOGI("Looking at '%s', removing data dir: '%s', appDir: '%s'", temp->codePath.c_str(), temp->dDir.c_str(), temp->appDir.c_str());
if (removeDir(temp->dDir) != 0) {
LOGI("Unable to removeDir '%s'\n", temp->dDir.c_str());
return -1;
}
}
}
temp = temp->next;
}
return 0;
}
int fixPermissions::fixDataApps() {
bool fix = false;
int new_gid = 0;
string perms = "0000";
temp = head;
while (temp != NULL) {
if (TWFunc::Path_Exists(temp->codePath)) {
if (temp->appDir.compare("/data/app") == 0 || temp->appDir.compare("/sd-ext/app") == 0) {
fix = true;
new_gid = 1000;
perms = "0644";
} else if (temp->appDir.compare("/data/app-private") == 0 || temp->appDir.compare("/sd-ext/app-private") == 0) {
fix = true;
new_gid = temp->gid;
perms = "0640";
} else
fix = false;
if (fix) {
if (debug) {
LOGI("Looking at '%s'\n", temp->codePath.c_str());
LOGI("Fixing permissions on '%s'\n", temp->pkgName.c_str());
LOGI("Directory: '%s'\n", temp->appDir.c_str());
LOGI("Original package owner: %d, group: %d\n", temp->uid, temp->gid);
}
if (pchown(temp->codePath, 1000, new_gid) != 0)
return -1;
if (pchmod(temp->codePath, perms) != 0)
return -1;
}
} else {
//Remove data directory since app isn't installed
if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") {
if (debug)
LOGI("Looking at '%s', removing data dir: '%s', appDir: '%s'", temp->codePath.c_str(), temp->dDir.c_str(), temp->appDir.c_str());
if (removeDir(temp->dDir) != 0) {
LOGI("Unable to removeDir '%s'\n", temp->dDir.c_str());
return -1;
}
}
}
temp = temp->next;
}
return 0;
}
int fixPermissions::fixAllFiles(string directory, int gid, int uid, string file_perms) {
vector <string> files;
string file;
files = listAllFiles(directory);
for (unsigned i = 0; i < files.size(); ++i) {
file = directory + "/";
file.append(files.at(i));
if (debug)
LOGI("Looking at file '%s'\n", file.c_str());
if (pchmod(file, file_perms) != 0)
return -1;
if (pchown(file, uid, gid) != 0)
return -1;
}
return 0;
}
int fixPermissions::fixDataData(string dataDir) {
string directory, dir;
temp = head;
while (temp != NULL) {
dir = dataDir + temp->dDir;
if (TWFunc::Path_Exists(dir)) {
vector <string> dataDataDirs = listAllDirectories(dir);
for (unsigned n = 0; n < dataDataDirs.size(); ++n) {
directory = dir + "/";
directory.append(dataDataDirs.at(n));
if (debug)
LOGI("Looking at data directory: '%s'\n", directory.c_str());
if (dataDataDirs.at(n) == ".") {
if (pchmod(directory, "0755") != 0)
return -1;
if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
return -1;
if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0)
return -1;
}
else if (dataDataDirs.at(n) == "..") {
if (debug)
LOGI("Skipping ..\n");
continue;
}
else if (dataDataDirs.at(n) == "lib") {
if (pchmod(directory.c_str(), "0755") != 0)
return -1;
if (pchown(directory.c_str(), 1000, 1000) != 0)
return -1;
if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0)
return -1;
}
else if (dataDataDirs.at(n) == "shared_prefs") {
if (pchmod(directory.c_str(), "0771") != 0)
return -1;
if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
return -1;
if (fixAllFiles(directory, temp->uid, temp->gid, "0660") != 0)
return -1;
}
else if (dataDataDirs.at(n) == "databases") {
if (pchmod(directory.c_str(), "0771") != 0)
return -1;
if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
return -1;
if (fixAllFiles(directory, temp->uid, temp->gid, "0660") != 0)
return -1;
}
else if (dataDataDirs.at(n) == "cache") {
if (pchmod(directory.c_str(), "0771") != 0)
return -1;
if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
return -1;
if (fixAllFiles(directory, temp->uid, temp->gid, "0600") != 0)
return -1;
}
else {
if (pchmod(directory.c_str(), "0771") != 0)
return -1;
if (pchown(directory.c_str(), temp->uid, temp->gid) != 0)
return -1;
if (fixAllFiles(directory, temp->uid, temp->gid, "0755") != 0)
return -1;
}
}
}
temp = temp->next;
}
return 0;
}
vector <string> fixPermissions::listAllDirectories(string path) {
DIR *dir = opendir(path.c_str());
vector <string> dirs;
if (dir == NULL) {
LOGE("Error opening '%s'\n", path.c_str());
return dirs;
}
struct dirent *entry = readdir(dir);
while (entry != NULL) {
if (entry->d_type == DT_DIR)
dirs.push_back(entry->d_name);
entry = readdir(dir);
}
closedir(dir);
return dirs;
}
vector <string> fixPermissions::listAllFiles(string path) {
DIR *dir = opendir(path.c_str());
vector <string> files;
if (dir == NULL) {
LOGE("Error opening '%s'\n", path.c_str());
return files;
}
struct dirent *entry = readdir(dir);
while (entry != NULL) {
if (entry->d_type == DT_REG)
files.push_back(entry->d_name);
entry = readdir(dir);
}
closedir(dir);
return files;
}
int fixPermissions::getPackages() {
int len = 0;
bool skiploop = false;
vector <string> skip;
string name;
head = NULL;
skip.push_back("/system/framework/framework-res.apk");
skip.push_back("/system/framework/com.htc.resources.apk");
ifstream xmlFile(packageFile.c_str());
xmlFile.seekg(0, ios::end);
len = (int) xmlFile.tellg();
xmlFile.seekg(0, ios::beg);
char xmlBuf[len + 1];
xmlFile.read(&xmlBuf[0], len);
xmlBuf[len] = '\0';
xml_document<> pkgDoc;
pkgDoc.parse<parse_full>(&xmlBuf[0]);
xml_node<> * pkgNode = pkgDoc.first_node("packages");
xml_node <> * next = pkgNode->first_node("package");
if (next == NULL) {
LOGE("No packages found to fix.\n");
return -1;
}
//Get packages
while (next->first_attribute("name") != NULL) {
package* temp = new package;
for (unsigned n = 0; n < skip.size(); ++n) {
if (skip.at(n).compare(next->first_attribute("codePath")->value()) == 0) {
skiploop = true;
break;
}
}
if (skiploop == true) {
if (debug)
LOGI("Skipping package %s\n", next->first_attribute("codePath")->value());
free(temp);
next = next->next_sibling();
skiploop = false;
continue;
}
name.append((next->first_attribute("name")->value()));
temp->pkgName = next->first_attribute("name")->value();
if (debug)
LOGI("Loading pkg: %s\n", next->first_attribute("name")->value());
if (next->first_attribute("codePath") == NULL) {
LOGI("Problem with codePath on %s\n", next->first_attribute("name")->value());
} else {
temp->codePath = next->first_attribute("codePath")->value();
temp->app = basename(next->first_attribute("codePath")->value());
temp->appDir = dirname(next->first_attribute("codePath")->value());
}
temp->dDir = name;
if ( next->first_attribute("sharedUserId") != NULL) {
temp->uid = atoi(next->first_attribute("sharedUserId")->value());
temp->gid = atoi(next->first_attribute("sharedUserId")->value());
}
else {
if (next->first_attribute("userId") == NULL) {
LOGI("Problem with userID on %s\n", next->first_attribute("name")->value());
} else {
temp->uid = atoi(next->first_attribute("userId")->value());
temp->gid = atoi(next->first_attribute("userId")->value());
}
}
temp->next = head;
head = temp;
if (next->next_sibling("package") == NULL)
break;
name.clear();
next = next->next_sibling("package");
}
//Get updated packages
next = pkgNode->first_node("updated-package");
if (next != NULL) {
while (next->first_attribute("name") != NULL) {
package* temp = new package;
for (unsigned n = 0; n < skip.size(); ++n) {
if (skip.at(n).compare(next->first_attribute("codePath")->value()) == 0) {
skiploop = true;
break;
}
}
if (skiploop == true) {
if (debug)
LOGI("Skipping package %s\n", next->first_attribute("codePath")->value());
free(temp);
next = next->next_sibling();
skiploop = false;
continue;
}
name.append((next->first_attribute("name")->value()));
temp->pkgName = next->first_attribute("name")->value();
if (debug)
LOGI("Loading pkg: %s\n", next->first_attribute("name")->value());
if (next->first_attribute("codePath") == NULL) {
LOGI("Problem with codePath on %s\n", next->first_attribute("name")->value());
} else {
temp->codePath = next->first_attribute("codePath")->value();
temp->app = basename(next->first_attribute("codePath")->value());
temp->appDir = dirname(next->first_attribute("codePath")->value());
}
temp->dDir = name;
if ( next->first_attribute("sharedUserId") != NULL) {
temp->uid = atoi(next->first_attribute("sharedUserId")->value());
temp->gid = atoi(next->first_attribute("sharedUserId")->value());
}
else {
if (next->first_attribute("userId") == NULL) {
LOGI("Problem with userID on %s\n", next->first_attribute("name")->value());
} else {
temp->uid = atoi(next->first_attribute("userId")->value());
temp->gid = atoi(next->first_attribute("userId")->value());
}
}
temp->next = head;
head = temp;
if (next->next_sibling("package") == NULL)
break;
name.clear();
next = next->next_sibling("package");
}
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/ubuntu-flyme/TWRP_CN.git
git@gitee.com:ubuntu-flyme/TWRP_CN.git
ubuntu-flyme
TWRP_CN
TWRP_CN
master

搜索帮助