blob: 07912cd43f07e2298a1e0566be9cef0ccbe7fc29 [file] [log] [blame]
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070019import static com.android.internal.util.ArrayUtils.appendInt;
20
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -080021import android.app.ActivityManager;
Ruben Brunk98576cf2016-03-07 18:54:28 -080022import android.content.ComponentName;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070023import android.content.pm.FeatureInfo;
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070024import android.content.pm.PackageManager;
25import android.os.Environment;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070026import android.os.Process;
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070027import android.os.storage.StorageManager;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070028import android.util.ArrayMap;
29import android.util.ArraySet;
30import android.util.Slog;
31import android.util.SparseArray;
32import android.util.Xml;
Jeff Sharkey1c4ae802014-12-19 11:08:55 -080033
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070034import com.android.internal.util.XmlUtils;
Jeff Sharkey1c4ae802014-12-19 11:08:55 -080035
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070036import libcore.io.IoUtils;
37
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070038import org.xmlpull.v1.XmlPullParser;
39import org.xmlpull.v1.XmlPullParserException;
40
41import java.io.File;
42import java.io.FileNotFoundException;
43import java.io.FileReader;
44import java.io.IOException;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070045
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070046/**
47 * Loads global system configuration info.
48 */
49public class SystemConfig {
50 static final String TAG = "SystemConfig";
51
52 static SystemConfig sInstance;
53
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080054 // permission flag, determines which types of configuration are allowed to be read
55 private static final int ALLOW_FEATURES = 0x01;
56 private static final int ALLOW_LIBS = 0x02;
57 private static final int ALLOW_PERMISSIONS = 0x04;
58 private static final int ALLOW_APP_CONFIGS = 0x08;
59 private static final int ALLOW_ALL = ~0;
60
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070061 // Group-ids that are given to all packages as read from etc/permissions/*.xml.
62 int[] mGlobalGids;
63
64 // These are the built-in uid -> permission mappings that were read from the
65 // system configuration files.
Jeff Sharkey9f837a92014-10-24 12:07:24 -070066 final SparseArray<ArraySet<String>> mSystemPermissions = new SparseArray<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070067
68 // These are the built-in shared libraries that were read from the
69 // system configuration files. Keys are the library names; strings are the
70 // paths to the libraries.
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -070071 final ArrayMap<String, String> mSharedLibraries = new ArrayMap<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070072
73 // These are the features this devices supports that were read from the
74 // system configuration files.
Jeff Sharkey9f837a92014-10-24 12:07:24 -070075 final ArrayMap<String, FeatureInfo> mAvailableFeatures = new ArrayMap<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070076
Jeff Sharkey1c4ae802014-12-19 11:08:55 -080077 // These are the features which this device doesn't support; the OEM
78 // partition uses these to opt-out of features from the system image.
79 final ArraySet<String> mUnavailableFeatures = new ArraySet<>();
80
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070081 public static final class PermissionEntry {
82 public final String name;
83 public int[] gids;
Jeff Sharkey00f39042015-03-23 16:51:22 -070084 public boolean perUser;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070085
Jeff Sharkey00f39042015-03-23 16:51:22 -070086 PermissionEntry(String name, boolean perUser) {
87 this.name = name;
88 this.perUser = perUser;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070089 }
90 }
91
92 // These are the permission -> gid mappings that were read from the
93 // system configuration files.
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -070094 final ArrayMap<String, PermissionEntry> mPermissions = new ArrayMap<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070095
96 // These are the packages that are white-listed to be able to run in the
Dianne Hackborn4a503b12015-08-06 22:19:06 -070097 // background while in power save mode (but not whitelisted from device idle modes),
98 // as read from the configuration files.
99 final ArraySet<String> mAllowInPowerSaveExceptIdle = new ArraySet<>();
100
101 // These are the packages that are white-listed to be able to run in the
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700102 // background while in power save mode, as read from the configuration files.
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -0700103 final ArraySet<String> mAllowInPowerSave = new ArraySet<>();
104
Felipe Lemea9505cc2016-02-26 10:28:41 -0800105 // These are the packages that are white-listed to be able to run in the
106 // background while in data-usage save mode, as read from the configuration files.
107 final ArraySet<String> mAllowInDataUsageSave = new ArraySet<>();
108
Christopher Tate01e18642015-07-07 18:10:38 -0700109 // These are the package names of apps which should be in the 'always'
110 // URL-handling state upon factory reset.
111 final ArraySet<String> mLinkedApps = new ArraySet<>();
112
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700113 // These are the packages that are whitelisted to be able to run as system user
114 final ArraySet<String> mSystemUserWhitelistedApps = new ArraySet<>();
115
116 // These are the packages that should not run under system user
117 final ArraySet<String> mSystemUserBlacklistedApps = new ArraySet<>();
118
Ruben Brunk98576cf2016-03-07 18:54:28 -0800119 // These are the components that are enabled by default as VR mode listener services.
120 final ArraySet<ComponentName> mDefaultVrComponents = new ArraySet<>();
121
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700122 public static SystemConfig getInstance() {
123 synchronized (SystemConfig.class) {
124 if (sInstance == null) {
125 sInstance = new SystemConfig();
126 }
127 return sInstance;
128 }
129 }
130
131 public int[] getGlobalGids() {
132 return mGlobalGids;
133 }
134
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700135 public SparseArray<ArraySet<String>> getSystemPermissions() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700136 return mSystemPermissions;
137 }
138
139 public ArrayMap<String, String> getSharedLibraries() {
140 return mSharedLibraries;
141 }
142
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700143 public ArrayMap<String, FeatureInfo> getAvailableFeatures() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700144 return mAvailableFeatures;
145 }
146
147 public ArrayMap<String, PermissionEntry> getPermissions() {
148 return mPermissions;
149 }
150
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700151 public ArraySet<String> getAllowInPowerSaveExceptIdle() {
152 return mAllowInPowerSaveExceptIdle;
153 }
154
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700155 public ArraySet<String> getAllowInPowerSave() {
156 return mAllowInPowerSave;
157 }
158
Felipe Lemea9505cc2016-02-26 10:28:41 -0800159 public ArraySet<String> getAllowInDataUsageSave() {
160 return mAllowInDataUsageSave;
161 }
162
Christopher Tate01e18642015-07-07 18:10:38 -0700163 public ArraySet<String> getLinkedApps() {
164 return mLinkedApps;
165 }
166
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700167 public ArraySet<String> getSystemUserWhitelistedApps() {
168 return mSystemUserWhitelistedApps;
169 }
170
171 public ArraySet<String> getSystemUserBlacklistedApps() {
172 return mSystemUserBlacklistedApps;
173 }
174
Ruben Brunk98576cf2016-03-07 18:54:28 -0800175 public ArraySet<ComponentName> getDefaultVrComponents() {
176 return mDefaultVrComponents;
177 }
178
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700179 SystemConfig() {
180 // Read configuration from system
181 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800182 Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700183 // Read configuration from the old permissions dir
184 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800185 Environment.getRootDirectory(), "etc", "permissions"), ALLOW_ALL);
186 // Allow ODM to customize system configs around libs, features and apps
187 int odmPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_APP_CONFIGS;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700188 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800189 Environment.getOdmDirectory(), "etc", "sysconfig"), odmPermissionFlag);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700190 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800191 Environment.getOdmDirectory(), "etc", "permissions"), odmPermissionFlag);
192 // Only allow OEM to customize features
193 readPermissions(Environment.buildPath(
194 Environment.getOemDirectory(), "etc", "sysconfig"), ALLOW_FEATURES);
195 readPermissions(Environment.buildPath(
196 Environment.getOemDirectory(), "etc", "permissions"), ALLOW_FEATURES);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700197 }
198
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800199 void readPermissions(File libraryDir, int permissionFlag) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700200 // Read permissions from given directory.
201 if (!libraryDir.exists() || !libraryDir.isDirectory()) {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800202 if (permissionFlag == ALLOW_ALL) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700203 Slog.w(TAG, "No directory " + libraryDir + ", skipping");
204 }
205 return;
206 }
207 if (!libraryDir.canRead()) {
208 Slog.w(TAG, "Directory " + libraryDir + " cannot be read");
209 return;
210 }
211
212 // Iterate over the files in the directory and scan .xml files
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800213 File platformFile = null;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700214 for (File f : libraryDir.listFiles()) {
215 // We'll read platform.xml last
216 if (f.getPath().endsWith("etc/permissions/platform.xml")) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800217 platformFile = f;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700218 continue;
219 }
220
221 if (!f.getPath().endsWith(".xml")) {
222 Slog.i(TAG, "Non-xml file " + f + " in " + libraryDir + " directory, ignoring");
223 continue;
224 }
225 if (!f.canRead()) {
226 Slog.w(TAG, "Permissions library file " + f + " cannot be read");
227 continue;
228 }
229
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800230 readPermissionsFromXml(f, permissionFlag);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700231 }
232
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800233 // Read platform permissions last so it will take precedence
234 if (platformFile != null) {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800235 readPermissionsFromXml(platformFile, permissionFlag);
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800236 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700237 }
238
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800239 private void readPermissionsFromXml(File permFile, int permissionFlag) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700240 FileReader permReader = null;
241 try {
242 permReader = new FileReader(permFile);
243 } catch (FileNotFoundException e) {
244 Slog.w(TAG, "Couldn't find or open permissions file " + permFile);
245 return;
246 }
247
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800248 final boolean lowRam = ActivityManager.isLowRamDeviceStatic();
249
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700250 try {
251 XmlPullParser parser = Xml.newPullParser();
252 parser.setInput(permReader);
253
254 int type;
255 while ((type=parser.next()) != parser.START_TAG
256 && type != parser.END_DOCUMENT) {
257 ;
258 }
259
260 if (type != parser.START_TAG) {
261 throw new XmlPullParserException("No start tag found");
262 }
263
264 if (!parser.getName().equals("permissions") && !parser.getName().equals("config")) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800265 throw new XmlPullParserException("Unexpected start tag in " + permFile
266 + ": found " + parser.getName() + ", expected 'permissions' or 'config'");
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700267 }
268
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800269 boolean allowAll = permissionFlag == ALLOW_ALL;
270 boolean allowLibs = (permissionFlag & ALLOW_LIBS) != 0;
271 boolean allowFeatures = (permissionFlag & ALLOW_FEATURES) != 0;
272 boolean allowPermissions = (permissionFlag & ALLOW_PERMISSIONS) != 0;
273 boolean allowAppConfigs = (permissionFlag & ALLOW_APP_CONFIGS) != 0;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700274 while (true) {
275 XmlUtils.nextElement(parser);
276 if (parser.getEventType() == XmlPullParser.END_DOCUMENT) {
277 break;
278 }
279
280 String name = parser.getName();
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800281 if ("group".equals(name) && allowAll) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700282 String gidStr = parser.getAttributeValue(null, "gid");
283 if (gidStr != null) {
284 int gid = android.os.Process.getGidForName(gidStr);
285 mGlobalGids = appendInt(mGlobalGids, gid);
286 } else {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800287 Slog.w(TAG, "<group> without gid in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700288 + parser.getPositionDescription());
289 }
290
291 XmlUtils.skipCurrentTag(parser);
292 continue;
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800293 } else if ("permission".equals(name) && allowPermissions) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700294 String perm = parser.getAttributeValue(null, "name");
295 if (perm == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800296 Slog.w(TAG, "<permission> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700297 + parser.getPositionDescription());
298 XmlUtils.skipCurrentTag(parser);
299 continue;
300 }
301 perm = perm.intern();
302 readPermission(parser, perm);
303
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800304 } else if ("assign-permission".equals(name) && allowPermissions) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700305 String perm = parser.getAttributeValue(null, "name");
306 if (perm == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800307 Slog.w(TAG, "<assign-permission> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700308 + parser.getPositionDescription());
309 XmlUtils.skipCurrentTag(parser);
310 continue;
311 }
312 String uidStr = parser.getAttributeValue(null, "uid");
313 if (uidStr == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800314 Slog.w(TAG, "<assign-permission> without uid in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700315 + parser.getPositionDescription());
316 XmlUtils.skipCurrentTag(parser);
317 continue;
318 }
319 int uid = Process.getUidForName(uidStr);
320 if (uid < 0) {
321 Slog.w(TAG, "<assign-permission> with unknown uid \""
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800322 + uidStr + " in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700323 + parser.getPositionDescription());
324 XmlUtils.skipCurrentTag(parser);
325 continue;
326 }
327 perm = perm.intern();
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700328 ArraySet<String> perms = mSystemPermissions.get(uid);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700329 if (perms == null) {
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700330 perms = new ArraySet<String>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700331 mSystemPermissions.put(uid, perms);
332 }
333 perms.add(perm);
334 XmlUtils.skipCurrentTag(parser);
335
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800336 } else if ("library".equals(name) && allowLibs) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700337 String lname = parser.getAttributeValue(null, "name");
338 String lfile = parser.getAttributeValue(null, "file");
339 if (lname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800340 Slog.w(TAG, "<library> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700341 + parser.getPositionDescription());
342 } else if (lfile == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800343 Slog.w(TAG, "<library> without file in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700344 + parser.getPositionDescription());
345 } else {
346 //Log.i(TAG, "Got library " + lname + " in " + lfile);
347 mSharedLibraries.put(lname, lfile);
348 }
349 XmlUtils.skipCurrentTag(parser);
350 continue;
351
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800352 } else if ("feature".equals(name) && allowFeatures) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700353 String fname = parser.getAttributeValue(null, "name");
Arunesh Mishra4e0bdf32016-02-15 23:50:38 -0800354 int fversion = XmlUtils.readIntAttribute(parser, "version", 0);
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800355 boolean allowed;
356 if (!lowRam) {
357 allowed = true;
358 } else {
359 String notLowRam = parser.getAttributeValue(null, "notLowRam");
360 allowed = !"true".equals(notLowRam);
361 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700362 if (fname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800363 Slog.w(TAG, "<feature> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700364 + parser.getPositionDescription());
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800365 } else if (allowed) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700366 addFeature(fname, fversion);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700367 }
368 XmlUtils.skipCurrentTag(parser);
369 continue;
370
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800371 } else if ("unavailable-feature".equals(name) && allowFeatures) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800372 String fname = parser.getAttributeValue(null, "name");
373 if (fname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800374 Slog.w(TAG, "<unavailable-feature> without name in " + permFile + " at "
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800375 + parser.getPositionDescription());
376 } else {
377 mUnavailableFeatures.add(fname);
378 }
379 XmlUtils.skipCurrentTag(parser);
380 continue;
381
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800382 } else if ("allow-in-power-save-except-idle".equals(name) && allowAll) {
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700383 String pkgname = parser.getAttributeValue(null, "package");
384 if (pkgname == null) {
385 Slog.w(TAG, "<allow-in-power-save-except-idle> without package in "
386 + permFile + " at " + parser.getPositionDescription());
387 } else {
388 mAllowInPowerSaveExceptIdle.add(pkgname);
389 }
390 XmlUtils.skipCurrentTag(parser);
391 continue;
392
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800393 } else if ("allow-in-power-save".equals(name) && allowAll) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700394 String pkgname = parser.getAttributeValue(null, "package");
395 if (pkgname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800396 Slog.w(TAG, "<allow-in-power-save> without package in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700397 + parser.getPositionDescription());
398 } else {
399 mAllowInPowerSave.add(pkgname);
400 }
401 XmlUtils.skipCurrentTag(parser);
402 continue;
403
Felipe Lemea9505cc2016-02-26 10:28:41 -0800404 } else if ("allow-in-data-usage-save".equals(name) && allowAll) {
405 String pkgname = parser.getAttributeValue(null, "package");
406 if (pkgname == null) {
407 Slog.w(TAG, "<allow-in-data-usage-save> without package in " + permFile
408 + " at " + parser.getPositionDescription());
409 } else {
410 mAllowInDataUsageSave.add(pkgname);
411 }
412 XmlUtils.skipCurrentTag(parser);
413 continue;
414
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800415 } else if ("app-link".equals(name) && allowAppConfigs) {
Christopher Tate01e18642015-07-07 18:10:38 -0700416 String pkgname = parser.getAttributeValue(null, "package");
417 if (pkgname == null) {
418 Slog.w(TAG, "<app-link> without package in " + permFile + " at "
419 + parser.getPositionDescription());
420 } else {
421 mLinkedApps.add(pkgname);
422 }
423 XmlUtils.skipCurrentTag(parser);
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800424 } else if ("system-user-whitelisted-app".equals(name) && allowAppConfigs) {
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700425 String pkgname = parser.getAttributeValue(null, "package");
426 if (pkgname == null) {
427 Slog.w(TAG, "<system-user-whitelisted-app> without package in " + permFile
428 + " at " + parser.getPositionDescription());
429 } else {
430 mSystemUserWhitelistedApps.add(pkgname);
431 }
432 XmlUtils.skipCurrentTag(parser);
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800433 } else if ("system-user-blacklisted-app".equals(name) && allowAppConfigs) {
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700434 String pkgname = parser.getAttributeValue(null, "package");
435 if (pkgname == null) {
436 Slog.w(TAG, "<system-user-blacklisted-app without package in " + permFile
437 + " at " + parser.getPositionDescription());
438 } else {
439 mSystemUserBlacklistedApps.add(pkgname);
440 }
441 XmlUtils.skipCurrentTag(parser);
Ruben Brunk98576cf2016-03-07 18:54:28 -0800442 } else if ("default-enabled-vr-app".equals(name) && allowAppConfigs) {
443 String pkgname = parser.getAttributeValue(null, "package");
444 String clsname = parser.getAttributeValue(null, "class");
445 if (pkgname == null) {
446 Slog.w(TAG, "<default-enabled-vr-app without package in " + permFile
447 + " at " + parser.getPositionDescription());
448 } else if (clsname == null) {
449 Slog.w(TAG, "<default-enabled-vr-app without class in " + permFile
450 + " at " + parser.getPositionDescription());
451 } else {
452 mDefaultVrComponents.add(new ComponentName(pkgname, clsname));
453 }
454 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700455 } else {
456 XmlUtils.skipCurrentTag(parser);
457 continue;
458 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700459 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700460 } catch (XmlPullParserException e) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800461 Slog.w(TAG, "Got exception parsing permissions.", e);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700462 } catch (IOException e) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800463 Slog.w(TAG, "Got exception parsing permissions.", e);
464 } finally {
465 IoUtils.closeQuietly(permReader);
466 }
467
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -0700468 // Some devices can be field-converted to FBE, so offer to splice in
469 // those features if not already defined by the static config
Paul Lawrence20be5d62016-02-26 13:51:17 -0800470 if (StorageManager.isFileEncryptedNativeOnly()) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700471 addFeature(PackageManager.FEATURE_FILE_BASED_ENCRYPTION, 0);
472 addFeature(PackageManager.FEATURE_SECURELY_REMOVES_USERS, 0);
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -0700473 }
474
475 for (String featureName : mUnavailableFeatures) {
476 removeFeature(featureName);
477 }
478 }
479
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700480 private void addFeature(String name, int version) {
481 FeatureInfo fi = mAvailableFeatures.get(name);
482 if (fi == null) {
483 fi = new FeatureInfo();
484 fi.name = name;
485 fi.version = version;
486 mAvailableFeatures.put(name, fi);
487 } else {
488 fi.version = Math.max(fi.version, version);
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -0700489 }
490 }
491
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700492 private void removeFeature(String name) {
493 if (mAvailableFeatures.remove(name) != null) {
494 Slog.d(TAG, "Removed unavailable feature " + name);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700495 }
496 }
497
498 void readPermission(XmlPullParser parser, String name)
499 throws IOException, XmlPullParserException {
Jeff Sharkey00f39042015-03-23 16:51:22 -0700500 if (mPermissions.containsKey(name)) {
501 throw new IllegalStateException("Duplicate permission definition for " + name);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700502 }
Jeff Sharkey00f39042015-03-23 16:51:22 -0700503
504 final boolean perUser = XmlUtils.readBooleanAttribute(parser, "perUser", false);
505 final PermissionEntry perm = new PermissionEntry(name, perUser);
506 mPermissions.put(name, perm);
507
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700508 int outerDepth = parser.getDepth();
509 int type;
510 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
511 && (type != XmlPullParser.END_TAG
512 || parser.getDepth() > outerDepth)) {
513 if (type == XmlPullParser.END_TAG
514 || type == XmlPullParser.TEXT) {
515 continue;
516 }
517
518 String tagName = parser.getName();
519 if ("group".equals(tagName)) {
520 String gidStr = parser.getAttributeValue(null, "gid");
521 if (gidStr != null) {
522 int gid = Process.getGidForName(gidStr);
523 perm.gids = appendInt(perm.gids, gid);
524 } else {
525 Slog.w(TAG, "<group> without gid at "
526 + parser.getPositionDescription());
527 }
528 }
529 XmlUtils.skipCurrentTag(parser);
530 }
531 }
532}