blob: 30e0ceb52af305ff02cbe0fb81625056116f7564 [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;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070022import android.content.pm.FeatureInfo;
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070023import android.content.pm.PackageManager;
24import android.os.Environment;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070025import android.os.Process;
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070026import android.os.storage.StorageManager;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070027import android.util.ArrayMap;
28import android.util.ArraySet;
29import android.util.Slog;
30import android.util.SparseArray;
31import android.util.Xml;
Jeff Sharkey1c4ae802014-12-19 11:08:55 -080032
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070033import com.android.internal.util.XmlUtils;
Jeff Sharkey1c4ae802014-12-19 11:08:55 -080034
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -070035import libcore.io.IoUtils;
36
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070037import org.xmlpull.v1.XmlPullParser;
38import org.xmlpull.v1.XmlPullParserException;
39
40import java.io.File;
41import java.io.FileNotFoundException;
42import java.io.FileReader;
43import java.io.IOException;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070044
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070045/**
46 * Loads global system configuration info.
47 */
48public class SystemConfig {
49 static final String TAG = "SystemConfig";
50
51 static SystemConfig sInstance;
52
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +080053 // permission flag, determines which types of configuration are allowed to be read
54 private static final int ALLOW_FEATURES = 0x01;
55 private static final int ALLOW_LIBS = 0x02;
56 private static final int ALLOW_PERMISSIONS = 0x04;
57 private static final int ALLOW_APP_CONFIGS = 0x08;
58 private static final int ALLOW_ALL = ~0;
59
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070060 // Group-ids that are given to all packages as read from etc/permissions/*.xml.
61 int[] mGlobalGids;
62
63 // These are the built-in uid -> permission mappings that were read from the
64 // system configuration files.
Jeff Sharkey9f837a92014-10-24 12:07:24 -070065 final SparseArray<ArraySet<String>> mSystemPermissions = new SparseArray<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070066
67 // These are the built-in shared libraries that were read from the
68 // system configuration files. Keys are the library names; strings are the
69 // paths to the libraries.
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -070070 final ArrayMap<String, String> mSharedLibraries = new ArrayMap<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070071
72 // These are the features this devices supports that were read from the
73 // system configuration files.
Jeff Sharkey9f837a92014-10-24 12:07:24 -070074 final ArrayMap<String, FeatureInfo> mAvailableFeatures = new ArrayMap<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070075
Jeff Sharkey1c4ae802014-12-19 11:08:55 -080076 // These are the features which this device doesn't support; the OEM
77 // partition uses these to opt-out of features from the system image.
78 final ArraySet<String> mUnavailableFeatures = new ArraySet<>();
79
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070080 public static final class PermissionEntry {
81 public final String name;
82 public int[] gids;
Jeff Sharkey00f39042015-03-23 16:51:22 -070083 public boolean perUser;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070084
Jeff Sharkey00f39042015-03-23 16:51:22 -070085 PermissionEntry(String name, boolean perUser) {
86 this.name = name;
87 this.perUser = perUser;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070088 }
89 }
90
91 // These are the permission -> gid mappings that were read from the
92 // system configuration files.
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -070093 final ArrayMap<String, PermissionEntry> mPermissions = new ArrayMap<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -070094
95 // These are the packages that are white-listed to be able to run in the
Dianne Hackborn4a503b12015-08-06 22:19:06 -070096 // background while in power save mode (but not whitelisted from device idle modes),
97 // as read from the configuration files.
98 final ArraySet<String> mAllowInPowerSaveExceptIdle = new ArraySet<>();
99
100 // These are the packages that are white-listed to be able to run in the
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700101 // background while in power save mode, as read from the configuration files.
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -0700102 final ArraySet<String> mAllowInPowerSave = new ArraySet<>();
103
Felipe Lemea9505cc2016-02-26 10:28:41 -0800104 // These are the packages that are white-listed to be able to run in the
105 // background while in data-usage save mode, as read from the configuration files.
106 final ArraySet<String> mAllowInDataUsageSave = new ArraySet<>();
107
Christopher Tate01e18642015-07-07 18:10:38 -0700108 // These are the package names of apps which should be in the 'always'
109 // URL-handling state upon factory reset.
110 final ArraySet<String> mLinkedApps = new ArraySet<>();
111
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700112 // These are the packages that are whitelisted to be able to run as system user
113 final ArraySet<String> mSystemUserWhitelistedApps = new ArraySet<>();
114
115 // These are the packages that should not run under system user
116 final ArraySet<String> mSystemUserBlacklistedApps = new ArraySet<>();
117
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700118 public static SystemConfig getInstance() {
119 synchronized (SystemConfig.class) {
120 if (sInstance == null) {
121 sInstance = new SystemConfig();
122 }
123 return sInstance;
124 }
125 }
126
127 public int[] getGlobalGids() {
128 return mGlobalGids;
129 }
130
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700131 public SparseArray<ArraySet<String>> getSystemPermissions() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700132 return mSystemPermissions;
133 }
134
135 public ArrayMap<String, String> getSharedLibraries() {
136 return mSharedLibraries;
137 }
138
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700139 public ArrayMap<String, FeatureInfo> getAvailableFeatures() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700140 return mAvailableFeatures;
141 }
142
143 public ArrayMap<String, PermissionEntry> getPermissions() {
144 return mPermissions;
145 }
146
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700147 public ArraySet<String> getAllowInPowerSaveExceptIdle() {
148 return mAllowInPowerSaveExceptIdle;
149 }
150
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700151 public ArraySet<String> getAllowInPowerSave() {
152 return mAllowInPowerSave;
153 }
154
Felipe Lemea9505cc2016-02-26 10:28:41 -0800155 public ArraySet<String> getAllowInDataUsageSave() {
156 return mAllowInDataUsageSave;
157 }
158
Christopher Tate01e18642015-07-07 18:10:38 -0700159 public ArraySet<String> getLinkedApps() {
160 return mLinkedApps;
161 }
162
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700163 public ArraySet<String> getSystemUserWhitelistedApps() {
164 return mSystemUserWhitelistedApps;
165 }
166
167 public ArraySet<String> getSystemUserBlacklistedApps() {
168 return mSystemUserBlacklistedApps;
169 }
170
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700171 SystemConfig() {
172 // Read configuration from system
173 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800174 Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700175 // Read configuration from the old permissions dir
176 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800177 Environment.getRootDirectory(), "etc", "permissions"), ALLOW_ALL);
178 // Allow ODM to customize system configs around libs, features and apps
179 int odmPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_APP_CONFIGS;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700180 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800181 Environment.getOdmDirectory(), "etc", "sysconfig"), odmPermissionFlag);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700182 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800183 Environment.getOdmDirectory(), "etc", "permissions"), odmPermissionFlag);
184 // Only allow OEM to customize features
185 readPermissions(Environment.buildPath(
186 Environment.getOemDirectory(), "etc", "sysconfig"), ALLOW_FEATURES);
187 readPermissions(Environment.buildPath(
188 Environment.getOemDirectory(), "etc", "permissions"), ALLOW_FEATURES);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700189 }
190
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800191 void readPermissions(File libraryDir, int permissionFlag) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700192 // Read permissions from given directory.
193 if (!libraryDir.exists() || !libraryDir.isDirectory()) {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800194 if (permissionFlag == ALLOW_ALL) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700195 Slog.w(TAG, "No directory " + libraryDir + ", skipping");
196 }
197 return;
198 }
199 if (!libraryDir.canRead()) {
200 Slog.w(TAG, "Directory " + libraryDir + " cannot be read");
201 return;
202 }
203
204 // Iterate over the files in the directory and scan .xml files
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800205 File platformFile = null;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700206 for (File f : libraryDir.listFiles()) {
207 // We'll read platform.xml last
208 if (f.getPath().endsWith("etc/permissions/platform.xml")) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800209 platformFile = f;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700210 continue;
211 }
212
213 if (!f.getPath().endsWith(".xml")) {
214 Slog.i(TAG, "Non-xml file " + f + " in " + libraryDir + " directory, ignoring");
215 continue;
216 }
217 if (!f.canRead()) {
218 Slog.w(TAG, "Permissions library file " + f + " cannot be read");
219 continue;
220 }
221
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800222 readPermissionsFromXml(f, permissionFlag);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700223 }
224
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800225 // Read platform permissions last so it will take precedence
226 if (platformFile != null) {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800227 readPermissionsFromXml(platformFile, permissionFlag);
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800228 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700229 }
230
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800231 private void readPermissionsFromXml(File permFile, int permissionFlag) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700232 FileReader permReader = null;
233 try {
234 permReader = new FileReader(permFile);
235 } catch (FileNotFoundException e) {
236 Slog.w(TAG, "Couldn't find or open permissions file " + permFile);
237 return;
238 }
239
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800240 final boolean lowRam = ActivityManager.isLowRamDeviceStatic();
241
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700242 try {
243 XmlPullParser parser = Xml.newPullParser();
244 parser.setInput(permReader);
245
246 int type;
247 while ((type=parser.next()) != parser.START_TAG
248 && type != parser.END_DOCUMENT) {
249 ;
250 }
251
252 if (type != parser.START_TAG) {
253 throw new XmlPullParserException("No start tag found");
254 }
255
256 if (!parser.getName().equals("permissions") && !parser.getName().equals("config")) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800257 throw new XmlPullParserException("Unexpected start tag in " + permFile
258 + ": found " + parser.getName() + ", expected 'permissions' or 'config'");
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700259 }
260
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800261 boolean allowAll = permissionFlag == ALLOW_ALL;
262 boolean allowLibs = (permissionFlag & ALLOW_LIBS) != 0;
263 boolean allowFeatures = (permissionFlag & ALLOW_FEATURES) != 0;
264 boolean allowPermissions = (permissionFlag & ALLOW_PERMISSIONS) != 0;
265 boolean allowAppConfigs = (permissionFlag & ALLOW_APP_CONFIGS) != 0;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700266 while (true) {
267 XmlUtils.nextElement(parser);
268 if (parser.getEventType() == XmlPullParser.END_DOCUMENT) {
269 break;
270 }
271
272 String name = parser.getName();
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800273 if ("group".equals(name) && allowAll) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700274 String gidStr = parser.getAttributeValue(null, "gid");
275 if (gidStr != null) {
276 int gid = android.os.Process.getGidForName(gidStr);
277 mGlobalGids = appendInt(mGlobalGids, gid);
278 } else {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800279 Slog.w(TAG, "<group> without gid in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700280 + parser.getPositionDescription());
281 }
282
283 XmlUtils.skipCurrentTag(parser);
284 continue;
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800285 } else if ("permission".equals(name) && allowPermissions) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700286 String perm = parser.getAttributeValue(null, "name");
287 if (perm == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800288 Slog.w(TAG, "<permission> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700289 + parser.getPositionDescription());
290 XmlUtils.skipCurrentTag(parser);
291 continue;
292 }
293 perm = perm.intern();
294 readPermission(parser, perm);
295
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800296 } else if ("assign-permission".equals(name) && allowPermissions) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700297 String perm = parser.getAttributeValue(null, "name");
298 if (perm == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800299 Slog.w(TAG, "<assign-permission> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700300 + parser.getPositionDescription());
301 XmlUtils.skipCurrentTag(parser);
302 continue;
303 }
304 String uidStr = parser.getAttributeValue(null, "uid");
305 if (uidStr == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800306 Slog.w(TAG, "<assign-permission> without uid in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700307 + parser.getPositionDescription());
308 XmlUtils.skipCurrentTag(parser);
309 continue;
310 }
311 int uid = Process.getUidForName(uidStr);
312 if (uid < 0) {
313 Slog.w(TAG, "<assign-permission> with unknown uid \""
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800314 + uidStr + " in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700315 + parser.getPositionDescription());
316 XmlUtils.skipCurrentTag(parser);
317 continue;
318 }
319 perm = perm.intern();
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700320 ArraySet<String> perms = mSystemPermissions.get(uid);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700321 if (perms == null) {
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700322 perms = new ArraySet<String>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700323 mSystemPermissions.put(uid, perms);
324 }
325 perms.add(perm);
326 XmlUtils.skipCurrentTag(parser);
327
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800328 } else if ("library".equals(name) && allowLibs) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700329 String lname = parser.getAttributeValue(null, "name");
330 String lfile = parser.getAttributeValue(null, "file");
331 if (lname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800332 Slog.w(TAG, "<library> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700333 + parser.getPositionDescription());
334 } else if (lfile == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800335 Slog.w(TAG, "<library> without file in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700336 + parser.getPositionDescription());
337 } else {
338 //Log.i(TAG, "Got library " + lname + " in " + lfile);
339 mSharedLibraries.put(lname, lfile);
340 }
341 XmlUtils.skipCurrentTag(parser);
342 continue;
343
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800344 } else if ("feature".equals(name) && allowFeatures) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700345 String fname = parser.getAttributeValue(null, "name");
Arunesh Mishra4e0bdf32016-02-15 23:50:38 -0800346 int fversion = XmlUtils.readIntAttribute(parser, "version", 0);
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800347 boolean allowed;
348 if (!lowRam) {
349 allowed = true;
350 } else {
351 String notLowRam = parser.getAttributeValue(null, "notLowRam");
352 allowed = !"true".equals(notLowRam);
353 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700354 if (fname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800355 Slog.w(TAG, "<feature> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700356 + parser.getPositionDescription());
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800357 } else if (allowed) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700358 addFeature(fname, fversion);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700359 }
360 XmlUtils.skipCurrentTag(parser);
361 continue;
362
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800363 } else if ("unavailable-feature".equals(name) && allowFeatures) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800364 String fname = parser.getAttributeValue(null, "name");
365 if (fname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800366 Slog.w(TAG, "<unavailable-feature> without name in " + permFile + " at "
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800367 + parser.getPositionDescription());
368 } else {
369 mUnavailableFeatures.add(fname);
370 }
371 XmlUtils.skipCurrentTag(parser);
372 continue;
373
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800374 } else if ("allow-in-power-save-except-idle".equals(name) && allowAll) {
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700375 String pkgname = parser.getAttributeValue(null, "package");
376 if (pkgname == null) {
377 Slog.w(TAG, "<allow-in-power-save-except-idle> without package in "
378 + permFile + " at " + parser.getPositionDescription());
379 } else {
380 mAllowInPowerSaveExceptIdle.add(pkgname);
381 }
382 XmlUtils.skipCurrentTag(parser);
383 continue;
384
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800385 } else if ("allow-in-power-save".equals(name) && allowAll) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700386 String pkgname = parser.getAttributeValue(null, "package");
387 if (pkgname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800388 Slog.w(TAG, "<allow-in-power-save> without package in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700389 + parser.getPositionDescription());
390 } else {
391 mAllowInPowerSave.add(pkgname);
392 }
393 XmlUtils.skipCurrentTag(parser);
394 continue;
395
Felipe Lemea9505cc2016-02-26 10:28:41 -0800396 } else if ("allow-in-data-usage-save".equals(name) && allowAll) {
397 String pkgname = parser.getAttributeValue(null, "package");
398 if (pkgname == null) {
399 Slog.w(TAG, "<allow-in-data-usage-save> without package in " + permFile
400 + " at " + parser.getPositionDescription());
401 } else {
402 mAllowInDataUsageSave.add(pkgname);
403 }
404 XmlUtils.skipCurrentTag(parser);
405 continue;
406
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800407 } else if ("app-link".equals(name) && allowAppConfigs) {
Christopher Tate01e18642015-07-07 18:10:38 -0700408 String pkgname = parser.getAttributeValue(null, "package");
409 if (pkgname == null) {
410 Slog.w(TAG, "<app-link> without package in " + permFile + " at "
411 + parser.getPositionDescription());
412 } else {
413 mLinkedApps.add(pkgname);
414 }
415 XmlUtils.skipCurrentTag(parser);
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800416 } else if ("system-user-whitelisted-app".equals(name) && allowAppConfigs) {
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700417 String pkgname = parser.getAttributeValue(null, "package");
418 if (pkgname == null) {
419 Slog.w(TAG, "<system-user-whitelisted-app> without package in " + permFile
420 + " at " + parser.getPositionDescription());
421 } else {
422 mSystemUserWhitelistedApps.add(pkgname);
423 }
424 XmlUtils.skipCurrentTag(parser);
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800425 } else if ("system-user-blacklisted-app".equals(name) && allowAppConfigs) {
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700426 String pkgname = parser.getAttributeValue(null, "package");
427 if (pkgname == null) {
428 Slog.w(TAG, "<system-user-blacklisted-app without package in " + permFile
429 + " at " + parser.getPositionDescription());
430 } else {
431 mSystemUserBlacklistedApps.add(pkgname);
432 }
433 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700434 } else {
435 XmlUtils.skipCurrentTag(parser);
436 continue;
437 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700438 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700439 } catch (XmlPullParserException e) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800440 Slog.w(TAG, "Got exception parsing permissions.", e);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700441 } catch (IOException e) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800442 Slog.w(TAG, "Got exception parsing permissions.", e);
443 } finally {
444 IoUtils.closeQuietly(permReader);
445 }
446
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -0700447 // Some devices can be field-converted to FBE, so offer to splice in
448 // those features if not already defined by the static config
Paul Lawrence20be5d62016-02-26 13:51:17 -0800449 if (StorageManager.isFileEncryptedNativeOnly()) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700450 addFeature(PackageManager.FEATURE_FILE_BASED_ENCRYPTION, 0);
451 addFeature(PackageManager.FEATURE_SECURELY_REMOVES_USERS, 0);
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -0700452 }
453
454 for (String featureName : mUnavailableFeatures) {
455 removeFeature(featureName);
456 }
457 }
458
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700459 private void addFeature(String name, int version) {
460 FeatureInfo fi = mAvailableFeatures.get(name);
461 if (fi == null) {
462 fi = new FeatureInfo();
463 fi.name = name;
464 fi.version = version;
465 mAvailableFeatures.put(name, fi);
466 } else {
467 fi.version = Math.max(fi.version, version);
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -0700468 }
469 }
470
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700471 private void removeFeature(String name) {
472 if (mAvailableFeatures.remove(name) != null) {
473 Slog.d(TAG, "Removed unavailable feature " + name);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700474 }
475 }
476
477 void readPermission(XmlPullParser parser, String name)
478 throws IOException, XmlPullParserException {
Jeff Sharkey00f39042015-03-23 16:51:22 -0700479 if (mPermissions.containsKey(name)) {
480 throw new IllegalStateException("Duplicate permission definition for " + name);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700481 }
Jeff Sharkey00f39042015-03-23 16:51:22 -0700482
483 final boolean perUser = XmlUtils.readBooleanAttribute(parser, "perUser", false);
484 final PermissionEntry perm = new PermissionEntry(name, perUser);
485 mPermissions.put(name, perm);
486
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700487 int outerDepth = parser.getDepth();
488 int type;
489 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
490 && (type != XmlPullParser.END_TAG
491 || parser.getDepth() > outerDepth)) {
492 if (type == XmlPullParser.END_TAG
493 || type == XmlPullParser.TEXT) {
494 continue;
495 }
496
497 String tagName = parser.getName();
498 if ("group".equals(tagName)) {
499 String gidStr = parser.getAttributeValue(null, "gid");
500 if (gidStr != null) {
501 int gid = Process.getGidForName(gidStr);
502 perm.gids = appendInt(perm.gids, gid);
503 } else {
504 Slog.w(TAG, "<group> without gid at "
505 + parser.getPositionDescription());
506 }
507 }
508 XmlUtils.skipCurrentTag(parser);
509 }
510 }
511}