blob: 7bdd3e2c6e054efceeb189df937e971c4788bcbe [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
104 // These are the app package names that should not allow IME switching.
105 final ArraySet<String> mFixedImeApps = new ArraySet<>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700106
Christopher Tate01e18642015-07-07 18:10:38 -0700107 // These are the package names of apps which should be in the 'always'
108 // URL-handling state upon factory reset.
109 final ArraySet<String> mLinkedApps = new ArraySet<>();
110
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700111 // These are the packages that are whitelisted to be able to run as system user
112 final ArraySet<String> mSystemUserWhitelistedApps = new ArraySet<>();
113
114 // These are the packages that should not run under system user
115 final ArraySet<String> mSystemUserBlacklistedApps = new ArraySet<>();
116
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700117 public static SystemConfig getInstance() {
118 synchronized (SystemConfig.class) {
119 if (sInstance == null) {
120 sInstance = new SystemConfig();
121 }
122 return sInstance;
123 }
124 }
125
126 public int[] getGlobalGids() {
127 return mGlobalGids;
128 }
129
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700130 public SparseArray<ArraySet<String>> getSystemPermissions() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700131 return mSystemPermissions;
132 }
133
134 public ArrayMap<String, String> getSharedLibraries() {
135 return mSharedLibraries;
136 }
137
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700138 public ArrayMap<String, FeatureInfo> getAvailableFeatures() {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700139 return mAvailableFeatures;
140 }
141
142 public ArrayMap<String, PermissionEntry> getPermissions() {
143 return mPermissions;
144 }
145
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700146 public ArraySet<String> getAllowInPowerSaveExceptIdle() {
147 return mAllowInPowerSaveExceptIdle;
148 }
149
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700150 public ArraySet<String> getAllowInPowerSave() {
151 return mAllowInPowerSave;
152 }
153
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -0700154 public ArraySet<String> getFixedImeApps() {
155 return mFixedImeApps;
156 }
157
Christopher Tate01e18642015-07-07 18:10:38 -0700158 public ArraySet<String> getLinkedApps() {
159 return mLinkedApps;
160 }
161
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700162 public ArraySet<String> getSystemUserWhitelistedApps() {
163 return mSystemUserWhitelistedApps;
164 }
165
166 public ArraySet<String> getSystemUserBlacklistedApps() {
167 return mSystemUserBlacklistedApps;
168 }
169
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700170 SystemConfig() {
171 // Read configuration from system
172 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800173 Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700174 // Read configuration from the old permissions dir
175 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800176 Environment.getRootDirectory(), "etc", "permissions"), ALLOW_ALL);
177 // Allow ODM to customize system configs around libs, features and apps
178 int odmPermissionFlag = ALLOW_LIBS | ALLOW_FEATURES | ALLOW_APP_CONFIGS;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700179 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800180 Environment.getOdmDirectory(), "etc", "sysconfig"), odmPermissionFlag);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700181 readPermissions(Environment.buildPath(
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800182 Environment.getOdmDirectory(), "etc", "permissions"), odmPermissionFlag);
183 // Only allow OEM to customize features
184 readPermissions(Environment.buildPath(
185 Environment.getOemDirectory(), "etc", "sysconfig"), ALLOW_FEATURES);
186 readPermissions(Environment.buildPath(
187 Environment.getOemDirectory(), "etc", "permissions"), ALLOW_FEATURES);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700188 }
189
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800190 void readPermissions(File libraryDir, int permissionFlag) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700191 // Read permissions from given directory.
192 if (!libraryDir.exists() || !libraryDir.isDirectory()) {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800193 if (permissionFlag == ALLOW_ALL) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700194 Slog.w(TAG, "No directory " + libraryDir + ", skipping");
195 }
196 return;
197 }
198 if (!libraryDir.canRead()) {
199 Slog.w(TAG, "Directory " + libraryDir + " cannot be read");
200 return;
201 }
202
203 // Iterate over the files in the directory and scan .xml files
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800204 File platformFile = null;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700205 for (File f : libraryDir.listFiles()) {
206 // We'll read platform.xml last
207 if (f.getPath().endsWith("etc/permissions/platform.xml")) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800208 platformFile = f;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700209 continue;
210 }
211
212 if (!f.getPath().endsWith(".xml")) {
213 Slog.i(TAG, "Non-xml file " + f + " in " + libraryDir + " directory, ignoring");
214 continue;
215 }
216 if (!f.canRead()) {
217 Slog.w(TAG, "Permissions library file " + f + " cannot be read");
218 continue;
219 }
220
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800221 readPermissionsFromXml(f, permissionFlag);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700222 }
223
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800224 // Read platform permissions last so it will take precedence
225 if (platformFile != null) {
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800226 readPermissionsFromXml(platformFile, permissionFlag);
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800227 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700228 }
229
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800230 private void readPermissionsFromXml(File permFile, int permissionFlag) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700231 FileReader permReader = null;
232 try {
233 permReader = new FileReader(permFile);
234 } catch (FileNotFoundException e) {
235 Slog.w(TAG, "Couldn't find or open permissions file " + permFile);
236 return;
237 }
238
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800239 final boolean lowRam = ActivityManager.isLowRamDeviceStatic();
240
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700241 try {
242 XmlPullParser parser = Xml.newPullParser();
243 parser.setInput(permReader);
244
245 int type;
246 while ((type=parser.next()) != parser.START_TAG
247 && type != parser.END_DOCUMENT) {
248 ;
249 }
250
251 if (type != parser.START_TAG) {
252 throw new XmlPullParserException("No start tag found");
253 }
254
255 if (!parser.getName().equals("permissions") && !parser.getName().equals("config")) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800256 throw new XmlPullParserException("Unexpected start tag in " + permFile
257 + ": found " + parser.getName() + ", expected 'permissions' or 'config'");
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700258 }
259
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800260 boolean allowAll = permissionFlag == ALLOW_ALL;
261 boolean allowLibs = (permissionFlag & ALLOW_LIBS) != 0;
262 boolean allowFeatures = (permissionFlag & ALLOW_FEATURES) != 0;
263 boolean allowPermissions = (permissionFlag & ALLOW_PERMISSIONS) != 0;
264 boolean allowAppConfigs = (permissionFlag & ALLOW_APP_CONFIGS) != 0;
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700265 while (true) {
266 XmlUtils.nextElement(parser);
267 if (parser.getEventType() == XmlPullParser.END_DOCUMENT) {
268 break;
269 }
270
271 String name = parser.getName();
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800272 if ("group".equals(name) && allowAll) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700273 String gidStr = parser.getAttributeValue(null, "gid");
274 if (gidStr != null) {
275 int gid = android.os.Process.getGidForName(gidStr);
276 mGlobalGids = appendInt(mGlobalGids, gid);
277 } else {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800278 Slog.w(TAG, "<group> without gid in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700279 + parser.getPositionDescription());
280 }
281
282 XmlUtils.skipCurrentTag(parser);
283 continue;
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800284 } else if ("permission".equals(name) && allowPermissions) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700285 String perm = parser.getAttributeValue(null, "name");
286 if (perm == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800287 Slog.w(TAG, "<permission> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700288 + parser.getPositionDescription());
289 XmlUtils.skipCurrentTag(parser);
290 continue;
291 }
292 perm = perm.intern();
293 readPermission(parser, perm);
294
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800295 } else if ("assign-permission".equals(name) && allowPermissions) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700296 String perm = parser.getAttributeValue(null, "name");
297 if (perm == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800298 Slog.w(TAG, "<assign-permission> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700299 + parser.getPositionDescription());
300 XmlUtils.skipCurrentTag(parser);
301 continue;
302 }
303 String uidStr = parser.getAttributeValue(null, "uid");
304 if (uidStr == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800305 Slog.w(TAG, "<assign-permission> without uid in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700306 + parser.getPositionDescription());
307 XmlUtils.skipCurrentTag(parser);
308 continue;
309 }
310 int uid = Process.getUidForName(uidStr);
311 if (uid < 0) {
312 Slog.w(TAG, "<assign-permission> with unknown uid \""
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800313 + uidStr + " in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700314 + parser.getPositionDescription());
315 XmlUtils.skipCurrentTag(parser);
316 continue;
317 }
318 perm = perm.intern();
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700319 ArraySet<String> perms = mSystemPermissions.get(uid);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700320 if (perms == null) {
Jeff Sharkey9f837a92014-10-24 12:07:24 -0700321 perms = new ArraySet<String>();
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700322 mSystemPermissions.put(uid, perms);
323 }
324 perms.add(perm);
325 XmlUtils.skipCurrentTag(parser);
326
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800327 } else if ("library".equals(name) && allowLibs) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700328 String lname = parser.getAttributeValue(null, "name");
329 String lfile = parser.getAttributeValue(null, "file");
330 if (lname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800331 Slog.w(TAG, "<library> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700332 + parser.getPositionDescription());
333 } else if (lfile == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800334 Slog.w(TAG, "<library> without file in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700335 + parser.getPositionDescription());
336 } else {
337 //Log.i(TAG, "Got library " + lname + " in " + lfile);
338 mSharedLibraries.put(lname, lfile);
339 }
340 XmlUtils.skipCurrentTag(parser);
341 continue;
342
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800343 } else if ("feature".equals(name) && allowFeatures) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700344 String fname = parser.getAttributeValue(null, "name");
Arunesh Mishra4e0bdf32016-02-15 23:50:38 -0800345 int fversion = XmlUtils.readIntAttribute(parser, "version", 0);
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800346 boolean allowed;
347 if (!lowRam) {
348 allowed = true;
349 } else {
350 String notLowRam = parser.getAttributeValue(null, "notLowRam");
351 allowed = !"true".equals(notLowRam);
352 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700353 if (fname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800354 Slog.w(TAG, "<feature> without name in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700355 + parser.getPositionDescription());
Dianne Hackbornc0e4aaa2014-11-14 10:55:50 -0800356 } else if (allowed) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700357 addFeature(fname, fversion);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700358 }
359 XmlUtils.skipCurrentTag(parser);
360 continue;
361
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800362 } else if ("unavailable-feature".equals(name) && allowFeatures) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800363 String fname = parser.getAttributeValue(null, "name");
364 if (fname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800365 Slog.w(TAG, "<unavailable-feature> without name in " + permFile + " at "
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800366 + parser.getPositionDescription());
367 } else {
368 mUnavailableFeatures.add(fname);
369 }
370 XmlUtils.skipCurrentTag(parser);
371 continue;
372
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800373 } else if ("allow-in-power-save-except-idle".equals(name) && allowAll) {
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700374 String pkgname = parser.getAttributeValue(null, "package");
375 if (pkgname == null) {
376 Slog.w(TAG, "<allow-in-power-save-except-idle> without package in "
377 + permFile + " at " + parser.getPositionDescription());
378 } else {
379 mAllowInPowerSaveExceptIdle.add(pkgname);
380 }
381 XmlUtils.skipCurrentTag(parser);
382 continue;
383
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800384 } else if ("allow-in-power-save".equals(name) && allowAll) {
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700385 String pkgname = parser.getAttributeValue(null, "package");
386 if (pkgname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800387 Slog.w(TAG, "<allow-in-power-save> without package in " + permFile + " at "
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700388 + parser.getPositionDescription());
389 } else {
390 mAllowInPowerSave.add(pkgname);
391 }
392 XmlUtils.skipCurrentTag(parser);
393 continue;
394
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800395 } else if ("fixed-ime-app".equals(name) && allowAll) {
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -0700396 String pkgname = parser.getAttributeValue(null, "package");
397 if (pkgname == null) {
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800398 Slog.w(TAG, "<fixed-ime-app> without package in " + permFile + " at "
Dianne Hackbornbb8aa5a2014-09-17 13:20:38 -0700399 + parser.getPositionDescription());
400 } else {
401 mFixedImeApps.add(pkgname);
402 }
403 XmlUtils.skipCurrentTag(parser);
404 continue;
405
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800406 } else if ("app-link".equals(name) && allowAppConfigs) {
Christopher Tate01e18642015-07-07 18:10:38 -0700407 String pkgname = parser.getAttributeValue(null, "package");
408 if (pkgname == null) {
409 Slog.w(TAG, "<app-link> without package in " + permFile + " at "
410 + parser.getPositionDescription());
411 } else {
412 mLinkedApps.add(pkgname);
413 }
414 XmlUtils.skipCurrentTag(parser);
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800415 } else if ("system-user-whitelisted-app".equals(name) && allowAppConfigs) {
Fyodor Kupolov7db5af12015-07-31 16:50:27 -0700416 String pkgname = parser.getAttributeValue(null, "package");
417 if (pkgname == null) {
418 Slog.w(TAG, "<system-user-whitelisted-app> without package in " + permFile
419 + " at " + parser.getPositionDescription());
420 } else {
421 mSystemUserWhitelistedApps.add(pkgname);
422 }
423 XmlUtils.skipCurrentTag(parser);
Hung-ying Tyanbdc9d582015-11-20 11:53:39 +0800424 } else if ("system-user-blacklisted-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-blacklisted-app without package in " + permFile
428 + " at " + parser.getPositionDescription());
429 } else {
430 mSystemUserBlacklistedApps.add(pkgname);
431 }
432 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700433 } else {
434 XmlUtils.skipCurrentTag(parser);
435 continue;
436 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700437 }
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700438 } catch (XmlPullParserException e) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800439 Slog.w(TAG, "Got exception parsing permissions.", e);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700440 } catch (IOException e) {
Jeff Sharkey1c4ae802014-12-19 11:08:55 -0800441 Slog.w(TAG, "Got exception parsing permissions.", e);
442 } finally {
443 IoUtils.closeQuietly(permReader);
444 }
445
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -0700446 // Some devices can be field-converted to FBE, so offer to splice in
447 // those features if not already defined by the static config
448 if (StorageManager.isNativeFileBasedEncryptionEnabled()) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700449 addFeature(PackageManager.FEATURE_FILE_BASED_ENCRYPTION, 0);
450 addFeature(PackageManager.FEATURE_SECURELY_REMOVES_USERS, 0);
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -0700451 }
452
453 for (String featureName : mUnavailableFeatures) {
454 removeFeature(featureName);
455 }
456 }
457
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700458 private void addFeature(String name, int version) {
459 FeatureInfo fi = mAvailableFeatures.get(name);
460 if (fi == null) {
461 fi = new FeatureInfo();
462 fi.name = name;
463 fi.version = version;
464 mAvailableFeatures.put(name, fi);
465 } else {
466 fi.version = Math.max(fi.version, version);
Jeff Sharkeyb92b05b2016-01-28 09:50:00 -0700467 }
468 }
469
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700470 private void removeFeature(String name) {
471 if (mAvailableFeatures.remove(name) != null) {
472 Slog.d(TAG, "Removed unavailable feature " + name);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700473 }
474 }
475
476 void readPermission(XmlPullParser parser, String name)
477 throws IOException, XmlPullParserException {
Jeff Sharkey00f39042015-03-23 16:51:22 -0700478 if (mPermissions.containsKey(name)) {
479 throw new IllegalStateException("Duplicate permission definition for " + name);
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700480 }
Jeff Sharkey00f39042015-03-23 16:51:22 -0700481
482 final boolean perUser = XmlUtils.readBooleanAttribute(parser, "perUser", false);
483 final PermissionEntry perm = new PermissionEntry(name, perUser);
484 mPermissions.put(name, perm);
485
Dianne Hackbornbe7c50e2014-06-30 14:43:28 -0700486 int outerDepth = parser.getDepth();
487 int type;
488 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
489 && (type != XmlPullParser.END_TAG
490 || parser.getDepth() > outerDepth)) {
491 if (type == XmlPullParser.END_TAG
492 || type == XmlPullParser.TEXT) {
493 continue;
494 }
495
496 String tagName = parser.getName();
497 if ("group".equals(tagName)) {
498 String gidStr = parser.getAttributeValue(null, "gid");
499 if (gidStr != null) {
500 int gid = Process.getGidForName(gidStr);
501 perm.gids = appendInt(perm.gids, gid);
502 } else {
503 Slog.w(TAG, "<group> without gid at "
504 + parser.getPositionDescription());
505 }
506 }
507 XmlUtils.skipCurrentTag(parser);
508 }
509 }
510}