aboutsummaryrefslogtreecommitdiff
path: root/assets/fix_permissions
blob: f2571ff911c61fcdde3f400d655d10d0e550028c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#
#
sysrw() {
busybox mount -o remount,rw /system
}
sysro() {
busybox mount -o remount,ro /system
}

get_runtime() {
starttime=$1
stoptime=$2
runtime=`busybox expr $stoptime - $starttime`
hours=`busybox expr $runtime / 3600`
remainder=`busybox expr $runtime % 3600`
mins=`busybox expr $remainder / 60`
secs=`busybox expr $remainder % 60`
busybox printf "%02d:%02d:%02d\n" "$hours" "$mins" "$secs"
}

fp_print(){
   MSG="$*";
   if [ $LOGGING -eq 1 ]; then
    busybox echo "$MSG" >> $LOG_FILE;
   fi
}

set_package_permission() {
	packagename=$1
	apk_path=$2
	packageuid=`busybox grep $apk_path /data/system/packages.xml | busybox sed 's%.*serId="\(.*\)".*%\1%' |  busybox cut -d '"' -f1 `
	data_path=/data/data/$packagename

	if busybox [ -e $apk_path ]; then

		appdir=`busybox dirname $apk_path `

		if busybox [ $appdir == /system/app ]; then
			busybox chown 0 $apk_path
			busybox chown :0 $apk_path
			busybox chmod 644 $apk_path
			fp_print "${apk_path}:0:0:rw-r--r--";

		elif busybox [ $appdir == /data/app ]; then
			busybox chown 1000 $apk_path
			busybox chown :1000 $apk_path
			busybox chmod 644 $apk_path
			fp_print "${apk_path}:1000:1000:rw-r--r--";

		elif busybox [ $appdir == /sd-ext/app ]; then
			busybox chown 1000 $apk_path
			busybox chown :1000 $apk_path
			busybox chmod 644 $apk_path
			fp_print "${apk_path}:1000:1000:rw-r--r--";

		elif busybox [ $appdir == /data/app-private ]; then
			busybox chown 1000 $apk_path
			busybox chown :$packageuid $apk_path
			busybox chmod 640 $apk_path
			fp_print "${apk_path}:1000:${packageuid}:rw-r-----";

		elif busybox [ $appdir == /sd-ext/app-private ]; then
			busybox chown 1000 $apk_path
			busybox chown :$packageuid $apk_path
			busybox chmod 640 $apk_path
			fp_print "${apk_path}:1000:${packageuid}:rw-r-----";
		fi

		if busybox [ -d $data_path ]; then

			busybox chmod 755 $data_path
			busybox chown $packageuid $data_path
			busybox chown :$packageuid $data_path
            fp_print "${data_path}:${packageuid}:${packageuid}:rwxr-xr-x";
			dirs=`busybox find $data_path -mindepth 1 -type d `

			for file in $dirs; do
				perm=755
				newuid=$packageuid
				newgid=$packageuid
				fname=`busybox basename $file `
				case $fname in
					lib)
						busybox chmod 755 $file
						newuid=1000
						newgid=1000
						perm=755
					;;
					shared_prefs)
						busybox chmod 771 $file
						perm=660
					;;
					databases)
						busybox chmod 771 $file
						perm=660
					;;
					cache)
						busybox chmod 771 $file
						perm=600
					;;
					*)
						busybox chmod 771 $file
						perm=771
					;;
				esac

				busybox chown $newuid $file
				busybox chown :$newgid $file

				busybox find $file -type f -maxdepth 1 ! -perm $perm -exec busybox chmod $perm {} ';'
				busybox find $file -type f -maxdepth 1 ! -user $newuid -exec busybox chown $newuid {} ';'
				busybox find $file -type f -maxdepth 1 ! -group $newgid -exec busybox chown :$newgid {} ';'

			done
		fi
	fi
}

fp_all() {
	starttime=`busybox date +%s `
	packages=`pm list packages -f | busybox cut -d: -f2 `

	fp_print "Fixing permissions start at $FPSTART";

	sysrw

	for package in $packages; do
		packagename=`echo $package | busybox cut -d '=' -f2 `
		apk_path=`echo $package | busybox cut -d '=' -f1 `
		set_package_permission $packagename $apk_path
	done

	sysro
	sync

	stoptime=`busybox date +%s `
	runtime=`get_runtime $starttime $stoptime `

	fp_print "Fix permissions complete! Runtime: ${runtime}";

}
FPSTART=$( busybox date +"%m-%d-%Y %H:%M:%S" );
SD=`busybox mount | busybox egrep -v "asec|android_secure|external_sd|sdcard1" | busybox egrep -i "(sdcard|sdcard0)" | busybox awk '{print $3}'`;
if [ "$SD" == "" ]; then
      LOG_FILE="/data/fix_permissions.log"
else
      LOG_FILE="$SD/fix_permissions.log"
fi
busybox rm -f "$LOG_FILE";

arg=$1;
if [ "$arg" == "-l" ]; then
    LOGGING=1;
else
    LOGGING=0;
fi;
if [ $LOGGING -eq 1 ]; then
   busybox touch "$LOG_FILE";
fi
fp_all;