Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.accounts; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 18 | |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 20 | import android.accounts.AbstractAccountAuthenticator; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 21 | import android.accounts.Account; |
| 22 | import android.accounts.AccountAndUser; |
| 23 | import android.accounts.AccountAuthenticatorResponse; |
| 24 | import android.accounts.AccountManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 25 | import android.accounts.AccountManagerInternal; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 26 | import android.accounts.AuthenticatorDescription; |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 27 | import android.accounts.CantAddAccountActivity; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 28 | import android.accounts.GrantCredentialsPermissionActivity; |
| 29 | import android.accounts.IAccountAuthenticator; |
| 30 | import android.accounts.IAccountAuthenticatorResponse; |
| 31 | import android.accounts.IAccountManager; |
| 32 | import android.accounts.IAccountManagerResponse; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 33 | import android.annotation.IntRange; |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 34 | import android.annotation.NonNull; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 35 | import android.annotation.Nullable; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 36 | import android.app.ActivityManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 37 | import android.app.ActivityThread; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 38 | import android.app.AppGlobals; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 39 | import android.app.AppOpsManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 40 | import android.app.INotificationManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 41 | import android.app.Notification; |
| 42 | import android.app.NotificationManager; |
| 43 | import android.app.PendingIntent; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 44 | import android.app.admin.DeviceAdminInfo; |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 45 | import android.app.admin.DevicePolicyManager; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 46 | import android.app.admin.DevicePolicyManagerInternal; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 47 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 48 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 49 | import android.content.Context; |
| 50 | import android.content.Intent; |
| 51 | import android.content.IntentFilter; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 52 | import android.content.IntentSender; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 53 | import android.content.ServiceConnection; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 54 | import android.content.pm.ActivityInfo; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 55 | import android.content.pm.ApplicationInfo; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 56 | import android.content.pm.IPackageManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 57 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 58 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 59 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 60 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 61 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 62 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 63 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 64 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 65 | import android.database.Cursor; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 66 | import android.database.sqlite.SQLiteStatement; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 67 | import android.net.Uri; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 68 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 69 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 70 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 71 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 72 | import android.os.IBinder; |
| 73 | import android.os.Looper; |
| 74 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 75 | import android.os.Parcel; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 76 | import android.os.Process; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 77 | import android.os.RemoteCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 78 | import android.os.RemoteException; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 79 | import android.os.StrictMode; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 80 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 81 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 82 | import android.os.UserManager; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 83 | import android.os.storage.StorageManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 84 | import android.text.TextUtils; |
| 85 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 86 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 87 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 88 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 89 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 90 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 91 | import com.android.internal.R; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 92 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 93 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 94 | import com.android.internal.content.PackageMonitor; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 95 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 96 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 97 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 98 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 99 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 100 | import com.android.server.SystemService; |
| 101 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 102 | import com.google.android.collect.Lists; |
| 103 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 104 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 105 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 106 | import java.io.FileDescriptor; |
| 107 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 108 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 109 | import java.security.MessageDigest; |
| 110 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 111 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 112 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 113 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 114 | import java.util.Collection; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 115 | import java.util.Collections; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 116 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 117 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 118 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 119 | import java.util.LinkedHashMap; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 120 | import java.util.LinkedHashSet; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 121 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 122 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 123 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 124 | import java.util.Objects; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 125 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 126 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 127 | import java.util.concurrent.atomic.AtomicInteger; |
| 128 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 129 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 130 | /** |
| 131 | * A system service that provides account, password, and authtoken management for all |
| 132 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 133 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 134 | * instead one uses an instance of {@link AccountManager}, which can be accessed as follows: |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 135 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 136 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 137 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 138 | public class AccountManagerService |
| 139 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 140 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 141 | private static final String TAG = "AccountManagerService"; |
| 142 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 143 | public static class Lifecycle extends SystemService { |
| 144 | private AccountManagerService mService; |
| 145 | |
| 146 | public Lifecycle(Context context) { |
| 147 | super(context); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onStart() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 152 | mService = new AccountManagerService(new Injector(getContext())); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 153 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 154 | } |
| 155 | |
| 156 | @Override |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 157 | public void onUnlockUser(int userHandle) { |
| 158 | mService.onUnlockUser(userHandle); |
| 159 | } |
| 160 | } |
| 161 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 162 | final Context mContext; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 163 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 164 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 165 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 166 | private UserManager mUserManager; |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 167 | private final Injector mInjector; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 168 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 169 | final MessageHandler mHandler; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 170 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 171 | // Messages that can be sent on mHandler |
| 172 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 173 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 174 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 175 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 176 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 177 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 178 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 179 | private static final int SIGNATURE_CHECK_MISMATCH = 0; |
| 180 | private static final int SIGNATURE_CHECK_MATCH = 1; |
| 181 | private static final int SIGNATURE_CHECK_UID_MATCH = 2; |
| 182 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 183 | static { |
| 184 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
| 185 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 186 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 187 | |
| 188 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 189 | private final AtomicInteger mNotificationIds = new AtomicInteger(1); |
| 190 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 191 | static class UserAccounts { |
| 192 | private final int userId; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 193 | final AccountsDb accountsDb; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 194 | private final HashMap<Pair<Pair<Account, String>, Integer>, Integer> |
| 195 | credentialsPermissionNotificationIds = |
| 196 | new HashMap<Pair<Pair<Account, String>, Integer>, Integer>(); |
| 197 | private final HashMap<Account, Integer> signinRequiredNotificationIds = |
| 198 | new HashMap<Account, Integer>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 199 | final Object cacheLock = new Object(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 200 | /** protected by the {@link #cacheLock} */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 201 | final HashMap<String, Account[]> accountCache = |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 202 | new LinkedHashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 203 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 204 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 205 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 206 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 207 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 208 | private final TokenCache accountTokenCaches = new TokenCache(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 209 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 210 | /** protected by the {@link #cacheLock} */ |
| 211 | // TODO use callback to set up the map. |
| 212 | private final Map<String, LinkedHashSet<String>> mApplicationAccountRequestMappings = |
| 213 | new HashMap<>(); |
| 214 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 215 | /** |
| 216 | * protected by the {@link #cacheLock} |
| 217 | * |
| 218 | * Caches the previous names associated with an account. Previous names |
| 219 | * should be cached because we expect that when an Account is renamed, |
| 220 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 221 | * want to know if the accounts they care about have been renamed. |
| 222 | * |
| 223 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 224 | * we can distinguish between those accounts with no previous names and |
| 225 | * those whose previous names haven't been cached (yet). |
| 226 | */ |
| 227 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 228 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 229 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 230 | private int debugDbInsertionPoint = -1; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 231 | private SQLiteStatement statementForLogging; // TODO Move to AccountsDb |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 232 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 233 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 234 | this.userId = userId; |
| 235 | synchronized (cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 236 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 241 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 242 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 243 | // Not thread-safe. Only use in synchronized context |
| 244 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 245 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 246 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 247 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 248 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 249 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 250 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 251 | /** |
| 252 | * This should only be called by system code. One should only call this after the service |
| 253 | * has started. |
| 254 | * @return a reference to the AccountManagerService instance |
| 255 | * @hide |
| 256 | */ |
| 257 | public static AccountManagerService getSingleton() { |
| 258 | return sThis.get(); |
| 259 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 260 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 261 | public AccountManagerService(Injector injector) { |
| 262 | mInjector = injector; |
| 263 | mContext = injector.getContext(); |
| 264 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 265 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 266 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 267 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 268 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 269 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 270 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 271 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 272 | IntentFilter intentFilter = new IntentFilter(); |
| 273 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 274 | intentFilter.addDataScheme("package"); |
| 275 | mContext.registerReceiver(new BroadcastReceiver() { |
| 276 | @Override |
| 277 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 278 | // Don't delete accounts when updating a authenticator's |
| 279 | // package. |
| 280 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 281 | /* Purging data requires file io, don't block the main thread. This is probably |
| 282 | * less than ideal because we are introducing a race condition where old grants |
| 283 | * could be exercised until they are purged. But that race condition existed |
| 284 | * anyway with the broadcast receiver. |
| 285 | * |
| 286 | * Ideally, we would completely clear the cache, purge data from the database, |
| 287 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 288 | * large at this point. |
| 289 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 290 | final String removedPackageName = intent.getData().toString(); |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 291 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 292 | @Override |
| 293 | public void run() { |
| 294 | purgeOldGrantsAll(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 295 | // Notify authenticator about removed app? |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 296 | removeVisibilityValuesForPackage(removedPackageName); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 297 | } |
| 298 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 299 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 300 | } |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 301 | } |
| 302 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 303 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 304 | IntentFilter userFilter = new IntentFilter(); |
| 305 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 306 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 307 | @Override |
| 308 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 309 | String action = intent.getAction(); |
| 310 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 311 | onUserRemoved(intent); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 312 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 313 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 314 | }, UserHandle.ALL, userFilter, null, null); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 315 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 316 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 317 | |
| 318 | // Need to cancel account request notifications if the update/install can access the account |
| 319 | new PackageMonitor() { |
| 320 | @Override |
| 321 | public void onPackageAdded(String packageName, int uid) { |
| 322 | // Called on a handler, and running as the system |
| 323 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 328 | // Called on a handler, and running as the system |
| 329 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 330 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 331 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 332 | |
| 333 | // Cancel account request notification if an app op was preventing the account access |
| 334 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 335 | new AppOpsManager.OnOpChangedInternalListener() { |
| 336 | @Override |
| 337 | public void onOpChanged(int op, String packageName) { |
| 338 | try { |
| 339 | final int userId = ActivityManager.getCurrentUser(); |
| 340 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 341 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 342 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 343 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 344 | final long identity = Binder.clearCallingIdentity(); |
| 345 | try { |
| 346 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 347 | } finally { |
| 348 | Binder.restoreCallingIdentity(identity); |
| 349 | } |
| 350 | } |
| 351 | } catch (NameNotFoundException e) { |
| 352 | /* ignore */ |
| 353 | } |
| 354 | } |
| 355 | }); |
| 356 | |
| 357 | // Cancel account request notification if a permission was preventing the account access |
| 358 | mPackageManager.addOnPermissionsChangeListener( |
| 359 | (int uid) -> { |
| 360 | Account[] accounts = null; |
| 361 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 362 | if (packageNames != null) { |
| 363 | final int userId = UserHandle.getUserId(uid); |
| 364 | final long identity = Binder.clearCallingIdentity(); |
| 365 | try { |
| 366 | for (String packageName : packageNames) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 367 | // if app asked for permission we need to cancel notification even |
| 368 | // for O+ applications. |
| 369 | if (mPackageManager.checkPermission( |
| 370 | Manifest.permission.GET_ACCOUNTS, |
| 371 | packageName) != PackageManager.PERMISSION_GRANTED) { |
| 372 | continue; |
| 373 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 374 | |
| 375 | if (accounts == null) { |
| 376 | accounts = getAccountsAsUser(null, userId, "android"); |
| 377 | if (ArrayUtils.isEmpty(accounts)) { |
| 378 | return; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | for (Account account : accounts) { |
| 383 | cancelAccountAccessRequestNotificationIfNeeded( |
| 384 | account, uid, packageName, true); |
| 385 | } |
| 386 | } |
| 387 | } finally { |
| 388 | Binder.restoreCallingIdentity(identity); |
| 389 | } |
| 390 | } |
| 391 | }); |
| 392 | } |
| 393 | |
| 394 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 395 | boolean checkAccess) { |
| 396 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 397 | for (Account account : accounts) { |
| 398 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 403 | boolean checkAccess) { |
| 404 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 405 | for (Account account : accounts) { |
| 406 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 411 | boolean checkAccess) { |
| 412 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 413 | if (packageNames != null) { |
| 414 | for (String packageName : packageNames) { |
| 415 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 416 | packageName, checkAccess); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 422 | int uid, String packageName, boolean checkAccess) { |
| 423 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 424 | UserHandle.getUserHandleForUid(uid))) { |
| 425 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 426 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 427 | UserHandle.getUserHandleForUid(uid)); |
| 428 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 431 | @Override |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 432 | public boolean addAccountExplicitlyWithVisibility(Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 433 | Bundle extras, Map packageToVisibility) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 434 | Bundle.setDefusable(extras, true); |
| 435 | |
| 436 | final int callingUid = Binder.getCallingUid(); |
| 437 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 438 | Log.v(TAG, "addAccountExplicitly: " + account + ", caller's uid " + callingUid |
| 439 | + ", pid " + Binder.getCallingPid()); |
| 440 | } |
| 441 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 442 | int userId = UserHandle.getCallingUserId(); |
| 443 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
| 444 | String msg = String.format("uid %s cannot explicitly add accounts of type: %s", |
| 445 | callingUid, account.type); |
| 446 | throw new SecurityException(msg); |
| 447 | } |
| 448 | /* |
| 449 | * Child users are not allowed to add accounts. Only the accounts that are shared by the |
| 450 | * parent profile can be added to child profile. |
| 451 | * |
| 452 | * TODO: Only allow accounts that were shared to be added by a limited user. |
| 453 | */ |
| 454 | // fails if the account already exists |
| 455 | long identityToken = clearCallingIdentity(); |
| 456 | try { |
| 457 | UserAccounts accounts = getUserAccounts(userId); |
| 458 | return addAccountInternal(accounts, account, password, extras, callingUid, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 459 | (Map<String, Integer>) packageToVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 460 | } finally { |
| 461 | restoreCallingIdentity(identityToken); |
| 462 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | @Override |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 466 | public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 467 | String accountType) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 468 | int callingUid = Binder.getCallingUid(); |
| 469 | boolean isSystemUid = UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); |
| 470 | List<String> managedTypes = |
| 471 | getTypesForCaller(callingUid, UserHandle.getUserId(callingUid), isSystemUid); |
| 472 | |
| 473 | if ((accountType != null && !managedTypes.contains(accountType)) |
| 474 | || (accountType == null && !isSystemUid)) { |
| 475 | throw new SecurityException( |
| 476 | "getAccountsAndVisibilityForPackage() called from unauthorized uid " |
| 477 | + callingUid + " with packageName=" + packageName); |
| 478 | } |
| 479 | if (accountType != null) { |
| 480 | managedTypes = new ArrayList<String>(); |
| 481 | managedTypes.add(accountType); |
| 482 | } |
| 483 | |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame^] | 484 | long identityToken = clearCallingIdentity(); |
| 485 | try { |
| 486 | return getAccountsAndVisibilityForPackage(packageName, managedTypes, callingUid, |
| 487 | getUserAccounts(UserHandle.getUserId(callingUid))); |
| 488 | } finally { |
| 489 | restoreCallingIdentity(identityToken); |
| 490 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /* |
| 494 | * accountTypes may not be null |
| 495 | */ |
| 496 | private Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 497 | List<String> accountTypes, Integer callingUid, UserAccounts accounts) { |
| 498 | int uid = 0; |
| 499 | try { |
| 500 | uid = mPackageManager.getPackageUidAsUser(packageName, |
| 501 | UserHandle.getUserId(callingUid)); |
| 502 | } catch (NameNotFoundException e) { |
| 503 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 504 | return new HashMap<>(); |
| 505 | } |
| 506 | |
| 507 | Map<Account, Integer> result = new HashMap<>(); |
| 508 | for (String accountType : accountTypes) { |
| 509 | synchronized (accounts.cacheLock) { |
| 510 | final Account[] accountsOfType = accounts.accountCache.get(accountType); |
| 511 | if (accountsOfType != null) { |
| 512 | for (Account account : accountsOfType) { |
| 513 | result.put(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 514 | resolveAccountVisibility(account, packageName, accounts)); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | return filterSharedAccounts(accounts, result, callingUid, packageName); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | @Override |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 523 | public Map<String, Integer> getPackagesAndVisibilityForAccount(Account account) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 524 | if (account == null) throw new IllegalArgumentException("account is null"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 525 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 526 | int userId = UserHandle.getUserId(callingUid); |
| 527 | UserAccounts accounts = getUserAccounts(userId); |
| 528 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
| 529 | && !isSystemUid(callingUid)) { |
| 530 | String msg = |
| 531 | String.format("uid %s cannot get secrets for account %s", callingUid, account); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 532 | throw new SecurityException(msg); |
| 533 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 534 | return getPackagesAndVisibilityForAccount(account, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 538 | * Returns all package names and visibility values, which were set for given account. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 539 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 540 | * @param account Account to get visibility values. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 541 | * @param accounts UserAccount that currently hosts the account and application |
| 542 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 543 | * @return Map from package names to visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 544 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 545 | private Map<String, Integer> getPackagesAndVisibilityForAccount(Account account, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 546 | UserAccounts accounts) { |
| 547 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 548 | try { |
| 549 | return accounts.accountsDb.findAllVisibilityValuesForAccount(account); |
| 550 | } finally { |
| 551 | StrictMode.setThreadPolicy(oldPolicy); |
| 552 | } |
| 553 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | @Override |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 557 | public int getAccountVisibility(Account a, String packageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 558 | if (a == null) throw new IllegalArgumentException("account is null"); |
| 559 | int callingUid = Binder.getCallingUid(); |
| 560 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid)) |
| 561 | && !isSystemUid(callingUid)) { |
| 562 | String msg = String.format( |
| 563 | "uid %s cannot get secrets for accounts of type: %s", |
| 564 | callingUid, |
| 565 | a.type); |
| 566 | throw new SecurityException(msg); |
| 567 | } |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame^] | 568 | return resolveAccountVisibility(a, packageName, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 569 | getUserAccounts(UserHandle.getUserId(callingUid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 573 | * Method returns visibility for given account and package name. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 574 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 575 | * @param account The account to check visibility. |
| 576 | * @param packageName Package name to check visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 577 | * @param accounts UserAccount that currently hosts the account and application |
| 578 | * |
| 579 | * @return Visibility value, AccountManager.VISIBILITY_UNDEFINED if no value was stored. |
| 580 | * |
| 581 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 582 | private int getAccountVisibility(Account account, String packageName, UserAccounts accounts) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 583 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 584 | try { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 585 | Integer visibility = accounts.accountsDb.findAccountVisibility(account, packageName); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 586 | return visibility != null ? visibility : AccountManager.VISIBILITY_UNDEFINED; |
| 587 | } finally { |
| 588 | StrictMode.setThreadPolicy(oldPolicy); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Method which handles default values for Account visibility. |
| 594 | * |
| 595 | * @param account The account to check visibility. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 596 | * @param packageName Package name to check visibility |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 597 | * @param accounts UserAccount that currently hosts the account and application |
| 598 | * |
| 599 | * @return Visibility value, the method never returns AccountManager.VISIBILITY_UNDEFINED |
| 600 | * |
| 601 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 602 | private Integer resolveAccountVisibility(Account account, @NonNull String packageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 603 | UserAccounts accounts) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 604 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 605 | |
| 606 | int uid = -1; |
| 607 | try { |
| 608 | long identityToken = clearCallingIdentity(); |
| 609 | try { |
| 610 | uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 611 | } finally { |
| 612 | restoreCallingIdentity(identityToken); |
| 613 | } |
| 614 | } catch (NameNotFoundException e) { |
| 615 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 616 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 617 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 618 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 619 | // System visibility can not be restricted. |
| 620 | if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) { |
| 621 | return AccountManager.VISIBILITY_VISIBLE; |
| 622 | } |
| 623 | |
| 624 | int signatureCheckResult = |
| 625 | checkPackageSignature(account.type, uid, accounts.userId); |
| 626 | |
| 627 | // Authenticator can not restrict visibility to itself. |
| 628 | if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) { |
| 629 | return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account |
| 630 | } |
| 631 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 632 | if (isSpecialPackageKey(packageName)) { |
| 633 | Log.d(TAG, "Package name is forbidden: " + packageName); |
| 634 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
| 635 | } |
| 636 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 637 | // Return stored value if it was set. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 638 | int visibility = getAccountVisibility(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 639 | |
| 640 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 641 | return visibility; |
| 642 | } |
| 643 | |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 644 | boolean isPrivileged = isPermittedForPackage(packageName, accounts.userId, |
| 645 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 646 | |
| 647 | // Device/Profile owner gets visibility by default. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 648 | if (isProfileOwner(uid)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 649 | return AccountManager.VISIBILITY_VISIBLE; |
| 650 | } |
| 651 | // Apps with READ_CONTACTS permission get visibility by default even post O. |
| 652 | boolean canReadContacts = checkReadContactsPermission(packageName, accounts.userId); |
| 653 | |
| 654 | boolean preO = isPreOApplication(packageName); |
| 655 | if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH) |
| 656 | || (preO && checkGetAccountsPermission(packageName, accounts.userId)) |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 657 | || canReadContacts || isPrivileged) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 658 | // Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature |
| 659 | // match. |
| 660 | visibility = getAccountVisibility(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 661 | AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 662 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 663 | visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 664 | } |
| 665 | } else { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 666 | visibility = getAccountVisibility(account, |
| 667 | AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 668 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 669 | visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 670 | } |
| 671 | } |
| 672 | return visibility; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Checks targetSdk for a package; |
| 677 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 678 | * @param packageName Package name |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 679 | * |
| 680 | * @return True if package's target SDK is below {@link android.os.Build.VERSION_CODES#O}, or |
| 681 | * undefined |
| 682 | */ |
| 683 | private boolean isPreOApplication(String packageName) { |
| 684 | try { |
| 685 | long identityToken = clearCallingIdentity(); |
| 686 | ApplicationInfo applicationInfo; |
| 687 | try { |
| 688 | applicationInfo = mPackageManager.getApplicationInfo(packageName, 0); |
| 689 | } finally { |
| 690 | restoreCallingIdentity(identityToken); |
| 691 | } |
| 692 | |
| 693 | if (applicationInfo != null) { |
| 694 | int version = applicationInfo.targetSdkVersion; |
| 695 | return version < android.os.Build.VERSION_CODES.O; |
| 696 | } |
| 697 | return true; |
| 698 | } catch (NameNotFoundException e) { |
| 699 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 700 | return true; |
| 701 | } |
Dmitry Dementyev | 58fa8362 | 2016-12-20 18:08:51 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | @Override |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 705 | public boolean setAccountVisibility(Account a, String packageName, int newVisibility) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 706 | if (a == null) throw new IllegalArgumentException("account is null"); |
| 707 | int callingUid = Binder.getCallingUid(); |
| 708 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid)) |
| 709 | && !isSystemUid(callingUid)) { |
| 710 | String msg = String.format( |
| 711 | "uid %s cannot get secrets for accounts of type: %s", |
| 712 | callingUid, |
| 713 | a.type); |
| 714 | throw new SecurityException(msg); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 715 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 716 | return setAccountVisibility(a, packageName, newVisibility, true /* notify */, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 717 | getUserAccounts(UserHandle.getUserId(callingUid))); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 721 | * Updates visibility for given account name and package. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 722 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 723 | * @param account Account to update visibility. |
| 724 | * @param packageName Package name for which visibility is updated. |
| 725 | * @param newVisibility New visibility calue |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 726 | * @param notify if the flag is set applications will get notification about visibility change |
| 727 | * @param accounts UserAccount that currently hosts the account and application |
| 728 | * |
| 729 | * @return True if account visibility was changed. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 730 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 731 | private boolean setAccountVisibility(Account account, String packageName, int newVisibility, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 732 | boolean notify, UserAccounts accounts) { |
| 733 | synchronized (accounts.cacheLock) { |
| 734 | LinkedHashSet<String> interestedPackages; |
| 735 | if (notify) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 736 | if (isSpecialPackageKey(packageName)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 737 | interestedPackages = getRequestingPackageNames(account.type, accounts); |
| 738 | } else { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 739 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 740 | return false; // package is not installed. |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 741 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 742 | interestedPackages = new LinkedHashSet<>(); |
| 743 | interestedPackages.add(packageName); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 744 | } |
| 745 | } else { |
| 746 | // Notifications will not be send. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 747 | if (!isSpecialPackageKey(packageName) && |
| 748 | !packageExistsForUser(packageName, accounts.userId)) { |
| 749 | // package is not installed and not meta value. |
| 750 | return false; |
| 751 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 752 | interestedPackages = new LinkedHashSet<>(); |
| 753 | } |
| 754 | Integer[] interestedPackagesVisibility = new Integer[interestedPackages.size()]; |
| 755 | |
| 756 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 757 | if (accountId < 0) { |
| 758 | return false; |
| 759 | } |
| 760 | int index = 0; |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 761 | for (String interestedPackage : interestedPackages) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 762 | interestedPackagesVisibility[index++] = |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 763 | resolveAccountVisibility(account, interestedPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
| 767 | try { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 768 | if (!accounts.accountsDb.setAccountVisibility(accountId, packageName, |
| 769 | newVisibility)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 770 | return false; |
| 771 | } |
| 772 | } finally { |
| 773 | StrictMode.setThreadPolicy(oldPolicy); |
| 774 | } |
| 775 | |
| 776 | index = 0; |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 777 | for (String interestedPackage : interestedPackages) { |
| 778 | int visibility = resolveAccountVisibility(account, interestedPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 779 | if (visibility != interestedPackagesVisibility[index++]) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 780 | sendNotification(interestedPackage, account, accounts.userId); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 781 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 782 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 783 | if (notify) { |
| 784 | sendAccountsChangedBroadcast(accounts.userId); |
| 785 | } |
| 786 | return true; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 787 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /** |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 791 | * Sends a direct intent to a package, notifying it of a visible account change. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 792 | * |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 793 | * @param packageName to send Account to |
| 794 | * @param account to send to package |
| 795 | * @param userId User |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 796 | */ |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 797 | private void sendNotification(String packageName, Account account, int userId) { |
| 798 | // TODO send notification so apps subscribed in runtime. |
| 799 | } |
| 800 | |
| 801 | private void sendNotification(Account account, UserAccounts accounts) { |
| 802 | LinkedHashSet<String> interestedPackages = getRequestingPackageNames(account.type, |
| 803 | accounts); |
| 804 | for (String packageName : interestedPackages) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 805 | int visibility = resolveAccountVisibility(account, packageName, accounts); |
| 806 | if (visibility != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 807 | sendNotification(packageName, account, accounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 812 | LinkedHashSet<String> getRequestingPackageNames(String accountType, UserAccounts accounts) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 813 | // TODO return packages registered to get notifications. |
| 814 | return new LinkedHashSet<String>(); |
| 815 | } |
| 816 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 817 | private boolean packageExistsForUser(String packageName, int userId) { |
| 818 | try { |
| 819 | long identityToken = clearCallingIdentity(); |
| 820 | try { |
| 821 | mPackageManager.getPackageUidAsUser(packageName, userId); |
| 822 | return true; // package exist |
| 823 | } finally { |
| 824 | restoreCallingIdentity(identityToken); |
| 825 | } |
| 826 | } catch (NameNotFoundException e) { |
| 827 | return false; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Returns true if packageName is one of special values. |
| 833 | */ |
| 834 | private boolean isSpecialPackageKey(String packageName) { |
| 835 | return (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName) |
| 836 | || AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)); |
| 837 | } |
| 838 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 839 | private void sendAccountsChangedBroadcast(int userId) { |
| 840 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 841 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
| 842 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 846 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 847 | throws RemoteException { |
| 848 | try { |
| 849 | return super.onTransact(code, data, reply, flags); |
| 850 | } catch (RuntimeException e) { |
| 851 | // The account manager only throws security exceptions, so let's |
| 852 | // log all others. |
| 853 | if (!(e instanceof SecurityException)) { |
| 854 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 855 | } |
| 856 | throw e; |
| 857 | } |
| 858 | } |
| 859 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 860 | private UserManager getUserManager() { |
| 861 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 862 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 863 | } |
| 864 | return mUserManager; |
| 865 | } |
| 866 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 867 | /** |
| 868 | * Validate internal set of accounts against installed authenticators for |
| 869 | * given user. Clears cached authenticators before validating. |
| 870 | */ |
| 871 | public void validateAccounts(int userId) { |
| 872 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 873 | // Invalidate user-specific cache to make sure we catch any |
| 874 | // removed authenticators. |
| 875 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Validate internal set of accounts against installed authenticators for |
| 880 | * given user. Clear cached authenticators before validating when requested. |
| 881 | */ |
| 882 | private void validateAccountsInternal( |
| 883 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 884 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 885 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 886 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 887 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 888 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 889 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 890 | if (invalidateAuthenticatorCache) { |
| 891 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 892 | } |
| 893 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 894 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 895 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 896 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 897 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 898 | synchronized (accounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 899 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 900 | |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 901 | // Get a map of stored authenticator types to UID |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 902 | final AccountsDb accountsDb = accounts.accountsDb; |
| 903 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 904 | // Create a list of authenticator type whose previous uid no longer exists |
| 905 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 906 | SparseBooleanArray knownUids = null; |
| 907 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 908 | String type = authToUidEntry.getKey(); |
| 909 | int uid = authToUidEntry.getValue(); |
| 910 | Integer knownUid = knownAuth.get(type); |
| 911 | if (knownUid != null && uid == knownUid) { |
| 912 | // Remove it from the knownAuth list if it's unchanged. |
| 913 | knownAuth.remove(type); |
| 914 | } else { |
| 915 | /* |
| 916 | * The authenticator is presently not cached and should only be triggered |
| 917 | * when we think an authenticator has been removed (or is being updated). |
| 918 | * But we still want to check if any data with the associated uid is |
| 919 | * around. This is an (imperfect) signal that the package may be updating. |
| 920 | * |
| 921 | * A side effect of this is that an authenticator sharing a uid with |
| 922 | * multiple apps won't get its credentials wiped as long as some app with |
| 923 | * that uid is still on the device. But I suspect that this is a rare case. |
| 924 | * And it isn't clear to me how an attacker could really exploit that |
| 925 | * feature. |
| 926 | * |
| 927 | * The upshot is that we don't have to worry about accounts getting |
| 928 | * uninstalled while the authenticator's package is being updated. |
| 929 | * |
| 930 | */ |
| 931 | if (knownUids == null) { |
| 932 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 933 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 934 | if (!knownUids.get(uid)) { |
| 935 | // The authenticator is not presently available to the cache. And the |
| 936 | // package no longer has a data directory (so we surmise it isn't updating). |
| 937 | // So purge its data from the account databases. |
| 938 | obsoleteAuthType.add(type); |
| 939 | // And delete it from the TABLE_META |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 940 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 941 | } |
| 942 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 945 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 946 | // been re-enabled (after being updated for example), then we just overwrite the old |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 947 | // values. |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 948 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 949 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 950 | } |
| 951 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 952 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 953 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 954 | accounts.accountCache.clear(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 955 | final HashMap<String, ArrayList<String>> accountNamesByType = new LinkedHashMap<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 956 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 957 | final long accountId = accountEntry.getKey(); |
| 958 | final Account account = accountEntry.getValue(); |
| 959 | if (obsoleteAuthType.contains(account.type)) { |
| 960 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 961 | + account.type + "'s registered authenticator no longer exist."); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 962 | accountsDb.beginTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 963 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 964 | accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 965 | // Also delete from CE table if user is unlocked; if user is currently |
| 966 | // locked the account will be removed later by syncDeCeAccountsLocked |
| 967 | if (userUnlocked) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 968 | accountsDb.deleteCeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 969 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 970 | accountsDb.setTransactionSuccessful(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 971 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 972 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 973 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 974 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 975 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 976 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 977 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 978 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 979 | accounts.userDataCache.remove(account); |
| 980 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 981 | accounts.accountTokenCaches.remove(account); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 982 | LinkedHashSet<String> interestedPackages = |
| 983 | getRequestingPackageNames(account.type, accounts); |
| 984 | for (String packageName : interestedPackages) { |
| 985 | sendNotification(packageName, null, accounts.userId); |
| 986 | } |
| 987 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 988 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 989 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 990 | if (accountNames == null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 991 | accountNames = new ArrayList<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 992 | accountNamesByType.put(account.type, accountNames); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 993 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 994 | accountNames.add(account.name); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 995 | } |
| 996 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 997 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 998 | final String accountType = cur.getKey(); |
| 999 | final ArrayList<String> accountNames = cur.getValue(); |
| 1000 | final Account[] accountsForType = new Account[accountNames.size()]; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1001 | for (int i = 0; i < accountsForType.length; i++) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1002 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1003 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1004 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1005 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1006 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1007 | } finally { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1008 | if (accountDeleted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1009 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1010 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1011 | } |
| 1012 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1015 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1016 | // Get the UIDs of all apps that might have data on the device. We want |
| 1017 | // to preserve user data if the app might otherwise be storing data. |
| 1018 | List<PackageInfo> pkgsWithData = |
| 1019 | mPackageManager.getInstalledPackagesAsUser( |
| 1020 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1021 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1022 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1023 | if (pkgInfo.applicationInfo != null |
| 1024 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1025 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1026 | } |
| 1027 | } |
| 1028 | return knownUids; |
| 1029 | } |
| 1030 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1031 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1032 | Context context, |
| 1033 | int userId) { |
| 1034 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1035 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1036 | } |
| 1037 | |
| 1038 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1039 | IAccountAuthenticatorCache authCache, |
| 1040 | int userId) { |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1041 | HashMap<String, Integer> knownAuth = new HashMap<>(); |
| 1042 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1043 | .getAllServices(userId)) { |
| 1044 | knownAuth.put(service.type.type, service.uid); |
| 1045 | } |
| 1046 | return knownAuth; |
| 1047 | } |
| 1048 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1049 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1050 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | protected UserAccounts getUserAccounts(int userId) { |
| 1054 | synchronized (mUsers) { |
| 1055 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1056 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1057 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1058 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1059 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1060 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1061 | initializeDebugDbSizeAndCompileSqlStatementForLogging(accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1062 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1063 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1064 | validateAccounts = true; |
| 1065 | } |
| 1066 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1067 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1068 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
| 1069 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1070 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1071 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1072 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1073 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1074 | } |
| 1075 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1076 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1077 | } |
| 1078 | return accounts; |
| 1079 | } |
| 1080 | } |
| 1081 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1082 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1083 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1084 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1085 | if (!accountsToRemove.isEmpty()) { |
| 1086 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1087 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1088 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1089 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1090 | |
| 1091 | for (Account account : accountsToRemove) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1092 | removeAccountInternal(accounts, account, Process.SYSTEM_UID); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1097 | private void purgeOldGrantsAll() { |
| 1098 | synchronized (mUsers) { |
| 1099 | for (int i = 0; i < mUsers.size(); i++) { |
| 1100 | purgeOldGrants(mUsers.valueAt(i)); |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | private void purgeOldGrants(UserAccounts accounts) { |
| 1106 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1107 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1108 | for (int uid : uids) { |
| 1109 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1110 | if (packageExists) { |
| 1111 | continue; |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1112 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1113 | Log.d(TAG, "deleting grants for UID " + uid |
| 1114 | + " because its package is no longer installed"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1115 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1120 | private void removeVisibilityValuesForPackage(String packageName) { |
| 1121 | synchronized (mUsers) { |
| 1122 | for (int i = 0; i < mUsers.size(); i++) { |
| 1123 | UserAccounts accounts = mUsers.valueAt(i); |
| 1124 | try { |
| 1125 | int uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 1126 | } catch (NameNotFoundException e) { |
| 1127 | // package does not exist - remove visibility values |
| 1128 | accounts.accountsDb.deleteAccountVisibilityForPackage(packageName); |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1134 | private void onUserRemoved(Intent intent) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1135 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1136 | if (userId < 1) return; |
| 1137 | |
| 1138 | UserAccounts accounts; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1139 | boolean userUnlocked; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1140 | synchronized (mUsers) { |
| 1141 | accounts = mUsers.get(userId); |
| 1142 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1143 | userUnlocked = mLocalUnlockedUsers.get(userId); |
| 1144 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1145 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1146 | if (accounts != null) { |
| 1147 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1148 | accounts.accountsDb.close(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1149 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1150 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1151 | Log.i(TAG, "Removing database files for user " + userId); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1152 | File dbFile = new File(mInjector.getDeDatabaseName(userId)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1153 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1154 | AccountsDb.deleteDbFileWarnIfFailed(dbFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1155 | // Remove CE file if user is unlocked, or FBE is not enabled |
| 1156 | boolean fbeEnabled = StorageManager.isFileEncryptedNativeOrEmulated(); |
| 1157 | if (!fbeEnabled || userUnlocked) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1158 | File ceDb = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1159 | if (ceDb.exists()) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1160 | AccountsDb.deleteDbFileWarnIfFailed(ceDb); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1165 | @VisibleForTesting |
| 1166 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1167 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1168 | } |
| 1169 | |
| 1170 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1171 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1172 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1173 | } |
| 1174 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1175 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1176 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1177 | if (userId < 1) return; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1178 | syncSharedAccounts(userId); |
| 1179 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1180 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1181 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1182 | // Check if there's a shared account that needs to be created as an account |
| 1183 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1184 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1185 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1186 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1187 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1188 | : UserHandle.USER_SYSTEM; |
| 1189 | if (parentUserId < 0) { |
| 1190 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1191 | return; |
| 1192 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1193 | for (Account sa : sharedAccounts) { |
| 1194 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1195 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1196 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1200 | @Override |
| 1201 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1202 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1205 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1206 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1207 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1208 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1209 | Log.v(TAG, "getPassword: " + account |
| 1210 | + ", caller's uid " + Binder.getCallingUid() |
| 1211 | + ", pid " + Binder.getCallingPid()); |
| 1212 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1213 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1214 | int userId = UserHandle.getCallingUserId(); |
| 1215 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1216 | String msg = String.format( |
| 1217 | "uid %s cannot get secrets for accounts of type: %s", |
| 1218 | callingUid, |
| 1219 | account.type); |
| 1220 | throw new SecurityException(msg); |
| 1221 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1222 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1223 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1224 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1225 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1226 | } finally { |
| 1227 | restoreCallingIdentity(identityToken); |
| 1228 | } |
| 1229 | } |
| 1230 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1231 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1232 | if (account == null) { |
| 1233 | return null; |
| 1234 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1235 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1236 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1237 | return null; |
| 1238 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1239 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1240 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1241 | return accounts.accountsDb.findAccountPasswordByNameAndType(account.name, account.type); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1245 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1246 | public String getPreviousName(Account account) { |
| 1247 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1248 | Log.v(TAG, "getPreviousName: " + account |
| 1249 | + ", caller's uid " + Binder.getCallingUid() |
| 1250 | + ", pid " + Binder.getCallingPid()); |
| 1251 | } |
| 1252 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1253 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1254 | long identityToken = clearCallingIdentity(); |
| 1255 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1256 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1257 | return readPreviousNameInternal(accounts, account); |
| 1258 | } finally { |
| 1259 | restoreCallingIdentity(identityToken); |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1264 | if (account == null) { |
| 1265 | return null; |
| 1266 | } |
| 1267 | synchronized (accounts.cacheLock) { |
| 1268 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1269 | if (previousNameRef == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1270 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1271 | previousNameRef = new AtomicReference<>(previousName); |
| 1272 | accounts.previousNameCache.put(account, previousNameRef); |
| 1273 | return previousName; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1274 | } else { |
| 1275 | return previousNameRef.get(); |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1281 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1282 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1283 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1284 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1285 | account, key, callingUid, Binder.getCallingPid()); |
| 1286 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1287 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1288 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1289 | if (key == null) throw new IllegalArgumentException("key is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1290 | int userId = UserHandle.getCallingUserId(); |
| 1291 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1292 | String msg = String.format( |
| 1293 | "uid %s cannot get user data for accounts of type: %s", |
| 1294 | callingUid, |
| 1295 | account.type); |
| 1296 | throw new SecurityException(msg); |
| 1297 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1298 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1299 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1300 | return null; |
| 1301 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1302 | long identityToken = clearCallingIdentity(); |
| 1303 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1304 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1305 | synchronized (accounts.cacheLock) { |
| 1306 | if (!accountExistsCacheLocked(accounts, account)) { |
| 1307 | return null; |
| 1308 | } |
| 1309 | return readUserDataInternalLocked(accounts, account, key); |
| 1310 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1311 | } finally { |
| 1312 | restoreCallingIdentity(identityToken); |
| 1313 | } |
| 1314 | } |
| 1315 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1316 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1317 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1318 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1319 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1320 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1321 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1322 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1323 | + ", pid " + Binder.getCallingPid()); |
| 1324 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1325 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1326 | if (isCrossUser(callingUid, userId)) { |
| 1327 | throw new SecurityException( |
| 1328 | String.format( |
| 1329 | "User %s tying to get authenticator types for %s" , |
| 1330 | UserHandle.getCallingUserId(), |
| 1331 | userId)); |
| 1332 | } |
| 1333 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1334 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1335 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1336 | return getAuthenticatorTypesInternal(userId); |
| 1337 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1338 | } finally { |
| 1339 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1340 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1343 | /** |
| 1344 | * Should only be called inside of a clearCallingIdentity block. |
| 1345 | */ |
| 1346 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1347 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1348 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1349 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1350 | AuthenticatorDescription[] types = |
| 1351 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1352 | int i = 0; |
| 1353 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1354 | : authenticatorCollection) { |
| 1355 | types[i] = authenticator.type; |
| 1356 | i++; |
| 1357 | } |
| 1358 | return types; |
| 1359 | } |
| 1360 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1361 | private boolean isCrossUser(int callingUid, int userId) { |
| 1362 | return (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1363 | && callingUid != Process.SYSTEM_UID |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1364 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1365 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1366 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1369 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1370 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1371 | return addAccountExplicitlyWithVisibility(account, password, extras, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1374 | @Override |
| 1375 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1376 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1377 | int callingUid = Binder.getCallingUid(); |
| 1378 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1379 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1380 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1381 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1382 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1383 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1384 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1385 | if (response != null) { |
| 1386 | Bundle result = new Bundle(); |
| 1387 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1388 | try { |
| 1389 | response.onResult(result); |
| 1390 | } catch (RemoteException e) { |
| 1391 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1392 | } |
| 1393 | } |
| 1394 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1395 | } |
| 1396 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1397 | Slog.d(TAG, "Copying account " + account.name |
| 1398 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1399 | long identityToken = clearCallingIdentity(); |
| 1400 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1401 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1402 | false /* stripAuthTokenFromResult */, account.name, |
| 1403 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1404 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1405 | protected String toDebugString(long now) { |
| 1406 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1407 | + ", " + account.type; |
| 1408 | } |
| 1409 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1410 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1411 | public void run() throws RemoteException { |
| 1412 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1413 | } |
| 1414 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1415 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1416 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1417 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1418 | if (result != null |
| 1419 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1420 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1421 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1422 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1423 | super.onResult(result); |
| 1424 | } |
| 1425 | } |
| 1426 | }.bind(); |
| 1427 | } finally { |
| 1428 | restoreCallingIdentity(identityToken); |
| 1429 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1430 | } |
| 1431 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1432 | @Override |
| 1433 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1434 | final int callingUid = Binder.getCallingUid(); |
| 1435 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1436 | String msg = String.format( |
| 1437 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1438 | account, |
| 1439 | callingUid); |
| 1440 | Log.v(TAG, msg); |
| 1441 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1442 | if (account == null) { |
| 1443 | throw new IllegalArgumentException("account is null"); |
| 1444 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1445 | int userId = UserHandle.getCallingUserId(); |
| 1446 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1447 | String msg = String.format( |
| 1448 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1449 | callingUid, |
| 1450 | account.type); |
| 1451 | throw new SecurityException(msg); |
| 1452 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1453 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1454 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1455 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1456 | return false; |
| 1457 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1458 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1459 | long identityToken = clearCallingIdentity(); |
| 1460 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1461 | UserAccounts accounts = getUserAccounts(userId); |
| 1462 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1463 | } finally { |
| 1464 | restoreCallingIdentity(identityToken); |
| 1465 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1469 | final UserAccounts accounts = getUserAccountsForCaller(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1470 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1471 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1472 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1475 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1476 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1477 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1478 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1479 | long id = clearCallingIdentity(); |
| 1480 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1481 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1482 | false /* stripAuthTokenFromResult */, account.name, |
| 1483 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1484 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1485 | protected String toDebugString(long now) { |
| 1486 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1487 | + ", " + account.type; |
| 1488 | } |
| 1489 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1490 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1491 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1492 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1493 | UserAccounts owner = getUserAccounts(parentUserId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1494 | synchronized (owner.cacheLock) { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1495 | for (Account acc : getAccounts(parentUserId, |
| 1496 | mContext.getOpPackageName())) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1497 | if (acc.equals(account)) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1498 | mAuthenticator.addAccountFromCredentials( |
| 1499 | this, account, accountCredentials); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1500 | break; |
| 1501 | } |
| 1502 | } |
| 1503 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1504 | } |
| 1505 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1506 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1507 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1508 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1509 | // TODO: Anything to do if if succedded? |
| 1510 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1511 | // account to avoid retries? |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1512 | // TODO: what we do with the visibility? |
| 1513 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1514 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1517 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1518 | public void onError(int errorCode, String errorMessage) { |
| 1519 | super.onError(errorCode, errorMessage); |
| 1520 | // TODO: Show error notification to user |
| 1521 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1522 | } |
| 1523 | |
| 1524 | }.bind(); |
| 1525 | } finally { |
| 1526 | restoreCallingIdentity(id); |
| 1527 | } |
| 1528 | } |
| 1529 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1530 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1531 | Bundle extras, int callingUid, Map<String, Integer> packageToVisibility) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1532 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1533 | if (account == null) { |
| 1534 | return false; |
| 1535 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1536 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1537 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1538 | + " is locked. callingUid=" + callingUid); |
| 1539 | return false; |
| 1540 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1541 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1542 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1543 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1544 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1545 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1546 | + ", skipping since the account already exists"); |
| 1547 | return false; |
| 1548 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1549 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1550 | if (accountId < 0) { |
| 1551 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1552 | + ", skipping the DB insert failed"); |
| 1553 | return false; |
| 1554 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1555 | // Insert into DE table |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1556 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1557 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1558 | + ", skipping the DB insert failed"); |
| 1559 | return false; |
| 1560 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1561 | if (extras != null) { |
| 1562 | for (String key : extras.keySet()) { |
| 1563 | final String value = extras.getString(key); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1564 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1565 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1566 | + ", skipping since insertExtra failed for key " + key); |
| 1567 | return false; |
| 1568 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1569 | } |
| 1570 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1571 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1572 | if (packageToVisibility != null) { |
| 1573 | for (Entry<String, Integer> entry : packageToVisibility.entrySet()) { |
| 1574 | setAccountVisibility(account, entry.getKey() /* package */, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1575 | entry.getValue() /* visibility */, false /* notify */, accounts); |
| 1576 | } |
| 1577 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1578 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1579 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1580 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, accountId, |
| 1581 | accounts, callingUid); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1582 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1583 | insertAccountIntoCacheLocked(accounts, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1584 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1585 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1586 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1587 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1588 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1589 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1590 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1591 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1592 | sendNotification(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1593 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1594 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1595 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1596 | return true; |
| 1597 | } |
| 1598 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1599 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1600 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1601 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1602 | } |
| 1603 | } |
| 1604 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1605 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1606 | * Adds the account to all linked restricted users as shared accounts. If the user is currently |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1607 | * running, then clone the account too. |
| 1608 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1609 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1610 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1611 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1612 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1613 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1614 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1615 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1616 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1617 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1618 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1624 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1625 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1626 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1627 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1628 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1629 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1630 | Log.v(TAG, "hasFeatures: " + account |
| 1631 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1632 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1633 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1634 | + ", pid " + Binder.getCallingPid()); |
| 1635 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1636 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1637 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1638 | if (features == null) throw new IllegalArgumentException("features is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1639 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1640 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1641 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1642 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1643 | long identityToken = clearCallingIdentity(); |
| 1644 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1645 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1646 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1647 | } finally { |
| 1648 | restoreCallingIdentity(identityToken); |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | private class TestFeaturesSession extends Session { |
| 1653 | private final String[] mFeatures; |
| 1654 | private final Account mAccount; |
| 1655 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1656 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1657 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1658 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1659 | true /* stripAuthTokenFromResult */, account.name, |
| 1660 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1661 | mFeatures = features; |
| 1662 | mAccount = account; |
| 1663 | } |
| 1664 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1665 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1666 | public void run() throws RemoteException { |
| 1667 | try { |
| 1668 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1669 | } catch (RemoteException e) { |
| 1670 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1671 | } |
| 1672 | } |
| 1673 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1674 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1675 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1676 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1677 | IAccountManagerResponse response = getResponseAndClose(); |
| 1678 | if (response != null) { |
| 1679 | try { |
| 1680 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1681 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1682 | return; |
| 1683 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1684 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1685 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1686 | + response); |
| 1687 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1688 | final Bundle newResult = new Bundle(); |
| 1689 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1690 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1691 | response.onResult(newResult); |
| 1692 | } catch (RemoteException e) { |
| 1693 | // if the caller is dead then there is no one to care about remote exceptions |
| 1694 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1695 | Log.v(TAG, "failure while notifying response", e); |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1701 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1702 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1703 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1704 | + ", " + mAccount |
| 1705 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1706 | } |
| 1707 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1708 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1709 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1710 | public void renameAccount( |
| 1711 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1712 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1713 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1714 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1715 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1716 | + ", pid " + Binder.getCallingPid()); |
| 1717 | } |
| 1718 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1719 | int userId = UserHandle.getCallingUserId(); |
| 1720 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1721 | String msg = String.format( |
| 1722 | "uid %s cannot rename accounts of type: %s", |
| 1723 | callingUid, |
| 1724 | accountToRename.type); |
| 1725 | throw new SecurityException(msg); |
| 1726 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1727 | long identityToken = clearCallingIdentity(); |
| 1728 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1729 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1730 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1731 | Bundle result = new Bundle(); |
| 1732 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1733 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1734 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1735 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1736 | try { |
| 1737 | response.onResult(result); |
| 1738 | } catch (RemoteException e) { |
| 1739 | Log.w(TAG, e.getMessage()); |
| 1740 | } |
| 1741 | } finally { |
| 1742 | restoreCallingIdentity(identityToken); |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1747 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1748 | Account resultAccount = null; |
| 1749 | /* |
| 1750 | * Cancel existing notifications. Let authenticators |
| 1751 | * re-post notifications as required. But we don't know if |
| 1752 | * the authenticators have bound their notifications to |
| 1753 | * now stale account name data. |
| 1754 | * |
| 1755 | * With a rename api, we might not need to do this anymore but it |
| 1756 | * shouldn't hurt. |
| 1757 | */ |
| 1758 | cancelNotification( |
| 1759 | getSigninRequiredNotificationId(accounts, accountToRename), |
| 1760 | new UserHandle(accounts.userId)); |
| 1761 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1762 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1763 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1764 | if (accountToRename.equals(pair.first.first)) { |
| 1765 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
| 1766 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1767 | } |
| 1768 | } |
| 1769 | } |
| 1770 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1771 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1772 | Account renamedAccount = new Account(newName, accountToRename.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1773 | if ((accounts.accountsDb.findCeAccountId(renamedAccount) >= 0)) { |
| 1774 | Log.e(TAG, "renameAccount failed - account with new name already exists"); |
| 1775 | return null; |
| 1776 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1777 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1778 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1779 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1780 | accounts.accountsDb.renameCeAccount(accountId, newName); |
Dmitry Dementyev | cf50dcf | 2016-11-17 14:14:11 -0800 | [diff] [blame] | 1781 | if (accounts.accountsDb.renameDeAccount( |
| 1782 | accountId, newName, accountToRename.name)) { |
| 1783 | accounts.accountsDb.setTransactionSuccessful(); |
| 1784 | } else { |
| 1785 | Log.e(TAG, "renameAccount failed"); |
| 1786 | return null; |
| 1787 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1788 | } else { |
| 1789 | Log.e(TAG, "renameAccount failed - old account does not exist"); |
| 1790 | return null; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1791 | } |
| 1792 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1793 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1794 | } |
| 1795 | /* |
| 1796 | * Database transaction was successful. Clean up cached |
| 1797 | * data associated with the account in the user profile. |
| 1798 | */ |
Svet Ganov | 5c4a512 | 2016-09-23 19:29:11 -0700 | [diff] [blame] | 1799 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1800 | /* |
| 1801 | * Extract the data and token caches before removing the |
| 1802 | * old account to preserve the user data associated with |
| 1803 | * the account. |
| 1804 | */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1805 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1806 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1807 | removeAccountFromCacheLocked(accounts, accountToRename); |
| 1808 | /* |
| 1809 | * Update the cached data associated with the renamed |
| 1810 | * account. |
| 1811 | */ |
| 1812 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1813 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 1814 | accounts.previousNameCache.put( |
| 1815 | renamedAccount, |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1816 | new AtomicReference<>(accountToRename.name)); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1817 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1818 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1819 | int parentUserId = accounts.userId; |
| 1820 | if (canHaveProfile(parentUserId)) { |
| 1821 | /* |
| 1822 | * Owner or system user account was renamed, rename the account for |
| 1823 | * those users with which the account was shared. |
| 1824 | */ |
| 1825 | List<UserInfo> users = getUserManager().getUsers(true); |
| 1826 | for (UserInfo user : users) { |
| 1827 | if (user.isRestricted() |
| 1828 | && (user.restrictedProfileParentId == parentUserId)) { |
| 1829 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1830 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1831 | } |
| 1832 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1833 | |
| 1834 | // Notify authenticator. |
| 1835 | sendNotification(resultAccount, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1836 | sendAccountsChangedBroadcast(accounts.userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1837 | } |
| 1838 | return resultAccount; |
| 1839 | } |
| 1840 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1841 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1842 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1843 | return userInfo != null && userInfo.canHaveProfile(); |
| 1844 | } |
| 1845 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1846 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1847 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1848 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1849 | removeAccountAsUser( |
| 1850 | response, |
| 1851 | account, |
| 1852 | expectActivityLaunch, |
| 1853 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | @Override |
| 1857 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1858 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1859 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1860 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1861 | Log.v(TAG, "removeAccount: " + account |
| 1862 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1863 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1864 | + ", pid " + Binder.getCallingPid() |
| 1865 | + ", for user id " + userId); |
| 1866 | } |
| 1867 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1868 | if (account == null) throw new IllegalArgumentException("account is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1869 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1870 | if (isCrossUser(callingUid, userId)) { |
| 1871 | throw new SecurityException( |
| 1872 | String.format( |
| 1873 | "User %s tying remove account for %s" , |
| 1874 | UserHandle.getCallingUserId(), |
| 1875 | userId)); |
| 1876 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1877 | /* |
| 1878 | * Only the system or authenticator should be allowed to remove accounts for that |
| 1879 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 1880 | * arbitrary applications (like competing authenticators). |
| 1881 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1882 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 1883 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 1884 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1885 | String msg = String.format( |
| 1886 | "uid %s cannot remove accounts of type: %s", |
| 1887 | callingUid, |
| 1888 | account.type); |
| 1889 | throw new SecurityException(msg); |
| 1890 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1891 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1892 | try { |
| 1893 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 1894 | "User cannot modify accounts"); |
| 1895 | } catch (RemoteException re) { |
| 1896 | } |
| 1897 | return; |
| 1898 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1899 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1900 | try { |
| 1901 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 1902 | "User cannot modify accounts of this type (policy)."); |
| 1903 | } catch (RemoteException re) { |
| 1904 | } |
| 1905 | return; |
| 1906 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1907 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1908 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1909 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1910 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1911 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1912 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1913 | if (account.equals(pair.first.first)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1914 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 1915 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1919 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 1920 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1921 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 1922 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 1923 | accountId, |
| 1924 | accounts, |
| 1925 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1926 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1927 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 1928 | } finally { |
| 1929 | restoreCallingIdentity(identityToken); |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | @Override |
| 1934 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1935 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1936 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1937 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1938 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1939 | + ", pid " + Binder.getCallingPid()); |
| 1940 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1941 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1942 | if (account == null) { |
| 1943 | /* |
| 1944 | * Null accounts should result in returning false, as per |
| 1945 | * AccountManage.addAccountExplicitly(...) java doc. |
| 1946 | */ |
| 1947 | Log.e(TAG, "account is null"); |
| 1948 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1949 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1950 | String msg = String.format( |
| 1951 | "uid %s cannot explicitly add accounts of type: %s", |
| 1952 | callingUid, |
| 1953 | account.type); |
| 1954 | throw new SecurityException(msg); |
| 1955 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1956 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1957 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 1958 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1959 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 1960 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 1961 | accountId, |
| 1962 | accounts, |
| 1963 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1964 | long identityToken = clearCallingIdentity(); |
| 1965 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1966 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1967 | } finally { |
| 1968 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1969 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1972 | private class RemoveAccountSession extends Session { |
| 1973 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1974 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1975 | Account account, boolean expectActivityLaunch) { |
| 1976 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1977 | true /* stripAuthTokenFromResult */, account.name, |
| 1978 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1979 | mAccount = account; |
| 1980 | } |
| 1981 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1982 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1983 | protected String toDebugString(long now) { |
| 1984 | return super.toDebugString(now) + ", removeAccount" |
| 1985 | + ", account " + mAccount; |
| 1986 | } |
| 1987 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1988 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1989 | public void run() throws RemoteException { |
| 1990 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 1991 | } |
| 1992 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1993 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1994 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1995 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1996 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 1997 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 1998 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1999 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2000 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2001 | } |
| 2002 | IAccountManagerResponse response = getResponseAndClose(); |
| 2003 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2004 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2005 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2006 | + response); |
| 2007 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2008 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2009 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2010 | try { |
| 2011 | response.onResult(result2); |
| 2012 | } catch (RemoteException e) { |
| 2013 | // ignore |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | super.onResult(result); |
| 2018 | } |
| 2019 | } |
| 2020 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2021 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2022 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2023 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2026 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2027 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2028 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2029 | if (!userUnlocked) { |
| 2030 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2031 | + " is still locked. CE data will be removed later"); |
| 2032 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2033 | synchronized (accounts.cacheLock) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2034 | LinkedHashSet<String> interestedPackages = |
| 2035 | accounts.mApplicationAccountRequestMappings.get(account.type); |
| 2036 | if (interestedPackages == null) { |
| 2037 | interestedPackages = new LinkedHashSet<>(); |
| 2038 | } |
| 2039 | int[] visibilityForInterestedPackages = new int[interestedPackages.size()]; |
| 2040 | int index = 0; |
| 2041 | for (String packageName : interestedPackages) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 2042 | int visibility = resolveAccountVisibility(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2043 | visibilityForInterestedPackages[index++] = visibility; |
| 2044 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2045 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2046 | // Set to a dummy value, this will only be used if the database |
| 2047 | // transaction succeeds. |
| 2048 | long accountId = -1; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2049 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2050 | accountId = accounts.accountsDb.findDeAccountId(account); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2051 | if (accountId >= 0) { |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2052 | isChanged = accounts.accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2053 | } |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2054 | // always delete from CE table if CE storage is available |
| 2055 | // DE account could be removed while CE was locked |
| 2056 | if (userUnlocked) { |
| 2057 | long ceAccountId = accounts.accountsDb.findCeAccountId(account); |
| 2058 | if (ceAccountId >= 0) { |
| 2059 | accounts.accountsDb.deleteCeAccount(ceAccountId); |
| 2060 | } |
| 2061 | } |
| 2062 | accounts.accountsDb.setTransactionSuccessful(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2063 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2064 | accounts.accountsDb.endTransaction(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2065 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2066 | if (isChanged) { |
| 2067 | removeAccountFromCacheLocked(accounts, account); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2068 | index = 0; |
| 2069 | for (String packageName : interestedPackages) { |
| 2070 | if ((visibilityForInterestedPackages[index] |
| 2071 | != AccountManager.VISIBILITY_NOT_VISIBLE) |
| 2072 | && (visibilityForInterestedPackages[index] |
| 2073 | != AccountManager.VISIBILITY_UNDEFINED)) { |
| 2074 | sendNotification(packageName, account, accounts.userId); |
| 2075 | } |
| 2076 | ++index; |
| 2077 | } |
| 2078 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2079 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occured. |
| 2080 | sendAccountsChangedBroadcast(accounts.userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2081 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2082 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2083 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2084 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2085 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2086 | long id = Binder.clearCallingIdentity(); |
| 2087 | try { |
| 2088 | int parentUserId = accounts.userId; |
| 2089 | if (canHaveProfile(parentUserId)) { |
| 2090 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2091 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2092 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2093 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2094 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2095 | } |
| 2096 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2097 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2098 | } finally { |
| 2099 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2100 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2101 | |
| 2102 | if (isChanged) { |
| 2103 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2104 | for (Pair<Pair<Account, String>, Integer> key |
| 2105 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2106 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2107 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2108 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2109 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2110 | account, uid, false)); |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | } |
| 2115 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2116 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2119 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2120 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2121 | int callerUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2122 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2123 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2124 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2125 | + ", pid " + Binder.getCallingPid()); |
| 2126 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2127 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 2128 | if (authToken == null) throw new IllegalArgumentException("authToken is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2129 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2130 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2131 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2132 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2133 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2134 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2135 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2136 | invalidateAuthTokenLocked(accounts, accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2137 | invalidateCustomTokenLocked(accounts, accountType, authToken); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2138 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2139 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2140 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2141 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2142 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2143 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2144 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2145 | } |
| 2146 | } |
| 2147 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2148 | private void invalidateCustomTokenLocked( |
| 2149 | UserAccounts accounts, |
| 2150 | String accountType, |
| 2151 | String authToken) { |
| 2152 | if (authToken == null || accountType == null) { |
| 2153 | return; |
| 2154 | } |
| 2155 | // Also wipe out cached token in memory. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2156 | accounts.accountTokenCaches.remove(accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2159 | private void invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
| 2160 | String authToken) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2161 | if (authToken == null || accountType == null) { |
| 2162 | return; |
| 2163 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2164 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2165 | try { |
| 2166 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2167 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2168 | String accountName = cursor.getString(1); |
| 2169 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2170 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2171 | writeAuthTokenIntoCacheLocked( |
| 2172 | accounts, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2173 | new Account(accountName, accountType), |
| 2174 | authTokenType, |
| 2175 | null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2176 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2177 | } finally { |
| 2178 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2179 | } |
| 2180 | } |
| 2181 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2182 | private void saveCachedToken( |
| 2183 | UserAccounts accounts, |
| 2184 | Account account, |
| 2185 | String callerPkg, |
| 2186 | byte[] callerSigDigest, |
| 2187 | String tokenType, |
| 2188 | String token, |
| 2189 | long expiryMillis) { |
| 2190 | |
| 2191 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2192 | return; |
| 2193 | } |
| 2194 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2195 | UserHandle.of(accounts.userId)); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2196 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2197 | accounts.accountTokenCaches.put( |
| 2198 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2202 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2203 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2204 | if (account == null || type == null) { |
| 2205 | return false; |
| 2206 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2207 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2208 | UserHandle.of(accounts.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2209 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2210 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2211 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2212 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2213 | if (accountId < 0) { |
| 2214 | return false; |
| 2215 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2216 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2217 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2218 | accounts.accountsDb.setTransactionSuccessful(); |
| 2219 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2220 | return true; |
| 2221 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2222 | return false; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2223 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2224 | accounts.accountsDb.endTransaction(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2225 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2229 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2230 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2231 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2232 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2233 | Log.v(TAG, "peekAuthToken: " + account |
| 2234 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2235 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2236 | + ", pid " + Binder.getCallingPid()); |
| 2237 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2238 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2239 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2240 | int userId = UserHandle.getCallingUserId(); |
| 2241 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2242 | String msg = String.format( |
| 2243 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2244 | callingUid, |
| 2245 | account.type); |
| 2246 | throw new SecurityException(msg); |
| 2247 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2248 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2249 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2250 | + callingUid); |
| 2251 | return null; |
| 2252 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2253 | long identityToken = clearCallingIdentity(); |
| 2254 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2255 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2256 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2257 | } finally { |
| 2258 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2259 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2262 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2263 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2264 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2265 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2266 | Log.v(TAG, "setAuthToken: " + account |
| 2267 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2268 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2269 | + ", pid " + Binder.getCallingPid()); |
| 2270 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2271 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2272 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2273 | int userId = UserHandle.getCallingUserId(); |
| 2274 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2275 | String msg = String.format( |
| 2276 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2277 | callingUid, |
| 2278 | account.type); |
| 2279 | throw new SecurityException(msg); |
| 2280 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2281 | long identityToken = clearCallingIdentity(); |
| 2282 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2283 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2284 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2285 | } finally { |
| 2286 | restoreCallingIdentity(identityToken); |
| 2287 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2290 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2291 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2292 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2293 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2294 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2295 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2296 | + ", pid " + Binder.getCallingPid()); |
| 2297 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2298 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2299 | int userId = UserHandle.getCallingUserId(); |
| 2300 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2301 | String msg = String.format( |
| 2302 | "uid %s cannot set secrets for accounts of type: %s", |
| 2303 | callingUid, |
| 2304 | account.type); |
| 2305 | throw new SecurityException(msg); |
| 2306 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2307 | long identityToken = clearCallingIdentity(); |
| 2308 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2309 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2310 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2311 | } finally { |
| 2312 | restoreCallingIdentity(identityToken); |
| 2313 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2314 | } |
| 2315 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2316 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2317 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2318 | if (account == null) { |
| 2319 | return; |
| 2320 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2321 | boolean isChanged = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2322 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2323 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2324 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2325 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2326 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2327 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2328 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2329 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2330 | accounts.accountTokenCaches.remove(account); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2331 | accounts.accountsDb.setTransactionSuccessful(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2332 | // If there is an account whose password will be updated and the database |
| 2333 | // transactions succeed, then we say that a change has occured. Even if the |
| 2334 | // new password is the same as the old and there were no authtokens to delete. |
| 2335 | isChanged = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2336 | String action = (password == null || password.length() == 0) ? |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2337 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2338 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2339 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, callingUid); |
Costin Manolache | f5ffe89 | 2011-01-19 09:35:32 -0800 | [diff] [blame] | 2340 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2341 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2342 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2343 | if (isChanged) { |
| 2344 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2345 | sendNotification(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2346 | sendAccountsChangedBroadcast(accounts.userId); |
| 2347 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2348 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2349 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2352 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2353 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2354 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2355 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2356 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2357 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2358 | + ", pid " + Binder.getCallingPid()); |
| 2359 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2360 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2361 | int userId = UserHandle.getCallingUserId(); |
| 2362 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2363 | String msg = String.format( |
| 2364 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2365 | callingUid, |
| 2366 | account.type); |
| 2367 | throw new SecurityException(msg); |
| 2368 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2369 | long identityToken = clearCallingIdentity(); |
| 2370 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2371 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2372 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2373 | } finally { |
| 2374 | restoreCallingIdentity(identityToken); |
| 2375 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2376 | } |
| 2377 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2378 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2379 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2380 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2381 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2382 | Log.v(TAG, "setUserData: " + account |
| 2383 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2384 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2385 | + ", pid " + Binder.getCallingPid()); |
| 2386 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2387 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2388 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2389 | int userId = UserHandle.getCallingUserId(); |
| 2390 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2391 | String msg = String.format( |
| 2392 | "uid %s cannot set user data for accounts of type: %s", |
| 2393 | callingUid, |
| 2394 | account.type); |
| 2395 | throw new SecurityException(msg); |
| 2396 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2397 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2398 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2399 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2400 | synchronized (accounts.cacheLock) { |
| 2401 | if (!accountExistsCacheLocked(accounts, account)) { |
| 2402 | return; |
| 2403 | } |
| 2404 | setUserdataInternalLocked(accounts, account, key, value); |
| 2405 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2406 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2407 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2408 | } |
| 2409 | } |
| 2410 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2411 | private boolean accountExistsCacheLocked(UserAccounts accounts, Account account) { |
| 2412 | if (accounts.accountCache.containsKey(account.type)) { |
| 2413 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2414 | if (acc.name.equals(account.name)) { |
| 2415 | return true; |
| 2416 | } |
| 2417 | } |
| 2418 | } |
| 2419 | return false; |
| 2420 | } |
| 2421 | |
| 2422 | private void setUserdataInternalLocked(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2423 | String value) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2424 | if (account == null || key == null) { |
| 2425 | return; |
| 2426 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2427 | accounts.accountsDb.beginTransaction(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2428 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2429 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2430 | if (accountId < 0) { |
| 2431 | return; |
| 2432 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2433 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2434 | if (extrasId < 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2435 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2436 | if (extrasId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2437 | return; |
| 2438 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2439 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2440 | return; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2441 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2442 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2443 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2444 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2445 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2446 | } |
| 2447 | } |
| 2448 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2449 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2450 | if (result == null) { |
| 2451 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2452 | } |
| 2453 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2454 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2455 | + response); |
| 2456 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2457 | try { |
| 2458 | response.onResult(result); |
| 2459 | } catch (RemoteException e) { |
| 2460 | // if the caller is dead then there is no one to care about remote |
| 2461 | // exceptions |
| 2462 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2463 | Log.v(TAG, "failure while notifying response", e); |
| 2464 | } |
| 2465 | } |
| 2466 | } |
| 2467 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2468 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2469 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2470 | final String authTokenType) |
| 2471 | throws RemoteException { |
| 2472 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2473 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 2474 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2475 | final int callingUid = getCallingUid(); |
| 2476 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2477 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2478 | throw new SecurityException("can only call from system"); |
| 2479 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2480 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2481 | long identityToken = clearCallingIdentity(); |
| 2482 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2483 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2484 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2485 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2486 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2487 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2488 | protected String toDebugString(long now) { |
| 2489 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2490 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2491 | + ", authTokenType " + authTokenType; |
| 2492 | } |
| 2493 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2494 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2495 | public void run() throws RemoteException { |
| 2496 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2497 | } |
| 2498 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2499 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2500 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2501 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2502 | if (result != null) { |
| 2503 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2504 | Bundle bundle = new Bundle(); |
| 2505 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2506 | super.onResult(bundle); |
| 2507 | return; |
| 2508 | } else { |
| 2509 | super.onResult(result); |
| 2510 | } |
| 2511 | } |
| 2512 | }.bind(); |
| 2513 | } finally { |
| 2514 | restoreCallingIdentity(identityToken); |
| 2515 | } |
| 2516 | } |
| 2517 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2518 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2519 | public void getAuthToken( |
| 2520 | IAccountManagerResponse response, |
| 2521 | final Account account, |
| 2522 | final String authTokenType, |
| 2523 | final boolean notifyOnAuthFailure, |
| 2524 | final boolean expectActivityLaunch, |
| 2525 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2526 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2527 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2528 | Log.v(TAG, "getAuthToken: " + account |
| 2529 | + ", response " + response |
| 2530 | + ", authTokenType " + authTokenType |
| 2531 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2532 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2533 | + ", caller's uid " + Binder.getCallingUid() |
| 2534 | + ", pid " + Binder.getCallingPid()); |
| 2535 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2536 | if (response == null) throw new IllegalArgumentException("response is null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2537 | try { |
| 2538 | if (account == null) { |
| 2539 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2540 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2541 | return; |
| 2542 | } |
| 2543 | if (authTokenType == null) { |
| 2544 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2545 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2546 | return; |
| 2547 | } |
| 2548 | } catch (RemoteException e) { |
| 2549 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2550 | return; |
| 2551 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2552 | int userId = UserHandle.getCallingUserId(); |
| 2553 | long ident = Binder.clearCallingIdentity(); |
| 2554 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2555 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2556 | try { |
| 2557 | accounts = getUserAccounts(userId); |
| 2558 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2559 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2560 | } finally { |
| 2561 | Binder.restoreCallingIdentity(ident); |
| 2562 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2563 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2564 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2565 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2566 | |
| 2567 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2568 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2569 | final boolean permissionGranted = |
| 2570 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2571 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2572 | // Get the calling package. We will use it for the purpose of caching. |
| 2573 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2574 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2575 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2576 | try { |
| 2577 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2578 | } finally { |
| 2579 | Binder.restoreCallingIdentity(ident); |
| 2580 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2581 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2582 | String msg = String.format( |
| 2583 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2584 | callerUid, |
| 2585 | callerPkg); |
| 2586 | throw new SecurityException(msg); |
| 2587 | } |
| 2588 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2589 | // let authenticator know the identity of the caller |
| 2590 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2591 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2592 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2593 | if (notifyOnAuthFailure) { |
| 2594 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2595 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2596 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2597 | long identityToken = clearCallingIdentity(); |
| 2598 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2599 | // Distill the caller's package signatures into a single digest. |
| 2600 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2601 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2602 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2603 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2604 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2605 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2606 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2607 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2608 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2609 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2610 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2611 | onResult(response, result); |
| 2612 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2613 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2614 | } |
| 2615 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2616 | if (customTokens) { |
| 2617 | /* |
| 2618 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2619 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2620 | * ANDORID_PACKAGE_NAME. |
| 2621 | */ |
| 2622 | String token = readCachedTokenInternal( |
| 2623 | accounts, |
| 2624 | account, |
| 2625 | authTokenType, |
| 2626 | callerPkg, |
| 2627 | callerPkgSigDigest); |
| 2628 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2629 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2630 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2631 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2632 | Bundle result = new Bundle(); |
| 2633 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2634 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2635 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2636 | onResult(response, result); |
| 2637 | return; |
| 2638 | } |
| 2639 | } |
| 2640 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2641 | new Session( |
| 2642 | accounts, |
| 2643 | response, |
| 2644 | account.type, |
| 2645 | expectActivityLaunch, |
| 2646 | false /* stripAuthTokenFromResult */, |
| 2647 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2648 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2649 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2650 | protected String toDebugString(long now) { |
| 2651 | if (loginOptions != null) loginOptions.keySet(); |
| 2652 | return super.toDebugString(now) + ", getAuthToken" |
| 2653 | + ", " + account |
| 2654 | + ", authTokenType " + authTokenType |
| 2655 | + ", loginOptions " + loginOptions |
| 2656 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2657 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2658 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2659 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2660 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2661 | // If the caller doesn't have permission then create and return the |
| 2662 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2663 | if (!permissionGranted) { |
| 2664 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2665 | } else { |
| 2666 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2667 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2670 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2671 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2672 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2673 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2674 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2675 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2676 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2677 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2678 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2679 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2680 | authTokenType, |
| 2681 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2682 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2683 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2684 | onResult(bundle); |
| 2685 | return; |
| 2686 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2687 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2688 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2689 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2690 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2691 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2692 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2693 | "the type and name should not be empty"); |
| 2694 | return; |
| 2695 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2696 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2697 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2698 | saveAuthTokenToDatabase( |
| 2699 | mAccounts, |
| 2700 | resultAccount, |
| 2701 | authTokenType, |
| 2702 | authToken); |
| 2703 | } |
| 2704 | long expiryMillis = result.getLong( |
| 2705 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2706 | if (customTokens |
| 2707 | && expiryMillis > System.currentTimeMillis()) { |
| 2708 | saveCachedToken( |
| 2709 | mAccounts, |
| 2710 | account, |
| 2711 | callerPkg, |
| 2712 | callerPkgSigDigest, |
| 2713 | authTokenType, |
| 2714 | authToken, |
| 2715 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2716 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2717 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2718 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2719 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2720 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2721 | /* |
| 2722 | * Make sure that the supplied intent is owned by the authenticator |
| 2723 | * giving it to the system. Otherwise a malicious authenticator could |
| 2724 | * have users launching arbitrary activities by tricking users to |
| 2725 | * interact with malicious notifications. |
| 2726 | */ |
| 2727 | checkKeyIntent( |
| 2728 | Binder.getCallingUid(), |
| 2729 | intent); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2730 | doNotification( |
| 2731 | mAccounts, |
| 2732 | account, |
| 2733 | result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2734 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2735 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2736 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2737 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2738 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2739 | }.bind(); |
| 2740 | } finally { |
| 2741 | restoreCallingIdentity(identityToken); |
| 2742 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2745 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2746 | MessageDigest digester; |
| 2747 | try { |
| 2748 | digester = MessageDigest.getInstance("SHA-256"); |
| 2749 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2750 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2751 | for (Signature sig : pkgInfo.signatures) { |
| 2752 | digester.update(sig.toByteArray()); |
| 2753 | } |
| 2754 | } catch (NoSuchAlgorithmException x) { |
| 2755 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2756 | digester = null; |
| 2757 | } catch (NameNotFoundException e) { |
| 2758 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2759 | digester = null; |
| 2760 | } |
| 2761 | return (digester == null) ? null : digester.digest(); |
| 2762 | } |
| 2763 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2764 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2765 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2766 | int uid = intent.getIntExtra( |
| 2767 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2768 | String authTokenType = intent.getStringExtra( |
| 2769 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2770 | final String titleAndSubtitle = |
| 2771 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2772 | account.name); |
| 2773 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2774 | String title = titleAndSubtitle; |
| 2775 | String subtitle = ""; |
| 2776 | if (index > 0) { |
| 2777 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2778 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2779 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2780 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2781 | Context contextForUser = getContextForUser(user); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 2782 | Notification n = new Notification.Builder(contextForUser) |
| 2783 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2784 | .setWhen(0) |
| 2785 | .setColor(contextForUser.getColor( |
| 2786 | com.android.internal.R.color.system_notification_accent_color)) |
| 2787 | .setContentTitle(title) |
| 2788 | .setContentText(subtitle) |
| 2789 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2790 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2791 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2792 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2793 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2794 | } |
| 2795 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2796 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 2797 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 2798 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2799 | |
| 2800 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2801 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2802 | if (startInNewTask) { |
| 2803 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 2804 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2805 | // the intent from a non-Activity context. This is the default behavior. |
| 2806 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2807 | } |
| 2808 | intent.addCategory(String.valueOf(getCredentialPermissionNotificationId(account, |
| 2809 | authTokenType, uid) + (packageName != null ? packageName : ""))); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2810 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2811 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2812 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2813 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2814 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2815 | return intent; |
| 2816 | } |
| 2817 | |
| 2818 | private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, |
| 2819 | int uid) { |
| 2820 | Integer id; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2821 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2822 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2823 | final Pair<Pair<Account, String>, Integer> key = |
| 2824 | new Pair<Pair<Account, String>, Integer>( |
| 2825 | new Pair<Account, String>(account, authTokenType), uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2826 | id = accounts.credentialsPermissionNotificationIds.get(key); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2827 | if (id == null) { |
| 2828 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2829 | accounts.credentialsPermissionNotificationIds.put(key, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | return id; |
| 2833 | } |
| 2834 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2835 | private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2836 | Integer id; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2837 | synchronized (accounts.signinRequiredNotificationIds) { |
| 2838 | id = accounts.signinRequiredNotificationIds.get(account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2839 | if (id == null) { |
| 2840 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2841 | accounts.signinRequiredNotificationIds.put(account, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | return id; |
| 2845 | } |
| 2846 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2847 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2848 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2849 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2850 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2851 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2852 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2853 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2854 | + ", response " + response |
| 2855 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2856 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2857 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2858 | + ", caller's uid " + Binder.getCallingUid() |
| 2859 | + ", pid " + Binder.getCallingPid()); |
| 2860 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2861 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2862 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2863 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2864 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2865 | final int uid = Binder.getCallingUid(); |
| 2866 | final int userId = UserHandle.getUserId(uid); |
| 2867 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2868 | try { |
| 2869 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2870 | "User is not allowed to add an account!"); |
| 2871 | } catch (RemoteException re) { |
| 2872 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2873 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2874 | return; |
| 2875 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2876 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2877 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2878 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2879 | "User cannot modify accounts of this type (policy)."); |
| 2880 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2881 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2882 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2883 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2884 | return; |
| 2885 | } |
| 2886 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2887 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2888 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2889 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2890 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2891 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2892 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2893 | long identityToken = clearCallingIdentity(); |
| 2894 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2895 | UserAccounts accounts = getUserAccounts(usrId); |
| 2896 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2897 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 2898 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2899 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2900 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2901 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2902 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2903 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2904 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2905 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2906 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2907 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2908 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2909 | protected String toDebugString(long now) { |
| 2910 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2911 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2912 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2913 | } |
| 2914 | }.bind(); |
| 2915 | } finally { |
| 2916 | restoreCallingIdentity(identityToken); |
| 2917 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2920 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2921 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 2922 | final String authTokenType, final String[] requiredFeatures, |
| 2923 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2924 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2925 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2926 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2927 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2928 | + ", response " + response |
| 2929 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2930 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2931 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2932 | + ", caller's uid " + Binder.getCallingUid() |
| 2933 | + ", pid " + Binder.getCallingPid() |
| 2934 | + ", for user id " + userId); |
| 2935 | } |
| 2936 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2937 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2938 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2939 | if (isCrossUser(callingUid, userId)) { |
| 2940 | throw new SecurityException( |
| 2941 | String.format( |
| 2942 | "User %s trying to add account for %s" , |
| 2943 | UserHandle.getCallingUserId(), |
| 2944 | userId)); |
| 2945 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2946 | |
| 2947 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2948 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2949 | try { |
| 2950 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2951 | "User is not allowed to add an account!"); |
| 2952 | } catch (RemoteException re) { |
| 2953 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2954 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2955 | return; |
| 2956 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2957 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2958 | try { |
| 2959 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2960 | "User cannot modify accounts of this type (policy)."); |
| 2961 | } catch (RemoteException re) { |
| 2962 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2963 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2964 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2965 | return; |
| 2966 | } |
| 2967 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2968 | final int pid = Binder.getCallingPid(); |
| 2969 | final int uid = Binder.getCallingUid(); |
| 2970 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2971 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2972 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2973 | |
| 2974 | long identityToken = clearCallingIdentity(); |
| 2975 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2976 | UserAccounts accounts = getUserAccounts(userId); |
| 2977 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2978 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 2979 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2980 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2981 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2982 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2983 | @Override |
| 2984 | public void run() throws RemoteException { |
| 2985 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 2986 | options); |
| 2987 | } |
| 2988 | |
| 2989 | @Override |
| 2990 | protected String toDebugString(long now) { |
| 2991 | return super.toDebugString(now) + ", addAccount" |
| 2992 | + ", accountType " + accountType |
| 2993 | + ", requiredFeatures " |
| 2994 | + (requiredFeatures != null |
| 2995 | ? TextUtils.join(",", requiredFeatures) |
| 2996 | : null); |
| 2997 | } |
| 2998 | }.bind(); |
| 2999 | } finally { |
| 3000 | restoreCallingIdentity(identityToken); |
| 3001 | } |
| 3002 | } |
| 3003 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3004 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3005 | public void startAddAccountSession( |
| 3006 | final IAccountManagerResponse response, |
| 3007 | final String accountType, |
| 3008 | final String authTokenType, |
| 3009 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3010 | final boolean expectActivityLaunch, |
| 3011 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3012 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3013 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3014 | Log.v(TAG, |
| 3015 | "startAddAccountSession: accountType " + accountType |
| 3016 | + ", response " + response |
| 3017 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3018 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3019 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3020 | + ", caller's uid " + Binder.getCallingUid() |
| 3021 | + ", pid " + Binder.getCallingPid()); |
| 3022 | } |
| 3023 | if (response == null) { |
| 3024 | throw new IllegalArgumentException("response is null"); |
| 3025 | } |
| 3026 | if (accountType == null) { |
| 3027 | throw new IllegalArgumentException("accountType is null"); |
| 3028 | } |
| 3029 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3030 | final int uid = Binder.getCallingUid(); |
| 3031 | final int userId = UserHandle.getUserId(uid); |
| 3032 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3033 | try { |
| 3034 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3035 | "User is not allowed to add an account!"); |
| 3036 | } catch (RemoteException re) { |
| 3037 | } |
| 3038 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3039 | return; |
| 3040 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3041 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3042 | try { |
| 3043 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3044 | "User cannot modify accounts of this type (policy)."); |
| 3045 | } catch (RemoteException re) { |
| 3046 | } |
| 3047 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3048 | userId); |
| 3049 | return; |
| 3050 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3051 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3052 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3053 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3054 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3055 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3056 | // Check to see if the Password should be included to the caller. |
| 3057 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3058 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3059 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3060 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3061 | long identityToken = clearCallingIdentity(); |
| 3062 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3063 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3064 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3065 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3066 | new StartAccountSession( |
| 3067 | accounts, |
| 3068 | response, |
| 3069 | accountType, |
| 3070 | expectActivityLaunch, |
| 3071 | null /* accountName */, |
| 3072 | false /* authDetailsRequired */, |
| 3073 | true /* updateLastAuthenticationTime */, |
| 3074 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3075 | @Override |
| 3076 | public void run() throws RemoteException { |
| 3077 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3078 | requiredFeatures, options); |
| 3079 | } |
| 3080 | |
| 3081 | @Override |
| 3082 | protected String toDebugString(long now) { |
| 3083 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3084 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3085 | + accountType + ", requiredFeatures " |
| 3086 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3087 | } |
| 3088 | }.bind(); |
| 3089 | } finally { |
| 3090 | restoreCallingIdentity(identityToken); |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3095 | private abstract class StartAccountSession extends Session { |
| 3096 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3097 | private final boolean mIsPasswordForwardingAllowed; |
| 3098 | |
| 3099 | public StartAccountSession( |
| 3100 | UserAccounts accounts, |
| 3101 | IAccountManagerResponse response, |
| 3102 | String accountType, |
| 3103 | boolean expectActivityLaunch, |
| 3104 | String accountName, |
| 3105 | boolean authDetailsRequired, |
| 3106 | boolean updateLastAuthenticationTime, |
| 3107 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3108 | super(accounts, response, accountType, expectActivityLaunch, |
| 3109 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3110 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3111 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | @Override |
| 3115 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3116 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3117 | mNumResults++; |
| 3118 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3119 | if (result != null |
| 3120 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3121 | checkKeyIntent( |
| 3122 | Binder.getCallingUid(), |
| 3123 | intent); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3124 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3125 | IAccountManagerResponse response; |
| 3126 | if (mExpectActivityLaunch && result != null |
| 3127 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3128 | response = mResponse; |
| 3129 | } else { |
| 3130 | response = getResponseAndClose(); |
| 3131 | } |
| 3132 | if (response == null) { |
| 3133 | return; |
| 3134 | } |
| 3135 | if (result == null) { |
| 3136 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3137 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3138 | + response); |
| 3139 | } |
| 3140 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3141 | "null bundle returned"); |
| 3142 | return; |
| 3143 | } |
| 3144 | |
| 3145 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3146 | // All AccountManager error codes are greater |
| 3147 | // than 0 |
| 3148 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3149 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3150 | return; |
| 3151 | } |
| 3152 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3153 | // Omit passwords if the caller isn't permitted to see them. |
| 3154 | if (!mIsPasswordForwardingAllowed) { |
| 3155 | result.remove(AccountManager.KEY_PASSWORD); |
| 3156 | } |
| 3157 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3158 | // Strip auth token from result. |
| 3159 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3160 | |
| 3161 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3162 | Log.v(TAG, |
| 3163 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3164 | } |
| 3165 | |
| 3166 | // Get the session bundle created by authenticator. The |
| 3167 | // bundle contains data necessary for finishing the session |
| 3168 | // later. The session bundle will be encrypted here and |
| 3169 | // decrypted later when trying to finish the session. |
| 3170 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3171 | if (sessionBundle != null) { |
| 3172 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3173 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3174 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3175 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3176 | } |
| 3177 | // Add accountType info to session bundle. This will |
| 3178 | // override any value set by authenticator. |
| 3179 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3180 | |
| 3181 | // Encrypt session bundle before returning to caller. |
| 3182 | try { |
| 3183 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3184 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3185 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3186 | } catch (GeneralSecurityException e) { |
| 3187 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3188 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3189 | } |
| 3190 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3191 | "failed to encrypt session bundle"); |
| 3192 | return; |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | sendResponse(response, result); |
| 3197 | } |
| 3198 | } |
| 3199 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3200 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3201 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3202 | @NonNull Bundle sessionBundle, |
| 3203 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3204 | Bundle appInfo, |
| 3205 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3206 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3207 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3208 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3209 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3210 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3211 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3212 | + ", caller's uid " + callingUid |
| 3213 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3214 | + ", pid " + Binder.getCallingPid() |
| 3215 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3216 | } |
| 3217 | if (response == null) { |
| 3218 | throw new IllegalArgumentException("response is null"); |
| 3219 | } |
| 3220 | |
| 3221 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3222 | // Account type is added to it before encryption. |
| 3223 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3224 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3225 | } |
| 3226 | |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 3227 | // Only allow the system process to finish session for other users. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3228 | if (isCrossUser(callingUid, userId)) { |
| 3229 | throw new SecurityException( |
| 3230 | String.format( |
| 3231 | "User %s trying to finish session for %s without cross user permission", |
| 3232 | UserHandle.getCallingUserId(), |
| 3233 | userId)); |
| 3234 | } |
| 3235 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3236 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3237 | sendErrorResponse(response, |
| 3238 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3239 | "User is not allowed to add an account!"); |
| 3240 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3241 | return; |
| 3242 | } |
| 3243 | |
| 3244 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3245 | final Bundle decryptedBundle; |
| 3246 | final String accountType; |
| 3247 | // First decrypt session bundle to get account type for checking permission. |
| 3248 | try { |
| 3249 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3250 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3251 | if (decryptedBundle == null) { |
| 3252 | sendErrorResponse( |
| 3253 | response, |
| 3254 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3255 | "failed to decrypt session bundle"); |
| 3256 | return; |
| 3257 | } |
| 3258 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3259 | // Account type cannot be null. This should not happen if session bundle was created |
| 3260 | // properly by #StartAccountSession. |
| 3261 | if (TextUtils.isEmpty(accountType)) { |
| 3262 | sendErrorResponse( |
| 3263 | response, |
| 3264 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3265 | "accountType is empty"); |
| 3266 | return; |
| 3267 | } |
| 3268 | |
| 3269 | // If by any chances, decryptedBundle contains colliding keys with |
| 3270 | // system info |
| 3271 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3272 | // update credentials flow, we should replace with the new values of the current call. |
| 3273 | if (appInfo != null) { |
| 3274 | decryptedBundle.putAll(appInfo); |
| 3275 | } |
| 3276 | |
| 3277 | // Add info that may be used by add account or update credentials flow. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3278 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3279 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3280 | } catch (GeneralSecurityException e) { |
| 3281 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3282 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3283 | } |
| 3284 | sendErrorResponse( |
| 3285 | response, |
| 3286 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3287 | "failed to decrypt session bundle"); |
| 3288 | return; |
| 3289 | } |
| 3290 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3291 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3292 | sendErrorResponse( |
| 3293 | response, |
| 3294 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3295 | "User cannot modify accounts of this type (policy)."); |
| 3296 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3297 | userId); |
| 3298 | return; |
| 3299 | } |
| 3300 | |
| 3301 | long identityToken = clearCallingIdentity(); |
| 3302 | try { |
| 3303 | UserAccounts accounts = getUserAccounts(userId); |
| 3304 | logRecordWithUid( |
| 3305 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3306 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3307 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3308 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3309 | new Session( |
| 3310 | accounts, |
| 3311 | response, |
| 3312 | accountType, |
| 3313 | expectActivityLaunch, |
| 3314 | true /* stripAuthTokenFromResult */, |
| 3315 | null /* accountName */, |
| 3316 | false /* authDetailsRequired */, |
| 3317 | true /* updateLastAuthenticationTime */) { |
| 3318 | @Override |
| 3319 | public void run() throws RemoteException { |
| 3320 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3321 | } |
| 3322 | |
| 3323 | @Override |
| 3324 | protected String toDebugString(long now) { |
| 3325 | return super.toDebugString(now) |
| 3326 | + ", finishSession" |
| 3327 | + ", accountType " + accountType; |
| 3328 | } |
| 3329 | }.bind(); |
| 3330 | } finally { |
| 3331 | restoreCallingIdentity(identityToken); |
| 3332 | } |
| 3333 | } |
| 3334 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3335 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3336 | final DevicePolicyManagerInternal dpmi = |
| 3337 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3338 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3339 | if (dpmi == null) { |
| 3340 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3341 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3342 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3343 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3344 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3345 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3346 | } |
| 3347 | if (intent == null) { |
| 3348 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3349 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3350 | long identityToken = clearCallingIdentity(); |
| 3351 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3352 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3353 | } finally { |
| 3354 | restoreCallingIdentity(identityToken); |
| 3355 | } |
| 3356 | } |
| 3357 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3358 | /** |
| 3359 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3360 | */ |
| 3361 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3362 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3363 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3364 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3365 | return cantAddAccount; |
| 3366 | } |
| 3367 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3368 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3369 | public void confirmCredentialsAsUser( |
| 3370 | IAccountManagerResponse response, |
| 3371 | final Account account, |
| 3372 | final Bundle options, |
| 3373 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3374 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3375 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3376 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3377 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3378 | Log.v(TAG, "confirmCredentials: " + account |
| 3379 | + ", response " + response |
| 3380 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3381 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3382 | + ", pid " + Binder.getCallingPid()); |
| 3383 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3384 | // Only allow the system process to read accounts of other users |
| 3385 | if (isCrossUser(callingUid, userId)) { |
| 3386 | throw new SecurityException( |
| 3387 | String.format( |
| 3388 | "User %s trying to confirm account credentials for %s" , |
| 3389 | UserHandle.getCallingUserId(), |
| 3390 | userId)); |
| 3391 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3392 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3393 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3394 | long identityToken = clearCallingIdentity(); |
| 3395 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3396 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3397 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3398 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3399 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3400 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3401 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3402 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3403 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3404 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3405 | protected String toDebugString(long now) { |
| 3406 | return super.toDebugString(now) + ", confirmCredentials" |
| 3407 | + ", " + account; |
| 3408 | } |
| 3409 | }.bind(); |
| 3410 | } finally { |
| 3411 | restoreCallingIdentity(identityToken); |
| 3412 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3413 | } |
| 3414 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3415 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3416 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3417 | final String authTokenType, final boolean expectActivityLaunch, |
| 3418 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3419 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3420 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3421 | Log.v(TAG, "updateCredentials: " + account |
| 3422 | + ", response " + response |
| 3423 | + ", authTokenType " + authTokenType |
| 3424 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3425 | + ", caller's uid " + Binder.getCallingUid() |
| 3426 | + ", pid " + Binder.getCallingPid()); |
| 3427 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3428 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3429 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3430 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3431 | long identityToken = clearCallingIdentity(); |
| 3432 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3433 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3434 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3435 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3436 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3437 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3438 | public void run() throws RemoteException { |
| 3439 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3440 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3441 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3442 | protected String toDebugString(long now) { |
| 3443 | if (loginOptions != null) loginOptions.keySet(); |
| 3444 | return super.toDebugString(now) + ", updateCredentials" |
| 3445 | + ", " + account |
| 3446 | + ", authTokenType " + authTokenType |
| 3447 | + ", loginOptions " + loginOptions; |
| 3448 | } |
| 3449 | }.bind(); |
| 3450 | } finally { |
| 3451 | restoreCallingIdentity(identityToken); |
| 3452 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3453 | } |
| 3454 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3455 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3456 | public void startUpdateCredentialsSession( |
| 3457 | IAccountManagerResponse response, |
| 3458 | final Account account, |
| 3459 | final String authTokenType, |
| 3460 | final boolean expectActivityLaunch, |
| 3461 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3462 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3463 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3464 | Log.v(TAG, |
| 3465 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3466 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3467 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3468 | + ", pid " + Binder.getCallingPid()); |
| 3469 | } |
| 3470 | if (response == null) { |
| 3471 | throw new IllegalArgumentException("response is null"); |
| 3472 | } |
| 3473 | if (account == null) { |
| 3474 | throw new IllegalArgumentException("account is null"); |
| 3475 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3476 | |
| 3477 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3478 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3479 | |
| 3480 | // Check to see if the Password should be included to the caller. |
| 3481 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3482 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3483 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3484 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3485 | long identityToken = clearCallingIdentity(); |
| 3486 | try { |
| 3487 | UserAccounts accounts = getUserAccounts(userId); |
| 3488 | new StartAccountSession( |
| 3489 | accounts, |
| 3490 | response, |
| 3491 | account.type, |
| 3492 | expectActivityLaunch, |
| 3493 | account.name, |
| 3494 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3495 | true /* updateLastCredentialTime */, |
| 3496 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3497 | @Override |
| 3498 | public void run() throws RemoteException { |
| 3499 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3500 | loginOptions); |
| 3501 | } |
| 3502 | |
| 3503 | @Override |
| 3504 | protected String toDebugString(long now) { |
| 3505 | if (loginOptions != null) |
| 3506 | loginOptions.keySet(); |
| 3507 | return super.toDebugString(now) |
| 3508 | + ", startUpdateCredentialsSession" |
| 3509 | + ", " + account |
| 3510 | + ", authTokenType " + authTokenType |
| 3511 | + ", loginOptions " + loginOptions; |
| 3512 | } |
| 3513 | }.bind(); |
| 3514 | } finally { |
| 3515 | restoreCallingIdentity(identityToken); |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3520 | public void isCredentialsUpdateSuggested( |
| 3521 | IAccountManagerResponse response, |
| 3522 | final Account account, |
| 3523 | final String statusToken) { |
| 3524 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3525 | Log.v(TAG, |
| 3526 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3527 | + ", caller's uid " + Binder.getCallingUid() |
| 3528 | + ", pid " + Binder.getCallingPid()); |
| 3529 | } |
| 3530 | if (response == null) { |
| 3531 | throw new IllegalArgumentException("response is null"); |
| 3532 | } |
| 3533 | if (account == null) { |
| 3534 | throw new IllegalArgumentException("account is null"); |
| 3535 | } |
| 3536 | if (TextUtils.isEmpty(statusToken)) { |
| 3537 | throw new IllegalArgumentException("status token is empty"); |
| 3538 | } |
| 3539 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3540 | int usrId = UserHandle.getCallingUserId(); |
| 3541 | long identityToken = clearCallingIdentity(); |
| 3542 | try { |
| 3543 | UserAccounts accounts = getUserAccounts(usrId); |
| 3544 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3545 | false /* stripAuthTokenFromResult */, account.name, |
| 3546 | false /* authDetailsRequired */) { |
| 3547 | @Override |
| 3548 | protected String toDebugString(long now) { |
| 3549 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3550 | + ", " + account; |
| 3551 | } |
| 3552 | |
| 3553 | @Override |
| 3554 | public void run() throws RemoteException { |
| 3555 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3556 | } |
| 3557 | |
| 3558 | @Override |
| 3559 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3560 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3561 | IAccountManagerResponse response = getResponseAndClose(); |
| 3562 | if (response == null) { |
| 3563 | return; |
| 3564 | } |
| 3565 | |
| 3566 | if (result == null) { |
| 3567 | sendErrorResponse( |
| 3568 | response, |
| 3569 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3570 | "null bundle"); |
| 3571 | return; |
| 3572 | } |
| 3573 | |
| 3574 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3575 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3576 | + response); |
| 3577 | } |
| 3578 | // Check to see if an error occurred. We know if an error occurred because all |
| 3579 | // error codes are greater than 0. |
| 3580 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3581 | sendErrorResponse(response, |
| 3582 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3583 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3584 | return; |
| 3585 | } |
| 3586 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3587 | sendErrorResponse( |
| 3588 | response, |
| 3589 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3590 | "no result in response"); |
| 3591 | return; |
| 3592 | } |
| 3593 | final Bundle newResult = new Bundle(); |
| 3594 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3595 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3596 | sendResponse(response, newResult); |
| 3597 | } |
| 3598 | }.bind(); |
| 3599 | } finally { |
| 3600 | restoreCallingIdentity(identityToken); |
| 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3605 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3606 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3607 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3608 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3609 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3610 | + ", response " + response |
| 3611 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3612 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3613 | + ", pid " + Binder.getCallingPid()); |
| 3614 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3615 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3616 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3617 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3618 | if (!isAccountManagedByCaller(accountType, callingUid, userId) |
| 3619 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3620 | String msg = String.format( |
| 3621 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3622 | callingUid, |
| 3623 | accountType); |
| 3624 | throw new SecurityException(msg); |
| 3625 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3626 | long identityToken = clearCallingIdentity(); |
| 3627 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3628 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3629 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3630 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3631 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3632 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3633 | public void run() throws RemoteException { |
| 3634 | mAuthenticator.editProperties(this, mAccountType); |
| 3635 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3636 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3637 | protected String toDebugString(long now) { |
| 3638 | return super.toDebugString(now) + ", editProperties" |
| 3639 | + ", accountType " + accountType; |
| 3640 | } |
| 3641 | }.bind(); |
| 3642 | } finally { |
| 3643 | restoreCallingIdentity(identityToken); |
| 3644 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3645 | } |
| 3646 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3647 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3648 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3649 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3650 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3651 | throw new SecurityException("Can be called only by system UID"); |
| 3652 | } |
| 3653 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3654 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3655 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3656 | |
| 3657 | final int userId = userHandle.getIdentifier(); |
| 3658 | |
| 3659 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3660 | |
| 3661 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3662 | int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3663 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3664 | } catch (NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3665 | Log.d(TAG, "Package not found " + e.getMessage()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3666 | return false; |
| 3667 | } |
| 3668 | } |
| 3669 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3670 | // Returns package with oldest target SDK for given UID. |
| 3671 | private String getPackageNameForUid(int uid) { |
| 3672 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3673 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3674 | return null; |
| 3675 | } |
| 3676 | // For app op checks related to permissions all packages in the UID |
| 3677 | // have the same app op state, so doesn't matter which one we pick. |
| 3678 | // Update: due to visibility changes we want to use package with oldest target SDK, |
| 3679 | |
| 3680 | String packageName = packageNames[0]; |
| 3681 | int oldestVersion = Integer.MAX_VALUE; |
| 3682 | for (String name : packageNames) { |
| 3683 | try { |
| 3684 | ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(name, 0); |
| 3685 | if (applicationInfo != null) { |
| 3686 | int version = applicationInfo.targetSdkVersion; |
| 3687 | if (version < oldestVersion) { |
| 3688 | oldestVersion = version; |
| 3689 | packageName = name; |
| 3690 | } |
| 3691 | } |
| 3692 | } catch (NameNotFoundException e) { |
| 3693 | // skip |
| 3694 | } |
| 3695 | } |
| 3696 | return packageName; |
| 3697 | } |
| 3698 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3699 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3700 | int uid) { |
| 3701 | if (packageName == null) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3702 | packageName = getPackageNameForUid(uid); |
| 3703 | if (packageName == null) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3704 | return false; |
| 3705 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | // Use null token which means any token. Having a token means the package |
| 3709 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3710 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3711 | return true; |
| 3712 | } |
| 3713 | // In addition to the permissions required to get an auth token we also allow |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3714 | // the account to be accessed by apps for which user or authenticator granted visibility. |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3715 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3716 | int visibility = resolveAccountVisibility(account, packageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3717 | getUserAccounts(UserHandle.getUserId(uid))); |
| 3718 | return (visibility == AccountManager.VISIBILITY_VISIBLE |
| 3719 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | @Override |
| 3723 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3724 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3725 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3726 | throw new SecurityException("Can be called only by system UID"); |
| 3727 | } |
| 3728 | |
| 3729 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3730 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3731 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3732 | |
| 3733 | final int userId = userHandle.getIdentifier(); |
| 3734 | |
| 3735 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3736 | |
| 3737 | final int uid; |
| 3738 | try { |
| 3739 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 3740 | } catch (NameNotFoundException e) { |
| 3741 | Slog.e(TAG, "Unknown package " + packageName); |
| 3742 | return null; |
| 3743 | } |
| 3744 | |
| 3745 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 3746 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3747 | final long identity = Binder.clearCallingIdentity(); |
| 3748 | try { |
| 3749 | return PendingIntent.getActivityAsUser( |
| 3750 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 3751 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 3752 | null, new UserHandle(userId)).getIntentSender(); |
| 3753 | } finally { |
| 3754 | Binder.restoreCallingIdentity(identity); |
| 3755 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 3759 | int uid, RemoteCallback callback) { |
| 3760 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 3761 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 3762 | @Override |
| 3763 | public void onResult(Bundle value) throws RemoteException { |
| 3764 | handleAuthenticatorResponse(true); |
| 3765 | } |
| 3766 | |
| 3767 | @Override |
| 3768 | public void onRequestContinued() { |
| 3769 | /* ignore */ |
| 3770 | } |
| 3771 | |
| 3772 | @Override |
| 3773 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 3774 | handleAuthenticatorResponse(false); |
| 3775 | } |
| 3776 | |
| 3777 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 3778 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3779 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3780 | UserHandle.getUserHandleForUid(uid)); |
| 3781 | if (callback != null) { |
| 3782 | Bundle result = new Bundle(); |
| 3783 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 3784 | callback.sendResult(result); |
| 3785 | } |
| 3786 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3787 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3791 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 3792 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 3793 | throw new SecurityException("Only system can check for accounts across users"); |
| 3794 | } |
| 3795 | final long token = Binder.clearCallingIdentity(); |
| 3796 | try { |
| 3797 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 3798 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 3799 | if (allAccounts[i].account.equals(account)) { |
| 3800 | return true; |
| 3801 | } |
| 3802 | } |
| 3803 | return false; |
| 3804 | } finally { |
| 3805 | Binder.restoreCallingIdentity(token); |
| 3806 | } |
| 3807 | } |
| 3808 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3809 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 3810 | private final String[] mFeatures; |
| 3811 | private volatile Account[] mAccountsOfType = null; |
| 3812 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 3813 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3814 | private final int mCallingUid; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3815 | private final String mPackageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3816 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3817 | public GetAccountsByTypeAndFeatureSession( |
| 3818 | UserAccounts accounts, |
| 3819 | IAccountManagerResponse response, |
| 3820 | String type, |
| 3821 | String[] features, |
| 3822 | int callingUid, |
| 3823 | String packageName) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3824 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3825 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3826 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3827 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3828 | mFeatures = features; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3829 | mPackageName = packageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3830 | } |
| 3831 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3832 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3833 | public void run() throws RemoteException { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3834 | synchronized (mAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3835 | mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType, mCallingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3836 | mPackageName, false /* include managed not visible*/); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3837 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3838 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 3839 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3840 | mCurrentAccount = 0; |
| 3841 | |
| 3842 | checkAccount(); |
| 3843 | } |
| 3844 | |
| 3845 | public void checkAccount() { |
| 3846 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 3847 | sendResult(); |
| 3848 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3849 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3850 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3851 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 3852 | if (accountAuthenticator == null) { |
| 3853 | // It is possible that the authenticator has died, which is indicated by |
| 3854 | // mAuthenticator being set to null. If this happens then just abort. |
| 3855 | // There is no need to send back a result or error in this case since |
| 3856 | // that already happened when mAuthenticator was cleared. |
| 3857 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3858 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 3859 | + " connected to the authenticator, " + toDebugString()); |
| 3860 | } |
| 3861 | return; |
| 3862 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3863 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3864 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3865 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3866 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3867 | } |
| 3868 | } |
| 3869 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3870 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3871 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3872 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3873 | mNumResults++; |
| 3874 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3875 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3876 | return; |
| 3877 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3878 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3879 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 3880 | } |
| 3881 | mCurrentAccount++; |
| 3882 | checkAccount(); |
| 3883 | } |
| 3884 | |
| 3885 | public void sendResult() { |
| 3886 | IAccountManagerResponse response = getResponseAndClose(); |
| 3887 | if (response != null) { |
| 3888 | try { |
| 3889 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 3890 | for (int i = 0; i < accounts.length; i++) { |
| 3891 | accounts[i] = mAccountsWithFeatures.get(i); |
| 3892 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3893 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3894 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3895 | + response); |
| 3896 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3897 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3898 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3899 | response.onResult(result); |
| 3900 | } catch (RemoteException e) { |
| 3901 | // if the caller is dead then there is no one to care about remote exceptions |
| 3902 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3903 | Log.v(TAG, "failure while notifying response", e); |
| 3904 | } |
| 3905 | } |
| 3906 | } |
| 3907 | } |
| 3908 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3909 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3910 | protected String toDebugString(long now) { |
| 3911 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 3912 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 3913 | } |
| 3914 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 3915 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3916 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3917 | * Returns the accounts visible to the client within the context of a specific user |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3918 | * @hide |
| 3919 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3920 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3921 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3922 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3923 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3924 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 3925 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3926 | if (visibleAccountTypes.isEmpty()) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3927 | return new Account[0]; |
| 3928 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3929 | long identityToken = clearCallingIdentity(); |
| 3930 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3931 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3932 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3933 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3934 | callingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3935 | opPackageName, |
| 3936 | visibleAccountTypes, |
| 3937 | false /* includeUserManagedNotVisible */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3938 | } finally { |
| 3939 | restoreCallingIdentity(identityToken); |
| 3940 | } |
| 3941 | } |
| 3942 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3943 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3944 | * Returns accounts for all running users, ignores visibility values. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3945 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3946 | * @hide |
| 3947 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3948 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3949 | public AccountAndUser[] getRunningAccounts() { |
| 3950 | final int[] runningUserIds; |
| 3951 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 3952 | runningUserIds = ActivityManager.getService().getRunningUserIds(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3953 | } catch (RemoteException e) { |
| 3954 | // Running in system_server; should never happen |
| 3955 | throw new RuntimeException(e); |
| 3956 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3957 | return getAccounts(runningUserIds); |
| 3958 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3959 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3960 | /** |
| 3961 | * Returns accounts for all users, ignores visibility values. |
| 3962 | * |
| 3963 | * @hide |
| 3964 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3965 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3966 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 3967 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3968 | final int[] userIds = new int[users.size()]; |
| 3969 | for (int i = 0; i < userIds.length; i++) { |
| 3970 | userIds[i] = users.get(i).id; |
| 3971 | } |
| 3972 | return getAccounts(userIds); |
| 3973 | } |
| 3974 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3975 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3976 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3977 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 3978 | for (int userId : userIds) { |
| 3979 | UserAccounts userAccounts = getUserAccounts(userId); |
| 3980 | if (userAccounts == null) continue; |
| 3981 | synchronized (userAccounts.cacheLock) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3982 | Account[] accounts = getAccountsFromCacheLocked( |
| 3983 | userAccounts, |
| 3984 | null /* type */, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3985 | Binder.getCallingUid(), |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3986 | null /* packageName */, |
| 3987 | false /* include managed not visible*/); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 3988 | for (int a = 0; a < accounts.length; a++) { |
| 3989 | runningAccounts.add(new AccountAndUser(accounts[a], userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3990 | } |
| 3991 | } |
| 3992 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3993 | |
| 3994 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 3995 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3996 | } |
| 3997 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3998 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3999 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4000 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4001 | int callingUid = Binder.getCallingUid(); |
| 4002 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 4003 | return getAccountsAsUser(type, userId, opPackageName /* callingPackage */, -1, |
| 4004 | opPackageName, false /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4005 | } |
| 4006 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4007 | @NonNull |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4008 | private Account[] getAccountsAsUser( |
| 4009 | String type, |
| 4010 | int userId, |
| 4011 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4012 | int packageUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4013 | String opPackageName, |
| 4014 | boolean includeUserManagedNotVisible) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4015 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4016 | // Only allow the system process to read accounts of other users |
| 4017 | if (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4018 | && callingUid != Process.SYSTEM_UID |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4019 | && mContext.checkCallingOrSelfPermission( |
| 4020 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4021 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4022 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4023 | + " trying to get account for " + userId); |
| 4024 | } |
| 4025 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4026 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4027 | Log.v(TAG, "getAccounts: accountType " + type |
| 4028 | + ", caller's uid " + Binder.getCallingUid() |
| 4029 | + ", pid " + Binder.getCallingPid()); |
| 4030 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4031 | |
| 4032 | // If the original calling app was using account choosing activity |
| 4033 | // provided by the framework or authenticator we'll passing in |
| 4034 | // the original caller's uid here, which is what should be used for filtering. |
| 4035 | List<String> managedTypes = |
| 4036 | getTypesManagedByCaller(callingUid, UserHandle.getUserId(callingUid)); |
| 4037 | if (packageUid != -1 && |
| 4038 | ((UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 4039 | || (type != null && managedTypes.contains(type))))) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4040 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4041 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4042 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4043 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4044 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4045 | if (visibleAccountTypes.isEmpty() |
| 4046 | || (type != null && !visibleAccountTypes.contains(type))) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4047 | return new Account[]{}; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4048 | } else if (visibleAccountTypes.contains(type)) { |
| 4049 | // Prune the list down to just the requested type. |
| 4050 | visibleAccountTypes = new ArrayList<>(); |
| 4051 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4052 | } // else aggregate all the visible accounts (it won't matter if the |
| 4053 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4054 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4055 | long identityToken = clearCallingIdentity(); |
| 4056 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4057 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 4058 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4059 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4060 | callingUid, |
Dmitry Dementyev | 2e22cfb | 2017-01-09 18:42:14 +0000 | [diff] [blame] | 4061 | callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4062 | visibleAccountTypes, |
| 4063 | includeUserManagedNotVisible); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4064 | } finally { |
| 4065 | restoreCallingIdentity(identityToken); |
| 4066 | } |
| 4067 | } |
| 4068 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4069 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4070 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4071 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4072 | int callingUid, |
| 4073 | String callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4074 | List<String> visibleAccountTypes, |
| 4075 | boolean includeUserManagedNotVisible) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4076 | synchronized (userAccounts.cacheLock) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4077 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4078 | for (String visibleType : visibleAccountTypes) { |
| 4079 | Account[] accountsForType = getAccountsFromCacheLocked( |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4080 | userAccounts, visibleType, callingUid, callingPackage, |
| 4081 | includeUserManagedNotVisible); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4082 | if (accountsForType != null) { |
| 4083 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
| 4084 | } |
| 4085 | } |
| 4086 | Account[] result = new Account[visibleAccounts.size()]; |
| 4087 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4088 | result[i] = visibleAccounts.get(i); |
| 4089 | } |
| 4090 | return result; |
| 4091 | } |
| 4092 | } |
| 4093 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4094 | @Override |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4095 | public void addSharedAccountsFromParentUser(int parentUserId, int userId, |
| 4096 | String opPackageName) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4097 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4098 | Account[] accounts = getAccountsAsUser(null, parentUserId, opPackageName); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4099 | for (Account account : accounts) { |
| 4100 | addSharedAccountAsUser(account, userId); |
| 4101 | } |
| 4102 | } |
| 4103 | |
| 4104 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4105 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4106 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4107 | accounts.accountsDb.deleteSharedAccount(account); |
| 4108 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4109 | if (accountId < 0) { |
| 4110 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4111 | + ", skipping the DB insert failed"); |
| 4112 | return false; |
| 4113 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4114 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4115 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4116 | return true; |
| 4117 | } |
| 4118 | |
| 4119 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4120 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4121 | userId = handleIncomingUser(userId); |
| 4122 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4123 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4124 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4125 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4126 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4127 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4128 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4129 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4130 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4131 | } |
| 4132 | return r > 0; |
| 4133 | } |
| 4134 | |
| 4135 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4136 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4137 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4138 | } |
| 4139 | |
| 4140 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4141 | userId = handleIncomingUser(userId); |
| 4142 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4143 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4144 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4145 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4146 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4147 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4148 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4149 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4150 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | @Override |
| 4154 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4155 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4156 | UserAccounts accounts = getUserAccounts(userId); |
| 4157 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4158 | Account[] accountArray = new Account[accountList.size()]; |
| 4159 | accountList.toArray(accountArray); |
| 4160 | return accountArray; |
| 4161 | } |
| 4162 | |
| 4163 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4164 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4165 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4166 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4167 | } |
| 4168 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4169 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4170 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4171 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4172 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4173 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4174 | // Don't do opPackageName check - caller is system. |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4175 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4176 | + callingUid + " with uid=" + uid); |
| 4177 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4178 | return getAccountsAsUser(null, UserHandle.getCallingUserId(), packageName, uid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4179 | opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4180 | } |
| 4181 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4182 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4183 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4184 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4185 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4186 | int callingUid = Binder.getCallingUid(); |
| 4187 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4188 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4189 | int packageUid = -1; |
| 4190 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4191 | packageUid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4192 | } catch (NameNotFoundException re) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4193 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
| 4194 | return new Account[0]; |
| 4195 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4196 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 4197 | && !isAccountManagedByCaller(type, callingUid, userId)) { |
| 4198 | return new Account[0]; |
| 4199 | } |
| 4200 | |
| 4201 | return getAccountsAsUser(type, userId, |
| 4202 | packageName, packageUid, opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4203 | } |
| 4204 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4205 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4206 | public void getAccountsByFeatures( |
| 4207 | IAccountManagerResponse response, |
| 4208 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4209 | String[] features, |
| 4210 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4211 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4212 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4213 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4214 | Log.v(TAG, "getAccounts: accountType " + type |
| 4215 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4216 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4217 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4218 | + ", pid " + Binder.getCallingPid()); |
| 4219 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4220 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4221 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4222 | int userId = UserHandle.getCallingUserId(); |
| 4223 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4224 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4225 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4226 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4227 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4228 | // Need to return just the accounts that are from matching signatures. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4229 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, new Account[0]); |
| 4230 | try { |
| 4231 | response.onResult(result); |
| 4232 | } catch (RemoteException e) { |
| 4233 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4234 | } |
| 4235 | return; |
| 4236 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4237 | long identityToken = clearCallingIdentity(); |
| 4238 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4239 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4240 | if (features == null || features.length == 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4241 | Account[] accounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4242 | synchronized (userAccounts.cacheLock) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4243 | accounts = getAccountsFromCacheLocked( |
| 4244 | userAccounts, type, callingUid, opPackageName, false); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4245 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4246 | Bundle result = new Bundle(); |
| 4247 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4248 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4249 | return; |
| 4250 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4251 | new GetAccountsByTypeAndFeatureSession( |
| 4252 | userAccounts, |
| 4253 | response, |
| 4254 | type, |
| 4255 | features, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4256 | callingUid, |
| 4257 | opPackageName).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4258 | } finally { |
| 4259 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4260 | } |
| 4261 | } |
| 4262 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4263 | @Override |
| 4264 | public void onAccountAccessed(String token) throws RemoteException { |
| 4265 | final int uid = Binder.getCallingUid(); |
| 4266 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4267 | return; |
| 4268 | } |
| 4269 | final int userId = UserHandle.getCallingUserId(); |
| 4270 | final long identity = Binder.clearCallingIdentity(); |
| 4271 | try { |
| 4272 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4273 | if (Objects.equals(account.getAccessId(), token)) { |
| 4274 | // An app just accessed the account. At this point it knows about |
| 4275 | // it and there is not need to hide this account from the app. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4276 | // Do we need to update account visibility here? |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4277 | if (!hasAccountAccess(account, null, uid)) { |
| 4278 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4279 | uid, true); |
| 4280 | } |
| 4281 | } |
| 4282 | } |
| 4283 | } finally { |
| 4284 | Binder.restoreCallingIdentity(identity); |
| 4285 | } |
| 4286 | } |
| 4287 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4288 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4289 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4290 | IAccountManagerResponse mResponse; |
| 4291 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4292 | final boolean mExpectActivityLaunch; |
| 4293 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4294 | final String mAccountName; |
| 4295 | // Indicates if we need to add auth details(like last credential time) |
| 4296 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4297 | // If set, we need to update the last authenticated time. This is |
| 4298 | // currently |
| 4299 | // used on |
| 4300 | // successful confirming credentials. |
| 4301 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4302 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4303 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4304 | private int mNumRequestContinued = 0; |
| 4305 | private int mNumErrors = 0; |
| 4306 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4307 | IAccountAuthenticator mAuthenticator = null; |
| 4308 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4309 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4310 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4311 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4312 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4313 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4314 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4315 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4316 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4317 | } |
| 4318 | |
| 4319 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4320 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4321 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4322 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4323 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4324 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4325 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4326 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4327 | mResponse = response; |
| 4328 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4329 | mExpectActivityLaunch = expectActivityLaunch; |
| 4330 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4331 | mAccountName = accountName; |
| 4332 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4333 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4334 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4335 | synchronized (mSessions) { |
| 4336 | mSessions.put(toString(), this); |
| 4337 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4338 | if (response != null) { |
| 4339 | try { |
| 4340 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4341 | } catch (RemoteException e) { |
| 4342 | mResponse = null; |
| 4343 | binderDied(); |
| 4344 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4345 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4346 | } |
| 4347 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4348 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4349 | if (mResponse == null) { |
| 4350 | // this session has already been closed |
| 4351 | return null; |
| 4352 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4353 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4354 | close(); // this clears mResponse so we need to save the response before this call |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4355 | return response; |
| 4356 | } |
| 4357 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4358 | /** |
| 4359 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4360 | * security policy. |
| 4361 | * |
| 4362 | * In particular we want to make sure that the Authenticator doesn't try to trick users |
| 4363 | * into launching aribtrary intents on the device via by tricking to click authenticator |
| 4364 | * supplied entries in the system Settings app. |
| 4365 | */ |
| 4366 | protected void checkKeyIntent( |
| 4367 | int authUid, |
| 4368 | Intent intent) throws SecurityException { |
| 4369 | long bid = Binder.clearCallingIdentity(); |
| 4370 | try { |
| 4371 | PackageManager pm = mContext.getPackageManager(); |
| 4372 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 4373 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4374 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Sandra Kwan | 0e961a1 | 2016-06-30 14:34:01 -0700 | [diff] [blame] | 4375 | if (!GrantCredentialsPermissionActivity.class.getName().equals( |
| 4376 | targetActivityInfo.getClass().getName()) |
| 4377 | && !CantAddAccountActivity.class |
| 4378 | .equals(targetActivityInfo.getClass().getName()) |
| 4379 | && PackageManager.SIGNATURE_MATCH != pm.checkSignatures(authUid, |
| 4380 | targetUid)) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4381 | String pkgName = targetActivityInfo.packageName; |
| 4382 | String activityName = targetActivityInfo.name; |
| 4383 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4384 | + "does not share a signature with the supplying authenticator (%s)."; |
| 4385 | throw new SecurityException( |
| 4386 | String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4387 | } |
| 4388 | } finally { |
| 4389 | Binder.restoreCallingIdentity(bid); |
| 4390 | } |
| 4391 | } |
| 4392 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4393 | private void close() { |
| 4394 | synchronized (mSessions) { |
| 4395 | if (mSessions.remove(toString()) == null) { |
| 4396 | // the session was already closed, so bail out now |
| 4397 | return; |
| 4398 | } |
| 4399 | } |
| 4400 | if (mResponse != null) { |
| 4401 | // stop listening for response deaths |
| 4402 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4403 | |
| 4404 | // clear this so that we don't accidentally send any further results |
| 4405 | mResponse = null; |
| 4406 | } |
| 4407 | cancelTimeout(); |
| 4408 | unbind(); |
| 4409 | } |
| 4410 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4411 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4412 | public void binderDied() { |
| 4413 | mResponse = null; |
| 4414 | close(); |
| 4415 | } |
| 4416 | |
| 4417 | protected String toDebugString() { |
| 4418 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4419 | } |
| 4420 | |
| 4421 | protected String toDebugString(long now) { |
| 4422 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4423 | + ", connected " + (mAuthenticator != null) |
| 4424 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4425 | + "/" + mNumErrors + ")" |
| 4426 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4427 | } |
| 4428 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4429 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4430 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4431 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4432 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4433 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4434 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4435 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4436 | } |
| 4437 | } |
| 4438 | |
| 4439 | private void unbind() { |
| 4440 | if (mAuthenticator != null) { |
| 4441 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4442 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4443 | } |
| 4444 | } |
| 4445 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4446 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4447 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4448 | } |
| 4449 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4450 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4451 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4452 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4453 | try { |
| 4454 | run(); |
| 4455 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4456 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4457 | "remote exception"); |
| 4458 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4459 | } |
| 4460 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4461 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4462 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4463 | mAuthenticator = null; |
| 4464 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4465 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4466 | try { |
| 4467 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4468 | "disconnected"); |
| 4469 | } catch (RemoteException e) { |
| 4470 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4471 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4472 | + "caught RemoteException while responding", e); |
| 4473 | } |
| 4474 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4475 | } |
| 4476 | } |
| 4477 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4478 | public abstract void run() throws RemoteException; |
| 4479 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4480 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4481 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4482 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4483 | try { |
| 4484 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4485 | "timeout"); |
| 4486 | } catch (RemoteException e) { |
| 4487 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4488 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4489 | e); |
| 4490 | } |
| 4491 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4492 | } |
| 4493 | } |
| 4494 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4495 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4496 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4497 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4498 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4499 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4500 | if (result != null) { |
| 4501 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4502 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4503 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4504 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4505 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4506 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4507 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4508 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4509 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4510 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4511 | if (needUpdate || mAuthDetailsRequired) { |
| 4512 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4513 | if (needUpdate && accountPresent) { |
| 4514 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4515 | } |
| 4516 | if (mAuthDetailsRequired) { |
| 4517 | long lastAuthenticatedTime = -1; |
| 4518 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4519 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4520 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4521 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4522 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4523 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4524 | lastAuthenticatedTime); |
| 4525 | } |
| 4526 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4527 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4528 | if (result != null |
| 4529 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4530 | checkKeyIntent( |
| 4531 | Binder.getCallingUid(), |
| 4532 | intent); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4533 | } |
| 4534 | if (result != null |
| 4535 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4536 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4537 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4538 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4539 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4540 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4541 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4542 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4543 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4544 | IAccountManagerResponse response; |
| 4545 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4546 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4547 | response = mResponse; |
| 4548 | } else { |
| 4549 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4550 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4551 | if (response != null) { |
| 4552 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4553 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4554 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4555 | Log.v(TAG, getClass().getSimpleName() |
| 4556 | + " calling onError() on response " + response); |
| 4557 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4558 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4559 | "null bundle returned"); |
| 4560 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4561 | if (mStripAuthTokenFromResult) { |
| 4562 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4563 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4564 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4565 | Log.v(TAG, getClass().getSimpleName() |
| 4566 | + " calling onResult() on response " + response); |
| 4567 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4568 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4569 | (intent == null)) { |
| 4570 | // All AccountManager error codes are greater than 0 |
| 4571 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4572 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4573 | } else { |
| 4574 | response.onResult(result); |
| 4575 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4576 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4577 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4578 | // if the caller is dead then there is no one to care about remote exceptions |
| 4579 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4580 | Log.v(TAG, "failure while notifying response", e); |
| 4581 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4582 | } |
| 4583 | } |
| 4584 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4585 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4586 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4587 | public void onRequestContinued() { |
| 4588 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4589 | } |
| 4590 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4591 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4592 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4593 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4594 | IAccountManagerResponse response = getResponseAndClose(); |
| 4595 | if (response != null) { |
| 4596 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4597 | Log.v(TAG, getClass().getSimpleName() |
| 4598 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4599 | } |
| 4600 | try { |
| 4601 | response.onError(errorCode, errorMessage); |
| 4602 | } catch (RemoteException e) { |
| 4603 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4604 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4605 | } |
| 4606 | } |
| 4607 | } else { |
| 4608 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4609 | Log.v(TAG, "Session.onError: already closed"); |
| 4610 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4611 | } |
| 4612 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4613 | |
| 4614 | /** |
| 4615 | * find the component name for the authenticator and initiate a bind |
| 4616 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4617 | */ |
| 4618 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4619 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4620 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4621 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4622 | if (authenticatorInfo == null) { |
| 4623 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4624 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 4625 | + ", bailing out"); |
| 4626 | } |
| 4627 | return false; |
| 4628 | } |
| 4629 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 4630 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 4631 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 4632 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 4633 | + " which isn't encryption aware"); |
| 4634 | return false; |
| 4635 | } |
| 4636 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4637 | Intent intent = new Intent(); |
| 4638 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 4639 | intent.setComponent(authenticatorInfo.componentName); |
| 4640 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4641 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 4642 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 4643 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4644 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4645 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4646 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 4647 | } |
| 4648 | return false; |
| 4649 | } |
| 4650 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4651 | return true; |
| 4652 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4653 | } |
| 4654 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 4655 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4656 | MessageHandler(Looper looper) { |
| 4657 | super(looper); |
| 4658 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4659 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4660 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4661 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4662 | switch (msg.what) { |
| 4663 | case MESSAGE_TIMED_OUT: |
| 4664 | Session session = (Session)msg.obj; |
| 4665 | session.onTimedOut(); |
| 4666 | break; |
| 4667 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4668 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 4669 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4670 | break; |
| 4671 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4672 | default: |
| 4673 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 4674 | } |
| 4675 | } |
| 4676 | } |
| 4677 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4678 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4679 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4680 | } |
| 4681 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4682 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4683 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4684 | } |
| 4685 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4686 | /* |
| 4687 | * This function receives an opened writable database. |
| 4688 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4689 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4690 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4691 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4695 | * This function receives an opened writable database and writes to it in a separate thread. |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4696 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4697 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4698 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4699 | |
| 4700 | class LogRecordTask implements Runnable { |
| 4701 | private final String action; |
| 4702 | private final String tableName; |
| 4703 | private final long accountId; |
| 4704 | private final UserAccounts userAccount; |
| 4705 | private final int callingUid; |
| 4706 | private final long userDebugDbInsertionPoint; |
| 4707 | |
| 4708 | LogRecordTask(final String action, |
| 4709 | final String tableName, |
| 4710 | final long accountId, |
| 4711 | final UserAccounts userAccount, |
| 4712 | final int callingUid, |
| 4713 | final long userDebugDbInsertionPoint) { |
| 4714 | this.action = action; |
| 4715 | this.tableName = tableName; |
| 4716 | this.accountId = accountId; |
| 4717 | this.userAccount = userAccount; |
| 4718 | this.callingUid = callingUid; |
| 4719 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 4720 | } |
| 4721 | |
| 4722 | public void run() { |
| 4723 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 4724 | logStatement.bindLong(1, accountId); |
| 4725 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4726 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4727 | logStatement.bindLong(4, callingUid); |
| 4728 | logStatement.bindString(5, tableName); |
| 4729 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 4730 | logStatement.execute(); |
| 4731 | logStatement.clearBindings(); |
| 4732 | } |
| 4733 | } |
| 4734 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4735 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 4736 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4737 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4738 | % AccountsDb.MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4739 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | /* |
| 4743 | * This should only be called once to compile the sql statement for logging |
| 4744 | * and to find the insertion point. |
| 4745 | */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4746 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(UserAccounts userAccount) { |
| 4747 | userAccount.debugDbInsertionPoint = userAccount.accountsDb |
| 4748 | .calculateDebugTableInsertionPoint(); |
| 4749 | userAccount.statementForLogging = userAccount.accountsDb.compileSqlStatementForLogging(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4750 | } |
| 4751 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4752 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4753 | return asBinder(); |
| 4754 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4755 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4756 | /** |
| 4757 | * Searches array of arguments for the specified string |
| 4758 | * @param args array of argument strings |
| 4759 | * @param value value to search for |
| 4760 | * @return true if the value is contained in the array |
| 4761 | */ |
| 4762 | private static boolean scanArgs(String[] args, String value) { |
| 4763 | if (args != null) { |
| 4764 | for (String arg : args) { |
| 4765 | if (value.equals(arg)) { |
| 4766 | return true; |
| 4767 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4768 | } |
| 4769 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4770 | return false; |
| 4771 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4772 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4773 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4774 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4775 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 4776 | != PackageManager.PERMISSION_GRANTED) { |
| 4777 | fout.println("Permission Denial: can't dump AccountsManager from from pid=" |
| 4778 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 4779 | + " without permission " + android.Manifest.permission.DUMP); |
| 4780 | return; |
| 4781 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4782 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4783 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4784 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4785 | final List<UserInfo> users = getUserManager().getUsers(); |
| 4786 | for (UserInfo user : users) { |
| 4787 | ipw.println("User " + user + ":"); |
| 4788 | ipw.increaseIndent(); |
| 4789 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 4790 | ipw.println(); |
| 4791 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4792 | } |
| 4793 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4794 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4795 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 4796 | String[] args, boolean isCheckinRequest) { |
| 4797 | synchronized (userAccounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4798 | if (isCheckinRequest) { |
| 4799 | // This is a checkin request. *Only* upload the account types and the count of each. |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4800 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4801 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4802 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4803 | Process.SYSTEM_UID, null /* packageName */, false); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4804 | fout.println("Accounts: " + accounts.length); |
| 4805 | for (Account account : accounts) { |
| 4806 | fout.println(" " + account); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4807 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 4808 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4809 | // Add debug information. |
| 4810 | fout.println(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4811 | userAccounts.accountsDb.dumpDebugTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4812 | fout.println(); |
| 4813 | synchronized (mSessions) { |
| 4814 | final long now = SystemClock.elapsedRealtime(); |
| 4815 | fout.println("Active Sessions: " + mSessions.size()); |
| 4816 | for (Session session : mSessions.values()) { |
| 4817 | fout.println(" " + session.toDebugString(now)); |
| 4818 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4819 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4820 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4821 | fout.println(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4822 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4823 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4824 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4825 | } |
| 4826 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4827 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4828 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4829 | long identityToken = clearCallingIdentity(); |
| 4830 | try { |
| 4831 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4832 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 4833 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4834 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4835 | if (intent.getComponent() != null && |
| 4836 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 4837 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4838 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4839 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4840 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4841 | final Integer notificationId = getSigninRequiredNotificationId(accounts, account); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4842 | intent.addCategory(String.valueOf(notificationId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4843 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4844 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4845 | contextForUser.getText(R.string.notification_title).toString(); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4846 | Notification n = new Notification.Builder(contextForUser) |
| 4847 | .setWhen(0) |
| 4848 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 4849 | .setColor(contextForUser.getColor( |
| 4850 | com.android.internal.R.color.system_notification_accent_color)) |
| 4851 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 4852 | .setContentText(message) |
| 4853 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 4854 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4855 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4856 | .build(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4857 | installNotification(notificationId, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4858 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4859 | } finally { |
| 4860 | restoreCallingIdentity(identityToken); |
| 4861 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4862 | } |
| 4863 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4864 | private void installNotification(int notificationId, final Notification notification, |
| 4865 | String packageName, int userId) { |
| 4866 | final long token = clearCallingIdentity(); |
| 4867 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4868 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4869 | try { |
| 4870 | notificationManager.enqueueNotificationWithTag(packageName, packageName, null, |
| 4871 | notificationId, notification, new int[1], userId); |
| 4872 | } catch (RemoteException e) { |
| 4873 | /* ignore - local call */ |
| 4874 | } |
| 4875 | } finally { |
| 4876 | Binder.restoreCallingIdentity(token); |
| 4877 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4878 | } |
| 4879 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4880 | private void cancelNotification(int id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4881 | cancelNotification(id, mContext.getPackageName(), user); |
| 4882 | } |
| 4883 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4884 | private void cancelNotification(int id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4885 | long identityToken = clearCallingIdentity(); |
| 4886 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4887 | INotificationManager service = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4888 | service.cancelNotificationWithTag(packageName, null, id, user.getIdentifier()); |
| 4889 | } catch (RemoteException e) { |
| 4890 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4891 | } finally { |
| 4892 | restoreCallingIdentity(identityToken); |
| 4893 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4894 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4895 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4896 | private boolean isPermittedForPackage(String packageName, int userId, String... permissions) { |
| 4897 | final long identity = Binder.clearCallingIdentity(); |
| 4898 | try { |
| 4899 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 4900 | for (String perm : permissions) { |
| 4901 | if (pm.checkPermission(perm, packageName, userId) |
| 4902 | == PackageManager.PERMISSION_GRANTED) { |
| 4903 | return true; |
| 4904 | } |
| 4905 | } |
| 4906 | } catch (RemoteException e) { |
| 4907 | /* ignore - local call */ |
| 4908 | } finally { |
| 4909 | Binder.restoreCallingIdentity(identity); |
| 4910 | } |
| 4911 | return false; |
| 4912 | } |
| 4913 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 4914 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 4915 | for (String perm : permissions) { |
| 4916 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 4917 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4918 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 4919 | } |
| 4920 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 4921 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 4922 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 4923 | return true; |
| 4924 | } |
| 4925 | } |
| 4926 | } |
| 4927 | return false; |
| 4928 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4929 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4930 | private int handleIncomingUser(int userId) { |
| 4931 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4932 | return ActivityManager.getService().handleIncomingUser( |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4933 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 4934 | } catch (RemoteException re) { |
| 4935 | // Shouldn't happen, local. |
| 4936 | } |
| 4937 | return userId; |
| 4938 | } |
| 4939 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 4940 | private boolean isPrivileged(int callingUid) { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 4941 | String[] packages; |
| 4942 | long identityToken = Binder.clearCallingIdentity(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4943 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 4944 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 4945 | } finally { |
| 4946 | Binder.restoreCallingIdentity(identityToken); |
| 4947 | } |
| 4948 | if (packages == null) { |
| 4949 | Log.d(TAG, "No packages for callingUid " + callingUid); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4950 | return false; |
| 4951 | } |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4952 | for (String name : packages) { |
| 4953 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 4954 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4955 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 4956 | && (packageInfo.applicationInfo.privateFlags |
| 4957 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4958 | return true; |
| 4959 | } |
| 4960 | } catch (PackageManager.NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4961 | Log.d(TAG, "Package not found " + e.getMessage()); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4962 | return false; |
| 4963 | } |
| 4964 | } |
| 4965 | return false; |
| 4966 | } |
| 4967 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4968 | private boolean permissionIsGranted( |
| 4969 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4970 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 4971 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4972 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 4973 | } |
| 4974 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4975 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4976 | |
| 4977 | if (isPrivileged(callerUid)) { |
| 4978 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4979 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 4980 | + callerUid + " privileged"); |
| 4981 | } |
| 4982 | return true; |
| 4983 | } |
| 4984 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 4985 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4986 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 4987 | + callerUid + " manages the account"); |
| 4988 | } |
| 4989 | return true; |
| 4990 | } |
| 4991 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 4992 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4993 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 4994 | + callerUid + " user granted access"); |
| 4995 | } |
| 4996 | return true; |
| 4997 | } |
| 4998 | |
| 4999 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5000 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5001 | } |
| 5002 | |
| 5003 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5004 | } |
| 5005 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5006 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5007 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5008 | if (accountType == null) { |
| 5009 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5010 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5011 | return getTypesVisibleToCaller(callingUid, userId, |
| 5012 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5013 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5016 | // Method checks visibility for applications targeing API level below {@link |
| 5017 | // android.os.Build.VERSION_CODES#O}, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5018 | // returns true if the the app has GET_ACCOUNTS or GET_ACCOUNTS_PRIVILEGED permission. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5019 | private boolean checkGetAccountsPermission(String packageName, int userId) { |
| 5020 | return isPermittedForPackage(packageName, userId, Manifest.permission.GET_ACCOUNTS, |
| 5021 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 5022 | } |
| 5023 | |
| 5024 | private boolean checkReadContactsPermission(String packageName, int userId) { |
| 5025 | return isPermittedForPackage(packageName, userId, Manifest.permission.READ_CONTACTS); |
| 5026 | } |
| 5027 | |
| 5028 | /** |
| 5029 | * Method checks package uid and signature with Authenticator which manages accountType. |
| 5030 | * |
| 5031 | * @return SIGNATURE_CHECK_UID_MATCH for uid match, SIGNATURE_CHECK_MATCH for signature match, |
| 5032 | * SIGNATURE_CHECK_MISMATCH otherwise. |
| 5033 | */ |
| 5034 | private int checkPackageSignature(String accountType, int callingUid, int userId) { |
| 5035 | if (accountType == null) { |
| 5036 | return SIGNATURE_CHECK_MISMATCH; |
| 5037 | } |
| 5038 | |
| 5039 | long identityToken = Binder.clearCallingIdentity(); |
| 5040 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5041 | try { |
| 5042 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5043 | } finally { |
| 5044 | Binder.restoreCallingIdentity(identityToken); |
| 5045 | } |
| 5046 | // Check for signature match with Authenticator. |
| 5047 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5048 | : serviceInfos) { |
| 5049 | if (accountType.equals(serviceInfo.type.type)) { |
| 5050 | if (serviceInfo.uid == callingUid) { |
| 5051 | return SIGNATURE_CHECK_UID_MATCH; |
| 5052 | } |
| 5053 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 5054 | if (sigChk == PackageManager.SIGNATURE_MATCH) { |
| 5055 | return SIGNATURE_CHECK_MATCH; |
| 5056 | } |
| 5057 | } |
| 5058 | } |
| 5059 | return SIGNATURE_CHECK_MISMATCH; |
| 5060 | } |
| 5061 | |
| 5062 | // returns true for applications with the same signature as authenticator. |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5063 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5064 | if (accountType == null) { |
| 5065 | return false; |
| 5066 | } else { |
| 5067 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5068 | } |
| 5069 | } |
| 5070 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5071 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5072 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5073 | return getTypesForCaller(callingUid, userId, true /* isOtherwisePermitted*/); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
Dmitry Dementyev | 2e22cfb | 2017-01-09 18:42:14 +0000 | [diff] [blame] | 5077 | return getTypesForCaller(callingUid, userId, false); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | private List<String> getTypesForCaller( |
| 5081 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5082 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5083 | long identityToken = Binder.clearCallingIdentity(); |
| 5084 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5085 | try { |
| 5086 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5087 | } finally { |
| 5088 | Binder.restoreCallingIdentity(identityToken); |
| 5089 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5090 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5091 | serviceInfos) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5092 | if (isOtherwisePermitted || (mPackageManager.checkSignatures(serviceInfo.uid, |
| 5093 | callingUid) == PackageManager.SIGNATURE_MATCH)) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5094 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5095 | } |
| 5096 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5097 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5098 | } |
| 5099 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5100 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5101 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5102 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5103 | if (account.name.equals(accountName)) { |
| 5104 | return true; |
| 5105 | } |
| 5106 | } |
| 5107 | } |
| 5108 | return false; |
| 5109 | } |
| 5110 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5111 | private static void checkManageUsersPermission(String message) { |
| 5112 | if (ActivityManager.checkComponentPermission( |
| 5113 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5114 | != PackageManager.PERMISSION_GRANTED) { |
| 5115 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5116 | } |
| 5117 | } |
| 5118 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5119 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5120 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5121 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5122 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5123 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5124 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5125 | + message); |
| 5126 | } |
| 5127 | } |
| 5128 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5129 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5130 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5131 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5132 | return true; |
| 5133 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5134 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5135 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5136 | long grantsCount; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5137 | if (authTokenType != null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5138 | grantsCount = accounts.accountsDb.findMatchingGrantsCount(callerUid, authTokenType, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5139 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5140 | } else { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5141 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5142 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5143 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5144 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5145 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5146 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5147 | // TODO: Skip this check when running automated tests. Replace this |
| 5148 | // with a more general solution. |
| 5149 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5150 | + authTokenType + " by uid " + callerUid |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5151 | + " but ignoring since device is in test harness."); |
| 5152 | return true; |
| 5153 | } |
| 5154 | return permissionGranted; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5155 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5156 | } |
| 5157 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5158 | private boolean isSystemUid(int callingUid) { |
| 5159 | String[] packages = null; |
| 5160 | long ident = Binder.clearCallingIdentity(); |
| 5161 | try { |
| 5162 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5163 | } finally { |
| 5164 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5165 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5166 | if (packages != null) { |
| 5167 | for (String name : packages) { |
| 5168 | try { |
| 5169 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5170 | if (packageInfo != null |
| 5171 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5172 | != 0) { |
| 5173 | return true; |
| 5174 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5175 | } catch (NameNotFoundException e) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5176 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5177 | } |
| 5178 | } |
| 5179 | } else { |
| 5180 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5181 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5182 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5185 | /** Succeeds if any of the specified permissions are granted. */ |
| 5186 | private void checkReadAccountsPermitted( |
| 5187 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5188 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5189 | int userId, |
| 5190 | String opPackageName) { |
| 5191 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5192 | String msg = String.format( |
| 5193 | "caller uid %s cannot access %s accounts", |
| 5194 | callingUid, |
| 5195 | accountType); |
| 5196 | Log.w(TAG, " " + msg); |
| 5197 | throw new SecurityException(msg); |
| 5198 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5199 | } |
| 5200 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5201 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5202 | // the managing app can always modify accounts |
| 5203 | if (isProfileOwner(callingUid)) { |
| 5204 | return true; |
| 5205 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5206 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5207 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5208 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5209 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5210 | return true; |
| 5211 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5212 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5213 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5214 | // the managing app can always modify accounts |
| 5215 | if (isProfileOwner(callingUid)) { |
| 5216 | return true; |
| 5217 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5218 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5219 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5220 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5221 | if (typesArray == null) { |
| 5222 | return true; |
| 5223 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5224 | for (String forbiddenType : typesArray) { |
| 5225 | if (forbiddenType.equals(accountType)) { |
| 5226 | return false; |
| 5227 | } |
| 5228 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5229 | return true; |
| 5230 | } |
| 5231 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5232 | private boolean isProfileOwner(int uid) { |
| 5233 | final DevicePolicyManagerInternal dpmi = |
| 5234 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5235 | return (dpmi != null) |
| 5236 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5237 | } |
| 5238 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5239 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5240 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5241 | throws RemoteException { |
| 5242 | final int callingUid = getCallingUid(); |
| 5243 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5244 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5245 | throw new SecurityException(); |
| 5246 | } |
| 5247 | |
| 5248 | if (value) { |
| 5249 | grantAppPermission(account, authTokenType, uid); |
| 5250 | } else { |
| 5251 | revokeAppPermission(account, authTokenType, uid); |
| 5252 | } |
| 5253 | } |
| 5254 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5255 | /** |
| 5256 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5257 | * <p> |
| 5258 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5259 | * which is in the system. This means we don't need to protect it with permissions. |
| 5260 | * @hide |
| 5261 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5262 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5263 | if (account == null || authTokenType == null) { |
| 5264 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5265 | return; |
| 5266 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5267 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5268 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5269 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5270 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5271 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5272 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5273 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5274 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5275 | |
| 5276 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5277 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5278 | |
| 5279 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5280 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5281 | : mAppPermissionChangeListeners) { |
| 5282 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5283 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | /** |
| 5287 | * Don't allow callers with the given uid permission to get credentials for |
| 5288 | * account/authTokenType. |
| 5289 | * <p> |
| 5290 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5291 | * which is in the system. This means we don't need to protect it with permissions. |
| 5292 | * @hide |
| 5293 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5294 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5295 | if (account == null || authTokenType == null) { |
| 5296 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5297 | return; |
| 5298 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5299 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5300 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5301 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5302 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5303 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5304 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5305 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5306 | accountId, authTokenType, uid); |
| 5307 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5308 | } |
| 5309 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5310 | accounts.accountsDb.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5311 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5312 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5313 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5314 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5315 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5316 | |
| 5317 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5318 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5319 | : mAppPermissionChangeListeners) { |
| 5320 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5321 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5322 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5323 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5324 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5325 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5326 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5327 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5328 | for (Account curAccount : oldAccountsForType) { |
| 5329 | if (!curAccount.equals(account)) { |
| 5330 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5331 | } |
| 5332 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5333 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5334 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5335 | } else { |
| 5336 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5337 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5338 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5339 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5340 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5341 | accounts.userDataCache.remove(account); |
| 5342 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5343 | accounts.previousNameCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | /** |
| 5347 | * This assumes that the caller has already checked that the account is not already present. |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5348 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5349 | * processes and if you will return this account to apps you should return the result. |
| 5350 | * @return The inserted account which is a new instance that is being tracked. |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5351 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5352 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5353 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5354 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5355 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5356 | if (accountsForType != null) { |
| 5357 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5358 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5359 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5360 | : UUID.randomUUID().toString(); |
| 5361 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5362 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5363 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5364 | } |
| 5365 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5366 | private Account[] filterAccounts(UserAccounts accounts, Account[] unfiltered, int callingUid, |
| 5367 | String callingPackage, boolean includeManagedNotVisible) { |
| 5368 | // filter based on visibility. |
| 5369 | Map<Account, Integer> firstPass = new HashMap<>(); |
| 5370 | for (Account account : unfiltered) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5371 | int visibility = resolveAccountVisibility(account, callingPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5372 | if ((visibility == AccountManager.VISIBILITY_VISIBLE |
| 5373 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) |
| 5374 | || (includeManagedNotVisible |
| 5375 | && (visibility |
| 5376 | == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE))) { |
| 5377 | firstPass.put(account, visibility); |
| 5378 | } |
| 5379 | } |
| 5380 | Map<Account, Integer> secondPass = |
| 5381 | filterSharedAccounts(accounts, firstPass, callingUid, callingPackage); |
| 5382 | |
| 5383 | Account[] filtered = new Account[secondPass.size()]; |
| 5384 | filtered = secondPass.keySet().toArray(filtered); |
| 5385 | return filtered; |
| 5386 | } |
| 5387 | |
| 5388 | private Map<Account, Integer> filterSharedAccounts(UserAccounts userAccounts, |
| 5389 | Map<Account, Integer> unfiltered, int callingUid, String callingPackage) { |
| 5390 | // first part is to filter shared accounts. |
| 5391 | // unfiltered type check is not necessary. |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5392 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5393 | || callingUid == Process.SYSTEM_UID) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5394 | return unfiltered; |
| 5395 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5396 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5397 | if (user != null && user.isRestricted()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5398 | String[] packages = |
| 5399 | mPackageManager.getPackagesForUid(callingUid); |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5400 | if (packages == null) { |
| 5401 | packages = new String[] {}; |
| 5402 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5403 | // If any of the packages is a visible listed package, return the full set, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5404 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5405 | // This might be a temporary way to specify a visible list |
| 5406 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5407 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5408 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5409 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5410 | return unfiltered; |
| 5411 | } |
| 5412 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5413 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5414 | if (ArrayUtils.isEmpty(sharedAccounts)) { |
| 5415 | return unfiltered; |
| 5416 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5417 | String requiredAccountType = ""; |
| 5418 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5419 | // If there's an explicit callingPackage specified, check if that package |
| 5420 | // opted in to see restricted accounts. |
| 5421 | if (callingPackage != null) { |
| 5422 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5423 | if (pi != null && pi.restrictedAccountType != null) { |
| 5424 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5425 | } |
| 5426 | } else { |
| 5427 | // Otherwise check if the callingUid has a package that has opted in |
| 5428 | for (String packageName : packages) { |
| 5429 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5430 | if (pi != null && pi.restrictedAccountType != null) { |
| 5431 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5432 | break; |
| 5433 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5434 | } |
| 5435 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5436 | } catch (NameNotFoundException e) { |
| 5437 | Log.d(TAG, "Package not found " + e.getMessage()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5438 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5439 | Map<Account, Integer> filtered = new HashMap<>(); |
| 5440 | for (Map.Entry<Account, Integer> entry : unfiltered.entrySet()) { |
| 5441 | Account account = entry.getKey(); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5442 | if (account.type.equals(requiredAccountType)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5443 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5444 | } else { |
| 5445 | boolean found = false; |
| 5446 | for (Account shared : sharedAccounts) { |
| 5447 | if (shared.equals(account)) { |
| 5448 | found = true; |
| 5449 | break; |
| 5450 | } |
| 5451 | } |
| 5452 | if (!found) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5453 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5454 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5455 | } |
| 5456 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5457 | return filtered; |
| 5458 | } else { |
| 5459 | return unfiltered; |
| 5460 | } |
| 5461 | } |
| 5462 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5463 | /* |
| 5464 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5465 | * that the package is not allowed to access. |
| 5466 | */ |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5467 | protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5468 | int callingUid, String callingPackage, boolean includeManagedNotVisible) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5469 | if (callingPackage == null) { |
| 5470 | callingPackage = getPackageNameForUid(callingUid); |
| 5471 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5472 | if (accountType != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5473 | final Account[] accounts = userAccounts.accountCache.get(accountType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5474 | if (accounts == null) { |
| 5475 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5476 | } else { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5477 | return filterAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
| 5478 | callingUid, callingPackage, includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5479 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5480 | } else { |
| 5481 | int totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5482 | for (Account[] accounts : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5483 | totalLength += accounts.length; |
| 5484 | } |
| 5485 | if (totalLength == 0) { |
| 5486 | return EMPTY_ACCOUNT_ARRAY; |
| 5487 | } |
| 5488 | Account[] accounts = new Account[totalLength]; |
| 5489 | totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5490 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5491 | System.arraycopy(accountsOfType, 0, accounts, totalLength, |
| 5492 | accountsOfType.length); |
| 5493 | totalLength += accountsOfType.length; |
| 5494 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5495 | return filterAccounts(userAccounts, accounts, callingUid, callingPackage, |
| 5496 | includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5497 | } |
| 5498 | } |
| 5499 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5500 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5501 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5502 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5503 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5504 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5505 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5506 | } |
| 5507 | if (value == null) { |
| 5508 | userDataForAccount.remove(key); |
| 5509 | } else { |
| 5510 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5511 | } |
| 5512 | } |
| 5513 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5514 | protected String readCachedTokenInternal( |
| 5515 | UserAccounts accounts, |
| 5516 | Account account, |
| 5517 | String tokenType, |
| 5518 | String callingPackage, |
| 5519 | byte[] pkgSigDigest) { |
| 5520 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5521 | return accounts.accountTokenCaches.get( |
| 5522 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5523 | } |
| 5524 | } |
| 5525 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5526 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5527 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5528 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5529 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5530 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5531 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5532 | } |
| 5533 | if (value == null) { |
| 5534 | authTokensForAccount.remove(key); |
| 5535 | } else { |
| 5536 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5537 | } |
| 5538 | } |
| 5539 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5540 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 5541 | String authTokenType) { |
| 5542 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5543 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5544 | if (authTokensForAccount == null) { |
| 5545 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5546 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5547 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5548 | } |
| 5549 | return authTokensForAccount.get(authTokenType); |
| 5550 | } |
| 5551 | } |
| 5552 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5553 | protected String readUserDataInternalLocked( |
| 5554 | UserAccounts accounts, Account account, String key) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5555 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5556 | if (userDataForAccount == null) { |
| 5557 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5558 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5559 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5560 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5561 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5562 | } |
| 5563 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5564 | private Context getContextForUser(UserHandle user) { |
| 5565 | try { |
| 5566 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 5567 | } catch (NameNotFoundException e) { |
| 5568 | // Default to mContext, not finding the package system is running as is unlikely. |
| 5569 | return mContext; |
| 5570 | } |
| 5571 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 5572 | |
| 5573 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 5574 | try { |
| 5575 | response.onResult(result); |
| 5576 | } catch (RemoteException e) { |
| 5577 | // if the caller is dead then there is no one to care about remote |
| 5578 | // exceptions |
| 5579 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5580 | Log.v(TAG, "failure while notifying response", e); |
| 5581 | } |
| 5582 | } |
| 5583 | } |
| 5584 | |
| 5585 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 5586 | String errorMessage) { |
| 5587 | try { |
| 5588 | response.onError(errorCode, errorMessage); |
| 5589 | } catch (RemoteException e) { |
| 5590 | // if the caller is dead then there is no one to care about remote |
| 5591 | // exceptions |
| 5592 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5593 | Log.v(TAG, "failure while notifying response", e); |
| 5594 | } |
| 5595 | } |
| 5596 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5597 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5598 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5599 | private final Object mLock = new Object(); |
| 5600 | |
| 5601 | @GuardedBy("mLock") |
| 5602 | private AccountManagerBackupHelper mBackupHelper; |
| 5603 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5604 | @Override |
| 5605 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 5606 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 5607 | if (account == null) { |
| 5608 | Slog.w(TAG, "account cannot be null"); |
| 5609 | return; |
| 5610 | } |
| 5611 | if (packageName == null) { |
| 5612 | Slog.w(TAG, "packageName cannot be null"); |
| 5613 | return; |
| 5614 | } |
| 5615 | if (userId < UserHandle.USER_SYSTEM) { |
| 5616 | Slog.w(TAG, "user id must be concrete"); |
| 5617 | return; |
| 5618 | } |
| 5619 | if (callback == null) { |
| 5620 | Slog.w(TAG, "callback cannot be null"); |
| 5621 | return; |
| 5622 | } |
| 5623 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5624 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 5625 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5626 | Bundle result = new Bundle(); |
| 5627 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 5628 | callback.sendResult(result); |
| 5629 | return; |
| 5630 | } |
| 5631 | |
| 5632 | final int uid; |
| 5633 | try { |
| 5634 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 5635 | } catch (NameNotFoundException e) { |
| 5636 | Slog.e(TAG, "Unknown package " + packageName); |
| 5637 | return; |
| 5638 | } |
| 5639 | |
| 5640 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5641 | final UserAccounts userAccounts; |
| 5642 | synchronized (mUsers) { |
| 5643 | userAccounts = mUsers.get(userId); |
| 5644 | } |
| 5645 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 5646 | } |
| 5647 | |
| 5648 | @Override |
| 5649 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 5650 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5651 | mAppPermissionChangeListeners.add(listener); |
| 5652 | } |
| 5653 | |
| 5654 | @Override |
| 5655 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 5656 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5657 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5658 | |
| 5659 | @Override |
| 5660 | public byte[] backupAccountAccessPermissions(int userId) { |
| 5661 | synchronized (mLock) { |
| 5662 | if (mBackupHelper == null) { |
| 5663 | mBackupHelper = new AccountManagerBackupHelper( |
| 5664 | AccountManagerService.this, this); |
| 5665 | } |
| 5666 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 5667 | } |
| 5668 | } |
| 5669 | |
| 5670 | @Override |
| 5671 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 5672 | synchronized (mLock) { |
| 5673 | if (mBackupHelper == null) { |
| 5674 | mBackupHelper = new AccountManagerBackupHelper( |
| 5675 | AccountManagerService.this, this); |
| 5676 | } |
| 5677 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 5678 | } |
| 5679 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5680 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5681 | |
| 5682 | @VisibleForTesting |
| 5683 | static class Injector { |
| 5684 | private final Context mContext; |
| 5685 | |
| 5686 | public Injector(Context context) { |
| 5687 | mContext = context; |
| 5688 | } |
| 5689 | |
| 5690 | Looper getMessageHandlerLooper() { |
| 5691 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 5692 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 5693 | serviceThread.start(); |
| 5694 | return serviceThread.getLooper(); |
| 5695 | } |
| 5696 | |
| 5697 | Context getContext() { |
| 5698 | return mContext; |
| 5699 | } |
| 5700 | |
| 5701 | void addLocalService(AccountManagerInternal service) { |
| 5702 | LocalServices.addService(AccountManagerInternal.class, service); |
| 5703 | } |
| 5704 | |
| 5705 | String getDeDatabaseName(int userId) { |
| 5706 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 5707 | AccountsDb.DE_DATABASE_NAME); |
| 5708 | return databaseFile.getPath(); |
| 5709 | } |
| 5710 | |
| 5711 | String getCeDatabaseName(int userId) { |
| 5712 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 5713 | AccountsDb.CE_DATABASE_NAME); |
| 5714 | return databaseFile.getPath(); |
| 5715 | } |
| 5716 | |
| 5717 | String getPreNDatabaseName(int userId) { |
| 5718 | File systemDir = Environment.getDataSystemDirectory(); |
| 5719 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 5720 | PRE_N_DATABASE_NAME); |
| 5721 | if (userId == 0) { |
| 5722 | // Migrate old file, if it exists, to the new location. |
| 5723 | // Make sure the new file doesn't already exist. A dummy file could have been |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5724 | // accidentally created in the old location, |
| 5725 | // causing the new one to become corrupted as well. |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5726 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 5727 | if (oldFile.exists() && !databaseFile.exists()) { |
| 5728 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 5729 | File userDir = Environment.getUserSystemDirectory(userId); |
| 5730 | if (!userDir.exists()) { |
| 5731 | if (!userDir.mkdirs()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5732 | throw new IllegalStateException( |
| 5733 | "User dir cannot be created: " + userDir); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5734 | } |
| 5735 | } |
| 5736 | if (!oldFile.renameTo(databaseFile)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5737 | throw new IllegalStateException( |
| 5738 | "User dir cannot be migrated: " + databaseFile); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5739 | } |
| 5740 | } |
| 5741 | } |
| 5742 | return databaseFile.getPath(); |
| 5743 | } |
| 5744 | |
| 5745 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 5746 | return new AccountAuthenticatorCache(mContext); |
| 5747 | } |
| 5748 | |
| 5749 | INotificationManager getNotificationManager() { |
| 5750 | return NotificationManager.getService(); |
| 5751 | } |
| 5752 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5753 | } |