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; |
| 25 | import android.accounts.AuthenticatorDescription; |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 26 | import android.accounts.CantAddAccountActivity; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 27 | import android.accounts.GrantCredentialsPermissionActivity; |
| 28 | import android.accounts.IAccountAuthenticator; |
| 29 | import android.accounts.IAccountAuthenticatorResponse; |
| 30 | import android.accounts.IAccountManager; |
| 31 | import android.accounts.IAccountManagerResponse; |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 32 | import android.annotation.NonNull; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 33 | import android.app.ActivityManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 34 | import android.app.ActivityManagerNative; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 35 | import android.app.AppGlobals; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 36 | import android.app.AppOpsManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 37 | import android.app.Notification; |
| 38 | import android.app.NotificationManager; |
| 39 | import android.app.PendingIntent; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 40 | import android.app.admin.DeviceAdminInfo; |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 41 | import android.app.admin.DevicePolicyManager; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 42 | import android.app.admin.DevicePolicyManagerInternal; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 43 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 44 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 45 | import android.content.ContentValues; |
| 46 | import android.content.Context; |
| 47 | import android.content.Intent; |
| 48 | import android.content.IntentFilter; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 49 | import android.content.ServiceConnection; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 50 | import android.content.pm.ActivityInfo; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 51 | import android.content.pm.ApplicationInfo; |
| 52 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 53 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 54 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 55 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 56 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 57 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 58 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 59 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 60 | import android.database.Cursor; |
| 61 | import android.database.DatabaseUtils; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 62 | import android.database.sqlite.SQLiteDatabase; |
| 63 | import android.database.sqlite.SQLiteOpenHelper; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 64 | import android.database.sqlite.SQLiteStatement; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 65 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 66 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 67 | import android.os.Environment; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 68 | import android.os.FileUtils; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 69 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 70 | import android.os.IBinder; |
| 71 | import android.os.Looper; |
| 72 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 73 | import android.os.Parcel; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 74 | import android.os.Process; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 75 | import android.os.RemoteException; |
| 76 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 77 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 78 | import android.os.UserManager; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 79 | import android.os.storage.StorageManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 80 | import android.text.TextUtils; |
| 81 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 82 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 83 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 84 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 85 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 86 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 87 | import com.android.internal.R; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 88 | import com.android.internal.annotations.VisibleForTesting; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 89 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 90 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 91 | import com.android.internal.util.Preconditions; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 92 | import com.android.server.FgThread; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 93 | import com.android.server.LocalServices; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 94 | import com.android.server.SystemService; |
| 95 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 96 | import com.google.android.collect.Lists; |
| 97 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 98 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 99 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 100 | import java.io.FileDescriptor; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 101 | import java.io.IOException; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 102 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 103 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 104 | import java.security.MessageDigest; |
| 105 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 106 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 107 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 108 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 109 | import java.util.Collection; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 110 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 111 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 112 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 113 | import java.util.LinkedHashMap; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 114 | import java.util.LinkedList; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 115 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 116 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 117 | import java.util.Map.Entry; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 118 | import java.util.Set; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 119 | import java.util.concurrent.atomic.AtomicInteger; |
| 120 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 121 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 122 | /** |
| 123 | * A system service that provides account, password, and authtoken management for all |
| 124 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 125 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 126 | * 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] | 127 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 128 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 129 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 130 | public class AccountManagerService |
| 131 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 132 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 133 | private static final String TAG = "AccountManagerService"; |
| 134 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 135 | public static class Lifecycle extends SystemService { |
| 136 | private AccountManagerService mService; |
| 137 | |
| 138 | public Lifecycle(Context context) { |
| 139 | super(context); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void onStart() { |
| 144 | mService = new AccountManagerService(getContext()); |
| 145 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onBootPhase(int phase) { |
| 150 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 151 | mService.systemReady(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void onUnlockUser(int userHandle) { |
| 157 | mService.onUnlockUser(userHandle); |
| 158 | } |
| 159 | } |
| 160 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 161 | private static final String DATABASE_NAME = "accounts.db"; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 162 | private static final int PRE_N_DATABASE_VERSION = 9; |
| 163 | private static final int CE_DATABASE_VERSION = 10; |
| 164 | private static final int DE_DATABASE_VERSION = 1; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 165 | |
| 166 | private static final int MAX_DEBUG_DB_SIZE = 64; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 167 | |
| 168 | private final Context mContext; |
| 169 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 170 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 171 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 172 | private UserManager mUserManager; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 173 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 174 | private final MessageHandler mMessageHandler; |
| 175 | |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 176 | /** |
| 177 | * Used to keep data read/write operations for logging purposes in a separate thread |
| 178 | * from main thread |
| 179 | */ |
| 180 | private final LinkedList<Runnable> mLogRecordRunnables = new LinkedList<Runnable>(); |
| 181 | private Thread mLogRecordThread; |
| 182 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 183 | // Messages that can be sent on mHandler |
| 184 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 185 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 186 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 187 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 188 | |
| 189 | private static final String TABLE_ACCOUNTS = "accounts"; |
| 190 | private static final String ACCOUNTS_ID = "_id"; |
| 191 | private static final String ACCOUNTS_NAME = "name"; |
| 192 | private static final String ACCOUNTS_TYPE = "type"; |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 193 | private static final String ACCOUNTS_TYPE_COUNT = "count(type)"; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 194 | private static final String ACCOUNTS_PASSWORD = "password"; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 195 | private static final String ACCOUNTS_PREVIOUS_NAME = "previous_name"; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 196 | private static final String ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS = |
| 197 | "last_password_entry_time_millis_epoch"; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 198 | |
| 199 | private static final String TABLE_AUTHTOKENS = "authtokens"; |
| 200 | private static final String AUTHTOKENS_ID = "_id"; |
| 201 | private static final String AUTHTOKENS_ACCOUNTS_ID = "accounts_id"; |
| 202 | private static final String AUTHTOKENS_TYPE = "type"; |
| 203 | private static final String AUTHTOKENS_AUTHTOKEN = "authtoken"; |
| 204 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 205 | private static final String TABLE_GRANTS = "grants"; |
| 206 | private static final String GRANTS_ACCOUNTS_ID = "accounts_id"; |
| 207 | private static final String GRANTS_AUTH_TOKEN_TYPE = "auth_token_type"; |
| 208 | private static final String GRANTS_GRANTEE_UID = "uid"; |
| 209 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 210 | private static final String TABLE_EXTRAS = "extras"; |
| 211 | private static final String EXTRAS_ID = "_id"; |
| 212 | private static final String EXTRAS_ACCOUNTS_ID = "accounts_id"; |
| 213 | private static final String EXTRAS_KEY = "key"; |
| 214 | private static final String EXTRAS_VALUE = "value"; |
| 215 | |
| 216 | private static final String TABLE_META = "meta"; |
| 217 | private static final String META_KEY = "key"; |
| 218 | private static final String META_VALUE = "value"; |
| 219 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 220 | private static final String TABLE_SHARED_ACCOUNTS = "shared_accounts"; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 221 | private static final String SHARED_ACCOUNTS_ID = "_id"; |
| 222 | |
| 223 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
| 224 | private static final String CE_DATABASE_NAME = "accounts_ce.db"; |
| 225 | private static final String DE_DATABASE_NAME = "accounts_de.db"; |
| 226 | private static final String CE_DB_PREFIX = "ceDb."; |
| 227 | private static final String CE_TABLE_ACCOUNTS = CE_DB_PREFIX + TABLE_ACCOUNTS; |
| 228 | private static final String CE_TABLE_AUTHTOKENS = CE_DB_PREFIX + TABLE_AUTHTOKENS; |
| 229 | private static final String CE_TABLE_EXTRAS = CE_DB_PREFIX + TABLE_EXTRAS; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 230 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 231 | private static final String[] ACCOUNT_TYPE_COUNT_PROJECTION = |
| 232 | new String[] { ACCOUNTS_TYPE, ACCOUNTS_TYPE_COUNT}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 233 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 234 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 235 | static { |
| 236 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
| 237 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 238 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 239 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 240 | private static final String COUNT_OF_MATCHING_GRANTS = "" |
| 241 | + "SELECT COUNT(*) FROM " + TABLE_GRANTS + ", " + TABLE_ACCOUNTS |
| 242 | + " WHERE " + GRANTS_ACCOUNTS_ID + "=" + ACCOUNTS_ID |
| 243 | + " AND " + GRANTS_GRANTEE_UID + "=?" |
| 244 | + " AND " + GRANTS_AUTH_TOKEN_TYPE + "=?" |
| 245 | + " AND " + ACCOUNTS_NAME + "=?" |
| 246 | + " AND " + ACCOUNTS_TYPE + "=?"; |
| 247 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 248 | private static final String SELECTION_AUTHTOKENS_BY_ACCOUNT = |
| 249 | AUTHTOKENS_ACCOUNTS_ID + "=(select _id FROM accounts WHERE name=? AND type=?)"; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 250 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 251 | private static final String[] COLUMNS_AUTHTOKENS_TYPE_AND_AUTHTOKEN = {AUTHTOKENS_TYPE, |
| 252 | AUTHTOKENS_AUTHTOKEN}; |
| 253 | |
| 254 | private static final String SELECTION_USERDATA_BY_ACCOUNT = |
| 255 | EXTRAS_ACCOUNTS_ID + "=(select _id FROM accounts WHERE name=? AND type=?)"; |
| 256 | private static final String[] COLUMNS_EXTRAS_KEY_AND_VALUE = {EXTRAS_KEY, EXTRAS_VALUE}; |
| 257 | |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 258 | private static final String META_KEY_FOR_AUTHENTICATOR_UID_FOR_TYPE_PREFIX = |
| 259 | "auth_uid_for_type:"; |
| 260 | private static final String META_KEY_DELIMITER = ":"; |
| 261 | private static final String SELECTION_META_BY_AUTHENTICATOR_TYPE = META_KEY + " LIKE ?"; |
| 262 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 263 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 264 | private final AtomicInteger mNotificationIds = new AtomicInteger(1); |
| 265 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 266 | private static final String NEW_ACCOUNT_VISIBLE = "android.accounts.NEW_ACCOUNT_VISIBLE"; |
| 267 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 268 | static class UserAccounts { |
| 269 | private final int userId; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 270 | private final DeDatabaseHelper openHelper; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 271 | private final HashMap<Pair<Pair<Account, String>, Integer>, Integer> |
| 272 | credentialsPermissionNotificationIds = |
| 273 | new HashMap<Pair<Pair<Account, String>, Integer>, Integer>(); |
| 274 | private final HashMap<Account, Integer> signinRequiredNotificationIds = |
| 275 | new HashMap<Account, Integer>(); |
| 276 | private final Object cacheLock = new Object(); |
| 277 | /** protected by the {@link #cacheLock} */ |
Amith Yamasani | b483a99 | 2012-05-22 13:14:25 -0700 | [diff] [blame] | 278 | private final HashMap<String, Account[]> accountCache = |
| 279 | new LinkedHashMap<String, Account[]>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 280 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 281 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 282 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 283 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 284 | |
| 285 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 286 | private final TokenCache accountTokenCaches = new TokenCache(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 287 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 288 | /** protected by the {@link #cacheLock} */ |
| 289 | private final Map<String, ArrayList<Integer>> mApplicationAccountRequestMappings = |
| 290 | new HashMap<>(); |
| 291 | |
| 292 | /* Together the below two Sparse Arrays serve as visible list. One maps UID to account |
| 293 | number. Another maps Account number to Account.*/ |
| 294 | |
| 295 | /** protected by the {@link #cacheLock} */ |
| 296 | private final SparseArray<ArrayList<Integer>> mVisibleListUidToMockAccountNumbers = |
| 297 | new SparseArray<>(); |
| 298 | |
| 299 | //TODO: Instead of using Mock Account IDs, use the actual account IDs. |
| 300 | /** protected by the {@link #cacheLock} */ |
| 301 | private final SparseArray<Account> mMockAccountIdToAccount = new SparseArray<>(); |
| 302 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 303 | /** |
| 304 | * protected by the {@link #cacheLock} |
| 305 | * |
| 306 | * Caches the previous names associated with an account. Previous names |
| 307 | * should be cached because we expect that when an Account is renamed, |
| 308 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 309 | * want to know if the accounts they care about have been renamed. |
| 310 | * |
| 311 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 312 | * we can distinguish between those accounts with no previous names and |
| 313 | * those whose previous names haven't been cached (yet). |
| 314 | */ |
| 315 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 316 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 317 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 318 | private int debugDbInsertionPoint = -1; |
| 319 | private SQLiteStatement statementForLogging; |
| 320 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 321 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 322 | this.userId = userId; |
| 323 | synchronized (cacheLock) { |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 324 | openHelper = DeDatabaseHelper.create(context, userId, preNDbFile, deDbFile); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 329 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 330 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 331 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 332 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 333 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 334 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 335 | /** |
| 336 | * This should only be called by system code. One should only call this after the service |
| 337 | * has started. |
| 338 | * @return a reference to the AccountManagerService instance |
| 339 | * @hide |
| 340 | */ |
| 341 | public static AccountManagerService getSingleton() { |
| 342 | return sThis.get(); |
| 343 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 344 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 345 | public AccountManagerService(Context context) { |
| 346 | this(context, context.getPackageManager(), new AccountAuthenticatorCache(context)); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 349 | public AccountManagerService(Context context, PackageManager packageManager, |
| 350 | IAccountAuthenticatorCache authenticatorCache) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 351 | mContext = context; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 352 | mPackageManager = packageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 353 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 354 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 355 | mMessageHandler = new MessageHandler(FgThread.get().getLooper()); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 356 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 357 | mAuthenticatorCache = authenticatorCache; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 358 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 359 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 360 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 361 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 362 | addRequestsForPreInstalledApplications(); |
| 363 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 364 | IntentFilter intentFilter = new IntentFilter(); |
| 365 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 366 | intentFilter.addDataScheme("package"); |
| 367 | mContext.registerReceiver(new BroadcastReceiver() { |
| 368 | @Override |
| 369 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 370 | // Don't delete accounts when updating a authenticator's |
| 371 | // package. |
| 372 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 373 | /* Purging data requires file io, don't block the main thread. This is probably |
| 374 | * less than ideal because we are introducing a race condition where old grants |
| 375 | * could be exercised until they are purged. But that race condition existed |
| 376 | * anyway with the broadcast receiver. |
| 377 | * |
| 378 | * Ideally, we would completely clear the cache, purge data from the database, |
| 379 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 380 | * large at this point. |
| 381 | */ |
| 382 | Runnable r = new Runnable() { |
| 383 | @Override |
| 384 | public void run() { |
| 385 | purgeOldGrantsAll(); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 386 | |
| 387 | /* clears application request's for account types supported */ |
| 388 | int uidOfUninstalledApplication = |
| 389 | intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 390 | if(uidOfUninstalledApplication != -1) { |
| 391 | clearRequestedAccountVisibility(uidOfUninstalledApplication, |
| 392 | getUserAccounts(UserHandle.getUserId( |
| 393 | uidOfUninstalledApplication))); |
| 394 | } |
| 395 | |
| 396 | /* removes visibility of previous UID of this uninstalled application*/ |
| 397 | removeAccountVisibilityAllAccounts(uidOfUninstalledApplication, |
| 398 | getUserAccounts(UserHandle.getUserId( |
| 399 | uidOfUninstalledApplication))); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 400 | } |
| 401 | }; |
| 402 | new Thread(r).start(); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 403 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 404 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 405 | } |
| 406 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 407 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 408 | IntentFilter packageAddedOrChangedFilter = new IntentFilter(); |
| 409 | intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 410 | packageAddedOrChangedFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 411 | packageAddedOrChangedFilter.addDataScheme("package"); |
| 412 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 413 | @Override |
| 414 | public void onReceive(Context context1, Intent intent) { |
| 415 | mMessageHandler.post(new Runnable() { |
| 416 | @Override |
| 417 | public void run() { |
| 418 | int uidOfInstalledApplication = |
| 419 | intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 420 | if(uidOfInstalledApplication != -1) { |
| 421 | registerAccountTypesSupported( |
| 422 | uidOfInstalledApplication, |
| 423 | getUserAccounts( |
| 424 | UserHandle.getUserId(uidOfInstalledApplication))); |
| 425 | } |
| 426 | } |
| 427 | }); |
| 428 | } |
| 429 | }, UserHandle.ALL, packageAddedOrChangedFilter, null, null); |
| 430 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 431 | IntentFilter userFilter = new IntentFilter(); |
| 432 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 433 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 434 | @Override |
| 435 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 436 | String action = intent.getAction(); |
| 437 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 438 | onUserRemoved(intent); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 439 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 440 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 441 | }, UserHandle.ALL, userFilter, null, null); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 444 | @Override |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 445 | public boolean addAccountExplicitlyWithUid(Account account, String password, Bundle extras, |
| 446 | int[] selectedUids) { |
| 447 | if(addAccountExplicitly(account,password,extras)) { |
| 448 | for(int thisUid : selectedUids) { |
| 449 | makeAccountVisible(account, thisUid); |
| 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | public int[] getRequestingUidsForType(String accountType) { |
| 458 | int callingUid = Binder.getCallingUid(); |
| 459 | if (!isAccountManagedByCaller(accountType, callingUid, UserHandle.getUserId(callingUid))) { |
| 460 | String msg = String.format( |
| 461 | "uid %s cannot get secrets for accounts of type: %s", |
| 462 | callingUid, |
| 463 | accountType); |
| 464 | throw new SecurityException(msg); |
| 465 | } |
| 466 | return getRequestingUidsForType(accountType, getUserAccounts( |
| 467 | UserHandle.getUserId(callingUid))); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Returns all UIDs for applications that requested the account type. This method |
| 472 | * is called indirectly by the Authenticator and AccountManager |
| 473 | * |
| 474 | * @param accountType authenticator would like to know the requesting apps of |
| 475 | * @param ua UserAccount that currently hosts the account and application |
| 476 | * |
| 477 | * @return ArrayList of all UIDs that support accounts of this |
| 478 | * account type that seek approval (to be used to know which accounts for |
| 479 | * the authenticator to include in addAccountExplicitly). Null if none. |
| 480 | */ |
| 481 | private int[] getRequestingUidsForType(String accountType, UserAccounts ua) { |
| 482 | synchronized(ua.cacheLock) { |
| 483 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 484 | ua.mApplicationAccountRequestMappings; |
| 485 | ArrayList<Integer> allUidsForAccountType = userApplicationAccountRequestMappings.get( |
| 486 | accountType); |
| 487 | if(allUidsForAccountType == null) { |
| 488 | return null; |
| 489 | } |
| 490 | int[] toReturn = new int[allUidsForAccountType.size()]; |
| 491 | for(int i = 0 ; i < toReturn.length ; i++) { |
| 492 | toReturn[i] = allUidsForAccountType.get(i); |
| 493 | } |
| 494 | return toReturn; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public boolean isAccountVisible(Account a, int uid) { |
| 500 | int callingUid = Binder.getCallingUid(); |
| 501 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 502 | String msg = String.format( |
| 503 | "uid %s cannot get secrets for accounts of type: %s", |
| 504 | callingUid, |
| 505 | a.type); |
| 506 | throw new SecurityException(msg); |
| 507 | } |
| 508 | return isAccountVisible(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Checks visibility of certain account of a process identified |
| 513 | * by a given UID. This is called by the Authenticator indirectly. |
| 514 | * |
| 515 | * @param a The account to check visibility of |
| 516 | * @param uid UID to check visibility of |
| 517 | * @param ua UserAccount that currently hosts the account and application |
| 518 | * |
| 519 | * @return True if application has access to the account |
| 520 | * |
| 521 | */ |
| 522 | private boolean isAccountVisible(Account a, int uid, UserAccounts ua) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 523 | if(isAccountManagedByCaller(a.type, uid, UserHandle.getUserId(uid))) { |
| 524 | return true; |
| 525 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 526 | int accountMapping = getMockAccountNumber(a, ua); |
| 527 | if(accountMapping < 0) { |
| 528 | return true; |
| 529 | } |
| 530 | synchronized(ua.cacheLock) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 531 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 532 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 533 | ua.mVisibleListUidToMockAccountNumbers; |
| 534 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 535 | int indexOfAccountMapping = userAcctIdToAcctMap.indexOfValueByValue(a); |
| 536 | return indexOfAccountMapping == -1 || (linkedAccountsToUid != null |
| 537 | && linkedAccountsToUid.contains(accountMapping)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | @Override |
| 542 | public boolean makeAccountVisible(Account a, int uid) { |
| 543 | int callingUid = Binder.getCallingUid(); |
| 544 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 545 | String msg = String.format( |
| 546 | "uid %s cannot get secrets for accounts of type: %s", |
| 547 | callingUid, |
| 548 | a.type); |
| 549 | throw new SecurityException(msg); |
| 550 | } |
| 551 | return makeAccountVisible(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Gives a certain UID, represented a application, access to an account. This method |
| 556 | * is called indirectly by the Authenticator. |
| 557 | * |
| 558 | * @param a Account to make visible |
| 559 | * @param uid to add visibility of the Account from |
| 560 | * @param ua UserAccount that currently hosts the account and application |
| 561 | * |
| 562 | * @return True if account made visible to application and was not previously visible. |
| 563 | */ |
| 564 | private boolean makeAccountVisible(Account a, int uid, UserAccounts ua) { |
| 565 | int accountMapping = getMockAccountNumber(a, ua); |
| 566 | if(accountMapping < 0) { |
| 567 | accountMapping = makeAccountNumber(a, ua); |
| 568 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 569 | synchronized(ua.cacheLock) { |
| 570 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 571 | ua.mVisibleListUidToMockAccountNumbers; |
| 572 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
| 573 | if(linkedAccountsToUid == null) { |
| 574 | linkedAccountsToUid = new ArrayList<>(); |
| 575 | linkedAccountsToUid.add(accountMapping); |
| 576 | userWlUidToMockAccountNums.put(uid, linkedAccountsToUid); |
| 577 | } else if(!linkedAccountsToUid.contains(accountMapping)) { |
| 578 | linkedAccountsToUid.add(accountMapping); |
| 579 | } else { |
| 580 | return false; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | String[] subPackages = mPackageManager.getPackagesForUid(uid); |
| 585 | if(subPackages != null) { |
| 586 | for(String subPackage : subPackages) { |
| 587 | sendNotification(subPackage, a); |
| 588 | } |
| 589 | } |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | @Override |
| 594 | public boolean removeAccountVisibility(Account a, int uid) { |
| 595 | int callingUid = Binder.getCallingUid(); |
| 596 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 597 | String msg = String.format( |
| 598 | "uid %s cannot get secrets for accounts of type: %s", |
| 599 | callingUid, |
| 600 | a.type); |
| 601 | throw new SecurityException(msg); |
| 602 | } |
| 603 | return removeAccountVisibility(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Removes visibility of certain account of a process identified |
| 608 | * by a given UID to an application. This is called directly by the |
| 609 | * AccountManager and indirectly by the Authenticator. |
| 610 | * |
| 611 | * @param a Account to remove visibility from |
| 612 | * @param uid UID to remove visibility of the Account from |
| 613 | * @param ua UserAccount that hosts the account and application |
| 614 | * |
| 615 | * @return True if application access to account removed and was previously visible. |
| 616 | */ |
| 617 | private boolean removeAccountVisibility(Account a, int uid, UserAccounts ua) { |
| 618 | int accountMapping = getMockAccountNumber(a, ua); |
| 619 | if(accountMapping < 0) { |
| 620 | return false; |
| 621 | } |
| 622 | synchronized(ua.cacheLock) { |
| 623 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 624 | ua.mVisibleListUidToMockAccountNumbers; |
| 625 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
| 626 | if(linkedAccountsToUid != null) { |
| 627 | boolean toReturn = linkedAccountsToUid.remove((Integer) accountMapping); |
| 628 | if(linkedAccountsToUid.size() == 0) { |
| 629 | userWlUidToMockAccountNums.remove(uid); |
| 630 | } |
| 631 | return toReturn; |
| 632 | } |
| 633 | } |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Registers an application's preferences for supported account types for login. This is |
| 639 | * a helper method of requestAccountVisibility and indirectly called by AccountManager. |
| 640 | * |
| 641 | * @param accountTypes account types third party app is willing to support |
| 642 | * @param uid of application requesting account visibility |
| 643 | * @param ua UserAccount that hosts the account and application |
| 644 | */ |
| 645 | private void addRequestedAccountsVisibility(String[] accountTypes, int uid, UserAccounts ua) { |
| 646 | synchronized(ua.cacheLock) { |
| 647 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 648 | ua.mApplicationAccountRequestMappings; |
| 649 | for(String accountType : accountTypes) { |
| 650 | ArrayList<Integer> accountUidAppList = userApplicationAccountRequestMappings |
| 651 | .get(accountType); |
| 652 | if(accountUidAppList == null) { |
| 653 | accountUidAppList = new ArrayList<>(); |
| 654 | accountUidAppList.add(uid); |
| 655 | userApplicationAccountRequestMappings.put(accountType, accountUidAppList); |
| 656 | } else if (!accountUidAppList.contains(uid)) { |
| 657 | accountUidAppList.add(uid); |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Registers the requested login account types requested by all the applications already |
| 665 | * installed on the device. |
| 666 | */ |
| 667 | private void addRequestsForPreInstalledApplications() { |
| 668 | List<PackageInfo> allInstalledPackages = mContext.getPackageManager(). |
| 669 | getInstalledPackages(0); |
| 670 | for(PackageInfo pi : allInstalledPackages) { |
| 671 | int currentUid = pi.applicationInfo.uid; |
| 672 | if(currentUid != -1) { |
| 673 | registerAccountTypesSupported(currentUid, |
| 674 | getUserAccounts(UserHandle.getUserId(currentUid))); |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Clears all preferences an application had for login account types it offered |
| 681 | * support for. This method is used by AccountManager after application is |
| 682 | * uninstalled. |
| 683 | * |
| 684 | * @param uid Uid of the application to clear account type preferences |
| 685 | * @param ua UserAccount that hosted the account and application |
| 686 | * |
| 687 | * @return true if any previous settings were overridden. |
| 688 | */ |
| 689 | private boolean clearRequestedAccountVisibility(int uid, UserAccounts ua) { |
| 690 | boolean accountsDeleted = false; |
| 691 | ArrayList<String> accountTypesToRemove = new ArrayList<>(); |
| 692 | synchronized(ua.cacheLock) { |
| 693 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 694 | ua.mApplicationAccountRequestMappings; |
| 695 | Set<Entry<String, ArrayList<Integer>>> accountTypeAppListEntries = |
| 696 | userApplicationAccountRequestMappings.entrySet(); |
| 697 | |
| 698 | for(Entry<String, ArrayList<Integer>> entry : accountTypeAppListEntries) { |
| 699 | ArrayList<Integer> supportedApps = entry.getValue(); |
| 700 | if(supportedApps.remove((Integer) uid)) { |
| 701 | accountsDeleted = true; |
| 702 | } |
| 703 | |
| 704 | if(supportedApps.isEmpty()) { |
| 705 | accountTypesToRemove.add(entry.getKey()); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | for(String s : accountTypesToRemove) { |
| 710 | userApplicationAccountRequestMappings.remove(s); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | return accountsDeleted; |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Retrieves the mock account number associated with an Account in order to later retrieve |
| 719 | * the account from the Integer-Account Mapping. An account number is not the same as |
| 720 | * accountId in the database. This method can be indirectly called by AccountManager and |
| 721 | * indirectly by the Authenticator. |
| 722 | * |
| 723 | * @param a account to retrieve account number mapping |
| 724 | * @param ua UserAccount that currently hosts the account and application |
| 725 | * |
| 726 | * @return account number affiliated with the Account in question. Negative number if none. |
| 727 | */ |
| 728 | private int getMockAccountNumber(Account a, UserAccounts ua) { |
| 729 | //TODO: Each account is linked to AccountId rather than generated mock account numbers |
| 730 | SparseArray<Account> userAcctIdToAcctMap = |
| 731 | ua.mMockAccountIdToAccount; |
| 732 | synchronized(ua.cacheLock) { |
| 733 | int indexOfAccount = userAcctIdToAcctMap.indexOfValueByValue(a); |
| 734 | if(indexOfAccount < 0) { |
| 735 | return -1; |
| 736 | } |
| 737 | return userAcctIdToAcctMap.keyAt(indexOfAccount); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * Returns a full list of accounts that a certain UID is allowed access |
| 743 | * based on the visible list entries. |
| 744 | * |
| 745 | * @param uid of application to retrieve visible listed accounts for |
| 746 | * @param ua UserAccount that currently hosts the account and application |
| 747 | * |
| 748 | * @return array of Account values that are accessible by the given uids |
| 749 | */ |
| 750 | private Account[] getVisibleListedAccounts(int uid, UserAccounts ua) { |
| 751 | ArrayList<Account> visibleListedAccounts = new ArrayList<>(); |
| 752 | synchronized(ua.cacheLock) { |
| 753 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 754 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 755 | ua.mVisibleListUidToMockAccountNumbers; |
| 756 | ArrayList<Integer> visibleListedUidAccountNumbers = |
| 757 | userWlUidToMockAccountNums.get(uid); |
| 758 | if(visibleListedUidAccountNumbers != null) { |
| 759 | for(Integer accountNumber : visibleListedUidAccountNumbers) { |
| 760 | Account currentAccount = userAcctIdToAcctMap.get(accountNumber); |
| 761 | visibleListedAccounts.add(currentAccount); |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | Account[] arrVisibleListedAccounts = new Account[visibleListedAccounts.size()]; |
| 766 | return visibleListedAccounts.toArray(arrVisibleListedAccounts); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Makes an account number for a given Account to be mapped to. |
| 771 | * This method is called by makeVisible if an Account does not have |
| 772 | * a mapping for the visible list. This method is thus indirectly |
| 773 | * called by the Authenticator. |
| 774 | * |
| 775 | * @param a account to make an account number mapping of |
| 776 | * @param ua UserAccount that currently hosts the account and application |
| 777 | * |
| 778 | * @return account number created to map to the given account |
| 779 | */ |
| 780 | // TODO: Remove this method and use accountId from DB. |
| 781 | private int makeAccountNumber(Account a, UserAccounts ua) { |
| 782 | synchronized(ua.cacheLock) { |
| 783 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 784 | int newAccountMapping = 0; |
| 785 | while(userAcctIdToAcctMap.get(newAccountMapping) != null) { |
| 786 | newAccountMapping++; |
| 787 | } |
| 788 | userAcctIdToAcctMap.put(newAccountMapping, a); |
| 789 | return newAccountMapping; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | |
| 794 | |
| 795 | /** |
| 796 | * Registers an application, represented by a UID, to support account types detailed in |
| 797 | * the applications manifest as well as allowing it to opt for notifications. |
| 798 | * |
| 799 | * @param uid UID of application |
| 800 | * @param ua UserAccount that currently hosts the account and application |
| 801 | */ |
| 802 | private void registerAccountTypesSupported(int uid, UserAccounts ua) { |
| 803 | /* Account types supported are drawn from the Android Manifest of the Application */ |
| 804 | String interestedPackages = null; |
| 805 | try { |
| 806 | String[] allPackages = mPackageManager.getPackagesForUid(uid); |
| 807 | for(String aPackage : allPackages) { |
| 808 | ApplicationInfo ai = mPackageManager.getApplicationInfo(aPackage, |
| 809 | PackageManager.GET_META_DATA); |
| 810 | Bundle b = ai.metaData; |
| 811 | if(b == null) { |
| 812 | return; |
| 813 | } |
| 814 | interestedPackages = b.getString("android.accounts.SupportedLoginTypes"); |
| 815 | } |
| 816 | } catch (PackageManager.NameNotFoundException e) { |
| 817 | Log.d("NameNotFoundException", e.getMessage()); |
| 818 | } |
| 819 | if(interestedPackages != null) { |
| 820 | /* request remote account types directly from here. Reads from Android Manifest */ |
| 821 | requestAccountVisibility(interestedPackages.split(";"), uid, ua); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * Allows AccountManager to register account types that an application has login |
| 827 | * support for. This method over-writes all of the application's previous settings |
| 828 | * for accounts it supported. |
| 829 | * |
| 830 | * @param accountTypes array of account types application wishes to support |
| 831 | * @param uid of application registering requested account types |
| 832 | * @param ua UserAccount that hosts the account and application |
| 833 | */ |
| 834 | private void requestAccountVisibility(String[] accountTypes, int uid, UserAccounts ua) { |
| 835 | if(accountTypes.length > 0) { |
| 836 | clearRequestedAccountVisibility(uid, ua); |
| 837 | addRequestedAccountsVisibility(accountTypes, uid, ua); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * Removes visibility of all Accounts to this particular UID. This is called when an |
| 843 | * application is uninstalled so another application that is installed with the same |
| 844 | * UID cannot access Accounts. This is called by AccountManager. |
| 845 | * |
| 846 | * @param uid of application to remove all Account visibility to |
| 847 | * @param ua UserAccount that hosts the current Account |
| 848 | */ |
| 849 | private void removeAccountVisibilityAllAccounts(int uid, UserAccounts ua) { |
| 850 | synchronized(ua.cacheLock) { |
| 851 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 852 | ua.mVisibleListUidToMockAccountNumbers; |
| 853 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 854 | ArrayList<Integer> allAccountNumbersList = userWlUidToMockAccountNums.get(uid); |
| 855 | if(allAccountNumbersList != null) { |
| 856 | Integer[] allAccountNumbers = allAccountNumbersList.toArray( |
| 857 | new Integer[allAccountNumbersList.size()]); |
| 858 | for(int accountNum : allAccountNumbers) { |
| 859 | removeAccountVisibility(userAcctIdToAcctMap.get(accountNum), uid, ua); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | /** |
| 866 | * Removes visible list functionality of a certain Account. |
| 867 | * This method is currently called by (1) addAccountExplicitly (as opposed to |
| 868 | * addAccountExplicitlyWithUid) and (2) removeAccountExplicitly. |
| 869 | * |
| 870 | * @param a the account to clear the visible list functionality for |
| 871 | * @param ua currently UserAccounts profile containing Account |
| 872 | * |
| 873 | * @return true if account previously had visible list functionality |
| 874 | */ |
| 875 | private boolean removeVisibleListFunctionality(Account a, UserAccounts ua) { |
| 876 | int mockAccountNum = getMockAccountNumber(a, ua); |
| 877 | if(mockAccountNum < 0) { |
| 878 | return false; |
| 879 | } |
| 880 | synchronized(ua.cacheLock) { |
| 881 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 882 | ua.mVisibleListUidToMockAccountNumbers; |
| 883 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 884 | |
| 885 | /* Removing mapping from account number to account removes visible list functionality*/ |
| 886 | userAcctIdToAcctMap.remove(mockAccountNum); |
| 887 | |
| 888 | for(int i = userWlUidToMockAccountNums.size() - 1 ; i >= 0 ; i--) { |
| 889 | int uidKey = userWlUidToMockAccountNums.keyAt(i); |
| 890 | ArrayList<Integer> allAccountNumbers = userWlUidToMockAccountNums.get(uidKey); |
| 891 | if(allAccountNumbers != null) { |
| 892 | allAccountNumbers.remove(mockAccountNum); |
| 893 | if(allAccountNumbers.isEmpty()) { |
| 894 | userWlUidToMockAccountNums.remove(uidKey); |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | return true; |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * Sends a direct intent to a package, notifying it of a visible account. This |
| 904 | * method is a helper method of makeAccountVisible. |
| 905 | * |
| 906 | * @param desiredPackage to send Account to |
| 907 | * @param visibleAccount to send to package |
| 908 | */ |
| 909 | private void sendNotification(String desiredPackage, Account visibleAccount) { |
| 910 | Intent intent = new Intent(); |
| 911 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 912 | intent.setAction(NEW_ACCOUNT_VISIBLE); |
| 913 | intent.setPackage(desiredPackage); |
| 914 | intent.putExtra("android.accounts.KEY_ACCOUNT", (Account) visibleAccount); |
| 915 | mContext.sendBroadcast(intent); |
| 916 | } |
| 917 | |
| 918 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 919 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 920 | throws RemoteException { |
| 921 | try { |
| 922 | return super.onTransact(code, data, reply, flags); |
| 923 | } catch (RuntimeException e) { |
| 924 | // The account manager only throws security exceptions, so let's |
| 925 | // log all others. |
| 926 | if (!(e instanceof SecurityException)) { |
| 927 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 928 | } |
| 929 | throw e; |
| 930 | } |
| 931 | } |
| 932 | |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 933 | public void systemReady() { |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 936 | private UserManager getUserManager() { |
| 937 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 938 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 939 | } |
| 940 | return mUserManager; |
| 941 | } |
| 942 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 943 | /** |
| 944 | * Validate internal set of accounts against installed authenticators for |
| 945 | * given user. Clears cached authenticators before validating. |
| 946 | */ |
| 947 | public void validateAccounts(int userId) { |
| 948 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 949 | // Invalidate user-specific cache to make sure we catch any |
| 950 | // removed authenticators. |
| 951 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * Validate internal set of accounts against installed authenticators for |
| 956 | * given user. Clear cached authenticators before validating when requested. |
| 957 | */ |
| 958 | private void validateAccountsInternal( |
| 959 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 960 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 961 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
| 962 | + " isCeDatabaseAttached=" + accounts.openHelper.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 963 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 964 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 965 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 966 | if (invalidateAuthenticatorCache) { |
| 967 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 968 | } |
| 969 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 970 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 971 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 972 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 973 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 974 | synchronized (accounts.cacheLock) { |
| 975 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 976 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 977 | |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 978 | // Get a map of stored authenticator types to UID |
| 979 | Map<String, Integer> metaAuthUid = AccountsDbUtils.findMetaAuthUid(db); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 980 | // Create a list of authenticator type whose previous uid no longer exists |
| 981 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 982 | SparseBooleanArray knownUids = null; |
| 983 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 984 | String type = authToUidEntry.getKey(); |
| 985 | int uid = authToUidEntry.getValue(); |
| 986 | Integer knownUid = knownAuth.get(type); |
| 987 | if (knownUid != null && uid == knownUid) { |
| 988 | // Remove it from the knownAuth list if it's unchanged. |
| 989 | knownAuth.remove(type); |
| 990 | } else { |
| 991 | /* |
| 992 | * The authenticator is presently not cached and should only be triggered |
| 993 | * when we think an authenticator has been removed (or is being updated). |
| 994 | * But we still want to check if any data with the associated uid is |
| 995 | * around. This is an (imperfect) signal that the package may be updating. |
| 996 | * |
| 997 | * A side effect of this is that an authenticator sharing a uid with |
| 998 | * multiple apps won't get its credentials wiped as long as some app with |
| 999 | * that uid is still on the device. But I suspect that this is a rare case. |
| 1000 | * And it isn't clear to me how an attacker could really exploit that |
| 1001 | * feature. |
| 1002 | * |
| 1003 | * The upshot is that we don't have to worry about accounts getting |
| 1004 | * uninstalled while the authenticator's package is being updated. |
| 1005 | * |
| 1006 | */ |
| 1007 | if (knownUids == null) { |
| 1008 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1009 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1010 | if (!knownUids.get(uid)) { |
| 1011 | // The authenticator is not presently available to the cache. And the |
| 1012 | // package no longer has a data directory (so we surmise it isn't updating). |
| 1013 | // So purge its data from the account databases. |
| 1014 | obsoleteAuthType.add(type); |
| 1015 | // And delete it from the TABLE_META |
| 1016 | AccountsDbUtils.deleteMetaByAuthTypeAndUid(db, type, uid); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1017 | } |
| 1018 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1021 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1022 | // 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] | 1023 | // values. |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1024 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
| 1025 | AccountsDbUtils.insertOrReplaceMetaAuthTypeAndUid(db, entry.getKey(), |
| 1026 | entry.getValue()); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1029 | final Map<Long, Account> accountsMap = AccountsDbUtils.findAllAccounts(db); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1030 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1031 | accounts.accountCache.clear(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1032 | final HashMap<String, ArrayList<String>> accountNamesByType = new LinkedHashMap<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1033 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1034 | final long accountId = accountEntry.getKey(); |
| 1035 | final Account account = accountEntry.getValue(); |
| 1036 | if (obsoleteAuthType.contains(account.type)) { |
| 1037 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 1038 | + account.type + "'s registered authenticator no longer exist."); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1039 | db.beginTransaction(); |
| 1040 | try { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1041 | AccountsDbUtils.deleteAccount(db, accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1042 | // Also delete from CE table if user is unlocked; if user is currently |
| 1043 | // locked the account will be removed later by syncDeCeAccountsLocked |
| 1044 | if (userUnlocked) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1045 | AccountsDbUtils.deleteCeAccount(db, accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1046 | } |
| 1047 | db.setTransactionSuccessful(); |
| 1048 | } finally { |
| 1049 | db.endTransaction(); |
| 1050 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1051 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1052 | |
| 1053 | logRecord(db, DebugDbHelper.ACTION_AUTHENTICATOR_REMOVE, TABLE_ACCOUNTS, |
| 1054 | accountId, accounts); |
| 1055 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1056 | accounts.userDataCache.remove(account); |
| 1057 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1058 | accounts.accountTokenCaches.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1059 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1060 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1061 | if (accountNames == null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1062 | accountNames = new ArrayList<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1063 | accountNamesByType.put(account.type, accountNames); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1064 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1065 | accountNames.add(account.name); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1066 | } |
| 1067 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1068 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1069 | final String accountType = cur.getKey(); |
| 1070 | final ArrayList<String> accountNames = cur.getValue(); |
| 1071 | final Account[] accountsForType = new Account[accountNames.size()]; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1072 | for (int i = 0; i < accountsForType.length; i++) { |
| 1073 | accountsForType[i] = new Account(accountNames.get(i), accountType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1074 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1075 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1076 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1077 | } finally { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1078 | if (accountDeleted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1079 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1080 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1081 | } |
| 1082 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1085 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1086 | // Get the UIDs of all apps that might have data on the device. We want |
| 1087 | // to preserve user data if the app might otherwise be storing data. |
| 1088 | List<PackageInfo> pkgsWithData = |
| 1089 | mPackageManager.getInstalledPackagesAsUser( |
| 1090 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1091 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1092 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1093 | if (pkgInfo.applicationInfo != null |
| 1094 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1095 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1096 | } |
| 1097 | } |
| 1098 | return knownUids; |
| 1099 | } |
| 1100 | |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1101 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1102 | Context context, |
| 1103 | int userId) { |
| 1104 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1105 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1106 | } |
| 1107 | |
| 1108 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1109 | IAccountAuthenticatorCache authCache, |
| 1110 | int userId) { |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1111 | HashMap<String, Integer> knownAuth = new HashMap<>(); |
| 1112 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1113 | .getAllServices(userId)) { |
| 1114 | knownAuth.put(service.type.type, service.uid); |
| 1115 | } |
| 1116 | return knownAuth; |
| 1117 | } |
| 1118 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1119 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1120 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | protected UserAccounts getUserAccounts(int userId) { |
| 1124 | synchronized (mUsers) { |
| 1125 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1126 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1127 | if (accounts == null) { |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1128 | File preNDbFile = new File(getPreNDatabaseName(userId)); |
| 1129 | File deDbFile = new File(getDeDatabaseName(userId)); |
| 1130 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1131 | initializeDebugDbSizeAndCompileSqlStatementForLogging( |
| 1132 | accounts.openHelper.getWritableDatabase(), accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1133 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1134 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1135 | validateAccounts = true; |
| 1136 | } |
| 1137 | // open CE database if necessary |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1138 | if (!accounts.openHelper.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1139 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
| 1140 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1141 | File preNDatabaseFile = new File(getPreNDatabaseName(userId)); |
| 1142 | File ceDatabaseFile = new File(getCeDatabaseName(userId)); |
| 1143 | CeDatabaseHelper.create(mContext, userId, preNDatabaseFile, ceDatabaseFile); |
| 1144 | accounts.openHelper.attachCeDatabase(ceDatabaseFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1145 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1146 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1147 | } |
| 1148 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1149 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1150 | } |
| 1151 | return accounts; |
| 1152 | } |
| 1153 | } |
| 1154 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1155 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1156 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
| 1157 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabaseUserIsUnlocked(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1158 | List<Account> accountsToRemove = AccountsDbUtils.findCeAccountsNotInDe(db); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1159 | if (!accountsToRemove.isEmpty()) { |
| 1160 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1161 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
| 1162 | logRecord(accounts, DebugDbHelper.ACTION_SYNC_DE_CE_ACCOUNTS, TABLE_ACCOUNTS); |
| 1163 | |
| 1164 | for (Account account : accountsToRemove) { |
| 1165 | removeAccountInternal(accounts, account, Process.myUid()); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1170 | private void purgeOldGrantsAll() { |
| 1171 | synchronized (mUsers) { |
| 1172 | for (int i = 0; i < mUsers.size(); i++) { |
| 1173 | purgeOldGrants(mUsers.valueAt(i)); |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | private void purgeOldGrants(UserAccounts accounts) { |
| 1179 | synchronized (accounts.cacheLock) { |
| 1180 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1181 | List<Integer> uids = AccountsDbUtils.findAllUidGrants(db); |
| 1182 | for (int uid : uids) { |
| 1183 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1184 | if (packageExists) { |
| 1185 | continue; |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1186 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1187 | Log.d(TAG, "deleting grants for UID " + uid |
| 1188 | + " because its package is no longer installed"); |
| 1189 | AccountsDbUtils.deleteGrantsByUid(db, uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1194 | private void onUserRemoved(Intent intent) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1195 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1196 | if (userId < 1) return; |
| 1197 | |
| 1198 | UserAccounts accounts; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1199 | boolean userUnlocked; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1200 | synchronized (mUsers) { |
| 1201 | accounts = mUsers.get(userId); |
| 1202 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1203 | userUnlocked = mLocalUnlockedUsers.get(userId); |
| 1204 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1205 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1206 | if (accounts != null) { |
| 1207 | synchronized (accounts.cacheLock) { |
| 1208 | accounts.openHelper.close(); |
| 1209 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1210 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1211 | Log.i(TAG, "Removing database files for user " + userId); |
| 1212 | File dbFile = new File(getDeDatabaseName(userId)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1213 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1214 | deleteDbFileWarnIfFailed(dbFile); |
| 1215 | // Remove CE file if user is unlocked, or FBE is not enabled |
| 1216 | boolean fbeEnabled = StorageManager.isFileEncryptedNativeOrEmulated(); |
| 1217 | if (!fbeEnabled || userUnlocked) { |
| 1218 | File ceDb = new File(getCeDatabaseName(userId)); |
| 1219 | if (ceDb.exists()) { |
| 1220 | deleteDbFileWarnIfFailed(ceDb); |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | private static void deleteDbFileWarnIfFailed(File dbFile) { |
| 1226 | if (!SQLiteDatabase.deleteDatabase(dbFile)) { |
| 1227 | Log.w(TAG, "Database at " + dbFile + " was not deleted successfully"); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1231 | @VisibleForTesting |
| 1232 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1233 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1234 | } |
| 1235 | |
| 1236 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1237 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1238 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1239 | } |
| 1240 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1241 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1242 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1243 | if (userId < 1) return; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1244 | syncSharedAccounts(userId); |
| 1245 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1246 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1247 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1248 | // Check if there's a shared account that needs to be created as an account |
| 1249 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1250 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1251 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1252 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1253 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1254 | : UserHandle.USER_SYSTEM; |
| 1255 | if (parentUserId < 0) { |
| 1256 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1257 | return; |
| 1258 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1259 | for (Account sa : sharedAccounts) { |
| 1260 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1261 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1262 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1266 | @Override |
| 1267 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1268 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1271 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1272 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1273 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1274 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1275 | Log.v(TAG, "getPassword: " + account |
| 1276 | + ", caller's uid " + Binder.getCallingUid() |
| 1277 | + ", pid " + Binder.getCallingPid()); |
| 1278 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1279 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1280 | int userId = UserHandle.getCallingUserId(); |
| 1281 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1282 | String msg = String.format( |
| 1283 | "uid %s cannot get secrets for accounts of type: %s", |
| 1284 | callingUid, |
| 1285 | account.type); |
| 1286 | throw new SecurityException(msg); |
| 1287 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1288 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1289 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1290 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1291 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1292 | } finally { |
| 1293 | restoreCallingIdentity(identityToken); |
| 1294 | } |
| 1295 | } |
| 1296 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1297 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1298 | if (account == null) { |
| 1299 | return null; |
| 1300 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1301 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1302 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1303 | return null; |
| 1304 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1305 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1306 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1307 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabaseUserIsUnlocked(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1308 | return AccountsDbUtils.findAccountPasswordByNameAndType(db, account.name, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1309 | account.type); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1313 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1314 | public String getPreviousName(Account account) { |
| 1315 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1316 | Log.v(TAG, "getPreviousName: " + account |
| 1317 | + ", caller's uid " + Binder.getCallingUid() |
| 1318 | + ", pid " + Binder.getCallingPid()); |
| 1319 | } |
| 1320 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1321 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1322 | long identityToken = clearCallingIdentity(); |
| 1323 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1324 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1325 | return readPreviousNameInternal(accounts, account); |
| 1326 | } finally { |
| 1327 | restoreCallingIdentity(identityToken); |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1332 | if (account == null) { |
| 1333 | return null; |
| 1334 | } |
| 1335 | synchronized (accounts.cacheLock) { |
| 1336 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1337 | if (previousNameRef == null) { |
| 1338 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1339 | String previousName = AccountsDbUtils.findAccountPreviousName(db, account); |
| 1340 | previousNameRef = new AtomicReference<>(previousName); |
| 1341 | accounts.previousNameCache.put(account, previousNameRef); |
| 1342 | return previousName; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1343 | } else { |
| 1344 | return previousNameRef.get(); |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1350 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1351 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1352 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1353 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1354 | account, key, callingUid, Binder.getCallingPid()); |
| 1355 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1356 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1357 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1358 | if (key == null) throw new IllegalArgumentException("key is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1359 | int userId = UserHandle.getCallingUserId(); |
| 1360 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1361 | String msg = String.format( |
| 1362 | "uid %s cannot get user data for accounts of type: %s", |
| 1363 | callingUid, |
| 1364 | account.type); |
| 1365 | throw new SecurityException(msg); |
| 1366 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1367 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1368 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1369 | return null; |
| 1370 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1371 | long identityToken = clearCallingIdentity(); |
| 1372 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1373 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1374 | synchronized (accounts.cacheLock) { |
| 1375 | if (!accountExistsCacheLocked(accounts, account)) { |
| 1376 | return null; |
| 1377 | } |
| 1378 | return readUserDataInternalLocked(accounts, account, key); |
| 1379 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1380 | } finally { |
| 1381 | restoreCallingIdentity(identityToken); |
| 1382 | } |
| 1383 | } |
| 1384 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1385 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1386 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1387 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1388 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1389 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1390 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1391 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1392 | + ", pid " + Binder.getCallingPid()); |
| 1393 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1394 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1395 | if (isCrossUser(callingUid, userId)) { |
| 1396 | throw new SecurityException( |
| 1397 | String.format( |
| 1398 | "User %s tying to get authenticator types for %s" , |
| 1399 | UserHandle.getCallingUserId(), |
| 1400 | userId)); |
| 1401 | } |
| 1402 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1403 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1404 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1405 | return getAuthenticatorTypesInternal(userId); |
| 1406 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1407 | } finally { |
| 1408 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1409 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1412 | /** |
| 1413 | * Should only be called inside of a clearCallingIdentity block. |
| 1414 | */ |
| 1415 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
| 1416 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1417 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1418 | AuthenticatorDescription[] types = |
| 1419 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1420 | int i = 0; |
| 1421 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1422 | : authenticatorCollection) { |
| 1423 | types[i] = authenticator.type; |
| 1424 | i++; |
| 1425 | } |
| 1426 | return types; |
| 1427 | } |
| 1428 | |
| 1429 | |
| 1430 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1431 | private boolean isCrossUser(int callingUid, int userId) { |
| 1432 | return (userId != UserHandle.getCallingUserId() |
| 1433 | && callingUid != Process.myUid() |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1434 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1435 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1436 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1437 | } |
| 1438 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1439 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1440 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1441 | Bundle.setDefusable(extras, true); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1442 | // clears the visible list functionality for this account because this method allows |
| 1443 | // default account access to all applications for account. |
| 1444 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1445 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1446 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1447 | Log.v(TAG, "addAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1448 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1449 | + ", pid " + Binder.getCallingPid()); |
| 1450 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1451 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1452 | int userId = UserHandle.getCallingUserId(); |
| 1453 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1454 | String msg = String.format( |
| 1455 | "uid %s cannot explicitly add accounts of type: %s", |
| 1456 | callingUid, |
| 1457 | account.type); |
| 1458 | throw new SecurityException(msg); |
| 1459 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1460 | removeVisibleListFunctionality(account, getUserAccounts(UserHandle.getUserId(callingUid))); |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1461 | /* |
| 1462 | * Child users are not allowed to add accounts. Only the accounts that are |
| 1463 | * shared by the parent profile can be added to child profile. |
| 1464 | * |
| 1465 | * TODO: Only allow accounts that were shared to be added by |
| 1466 | * a limited user. |
| 1467 | */ |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1468 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1469 | // fails if the account already exists |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1470 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1471 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1472 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1473 | return addAccountInternal(accounts, account, password, extras, callingUid); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1474 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1475 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1479 | @Override |
| 1480 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1481 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1482 | int callingUid = Binder.getCallingUid(); |
| 1483 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1484 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1485 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1486 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1487 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1488 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1489 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1490 | if (response != null) { |
| 1491 | Bundle result = new Bundle(); |
| 1492 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1493 | try { |
| 1494 | response.onResult(result); |
| 1495 | } catch (RemoteException e) { |
| 1496 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1497 | } |
| 1498 | } |
| 1499 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1500 | } |
| 1501 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1502 | Slog.d(TAG, "Copying account " + account.name |
| 1503 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1504 | long identityToken = clearCallingIdentity(); |
| 1505 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1506 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1507 | false /* stripAuthTokenFromResult */, account.name, |
| 1508 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1509 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1510 | protected String toDebugString(long now) { |
| 1511 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1512 | + ", " + account.type; |
| 1513 | } |
| 1514 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1515 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1516 | public void run() throws RemoteException { |
| 1517 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1518 | } |
| 1519 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1520 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1521 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1522 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1523 | if (result != null |
| 1524 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1525 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1526 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1527 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1528 | super.onResult(result); |
| 1529 | } |
| 1530 | } |
| 1531 | }.bind(); |
| 1532 | } finally { |
| 1533 | restoreCallingIdentity(identityToken); |
| 1534 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1537 | @Override |
| 1538 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1539 | final int callingUid = Binder.getCallingUid(); |
| 1540 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1541 | String msg = String.format( |
| 1542 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1543 | account, |
| 1544 | callingUid); |
| 1545 | Log.v(TAG, msg); |
| 1546 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1547 | if (account == null) { |
| 1548 | throw new IllegalArgumentException("account is null"); |
| 1549 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1550 | int userId = UserHandle.getCallingUserId(); |
| 1551 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1552 | String msg = String.format( |
| 1553 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1554 | callingUid, |
| 1555 | account.type); |
| 1556 | throw new SecurityException(msg); |
| 1557 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1558 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1559 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1560 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1561 | return false; |
| 1562 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1563 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1564 | long identityToken = clearCallingIdentity(); |
| 1565 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1566 | UserAccounts accounts = getUserAccounts(userId); |
| 1567 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1568 | } finally { |
| 1569 | restoreCallingIdentity(identityToken); |
| 1570 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1574 | final UserAccounts accounts = getUserAccountsForCaller(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1575 | synchronized (accounts.cacheLock) { |
| 1576 | final ContentValues values = new ContentValues(); |
| 1577 | values.put(ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS, System.currentTimeMillis()); |
| 1578 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 1579 | int i = db.update( |
| 1580 | TABLE_ACCOUNTS, |
| 1581 | values, |
| 1582 | ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE + "=?", |
| 1583 | new String[] { |
| 1584 | account.name, account.type |
| 1585 | }); |
| 1586 | if (i > 0) { |
| 1587 | return true; |
| 1588 | } |
| 1589 | } |
| 1590 | return false; |
| 1591 | } |
| 1592 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1593 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1594 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1595 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1596 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1597 | long id = clearCallingIdentity(); |
| 1598 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1599 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1600 | false /* stripAuthTokenFromResult */, account.name, |
| 1601 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1602 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1603 | protected String toDebugString(long now) { |
| 1604 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1605 | + ", " + account.type; |
| 1606 | } |
| 1607 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1608 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1609 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1610 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1611 | UserAccounts owner = getUserAccounts(parentUserId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1612 | synchronized (owner.cacheLock) { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1613 | for (Account acc : getAccounts(parentUserId, |
| 1614 | mContext.getOpPackageName())) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1615 | if (acc.equals(account)) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1616 | mAuthenticator.addAccountFromCredentials( |
| 1617 | this, account, accountCredentials); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1618 | break; |
| 1619 | } |
| 1620 | } |
| 1621 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1622 | } |
| 1623 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1624 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1625 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1626 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1627 | // TODO: Anything to do if if succedded? |
| 1628 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1629 | // account to avoid retries? |
| 1630 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1633 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1634 | public void onError(int errorCode, String errorMessage) { |
| 1635 | super.onError(errorCode, errorMessage); |
| 1636 | // TODO: Show error notification to user |
| 1637 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1638 | } |
| 1639 | |
| 1640 | }.bind(); |
| 1641 | } finally { |
| 1642 | restoreCallingIdentity(id); |
| 1643 | } |
| 1644 | } |
| 1645 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1646 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1647 | Bundle extras, int callingUid) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1648 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1649 | if (account == null) { |
| 1650 | return false; |
| 1651 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1652 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1653 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1654 | + " is locked. callingUid=" + callingUid); |
| 1655 | return false; |
| 1656 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1657 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1658 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1659 | db.beginTransaction(); |
| 1660 | try { |
| 1661 | long numMatches = DatabaseUtils.longForQuery(db, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1662 | "select count(*) from " + CE_TABLE_ACCOUNTS |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1663 | + " WHERE " + ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 1664 | new String[]{account.name, account.type}); |
| 1665 | if (numMatches > 0) { |
| 1666 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1667 | + ", skipping since the account already exists"); |
| 1668 | return false; |
| 1669 | } |
| 1670 | ContentValues values = new ContentValues(); |
| 1671 | values.put(ACCOUNTS_NAME, account.name); |
| 1672 | values.put(ACCOUNTS_TYPE, account.type); |
| 1673 | values.put(ACCOUNTS_PASSWORD, password); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1674 | long accountId = db.insert(CE_TABLE_ACCOUNTS, ACCOUNTS_NAME, values); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1675 | if (accountId < 0) { |
| 1676 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1677 | + ", skipping the DB insert failed"); |
| 1678 | return false; |
| 1679 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1680 | // Insert into DE table |
| 1681 | values = new ContentValues(); |
| 1682 | values.put(ACCOUNTS_ID, accountId); |
| 1683 | values.put(ACCOUNTS_NAME, account.name); |
| 1684 | values.put(ACCOUNTS_TYPE, account.type); |
| 1685 | values.put(ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS, |
| 1686 | System.currentTimeMillis()); |
| 1687 | if (db.insert(TABLE_ACCOUNTS, ACCOUNTS_NAME, values) < 0) { |
| 1688 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1689 | + ", skipping the DB insert failed"); |
| 1690 | return false; |
| 1691 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1692 | if (extras != null) { |
| 1693 | for (String key : extras.keySet()) { |
| 1694 | final String value = extras.getString(key); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1695 | if (AccountsDbUtils.insertExtra(db, accountId, key, value) < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1696 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1697 | + ", skipping since insertExtra failed for key " + key); |
| 1698 | return false; |
| 1699 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1700 | } |
| 1701 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1702 | db.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1703 | |
| 1704 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_ADD, TABLE_ACCOUNTS, accountId, |
| 1705 | accounts, callingUid); |
| 1706 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1707 | insertAccountIntoCacheLocked(accounts, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1708 | } finally { |
| 1709 | db.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1710 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1711 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1712 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1713 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1714 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1715 | |
| 1716 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1717 | sendAccountsChangedBroadcast(accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1718 | return true; |
| 1719 | } |
| 1720 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1721 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1722 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1723 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1727 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1728 | * 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] | 1729 | * running, then clone the account too. |
| 1730 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1731 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1732 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1733 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1734 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1735 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1736 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1737 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1738 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1739 | mMessageHandler.sendMessage(mMessageHandler.obtainMessage( |
| 1740 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1741 | } |
| 1742 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1746 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1747 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1748 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1749 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1750 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1751 | Log.v(TAG, "hasFeatures: " + account |
| 1752 | + ", response " + response |
| 1753 | + ", features " + stringArrayToString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1754 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1755 | + ", pid " + Binder.getCallingPid()); |
| 1756 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1757 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1758 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1759 | if (features == null) throw new IllegalArgumentException("features is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1760 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1761 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1762 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1763 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1764 | long identityToken = clearCallingIdentity(); |
| 1765 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1766 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1767 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1768 | } finally { |
| 1769 | restoreCallingIdentity(identityToken); |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | private class TestFeaturesSession extends Session { |
| 1774 | private final String[] mFeatures; |
| 1775 | private final Account mAccount; |
| 1776 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1777 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1778 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1779 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1780 | true /* stripAuthTokenFromResult */, account.name, |
| 1781 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1782 | mFeatures = features; |
| 1783 | mAccount = account; |
| 1784 | } |
| 1785 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1786 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1787 | public void run() throws RemoteException { |
| 1788 | try { |
| 1789 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1790 | } catch (RemoteException e) { |
| 1791 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1792 | } |
| 1793 | } |
| 1794 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1795 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1796 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1797 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1798 | IAccountManagerResponse response = getResponseAndClose(); |
| 1799 | if (response != null) { |
| 1800 | try { |
| 1801 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1802 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1803 | return; |
| 1804 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1805 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1806 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1807 | + response); |
| 1808 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1809 | final Bundle newResult = new Bundle(); |
| 1810 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1811 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1812 | response.onResult(newResult); |
| 1813 | } catch (RemoteException e) { |
| 1814 | // if the caller is dead then there is no one to care about remote exceptions |
| 1815 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1816 | Log.v(TAG, "failure while notifying response", e); |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | } |
| 1821 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1822 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1823 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1824 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1825 | + ", " + mAccount |
| 1826 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1827 | } |
| 1828 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1829 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1830 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1831 | public void renameAccount( |
| 1832 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1833 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1834 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1835 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1836 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1837 | + ", pid " + Binder.getCallingPid()); |
| 1838 | } |
| 1839 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1840 | int userId = UserHandle.getCallingUserId(); |
| 1841 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1842 | String msg = String.format( |
| 1843 | "uid %s cannot rename accounts of type: %s", |
| 1844 | callingUid, |
| 1845 | accountToRename.type); |
| 1846 | throw new SecurityException(msg); |
| 1847 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1848 | long identityToken = clearCallingIdentity(); |
| 1849 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1850 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1851 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1852 | Bundle result = new Bundle(); |
| 1853 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1854 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
| 1855 | try { |
| 1856 | response.onResult(result); |
| 1857 | } catch (RemoteException e) { |
| 1858 | Log.w(TAG, e.getMessage()); |
| 1859 | } |
| 1860 | } finally { |
| 1861 | restoreCallingIdentity(identityToken); |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1866 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1867 | Account resultAccount = null; |
| 1868 | /* |
| 1869 | * Cancel existing notifications. Let authenticators |
| 1870 | * re-post notifications as required. But we don't know if |
| 1871 | * the authenticators have bound their notifications to |
| 1872 | * now stale account name data. |
| 1873 | * |
| 1874 | * With a rename api, we might not need to do this anymore but it |
| 1875 | * shouldn't hurt. |
| 1876 | */ |
| 1877 | cancelNotification( |
| 1878 | getSigninRequiredNotificationId(accounts, accountToRename), |
| 1879 | new UserHandle(accounts.userId)); |
| 1880 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1881 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1882 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1883 | if (accountToRename.equals(pair.first.first)) { |
| 1884 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
| 1885 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1886 | } |
| 1887 | } |
| 1888 | } |
| 1889 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1890 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1891 | db.beginTransaction(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1892 | Account renamedAccount = new Account(newName, accountToRename.type); |
| 1893 | try { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1894 | final long accountId = AccountsDbUtils.findAccountId(db, accountToRename); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1895 | if (accountId >= 0) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1896 | final ContentValues values = new ContentValues(); |
| 1897 | values.put(ACCOUNTS_NAME, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1898 | final String[] argsAccountId = { String.valueOf(accountId) }; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1899 | db.update(CE_TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", argsAccountId); |
| 1900 | // Update NAME/PREVIOUS_NAME in DE accounts table |
| 1901 | values.put(ACCOUNTS_PREVIOUS_NAME, accountToRename.name); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1902 | db.update(TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", argsAccountId); |
| 1903 | db.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1904 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_RENAME, TABLE_ACCOUNTS, accountId, |
| 1905 | accounts); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1906 | } |
| 1907 | } finally { |
| 1908 | db.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1909 | } |
| 1910 | /* |
| 1911 | * Database transaction was successful. Clean up cached |
| 1912 | * data associated with the account in the user profile. |
| 1913 | */ |
| 1914 | insertAccountIntoCacheLocked(accounts, renamedAccount); |
| 1915 | /* |
| 1916 | * Extract the data and token caches before removing the |
| 1917 | * old account to preserve the user data associated with |
| 1918 | * the account. |
| 1919 | */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1920 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1921 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1922 | removeAccountFromCacheLocked(accounts, accountToRename); |
| 1923 | /* |
| 1924 | * Update the cached data associated with the renamed |
| 1925 | * account. |
| 1926 | */ |
| 1927 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1928 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 1929 | accounts.previousNameCache.put( |
| 1930 | renamedAccount, |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1931 | new AtomicReference<>(accountToRename.name)); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1932 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1933 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1934 | int parentUserId = accounts.userId; |
| 1935 | if (canHaveProfile(parentUserId)) { |
| 1936 | /* |
| 1937 | * Owner or system user account was renamed, rename the account for |
| 1938 | * those users with which the account was shared. |
| 1939 | */ |
| 1940 | List<UserInfo> users = getUserManager().getUsers(true); |
| 1941 | for (UserInfo user : users) { |
| 1942 | if (user.isRestricted() |
| 1943 | && (user.restrictedProfileParentId == parentUserId)) { |
| 1944 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1945 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1946 | } |
| 1947 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1948 | sendAccountsChangedBroadcast(accounts.userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1949 | } |
| 1950 | return resultAccount; |
| 1951 | } |
| 1952 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1953 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1954 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1955 | return userInfo != null && userInfo.canHaveProfile(); |
| 1956 | } |
| 1957 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1958 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1959 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1960 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1961 | removeAccountAsUser( |
| 1962 | response, |
| 1963 | account, |
| 1964 | expectActivityLaunch, |
| 1965 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | @Override |
| 1969 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1970 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1971 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1972 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1973 | Log.v(TAG, "removeAccount: " + account |
| 1974 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1975 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1976 | + ", pid " + Binder.getCallingPid() |
| 1977 | + ", for user id " + userId); |
| 1978 | } |
| 1979 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1980 | if (account == null) throw new IllegalArgumentException("account is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1981 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1982 | if (isCrossUser(callingUid, userId)) { |
| 1983 | throw new SecurityException( |
| 1984 | String.format( |
| 1985 | "User %s tying remove account for %s" , |
| 1986 | UserHandle.getCallingUserId(), |
| 1987 | userId)); |
| 1988 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1989 | /* |
| 1990 | * Only the system or authenticator should be allowed to remove accounts for that |
| 1991 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 1992 | * arbitrary applications (like competing authenticators). |
| 1993 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1994 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 1995 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 1996 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1997 | String msg = String.format( |
| 1998 | "uid %s cannot remove accounts of type: %s", |
| 1999 | callingUid, |
| 2000 | account.type); |
| 2001 | throw new SecurityException(msg); |
| 2002 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2003 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2004 | try { |
| 2005 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2006 | "User cannot modify accounts"); |
| 2007 | } catch (RemoteException re) { |
| 2008 | } |
| 2009 | return; |
| 2010 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2011 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2012 | try { |
| 2013 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2014 | "User cannot modify accounts of this type (policy)."); |
| 2015 | } catch (RemoteException re) { |
| 2016 | } |
| 2017 | return; |
| 2018 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2019 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2020 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2021 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2022 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2023 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2024 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2025 | if (account.equals(pair.first.first)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2026 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2027 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2028 | } |
| 2029 | } |
| 2030 | } |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2031 | SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2032 | final long accountId = AccountsDbUtils.findAccountId(db, account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2033 | logRecord( |
| 2034 | db, |
| 2035 | DebugDbHelper.ACTION_CALLED_ACCOUNT_REMOVE, |
| 2036 | TABLE_ACCOUNTS, |
| 2037 | accountId, |
| 2038 | accounts, |
| 2039 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2040 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2041 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2042 | } finally { |
| 2043 | restoreCallingIdentity(identityToken); |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | @Override |
| 2048 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2049 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2050 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2051 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2052 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2053 | + ", pid " + Binder.getCallingPid()); |
| 2054 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2055 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2056 | if (account == null) { |
| 2057 | /* |
| 2058 | * Null accounts should result in returning false, as per |
| 2059 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2060 | */ |
| 2061 | Log.e(TAG, "account is null"); |
| 2062 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2063 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2064 | String msg = String.format( |
| 2065 | "uid %s cannot explicitly add accounts of type: %s", |
| 2066 | callingUid, |
| 2067 | account.type); |
| 2068 | throw new SecurityException(msg); |
| 2069 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 2070 | removeVisibleListFunctionality(account, getUserAccounts(UserHandle.getUserId(callingUid))); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2071 | UserAccounts accounts = getUserAccountsForCaller(); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2072 | SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2073 | final long accountId = AccountsDbUtils.findAccountId(db, account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2074 | logRecord( |
| 2075 | db, |
| 2076 | DebugDbHelper.ACTION_CALLED_ACCOUNT_REMOVE, |
| 2077 | TABLE_ACCOUNTS, |
| 2078 | accountId, |
| 2079 | accounts, |
| 2080 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2081 | long identityToken = clearCallingIdentity(); |
| 2082 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2083 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2084 | } finally { |
| 2085 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2086 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2089 | private class RemoveAccountSession extends Session { |
| 2090 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2091 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2092 | Account account, boolean expectActivityLaunch) { |
| 2093 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2094 | true /* stripAuthTokenFromResult */, account.name, |
| 2095 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2096 | mAccount = account; |
| 2097 | } |
| 2098 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2099 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2100 | protected String toDebugString(long now) { |
| 2101 | return super.toDebugString(now) + ", removeAccount" |
| 2102 | + ", account " + mAccount; |
| 2103 | } |
| 2104 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2105 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2106 | public void run() throws RemoteException { |
| 2107 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2108 | } |
| 2109 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2110 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2111 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2112 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2113 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2114 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2115 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2116 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2117 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2118 | } |
| 2119 | IAccountManagerResponse response = getResponseAndClose(); |
| 2120 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2121 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2122 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2123 | + response); |
| 2124 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2125 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2126 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2127 | try { |
| 2128 | response.onResult(result2); |
| 2129 | } catch (RemoteException e) { |
| 2130 | // ignore |
| 2131 | } |
| 2132 | } |
| 2133 | } |
| 2134 | super.onResult(result); |
| 2135 | } |
| 2136 | } |
| 2137 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2138 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2139 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2140 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2141 | } |
| 2142 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2143 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2144 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2145 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2146 | if (!userUnlocked) { |
| 2147 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2148 | + " is still locked. CE data will be removed later"); |
| 2149 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2150 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2151 | final SQLiteDatabase db = userUnlocked |
| 2152 | ? accounts.openHelper.getWritableDatabaseUserIsUnlocked() |
| 2153 | : accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2154 | db.beginTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2155 | // Set to a dummy value, this will only be used if the database |
| 2156 | // transaction succeeds. |
| 2157 | long accountId = -1; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2158 | try { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2159 | accountId = AccountsDbUtils.findAccountId(db, account); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2160 | if (accountId >= 0) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2161 | AccountsDbUtils.deleteAccount(db, accountId); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2162 | if (userUnlocked) { |
| 2163 | // Delete from CE table |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2164 | AccountsDbUtils.deleteCeAccount(db, accountId); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2165 | } |
| 2166 | db.setTransactionSuccessful(); |
| 2167 | isChanged = true; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2168 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2169 | } finally { |
| 2170 | db.endTransaction(); |
| 2171 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2172 | if (isChanged) { |
| 2173 | removeAccountFromCacheLocked(accounts, account); |
| 2174 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occured. |
| 2175 | sendAccountsChangedBroadcast(accounts.userId); |
| 2176 | String action = userUnlocked ? DebugDbHelper.ACTION_ACCOUNT_REMOVE |
| 2177 | : DebugDbHelper.ACTION_ACCOUNT_REMOVE_DE; |
| 2178 | logRecord(db, action, TABLE_ACCOUNTS, accountId, accounts); |
| 2179 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2180 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2181 | long id = Binder.clearCallingIdentity(); |
| 2182 | try { |
| 2183 | int parentUserId = accounts.userId; |
| 2184 | if (canHaveProfile(parentUserId)) { |
| 2185 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2186 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2187 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2188 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2189 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2190 | } |
| 2191 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2192 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2193 | } finally { |
| 2194 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2195 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2196 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2199 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2200 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2201 | int callerUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2202 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2203 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2204 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2205 | + ", pid " + Binder.getCallingPid()); |
| 2206 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2207 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 2208 | if (authToken == null) throw new IllegalArgumentException("authToken is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2209 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2210 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2211 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2212 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2213 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2214 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2215 | db.beginTransaction(); |
| 2216 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2217 | invalidateAuthTokenLocked(accounts, db, accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2218 | invalidateCustomTokenLocked(accounts, accountType, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2219 | db.setTransactionSuccessful(); |
| 2220 | } finally { |
| 2221 | db.endTransaction(); |
| 2222 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2223 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2224 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2225 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2229 | private void invalidateCustomTokenLocked( |
| 2230 | UserAccounts accounts, |
| 2231 | String accountType, |
| 2232 | String authToken) { |
| 2233 | if (authToken == null || accountType == null) { |
| 2234 | return; |
| 2235 | } |
| 2236 | // Also wipe out cached token in memory. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2237 | accounts.accountTokenCaches.remove(accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2240 | private void invalidateAuthTokenLocked(UserAccounts accounts, SQLiteDatabase db, |
| 2241 | String accountType, String authToken) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2242 | if (authToken == null || accountType == null) { |
| 2243 | return; |
| 2244 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2245 | Cursor cursor = AccountsDbUtils.findAuthtokenForAllAccounts(db, accountType, authToken); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2246 | try { |
| 2247 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2248 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2249 | String accountName = cursor.getString(1); |
| 2250 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2251 | AccountsDbUtils.deleteAuthToken(db, authTokenId); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2252 | writeAuthTokenIntoCacheLocked( |
| 2253 | accounts, |
| 2254 | db, |
| 2255 | new Account(accountName, accountType), |
| 2256 | authTokenType, |
| 2257 | null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2258 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2259 | } finally { |
| 2260 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2261 | } |
| 2262 | } |
| 2263 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2264 | private void saveCachedToken( |
| 2265 | UserAccounts accounts, |
| 2266 | Account account, |
| 2267 | String callerPkg, |
| 2268 | byte[] callerSigDigest, |
| 2269 | String tokenType, |
| 2270 | String token, |
| 2271 | long expiryMillis) { |
| 2272 | |
| 2273 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2274 | return; |
| 2275 | } |
| 2276 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2277 | UserHandle.of(accounts.userId)); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2278 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2279 | accounts.accountTokenCaches.put( |
| 2280 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2281 | } |
| 2282 | } |
| 2283 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2284 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2285 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2286 | if (account == null || type == null) { |
| 2287 | return false; |
| 2288 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2289 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2290 | UserHandle.of(accounts.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2291 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2292 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2293 | db.beginTransaction(); |
| 2294 | try { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2295 | long accountId = AccountsDbUtils.findAccountId(db, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2296 | if (accountId < 0) { |
| 2297 | return false; |
| 2298 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2299 | AccountsDbUtils.deleteAuthtokensByAccountIdAndType(db, accountId, type); |
| 2300 | if (AccountsDbUtils.insertAuthToken(db, accountId, type, authToken) >= 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2301 | db.setTransactionSuccessful(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2302 | writeAuthTokenIntoCacheLocked(accounts, db, account, type, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2303 | return true; |
| 2304 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2305 | return false; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2306 | } finally { |
| 2307 | db.endTransaction(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2308 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2312 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2313 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2314 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2315 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2316 | Log.v(TAG, "peekAuthToken: " + account |
| 2317 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2318 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2319 | + ", pid " + Binder.getCallingPid()); |
| 2320 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2321 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2322 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2323 | int userId = UserHandle.getCallingUserId(); |
| 2324 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2325 | String msg = String.format( |
| 2326 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2327 | callingUid, |
| 2328 | account.type); |
| 2329 | throw new SecurityException(msg); |
| 2330 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2331 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2332 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2333 | + callingUid); |
| 2334 | return null; |
| 2335 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2336 | long identityToken = clearCallingIdentity(); |
| 2337 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2338 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2339 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2340 | } finally { |
| 2341 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2342 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2343 | } |
| 2344 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2345 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2346 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2347 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2348 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2349 | Log.v(TAG, "setAuthToken: " + account |
| 2350 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2351 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2352 | + ", pid " + Binder.getCallingPid()); |
| 2353 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2354 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2355 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2356 | int userId = UserHandle.getCallingUserId(); |
| 2357 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2358 | String msg = String.format( |
| 2359 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2360 | callingUid, |
| 2361 | account.type); |
| 2362 | throw new SecurityException(msg); |
| 2363 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2364 | long identityToken = clearCallingIdentity(); |
| 2365 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2366 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2367 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2368 | } finally { |
| 2369 | restoreCallingIdentity(identityToken); |
| 2370 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2373 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2374 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2375 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2376 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2377 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2378 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2379 | + ", pid " + Binder.getCallingPid()); |
| 2380 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2381 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2382 | int userId = UserHandle.getCallingUserId(); |
| 2383 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2384 | String msg = String.format( |
| 2385 | "uid %s cannot set secrets for accounts of type: %s", |
| 2386 | callingUid, |
| 2387 | account.type); |
| 2388 | throw new SecurityException(msg); |
| 2389 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2390 | long identityToken = clearCallingIdentity(); |
| 2391 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2392 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2393 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2394 | } finally { |
| 2395 | restoreCallingIdentity(identityToken); |
| 2396 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2399 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2400 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2401 | if (account == null) { |
| 2402 | return; |
| 2403 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2404 | boolean isChanged = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2405 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2406 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2407 | db.beginTransaction(); |
| 2408 | try { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2409 | final long accountId = AccountsDbUtils.findAccountId(db, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2410 | if (accountId >= 0) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2411 | AccountsDbUtils.updateAccountPassword(db, accountId, password); |
| 2412 | AccountsDbUtils.deleteAuthTokensByAccountId(db, accountId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2413 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2414 | accounts.accountTokenCaches.remove(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2415 | db.setTransactionSuccessful(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2416 | // If there is an account whose password will be updated and the database |
| 2417 | // transactions succeed, then we say that a change has occured. Even if the |
| 2418 | // new password is the same as the old and there were no authtokens to delete. |
| 2419 | isChanged = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2420 | String action = (password == null || password.length() == 0) ? |
| 2421 | DebugDbHelper.ACTION_CLEAR_PASSWORD |
| 2422 | : DebugDbHelper.ACTION_SET_PASSWORD; |
| 2423 | logRecord(db, action, TABLE_ACCOUNTS, accountId, accounts, callingUid); |
Costin Manolache | f5ffe89 | 2011-01-19 09:35:32 -0800 | [diff] [blame] | 2424 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2425 | } finally { |
| 2426 | db.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2427 | if (isChanged) { |
| 2428 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
| 2429 | sendAccountsChangedBroadcast(accounts.userId); |
| 2430 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2431 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2432 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2435 | private void sendAccountsChangedBroadcast(int userId) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2436 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 2437 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 2438 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2439 | } |
| 2440 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2441 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2442 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2443 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2444 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2445 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2446 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2447 | + ", pid " + Binder.getCallingPid()); |
| 2448 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2449 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2450 | int userId = UserHandle.getCallingUserId(); |
| 2451 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2452 | String msg = String.format( |
| 2453 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2454 | callingUid, |
| 2455 | account.type); |
| 2456 | throw new SecurityException(msg); |
| 2457 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2458 | long identityToken = clearCallingIdentity(); |
| 2459 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2460 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2461 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2462 | } finally { |
| 2463 | restoreCallingIdentity(identityToken); |
| 2464 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2467 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2468 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2469 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2470 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2471 | Log.v(TAG, "setUserData: " + account |
| 2472 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2473 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2474 | + ", pid " + Binder.getCallingPid()); |
| 2475 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2476 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2477 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2478 | int userId = UserHandle.getCallingUserId(); |
| 2479 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2480 | String msg = String.format( |
| 2481 | "uid %s cannot set user data for accounts of type: %s", |
| 2482 | callingUid, |
| 2483 | account.type); |
| 2484 | throw new SecurityException(msg); |
| 2485 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2486 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2487 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2488 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2489 | synchronized (accounts.cacheLock) { |
| 2490 | if (!accountExistsCacheLocked(accounts, account)) { |
| 2491 | return; |
| 2492 | } |
| 2493 | setUserdataInternalLocked(accounts, account, key, value); |
| 2494 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2495 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2496 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2497 | } |
| 2498 | } |
| 2499 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2500 | private boolean accountExistsCacheLocked(UserAccounts accounts, Account account) { |
| 2501 | if (accounts.accountCache.containsKey(account.type)) { |
| 2502 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2503 | if (acc.name.equals(account.name)) { |
| 2504 | return true; |
| 2505 | } |
| 2506 | } |
| 2507 | } |
| 2508 | return false; |
| 2509 | } |
| 2510 | |
| 2511 | private void setUserdataInternalLocked(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2512 | String value) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2513 | if (account == null || key == null) { |
| 2514 | return; |
| 2515 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2516 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 2517 | db.beginTransaction(); |
| 2518 | try { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2519 | long accountId = AccountsDbUtils.findAccountId(db, account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2520 | if (accountId < 0) { |
| 2521 | return; |
| 2522 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2523 | long extrasId = AccountsDbUtils.findExtrasIdByAccountId(db, accountId, key); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2524 | if (extrasId < 0) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2525 | extrasId = AccountsDbUtils.insertExtra(db, accountId, key, value); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2526 | if (extrasId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2527 | return; |
| 2528 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2529 | } else if (!AccountsDbUtils.updateExtra(db, extrasId, value)) { |
| 2530 | return; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2531 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2532 | writeUserDataIntoCacheLocked(accounts, db, account, key, value); |
| 2533 | db.setTransactionSuccessful(); |
| 2534 | } finally { |
| 2535 | db.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2536 | } |
| 2537 | } |
| 2538 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2539 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2540 | if (result == null) { |
| 2541 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2542 | } |
| 2543 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2544 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2545 | + response); |
| 2546 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2547 | try { |
| 2548 | response.onResult(result); |
| 2549 | } catch (RemoteException e) { |
| 2550 | // if the caller is dead then there is no one to care about remote |
| 2551 | // exceptions |
| 2552 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2553 | Log.v(TAG, "failure while notifying response", e); |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2558 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2559 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2560 | final String authTokenType) |
| 2561 | throws RemoteException { |
| 2562 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2563 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 2564 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2565 | final int callingUid = getCallingUid(); |
| 2566 | clearCallingIdentity(); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2567 | if (callingUid != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2568 | throw new SecurityException("can only call from system"); |
| 2569 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2570 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2571 | long identityToken = clearCallingIdentity(); |
| 2572 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2573 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2574 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2575 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2576 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2577 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2578 | protected String toDebugString(long now) { |
| 2579 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2580 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2581 | + ", authTokenType " + authTokenType; |
| 2582 | } |
| 2583 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2584 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2585 | public void run() throws RemoteException { |
| 2586 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2587 | } |
| 2588 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2589 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2590 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2591 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2592 | if (result != null) { |
| 2593 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2594 | Bundle bundle = new Bundle(); |
| 2595 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2596 | super.onResult(bundle); |
| 2597 | return; |
| 2598 | } else { |
| 2599 | super.onResult(result); |
| 2600 | } |
| 2601 | } |
| 2602 | }.bind(); |
| 2603 | } finally { |
| 2604 | restoreCallingIdentity(identityToken); |
| 2605 | } |
| 2606 | } |
| 2607 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2608 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2609 | public void getAuthToken( |
| 2610 | IAccountManagerResponse response, |
| 2611 | final Account account, |
| 2612 | final String authTokenType, |
| 2613 | final boolean notifyOnAuthFailure, |
| 2614 | final boolean expectActivityLaunch, |
| 2615 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2616 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2617 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2618 | Log.v(TAG, "getAuthToken: " + account |
| 2619 | + ", response " + response |
| 2620 | + ", authTokenType " + authTokenType |
| 2621 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2622 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2623 | + ", caller's uid " + Binder.getCallingUid() |
| 2624 | + ", pid " + Binder.getCallingPid()); |
| 2625 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2626 | if (response == null) throw new IllegalArgumentException("response is null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2627 | try { |
| 2628 | if (account == null) { |
| 2629 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2630 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2631 | return; |
| 2632 | } |
| 2633 | if (authTokenType == null) { |
| 2634 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2635 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2636 | return; |
| 2637 | } |
| 2638 | } catch (RemoteException e) { |
| 2639 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2640 | return; |
| 2641 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2642 | int userId = UserHandle.getCallingUserId(); |
| 2643 | long ident = Binder.clearCallingIdentity(); |
| 2644 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2645 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2646 | try { |
| 2647 | accounts = getUserAccounts(userId); |
| 2648 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2649 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2650 | } finally { |
| 2651 | Binder.restoreCallingIdentity(ident); |
| 2652 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2653 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2654 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2655 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2656 | |
| 2657 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2658 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2659 | final boolean permissionGranted = |
| 2660 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2661 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2662 | // Get the calling package. We will use it for the purpose of caching. |
| 2663 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2664 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2665 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2666 | try { |
| 2667 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2668 | } finally { |
| 2669 | Binder.restoreCallingIdentity(ident); |
| 2670 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2671 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2672 | String msg = String.format( |
| 2673 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2674 | callerUid, |
| 2675 | callerPkg); |
| 2676 | throw new SecurityException(msg); |
| 2677 | } |
| 2678 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2679 | // let authenticator know the identity of the caller |
| 2680 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2681 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2682 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2683 | if (notifyOnAuthFailure) { |
| 2684 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2685 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2686 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2687 | long identityToken = clearCallingIdentity(); |
| 2688 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2689 | // Distill the caller's package signatures into a single digest. |
| 2690 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2691 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2692 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2693 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2694 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2695 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2696 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2697 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2698 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2699 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2700 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2701 | onResult(response, result); |
| 2702 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2703 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2704 | } |
| 2705 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2706 | if (customTokens) { |
| 2707 | /* |
| 2708 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2709 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2710 | * ANDORID_PACKAGE_NAME. |
| 2711 | */ |
| 2712 | String token = readCachedTokenInternal( |
| 2713 | accounts, |
| 2714 | account, |
| 2715 | authTokenType, |
| 2716 | callerPkg, |
| 2717 | callerPkgSigDigest); |
| 2718 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2719 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2720 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2721 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2722 | Bundle result = new Bundle(); |
| 2723 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2724 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2725 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2726 | onResult(response, result); |
| 2727 | return; |
| 2728 | } |
| 2729 | } |
| 2730 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2731 | new Session( |
| 2732 | accounts, |
| 2733 | response, |
| 2734 | account.type, |
| 2735 | expectActivityLaunch, |
| 2736 | false /* stripAuthTokenFromResult */, |
| 2737 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2738 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2739 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2740 | protected String toDebugString(long now) { |
| 2741 | if (loginOptions != null) loginOptions.keySet(); |
| 2742 | return super.toDebugString(now) + ", getAuthToken" |
| 2743 | + ", " + account |
| 2744 | + ", authTokenType " + authTokenType |
| 2745 | + ", loginOptions " + loginOptions |
| 2746 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2747 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2748 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2749 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2750 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2751 | // If the caller doesn't have permission then create and return the |
| 2752 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2753 | if (!permissionGranted) { |
| 2754 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2755 | } else { |
| 2756 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2757 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2760 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2761 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2762 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2763 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2764 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2765 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2766 | account, |
| 2767 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2768 | new AccountAuthenticatorResponse(this), |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2769 | authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2770 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2771 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2772 | onResult(bundle); |
| 2773 | return; |
| 2774 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2775 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2776 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2777 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2778 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2779 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2780 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2781 | "the type and name should not be empty"); |
| 2782 | return; |
| 2783 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2784 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2785 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2786 | saveAuthTokenToDatabase( |
| 2787 | mAccounts, |
| 2788 | resultAccount, |
| 2789 | authTokenType, |
| 2790 | authToken); |
| 2791 | } |
| 2792 | long expiryMillis = result.getLong( |
| 2793 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2794 | if (customTokens |
| 2795 | && expiryMillis > System.currentTimeMillis()) { |
| 2796 | saveCachedToken( |
| 2797 | mAccounts, |
| 2798 | account, |
| 2799 | callerPkg, |
| 2800 | callerPkgSigDigest, |
| 2801 | authTokenType, |
| 2802 | authToken, |
| 2803 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2804 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2805 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2806 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2807 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2808 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2809 | /* |
| 2810 | * Make sure that the supplied intent is owned by the authenticator |
| 2811 | * giving it to the system. Otherwise a malicious authenticator could |
| 2812 | * have users launching arbitrary activities by tricking users to |
| 2813 | * interact with malicious notifications. |
| 2814 | */ |
| 2815 | checkKeyIntent( |
| 2816 | Binder.getCallingUid(), |
| 2817 | intent); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2818 | doNotification(mAccounts, |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2819 | account, result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2820 | intent, accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2821 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2822 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2823 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2824 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2825 | }.bind(); |
| 2826 | } finally { |
| 2827 | restoreCallingIdentity(identityToken); |
| 2828 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2831 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2832 | MessageDigest digester; |
| 2833 | try { |
| 2834 | digester = MessageDigest.getInstance("SHA-256"); |
| 2835 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2836 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2837 | for (Signature sig : pkgInfo.signatures) { |
| 2838 | digester.update(sig.toByteArray()); |
| 2839 | } |
| 2840 | } catch (NoSuchAlgorithmException x) { |
| 2841 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2842 | digester = null; |
| 2843 | } catch (NameNotFoundException e) { |
| 2844 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2845 | digester = null; |
| 2846 | } |
| 2847 | return (digester == null) ? null : digester.digest(); |
| 2848 | } |
| 2849 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2850 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
| 2851 | int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2852 | int uid = intent.getIntExtra( |
| 2853 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2854 | String authTokenType = intent.getStringExtra( |
| 2855 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2856 | final String titleAndSubtitle = |
| 2857 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2858 | account.name); |
| 2859 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2860 | String title = titleAndSubtitle; |
| 2861 | String subtitle = ""; |
| 2862 | if (index > 0) { |
| 2863 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2864 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2865 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2866 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2867 | Context contextForUser = getContextForUser(user); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 2868 | Notification n = new Notification.Builder(contextForUser) |
| 2869 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2870 | .setWhen(0) |
| 2871 | .setColor(contextForUser.getColor( |
| 2872 | com.android.internal.R.color.system_notification_accent_color)) |
| 2873 | .setContentTitle(title) |
| 2874 | .setContentText(subtitle) |
| 2875 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2876 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2877 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2878 | installNotification(getCredentialPermissionNotificationId( |
| 2879 | account, authTokenType, uid), n, user); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2880 | } |
| 2881 | |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2882 | private Intent newGrantCredentialsPermissionIntent(Account account, int uid, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2883 | AccountAuthenticatorResponse response, String authTokenType) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2884 | |
| 2885 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 2886 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
Costin Manolache | 9ec1736 | 2011-01-17 12:12:37 -0800 | [diff] [blame] | 2887 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2888 | // the intent from a non-Activity context. |
| 2889 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2890 | intent.addCategory( |
| 2891 | String.valueOf(getCredentialPermissionNotificationId(account, authTokenType, uid))); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2892 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2893 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2894 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2895 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2896 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2897 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2898 | return intent; |
| 2899 | } |
| 2900 | |
| 2901 | private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, |
| 2902 | int uid) { |
| 2903 | Integer id; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2904 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2905 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2906 | final Pair<Pair<Account, String>, Integer> key = |
| 2907 | new Pair<Pair<Account, String>, Integer>( |
| 2908 | new Pair<Account, String>(account, authTokenType), uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2909 | id = accounts.credentialsPermissionNotificationIds.get(key); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2910 | if (id == null) { |
| 2911 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2912 | accounts.credentialsPermissionNotificationIds.put(key, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2913 | } |
| 2914 | } |
| 2915 | return id; |
| 2916 | } |
| 2917 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2918 | private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2919 | Integer id; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2920 | synchronized (accounts.signinRequiredNotificationIds) { |
| 2921 | id = accounts.signinRequiredNotificationIds.get(account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2922 | if (id == null) { |
| 2923 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2924 | accounts.signinRequiredNotificationIds.put(account, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2925 | } |
| 2926 | } |
| 2927 | return id; |
| 2928 | } |
| 2929 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2930 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2931 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2932 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2933 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2934 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2935 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2936 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2937 | + ", response " + response |
| 2938 | + ", authTokenType " + authTokenType |
| 2939 | + ", requiredFeatures " + stringArrayToString(requiredFeatures) |
| 2940 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2941 | + ", caller's uid " + Binder.getCallingUid() |
| 2942 | + ", pid " + Binder.getCallingPid()); |
| 2943 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2944 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2945 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2946 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2947 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2948 | final int uid = Binder.getCallingUid(); |
| 2949 | final int userId = UserHandle.getUserId(uid); |
| 2950 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2951 | try { |
| 2952 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2953 | "User is not allowed to add an account!"); |
| 2954 | } catch (RemoteException re) { |
| 2955 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2956 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2957 | return; |
| 2958 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2959 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2960 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2961 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2962 | "User cannot modify accounts of this type (policy)."); |
| 2963 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2964 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2965 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2966 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2967 | return; |
| 2968 | } |
| 2969 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2970 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2971 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2972 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2973 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2974 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2975 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2976 | long identityToken = clearCallingIdentity(); |
| 2977 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2978 | UserAccounts accounts = getUserAccounts(usrId); |
| 2979 | logRecordWithUid( |
| 2980 | accounts, DebugDbHelper.ACTION_CALLED_ACCOUNT_ADD, TABLE_ACCOUNTS, uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2981 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2982 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2983 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2984 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2985 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2986 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2987 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2988 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2989 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2990 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2991 | protected String toDebugString(long now) { |
| 2992 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2993 | + ", accountType " + accountType |
| 2994 | + ", requiredFeatures " |
| 2995 | + (requiredFeatures != null |
| 2996 | ? TextUtils.join(",", requiredFeatures) |
| 2997 | : null); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2998 | } |
| 2999 | }.bind(); |
| 3000 | } finally { |
| 3001 | restoreCallingIdentity(identityToken); |
| 3002 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3003 | } |
| 3004 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3005 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3006 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 3007 | final String authTokenType, final String[] requiredFeatures, |
| 3008 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3009 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3010 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3011 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3012 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3013 | + ", response " + response |
| 3014 | + ", authTokenType " + authTokenType |
| 3015 | + ", requiredFeatures " + stringArrayToString(requiredFeatures) |
| 3016 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3017 | + ", caller's uid " + Binder.getCallingUid() |
| 3018 | + ", pid " + Binder.getCallingPid() |
| 3019 | + ", for user id " + userId); |
| 3020 | } |
| 3021 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3022 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3023 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3024 | if (isCrossUser(callingUid, userId)) { |
| 3025 | throw new SecurityException( |
| 3026 | String.format( |
| 3027 | "User %s trying to add account for %s" , |
| 3028 | UserHandle.getCallingUserId(), |
| 3029 | userId)); |
| 3030 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3031 | |
| 3032 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3033 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3034 | try { |
| 3035 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3036 | "User is not allowed to add an account!"); |
| 3037 | } catch (RemoteException re) { |
| 3038 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3039 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3040 | return; |
| 3041 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3042 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3043 | try { |
| 3044 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3045 | "User cannot modify accounts of this type (policy)."); |
| 3046 | } catch (RemoteException re) { |
| 3047 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3048 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3049 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3050 | return; |
| 3051 | } |
| 3052 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3053 | final int pid = Binder.getCallingPid(); |
| 3054 | final int uid = Binder.getCallingUid(); |
| 3055 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3056 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3057 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3058 | |
| 3059 | long identityToken = clearCallingIdentity(); |
| 3060 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3061 | UserAccounts accounts = getUserAccounts(userId); |
| 3062 | logRecordWithUid( |
| 3063 | accounts, DebugDbHelper.ACTION_CALLED_ACCOUNT_ADD, TABLE_ACCOUNTS, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3064 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3065 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3066 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3067 | @Override |
| 3068 | public void run() throws RemoteException { |
| 3069 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3070 | options); |
| 3071 | } |
| 3072 | |
| 3073 | @Override |
| 3074 | protected String toDebugString(long now) { |
| 3075 | return super.toDebugString(now) + ", addAccount" |
| 3076 | + ", accountType " + accountType |
| 3077 | + ", requiredFeatures " |
| 3078 | + (requiredFeatures != null |
| 3079 | ? TextUtils.join(",", requiredFeatures) |
| 3080 | : null); |
| 3081 | } |
| 3082 | }.bind(); |
| 3083 | } finally { |
| 3084 | restoreCallingIdentity(identityToken); |
| 3085 | } |
| 3086 | } |
| 3087 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3088 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3089 | public void startAddAccountSession( |
| 3090 | final IAccountManagerResponse response, |
| 3091 | final String accountType, |
| 3092 | final String authTokenType, |
| 3093 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3094 | final boolean expectActivityLaunch, |
| 3095 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3096 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3097 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3098 | Log.v(TAG, |
| 3099 | "startAddAccountSession: accountType " + accountType |
| 3100 | + ", response " + response |
| 3101 | + ", authTokenType " + authTokenType |
| 3102 | + ", requiredFeatures " + stringArrayToString(requiredFeatures) |
| 3103 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3104 | + ", caller's uid " + Binder.getCallingUid() |
| 3105 | + ", pid " + Binder.getCallingPid()); |
| 3106 | } |
| 3107 | if (response == null) { |
| 3108 | throw new IllegalArgumentException("response is null"); |
| 3109 | } |
| 3110 | if (accountType == null) { |
| 3111 | throw new IllegalArgumentException("accountType is null"); |
| 3112 | } |
| 3113 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3114 | final int uid = Binder.getCallingUid(); |
| 3115 | final int userId = UserHandle.getUserId(uid); |
| 3116 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3117 | try { |
| 3118 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3119 | "User is not allowed to add an account!"); |
| 3120 | } catch (RemoteException re) { |
| 3121 | } |
| 3122 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3123 | return; |
| 3124 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3125 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3126 | try { |
| 3127 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3128 | "User cannot modify accounts of this type (policy)."); |
| 3129 | } catch (RemoteException re) { |
| 3130 | } |
| 3131 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3132 | userId); |
| 3133 | return; |
| 3134 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3135 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3136 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3137 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3138 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3139 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3140 | // Check to see if the Password should be included to the caller. |
| 3141 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3142 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3143 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3144 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3145 | long identityToken = clearCallingIdentity(); |
| 3146 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3147 | UserAccounts accounts = getUserAccounts(userId); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3148 | logRecordWithUid(accounts, DebugDbHelper.ACTION_CALLED_START_ACCOUNT_ADD, |
| 3149 | TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3150 | new StartAccountSession( |
| 3151 | accounts, |
| 3152 | response, |
| 3153 | accountType, |
| 3154 | expectActivityLaunch, |
| 3155 | null /* accountName */, |
| 3156 | false /* authDetailsRequired */, |
| 3157 | true /* updateLastAuthenticationTime */, |
| 3158 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3159 | @Override |
| 3160 | public void run() throws RemoteException { |
| 3161 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3162 | requiredFeatures, options); |
| 3163 | } |
| 3164 | |
| 3165 | @Override |
| 3166 | protected String toDebugString(long now) { |
| 3167 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3168 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3169 | + accountType + ", requiredFeatures " |
| 3170 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3171 | } |
| 3172 | }.bind(); |
| 3173 | } finally { |
| 3174 | restoreCallingIdentity(identityToken); |
| 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3179 | private abstract class StartAccountSession extends Session { |
| 3180 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3181 | private final boolean mIsPasswordForwardingAllowed; |
| 3182 | |
| 3183 | public StartAccountSession( |
| 3184 | UserAccounts accounts, |
| 3185 | IAccountManagerResponse response, |
| 3186 | String accountType, |
| 3187 | boolean expectActivityLaunch, |
| 3188 | String accountName, |
| 3189 | boolean authDetailsRequired, |
| 3190 | boolean updateLastAuthenticationTime, |
| 3191 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3192 | super(accounts, response, accountType, expectActivityLaunch, |
| 3193 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3194 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3195 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | @Override |
| 3199 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3200 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3201 | mNumResults++; |
| 3202 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3203 | if (result != null |
| 3204 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3205 | checkKeyIntent( |
| 3206 | Binder.getCallingUid(), |
| 3207 | intent); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3208 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3209 | IAccountManagerResponse response; |
| 3210 | if (mExpectActivityLaunch && result != null |
| 3211 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3212 | response = mResponse; |
| 3213 | } else { |
| 3214 | response = getResponseAndClose(); |
| 3215 | } |
| 3216 | if (response == null) { |
| 3217 | return; |
| 3218 | } |
| 3219 | if (result == null) { |
| 3220 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3221 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3222 | + response); |
| 3223 | } |
| 3224 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3225 | "null bundle returned"); |
| 3226 | return; |
| 3227 | } |
| 3228 | |
| 3229 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3230 | // All AccountManager error codes are greater |
| 3231 | // than 0 |
| 3232 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3233 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3234 | return; |
| 3235 | } |
| 3236 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3237 | // Omit passwords if the caller isn't permitted to see them. |
| 3238 | if (!mIsPasswordForwardingAllowed) { |
| 3239 | result.remove(AccountManager.KEY_PASSWORD); |
| 3240 | } |
| 3241 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3242 | // Strip auth token from result. |
| 3243 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3244 | |
| 3245 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3246 | Log.v(TAG, |
| 3247 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3248 | } |
| 3249 | |
| 3250 | // Get the session bundle created by authenticator. The |
| 3251 | // bundle contains data necessary for finishing the session |
| 3252 | // later. The session bundle will be encrypted here and |
| 3253 | // decrypted later when trying to finish the session. |
| 3254 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3255 | if (sessionBundle != null) { |
| 3256 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3257 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3258 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3259 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3260 | } |
| 3261 | // Add accountType info to session bundle. This will |
| 3262 | // override any value set by authenticator. |
| 3263 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3264 | |
| 3265 | // Encrypt session bundle before returning to caller. |
| 3266 | try { |
| 3267 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3268 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3269 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3270 | } catch (GeneralSecurityException e) { |
| 3271 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3272 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3273 | } |
| 3274 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3275 | "failed to encrypt session bundle"); |
| 3276 | return; |
| 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | sendResponse(response, result); |
| 3281 | } |
| 3282 | } |
| 3283 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3284 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3285 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3286 | @NonNull Bundle sessionBundle, |
| 3287 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3288 | Bundle appInfo, |
| 3289 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3290 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3291 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3292 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3293 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3294 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3295 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3296 | + ", caller's uid " + callingUid |
| 3297 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3298 | + ", pid " + Binder.getCallingPid() |
| 3299 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3300 | } |
| 3301 | if (response == null) { |
| 3302 | throw new IllegalArgumentException("response is null"); |
| 3303 | } |
| 3304 | |
| 3305 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3306 | // Account type is added to it before encryption. |
| 3307 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3308 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3309 | } |
| 3310 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3311 | // Only allow the system process to finish session for other users |
| 3312 | if (isCrossUser(callingUid, userId)) { |
| 3313 | throw new SecurityException( |
| 3314 | String.format( |
| 3315 | "User %s trying to finish session for %s without cross user permission", |
| 3316 | UserHandle.getCallingUserId(), |
| 3317 | userId)); |
| 3318 | } |
| 3319 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3320 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3321 | sendErrorResponse(response, |
| 3322 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3323 | "User is not allowed to add an account!"); |
| 3324 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3325 | return; |
| 3326 | } |
| 3327 | |
| 3328 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3329 | final Bundle decryptedBundle; |
| 3330 | final String accountType; |
| 3331 | // First decrypt session bundle to get account type for checking permission. |
| 3332 | try { |
| 3333 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3334 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3335 | if (decryptedBundle == null) { |
| 3336 | sendErrorResponse( |
| 3337 | response, |
| 3338 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3339 | "failed to decrypt session bundle"); |
| 3340 | return; |
| 3341 | } |
| 3342 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3343 | // Account type cannot be null. This should not happen if session bundle was created |
| 3344 | // properly by #StartAccountSession. |
| 3345 | if (TextUtils.isEmpty(accountType)) { |
| 3346 | sendErrorResponse( |
| 3347 | response, |
| 3348 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3349 | "accountType is empty"); |
| 3350 | return; |
| 3351 | } |
| 3352 | |
| 3353 | // If by any chances, decryptedBundle contains colliding keys with |
| 3354 | // system info |
| 3355 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3356 | // update credentials flow, we should replace with the new values of the current call. |
| 3357 | if (appInfo != null) { |
| 3358 | decryptedBundle.putAll(appInfo); |
| 3359 | } |
| 3360 | |
| 3361 | // 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] | 3362 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3363 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3364 | } catch (GeneralSecurityException e) { |
| 3365 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3366 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3367 | } |
| 3368 | sendErrorResponse( |
| 3369 | response, |
| 3370 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3371 | "failed to decrypt session bundle"); |
| 3372 | return; |
| 3373 | } |
| 3374 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3375 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3376 | sendErrorResponse( |
| 3377 | response, |
| 3378 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3379 | "User cannot modify accounts of this type (policy)."); |
| 3380 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3381 | userId); |
| 3382 | return; |
| 3383 | } |
| 3384 | |
| 3385 | long identityToken = clearCallingIdentity(); |
| 3386 | try { |
| 3387 | UserAccounts accounts = getUserAccounts(userId); |
| 3388 | logRecordWithUid( |
| 3389 | accounts, |
| 3390 | DebugDbHelper.ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3391 | TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3392 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3393 | new Session( |
| 3394 | accounts, |
| 3395 | response, |
| 3396 | accountType, |
| 3397 | expectActivityLaunch, |
| 3398 | true /* stripAuthTokenFromResult */, |
| 3399 | null /* accountName */, |
| 3400 | false /* authDetailsRequired */, |
| 3401 | true /* updateLastAuthenticationTime */) { |
| 3402 | @Override |
| 3403 | public void run() throws RemoteException { |
| 3404 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3405 | } |
| 3406 | |
| 3407 | @Override |
| 3408 | protected String toDebugString(long now) { |
| 3409 | return super.toDebugString(now) |
| 3410 | + ", finishSession" |
| 3411 | + ", accountType " + accountType; |
| 3412 | } |
| 3413 | }.bind(); |
| 3414 | } finally { |
| 3415 | restoreCallingIdentity(identityToken); |
| 3416 | } |
| 3417 | } |
| 3418 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3419 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3420 | final DevicePolicyManagerInternal dpmi = |
| 3421 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3422 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame^] | 3423 | if (dpmi == null) { |
| 3424 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3425 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3426 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3427 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3428 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3429 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3430 | } |
| 3431 | if (intent == null) { |
| 3432 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3433 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3434 | long identityToken = clearCallingIdentity(); |
| 3435 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3436 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3437 | } finally { |
| 3438 | restoreCallingIdentity(identityToken); |
| 3439 | } |
| 3440 | } |
| 3441 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3442 | /** |
| 3443 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3444 | */ |
| 3445 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3446 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3447 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3448 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3449 | return cantAddAccount; |
| 3450 | } |
| 3451 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3452 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3453 | public void confirmCredentialsAsUser( |
| 3454 | IAccountManagerResponse response, |
| 3455 | final Account account, |
| 3456 | final Bundle options, |
| 3457 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3458 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3459 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3460 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3461 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3462 | Log.v(TAG, "confirmCredentials: " + account |
| 3463 | + ", response " + response |
| 3464 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3465 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3466 | + ", pid " + Binder.getCallingPid()); |
| 3467 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3468 | // Only allow the system process to read accounts of other users |
| 3469 | if (isCrossUser(callingUid, userId)) { |
| 3470 | throw new SecurityException( |
| 3471 | String.format( |
| 3472 | "User %s trying to confirm account credentials for %s" , |
| 3473 | UserHandle.getCallingUserId(), |
| 3474 | userId)); |
| 3475 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3476 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3477 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3478 | long identityToken = clearCallingIdentity(); |
| 3479 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3480 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3481 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3482 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3483 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3484 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3485 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3486 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3487 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3488 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3489 | protected String toDebugString(long now) { |
| 3490 | return super.toDebugString(now) + ", confirmCredentials" |
| 3491 | + ", " + account; |
| 3492 | } |
| 3493 | }.bind(); |
| 3494 | } finally { |
| 3495 | restoreCallingIdentity(identityToken); |
| 3496 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3497 | } |
| 3498 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3499 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3500 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3501 | final String authTokenType, final boolean expectActivityLaunch, |
| 3502 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3503 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3504 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3505 | Log.v(TAG, "updateCredentials: " + account |
| 3506 | + ", response " + response |
| 3507 | + ", authTokenType " + authTokenType |
| 3508 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3509 | + ", caller's uid " + Binder.getCallingUid() |
| 3510 | + ", pid " + Binder.getCallingPid()); |
| 3511 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3512 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3513 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3514 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3515 | long identityToken = clearCallingIdentity(); |
| 3516 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3517 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3518 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3519 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3520 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3521 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3522 | public void run() throws RemoteException { |
| 3523 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3524 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3525 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3526 | protected String toDebugString(long now) { |
| 3527 | if (loginOptions != null) loginOptions.keySet(); |
| 3528 | return super.toDebugString(now) + ", updateCredentials" |
| 3529 | + ", " + account |
| 3530 | + ", authTokenType " + authTokenType |
| 3531 | + ", loginOptions " + loginOptions; |
| 3532 | } |
| 3533 | }.bind(); |
| 3534 | } finally { |
| 3535 | restoreCallingIdentity(identityToken); |
| 3536 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3537 | } |
| 3538 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3539 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3540 | public void startUpdateCredentialsSession( |
| 3541 | IAccountManagerResponse response, |
| 3542 | final Account account, |
| 3543 | final String authTokenType, |
| 3544 | final boolean expectActivityLaunch, |
| 3545 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3546 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3547 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3548 | Log.v(TAG, |
| 3549 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3550 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3551 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3552 | + ", pid " + Binder.getCallingPid()); |
| 3553 | } |
| 3554 | if (response == null) { |
| 3555 | throw new IllegalArgumentException("response is null"); |
| 3556 | } |
| 3557 | if (account == null) { |
| 3558 | throw new IllegalArgumentException("account is null"); |
| 3559 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3560 | |
| 3561 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3562 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3563 | |
| 3564 | // Check to see if the Password should be included to the caller. |
| 3565 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3566 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3567 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3568 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3569 | long identityToken = clearCallingIdentity(); |
| 3570 | try { |
| 3571 | UserAccounts accounts = getUserAccounts(userId); |
| 3572 | new StartAccountSession( |
| 3573 | accounts, |
| 3574 | response, |
| 3575 | account.type, |
| 3576 | expectActivityLaunch, |
| 3577 | account.name, |
| 3578 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3579 | true /* updateLastCredentialTime */, |
| 3580 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3581 | @Override |
| 3582 | public void run() throws RemoteException { |
| 3583 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3584 | loginOptions); |
| 3585 | } |
| 3586 | |
| 3587 | @Override |
| 3588 | protected String toDebugString(long now) { |
| 3589 | if (loginOptions != null) |
| 3590 | loginOptions.keySet(); |
| 3591 | return super.toDebugString(now) |
| 3592 | + ", startUpdateCredentialsSession" |
| 3593 | + ", " + account |
| 3594 | + ", authTokenType " + authTokenType |
| 3595 | + ", loginOptions " + loginOptions; |
| 3596 | } |
| 3597 | }.bind(); |
| 3598 | } finally { |
| 3599 | restoreCallingIdentity(identityToken); |
| 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3604 | public void isCredentialsUpdateSuggested( |
| 3605 | IAccountManagerResponse response, |
| 3606 | final Account account, |
| 3607 | final String statusToken) { |
| 3608 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3609 | Log.v(TAG, |
| 3610 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3611 | + ", caller's uid " + Binder.getCallingUid() |
| 3612 | + ", pid " + Binder.getCallingPid()); |
| 3613 | } |
| 3614 | if (response == null) { |
| 3615 | throw new IllegalArgumentException("response is null"); |
| 3616 | } |
| 3617 | if (account == null) { |
| 3618 | throw new IllegalArgumentException("account is null"); |
| 3619 | } |
| 3620 | if (TextUtils.isEmpty(statusToken)) { |
| 3621 | throw new IllegalArgumentException("status token is empty"); |
| 3622 | } |
| 3623 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3624 | int usrId = UserHandle.getCallingUserId(); |
| 3625 | long identityToken = clearCallingIdentity(); |
| 3626 | try { |
| 3627 | UserAccounts accounts = getUserAccounts(usrId); |
| 3628 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3629 | false /* stripAuthTokenFromResult */, account.name, |
| 3630 | false /* authDetailsRequired */) { |
| 3631 | @Override |
| 3632 | protected String toDebugString(long now) { |
| 3633 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3634 | + ", " + account; |
| 3635 | } |
| 3636 | |
| 3637 | @Override |
| 3638 | public void run() throws RemoteException { |
| 3639 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3640 | } |
| 3641 | |
| 3642 | @Override |
| 3643 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3644 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3645 | IAccountManagerResponse response = getResponseAndClose(); |
| 3646 | if (response == null) { |
| 3647 | return; |
| 3648 | } |
| 3649 | |
| 3650 | if (result == null) { |
| 3651 | sendErrorResponse( |
| 3652 | response, |
| 3653 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3654 | "null bundle"); |
| 3655 | return; |
| 3656 | } |
| 3657 | |
| 3658 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3659 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3660 | + response); |
| 3661 | } |
| 3662 | // Check to see if an error occurred. We know if an error occurred because all |
| 3663 | // error codes are greater than 0. |
| 3664 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3665 | sendErrorResponse(response, |
| 3666 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3667 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3668 | return; |
| 3669 | } |
| 3670 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3671 | sendErrorResponse( |
| 3672 | response, |
| 3673 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3674 | "no result in response"); |
| 3675 | return; |
| 3676 | } |
| 3677 | final Bundle newResult = new Bundle(); |
| 3678 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3679 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3680 | sendResponse(response, newResult); |
| 3681 | } |
| 3682 | }.bind(); |
| 3683 | } finally { |
| 3684 | restoreCallingIdentity(identityToken); |
| 3685 | } |
| 3686 | } |
| 3687 | |
| 3688 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3689 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3690 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3691 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3692 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3693 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3694 | + ", response " + response |
| 3695 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3696 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3697 | + ", pid " + Binder.getCallingPid()); |
| 3698 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3699 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3700 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3701 | int userId = UserHandle.getCallingUserId(); |
| 3702 | if (!isAccountManagedByCaller(accountType, callingUid, userId) && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3703 | String msg = String.format( |
| 3704 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3705 | callingUid, |
| 3706 | accountType); |
| 3707 | throw new SecurityException(msg); |
| 3708 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3709 | long identityToken = clearCallingIdentity(); |
| 3710 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3711 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3712 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3713 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3714 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3715 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3716 | public void run() throws RemoteException { |
| 3717 | mAuthenticator.editProperties(this, mAccountType); |
| 3718 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3719 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3720 | protected String toDebugString(long now) { |
| 3721 | return super.toDebugString(now) + ", editProperties" |
| 3722 | + ", accountType " + accountType; |
| 3723 | } |
| 3724 | }.bind(); |
| 3725 | } finally { |
| 3726 | restoreCallingIdentity(identityToken); |
| 3727 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3728 | } |
| 3729 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3730 | @Override |
| 3731 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 3732 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 3733 | throw new SecurityException("Only system can check for accounts across users"); |
| 3734 | } |
| 3735 | final long token = Binder.clearCallingIdentity(); |
| 3736 | try { |
| 3737 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 3738 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 3739 | if (allAccounts[i].account.equals(account)) { |
| 3740 | return true; |
| 3741 | } |
| 3742 | } |
| 3743 | return false; |
| 3744 | } finally { |
| 3745 | Binder.restoreCallingIdentity(token); |
| 3746 | } |
| 3747 | } |
| 3748 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3749 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 3750 | private final String[] mFeatures; |
| 3751 | private volatile Account[] mAccountsOfType = null; |
| 3752 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 3753 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3754 | private final int mCallingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3755 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3756 | public GetAccountsByTypeAndFeatureSession(UserAccounts accounts, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3757 | IAccountManagerResponse response, String type, String[] features, int callingUid) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3758 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3759 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3760 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3761 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3762 | mFeatures = features; |
| 3763 | } |
| 3764 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3765 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3766 | public void run() throws RemoteException { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3767 | synchronized (mAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3768 | mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType, mCallingUid, |
| 3769 | null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3770 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3771 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 3772 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3773 | mCurrentAccount = 0; |
| 3774 | |
| 3775 | checkAccount(); |
| 3776 | } |
| 3777 | |
| 3778 | public void checkAccount() { |
| 3779 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 3780 | sendResult(); |
| 3781 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3782 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3783 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3784 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 3785 | if (accountAuthenticator == null) { |
| 3786 | // It is possible that the authenticator has died, which is indicated by |
| 3787 | // mAuthenticator being set to null. If this happens then just abort. |
| 3788 | // There is no need to send back a result or error in this case since |
| 3789 | // that already happened when mAuthenticator was cleared. |
| 3790 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3791 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 3792 | + " connected to the authenticator, " + toDebugString()); |
| 3793 | } |
| 3794 | return; |
| 3795 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3796 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3797 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3798 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3799 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3800 | } |
| 3801 | } |
| 3802 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3803 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3804 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3805 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3806 | mNumResults++; |
| 3807 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3808 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3809 | return; |
| 3810 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3811 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3812 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 3813 | } |
| 3814 | mCurrentAccount++; |
| 3815 | checkAccount(); |
| 3816 | } |
| 3817 | |
| 3818 | public void sendResult() { |
| 3819 | IAccountManagerResponse response = getResponseAndClose(); |
| 3820 | if (response != null) { |
| 3821 | try { |
| 3822 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 3823 | for (int i = 0; i < accounts.length; i++) { |
| 3824 | accounts[i] = mAccountsWithFeatures.get(i); |
| 3825 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3826 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3827 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3828 | + response); |
| 3829 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3830 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3831 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3832 | response.onResult(result); |
| 3833 | } catch (RemoteException e) { |
| 3834 | // if the caller is dead then there is no one to care about remote exceptions |
| 3835 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3836 | Log.v(TAG, "failure while notifying response", e); |
| 3837 | } |
| 3838 | } |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3843 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3844 | protected String toDebugString(long now) { |
| 3845 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 3846 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 3847 | } |
| 3848 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 3849 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3850 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3851 | * 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] | 3852 | * @hide |
| 3853 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3854 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3855 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3856 | int callingUid = Binder.getCallingUid(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3857 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 3858 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3859 | if (visibleAccountTypes.isEmpty()) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3860 | return new Account[0]; |
| 3861 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3862 | long identityToken = clearCallingIdentity(); |
| 3863 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3864 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3865 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3866 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3867 | callingUid, |
| 3868 | null, // packageName |
| 3869 | visibleAccountTypes); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3870 | } finally { |
| 3871 | restoreCallingIdentity(identityToken); |
| 3872 | } |
| 3873 | } |
| 3874 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3875 | /** |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3876 | * Returns accounts for all running users. |
| 3877 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3878 | * @hide |
| 3879 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3880 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3881 | public AccountAndUser[] getRunningAccounts() { |
| 3882 | final int[] runningUserIds; |
| 3883 | try { |
| 3884 | runningUserIds = ActivityManagerNative.getDefault().getRunningUserIds(); |
| 3885 | } catch (RemoteException e) { |
| 3886 | // Running in system_server; should never happen |
| 3887 | throw new RuntimeException(e); |
| 3888 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3889 | return getAccounts(runningUserIds); |
| 3890 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3891 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3892 | /** {@hide} */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3893 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3894 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 3895 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3896 | final int[] userIds = new int[users.size()]; |
| 3897 | for (int i = 0; i < userIds.length; i++) { |
| 3898 | userIds[i] = users.get(i).id; |
| 3899 | } |
| 3900 | return getAccounts(userIds); |
| 3901 | } |
| 3902 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3903 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3904 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3905 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 3906 | for (int userId : userIds) { |
| 3907 | UserAccounts userAccounts = getUserAccounts(userId); |
| 3908 | if (userAccounts == null) continue; |
| 3909 | synchronized (userAccounts.cacheLock) { |
| 3910 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null, |
| 3911 | Binder.getCallingUid(), null); |
| 3912 | for (int a = 0; a < accounts.length; a++) { |
| 3913 | runningAccounts.add(new AccountAndUser(accounts[a], userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3917 | |
| 3918 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 3919 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3920 | } |
| 3921 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3922 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3923 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3924 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
| 3925 | return getAccountsAsUser(type, userId, null, -1, opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3928 | @NonNull |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3929 | private Account[] getAccountsAsUser( |
| 3930 | String type, |
| 3931 | int userId, |
| 3932 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3933 | int packageUid, |
| 3934 | String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3935 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3936 | // Only allow the system process to read accounts of other users |
| 3937 | if (userId != UserHandle.getCallingUserId() |
Amith Yamasani | bb49e85 | 2013-03-30 19:20:18 -0700 | [diff] [blame] | 3938 | && callingUid != Process.myUid() |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 3939 | && mContext.checkCallingOrSelfPermission( |
| 3940 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 3941 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3942 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 3943 | + " trying to get account for " + userId); |
| 3944 | } |
| 3945 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3946 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3947 | Log.v(TAG, "getAccounts: accountType " + type |
| 3948 | + ", caller's uid " + Binder.getCallingUid() |
| 3949 | + ", pid " + Binder.getCallingPid()); |
| 3950 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3951 | // If the original calling app was using the framework account chooser activity, we'll |
| 3952 | // be passed in the original caller's uid here, which is what should be used for filtering. |
| 3953 | if (packageUid != -1 && UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 3954 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 3955 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3956 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3957 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 3958 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3959 | if (visibleAccountTypes.isEmpty() |
| 3960 | || (type != null && !visibleAccountTypes.contains(type))) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3961 | return new Account[0]; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3962 | } else if (visibleAccountTypes.contains(type)) { |
| 3963 | // Prune the list down to just the requested type. |
| 3964 | visibleAccountTypes = new ArrayList<>(); |
| 3965 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 3966 | } // else aggregate all the visible accounts (it won't matter if the |
| 3967 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3968 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 3969 | long identityToken = clearCallingIdentity(); |
| 3970 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3971 | UserAccounts accounts = getUserAccounts(userId); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 3972 | Account[] accountsToReturn = getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3973 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3974 | callingUid, |
| 3975 | callingPackage, |
| 3976 | visibleAccountTypes); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 3977 | ArrayList<Account> accountsToReturnList = new |
| 3978 | ArrayList<Account>(Arrays.asList(accountsToReturn)); |
| 3979 | for(int i = accountsToReturnList.size() - 1; i >= 0 ; i--) { |
| 3980 | // if account not visible to caller or managed by caller, remove from |
| 3981 | // accounts to return. Note that all accounts visible by default unless |
| 3982 | // visible list functionality implemented |
| 3983 | if(!(isAccountVisible(accountsToReturnList.get(i), callingUid, |
| 3984 | getUserAccounts(userId)))) { |
| 3985 | accountsToReturnList.remove(i); |
| 3986 | } |
| 3987 | } |
| 3988 | return accountsToReturnList.toArray(new Account[accountsToReturnList.size()]); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 3989 | } finally { |
| 3990 | restoreCallingIdentity(identityToken); |
| 3991 | } |
| 3992 | } |
| 3993 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3994 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3995 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3996 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3997 | int callingUid, |
| 3998 | String callingPackage, |
| 3999 | List<String> visibleAccountTypes) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4000 | synchronized (userAccounts.cacheLock) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4001 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4002 | for (String visibleType : visibleAccountTypes) { |
| 4003 | Account[] accountsForType = getAccountsFromCacheLocked( |
| 4004 | userAccounts, visibleType, callingUid, callingPackage); |
| 4005 | if (accountsForType != null) { |
| 4006 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
| 4007 | } |
| 4008 | } |
| 4009 | Account[] result = new Account[visibleAccounts.size()]; |
| 4010 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4011 | result[i] = visibleAccounts.get(i); |
| 4012 | } |
| 4013 | return result; |
| 4014 | } |
| 4015 | } |
| 4016 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4017 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4018 | public void addSharedAccountsFromParentUser(int parentUserId, int userId) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4019 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4020 | Account[] accounts = getAccountsAsUser(null, parentUserId, mContext.getOpPackageName()); |
| 4021 | for (Account account : accounts) { |
| 4022 | addSharedAccountAsUser(account, userId); |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4027 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4028 | UserAccounts accounts = getUserAccounts(userId); |
| 4029 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4030 | AccountsDbUtils.deleteSharedAccount(db, account); |
| 4031 | long accountId = AccountsDbUtils.insertSharedAccount(db, account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4032 | if (accountId < 0) { |
| 4033 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4034 | + ", skipping the DB insert failed"); |
| 4035 | return false; |
| 4036 | } |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4037 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_ADD, TABLE_SHARED_ACCOUNTS, accountId, accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4038 | return true; |
| 4039 | } |
| 4040 | |
| 4041 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4042 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4043 | userId = handleIncomingUser(userId); |
| 4044 | UserAccounts accounts = getUserAccounts(userId); |
| 4045 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4046 | long sharedTableAccountId = AccountsDbUtils.findSharedAccountId(db, account); |
| 4047 | int r = AccountsDbUtils.renameSharedAccount(db, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4048 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4049 | int callingUid = getCallingUid(); |
| 4050 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_RENAME, TABLE_SHARED_ACCOUNTS, |
| 4051 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4052 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4053 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4054 | } |
| 4055 | return r > 0; |
| 4056 | } |
| 4057 | |
| 4058 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4059 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4060 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4061 | } |
| 4062 | |
| 4063 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4064 | userId = handleIncomingUser(userId); |
| 4065 | UserAccounts accounts = getUserAccounts(userId); |
| 4066 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4067 | long sharedTableAccountId = AccountsDbUtils.findSharedAccountId(db, account); |
| 4068 | boolean deleted = AccountsDbUtils.deleteSharedAccount(db, account); |
| 4069 | if (deleted) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4070 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_REMOVE, TABLE_SHARED_ACCOUNTS, |
| 4071 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4072 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4073 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4074 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | @Override |
| 4078 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4079 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4080 | SQLiteDatabase db = getUserAccounts(userId).openHelper.getReadableDatabase(); |
| 4081 | List<Account> accountList = AccountsDbUtils.getSharedAccounts(db); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4082 | Account[] accountArray = new Account[accountList.size()]; |
| 4083 | accountList.toArray(accountArray); |
| 4084 | return accountArray; |
| 4085 | } |
| 4086 | |
| 4087 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4088 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4089 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4090 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4091 | } |
| 4092 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4093 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4094 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4095 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4096 | int callingUid = Binder.getCallingUid(); |
| 4097 | if (!UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 4098 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4099 | + callingUid + " with uid=" + uid); |
| 4100 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4101 | return getAccountsAsUser(null, UserHandle.getCallingUserId(), packageName, uid, |
| 4102 | opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4103 | } |
| 4104 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4105 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4106 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4107 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4108 | String opPackageName) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4109 | int packageUid = -1; |
| 4110 | try { |
| 4111 | packageUid = AppGlobals.getPackageManager().getPackageUid( |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 4112 | packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 4113 | UserHandle.getCallingUserId()); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4114 | } catch (RemoteException re) { |
| 4115 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
| 4116 | return new Account[0]; |
| 4117 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4118 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), packageName, |
| 4119 | packageUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4120 | } |
| 4121 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4122 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4123 | public void getAccountsByFeatures( |
| 4124 | IAccountManagerResponse response, |
| 4125 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4126 | String[] features, |
| 4127 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4128 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4129 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4130 | Log.v(TAG, "getAccounts: accountType " + type |
| 4131 | + ", response " + response |
| 4132 | + ", features " + stringArrayToString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4133 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4134 | + ", pid " + Binder.getCallingPid()); |
| 4135 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4136 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4137 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4138 | int userId = UserHandle.getCallingUserId(); |
| 4139 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4140 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4141 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4142 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4143 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4144 | // Need to return just the accounts that are from matching signatures. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4145 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, new Account[0]); |
| 4146 | try { |
| 4147 | response.onResult(result); |
| 4148 | } catch (RemoteException e) { |
| 4149 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4150 | } |
| 4151 | return; |
| 4152 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4153 | long identityToken = clearCallingIdentity(); |
| 4154 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4155 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4156 | if (features == null || features.length == 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4157 | Account[] accounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4158 | synchronized (userAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4159 | accounts = getAccountsFromCacheLocked(userAccounts, type, callingUid, null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4160 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4161 | Bundle result = new Bundle(); |
| 4162 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4163 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4164 | return; |
| 4165 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4166 | new GetAccountsByTypeAndFeatureSession( |
| 4167 | userAccounts, |
| 4168 | response, |
| 4169 | type, |
| 4170 | features, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4171 | callingUid).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4172 | } finally { |
| 4173 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4174 | } |
| 4175 | } |
| 4176 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4177 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4178 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4179 | IAccountManagerResponse mResponse; |
| 4180 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4181 | final boolean mExpectActivityLaunch; |
| 4182 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4183 | final String mAccountName; |
| 4184 | // Indicates if we need to add auth details(like last credential time) |
| 4185 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4186 | // If set, we need to update the last authenticated time. This is |
| 4187 | // currently |
| 4188 | // used on |
| 4189 | // successful confirming credentials. |
| 4190 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4191 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4192 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4193 | private int mNumRequestContinued = 0; |
| 4194 | private int mNumErrors = 0; |
| 4195 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4196 | IAccountAuthenticator mAuthenticator = null; |
| 4197 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4198 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4199 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4200 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4201 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4202 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4203 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4204 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4205 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4206 | } |
| 4207 | |
| 4208 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4209 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4210 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4211 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4212 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4213 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4214 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4215 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4216 | mResponse = response; |
| 4217 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4218 | mExpectActivityLaunch = expectActivityLaunch; |
| 4219 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4220 | mAccountName = accountName; |
| 4221 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4222 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4223 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4224 | synchronized (mSessions) { |
| 4225 | mSessions.put(toString(), this); |
| 4226 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4227 | if (response != null) { |
| 4228 | try { |
| 4229 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4230 | } catch (RemoteException e) { |
| 4231 | mResponse = null; |
| 4232 | binderDied(); |
| 4233 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4234 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4235 | } |
| 4236 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4237 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4238 | if (mResponse == null) { |
| 4239 | // this session has already been closed |
| 4240 | return null; |
| 4241 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4242 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4243 | 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] | 4244 | return response; |
| 4245 | } |
| 4246 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4247 | /** |
| 4248 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4249 | * security policy. |
| 4250 | * |
| 4251 | * In particular we want to make sure that the Authenticator doesn't try to trick users |
| 4252 | * into launching aribtrary intents on the device via by tricking to click authenticator |
| 4253 | * supplied entries in the system Settings app. |
| 4254 | */ |
| 4255 | protected void checkKeyIntent( |
| 4256 | int authUid, |
| 4257 | Intent intent) throws SecurityException { |
| 4258 | long bid = Binder.clearCallingIdentity(); |
| 4259 | try { |
| 4260 | PackageManager pm = mContext.getPackageManager(); |
| 4261 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 4262 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4263 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Sandra Kwan | 0e961a1 | 2016-06-30 14:34:01 -0700 | [diff] [blame] | 4264 | if (!GrantCredentialsPermissionActivity.class.getName().equals( |
| 4265 | targetActivityInfo.getClass().getName()) |
| 4266 | && !CantAddAccountActivity.class |
| 4267 | .equals(targetActivityInfo.getClass().getName()) |
| 4268 | && PackageManager.SIGNATURE_MATCH != pm.checkSignatures(authUid, |
| 4269 | targetUid)) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4270 | String pkgName = targetActivityInfo.packageName; |
| 4271 | String activityName = targetActivityInfo.name; |
| 4272 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4273 | + "does not share a signature with the supplying authenticator (%s)."; |
| 4274 | throw new SecurityException( |
| 4275 | String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4276 | } |
| 4277 | } finally { |
| 4278 | Binder.restoreCallingIdentity(bid); |
| 4279 | } |
| 4280 | } |
| 4281 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4282 | private void close() { |
| 4283 | synchronized (mSessions) { |
| 4284 | if (mSessions.remove(toString()) == null) { |
| 4285 | // the session was already closed, so bail out now |
| 4286 | return; |
| 4287 | } |
| 4288 | } |
| 4289 | if (mResponse != null) { |
| 4290 | // stop listening for response deaths |
| 4291 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4292 | |
| 4293 | // clear this so that we don't accidentally send any further results |
| 4294 | mResponse = null; |
| 4295 | } |
| 4296 | cancelTimeout(); |
| 4297 | unbind(); |
| 4298 | } |
| 4299 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4300 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4301 | public void binderDied() { |
| 4302 | mResponse = null; |
| 4303 | close(); |
| 4304 | } |
| 4305 | |
| 4306 | protected String toDebugString() { |
| 4307 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4308 | } |
| 4309 | |
| 4310 | protected String toDebugString(long now) { |
| 4311 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4312 | + ", connected " + (mAuthenticator != null) |
| 4313 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4314 | + "/" + mNumErrors + ")" |
| 4315 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4316 | } |
| 4317 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4318 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4319 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4320 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4321 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4322 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4323 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4324 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4325 | } |
| 4326 | } |
| 4327 | |
| 4328 | private void unbind() { |
| 4329 | if (mAuthenticator != null) { |
| 4330 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4331 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4332 | } |
| 4333 | } |
| 4334 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4335 | public void cancelTimeout() { |
| 4336 | mMessageHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
| 4337 | } |
| 4338 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4339 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4340 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4341 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4342 | try { |
| 4343 | run(); |
| 4344 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4345 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4346 | "remote exception"); |
| 4347 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4348 | } |
| 4349 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4350 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4351 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4352 | mAuthenticator = null; |
| 4353 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4354 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4355 | try { |
| 4356 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4357 | "disconnected"); |
| 4358 | } catch (RemoteException e) { |
| 4359 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4360 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4361 | + "caught RemoteException while responding", e); |
| 4362 | } |
| 4363 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4364 | } |
| 4365 | } |
| 4366 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4367 | public abstract void run() throws RemoteException; |
| 4368 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4369 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4370 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4371 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4372 | try { |
| 4373 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4374 | "timeout"); |
| 4375 | } catch (RemoteException e) { |
| 4376 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4377 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4378 | e); |
| 4379 | } |
| 4380 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4381 | } |
| 4382 | } |
| 4383 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4384 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4385 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4386 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4387 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4388 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4389 | if (result != null) { |
| 4390 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4391 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4392 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4393 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4394 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4395 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4396 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4397 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4398 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4399 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4400 | if (needUpdate || mAuthDetailsRequired) { |
| 4401 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4402 | if (needUpdate && accountPresent) { |
| 4403 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4404 | } |
| 4405 | if (mAuthDetailsRequired) { |
| 4406 | long lastAuthenticatedTime = -1; |
| 4407 | if (accountPresent) { |
| 4408 | lastAuthenticatedTime = DatabaseUtils.longForQuery( |
| 4409 | mAccounts.openHelper.getReadableDatabase(), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4410 | "SELECT " + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS |
| 4411 | + " FROM " + |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4412 | TABLE_ACCOUNTS + " WHERE " + ACCOUNTS_NAME + "=? AND " |
| 4413 | + ACCOUNTS_TYPE + "=?", |
| 4414 | new String[] { |
| 4415 | mAccountName, mAccountType |
| 4416 | }); |
| 4417 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4418 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4419 | lastAuthenticatedTime); |
| 4420 | } |
| 4421 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4422 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4423 | if (result != null |
| 4424 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4425 | checkKeyIntent( |
| 4426 | Binder.getCallingUid(), |
| 4427 | intent); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4428 | } |
| 4429 | if (result != null |
| 4430 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4431 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4432 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4433 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4434 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4435 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4436 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4437 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4438 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4439 | IAccountManagerResponse response; |
| 4440 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4441 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4442 | response = mResponse; |
| 4443 | } else { |
| 4444 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4445 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4446 | if (response != null) { |
| 4447 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4448 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4449 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4450 | Log.v(TAG, getClass().getSimpleName() |
| 4451 | + " calling onError() on response " + response); |
| 4452 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4453 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4454 | "null bundle returned"); |
| 4455 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4456 | if (mStripAuthTokenFromResult) { |
| 4457 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4458 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4459 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4460 | Log.v(TAG, getClass().getSimpleName() |
| 4461 | + " calling onResult() on response " + response); |
| 4462 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4463 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4464 | (intent == null)) { |
| 4465 | // All AccountManager error codes are greater than 0 |
| 4466 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4467 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4468 | } else { |
| 4469 | response.onResult(result); |
| 4470 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4471 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4472 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4473 | // if the caller is dead then there is no one to care about remote exceptions |
| 4474 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4475 | Log.v(TAG, "failure while notifying response", e); |
| 4476 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4477 | } |
| 4478 | } |
| 4479 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4480 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4481 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4482 | public void onRequestContinued() { |
| 4483 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4484 | } |
| 4485 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4486 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4487 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4488 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4489 | IAccountManagerResponse response = getResponseAndClose(); |
| 4490 | if (response != null) { |
| 4491 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4492 | Log.v(TAG, getClass().getSimpleName() |
| 4493 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4494 | } |
| 4495 | try { |
| 4496 | response.onError(errorCode, errorMessage); |
| 4497 | } catch (RemoteException e) { |
| 4498 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4499 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4500 | } |
| 4501 | } |
| 4502 | } else { |
| 4503 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4504 | Log.v(TAG, "Session.onError: already closed"); |
| 4505 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4506 | } |
| 4507 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4508 | |
| 4509 | /** |
| 4510 | * find the component name for the authenticator and initiate a bind |
| 4511 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4512 | */ |
| 4513 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4514 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4515 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4516 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4517 | if (authenticatorInfo == null) { |
| 4518 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4519 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 4520 | + ", bailing out"); |
| 4521 | } |
| 4522 | return false; |
| 4523 | } |
| 4524 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 4525 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 4526 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 4527 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 4528 | + " which isn't encryption aware"); |
| 4529 | return false; |
| 4530 | } |
| 4531 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4532 | Intent intent = new Intent(); |
| 4533 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 4534 | intent.setComponent(authenticatorInfo.componentName); |
| 4535 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4536 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 4537 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 4538 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4539 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4540 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4541 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 4542 | } |
| 4543 | return false; |
| 4544 | } |
| 4545 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4546 | return true; |
| 4547 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | private class MessageHandler extends Handler { |
| 4551 | MessageHandler(Looper looper) { |
| 4552 | super(looper); |
| 4553 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4554 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4555 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4556 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4557 | switch (msg.what) { |
| 4558 | case MESSAGE_TIMED_OUT: |
| 4559 | Session session = (Session)msg.obj; |
| 4560 | session.onTimedOut(); |
| 4561 | break; |
| 4562 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4563 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 4564 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4565 | break; |
| 4566 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4567 | default: |
| 4568 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 4569 | } |
| 4570 | } |
| 4571 | } |
| 4572 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 4573 | @VisibleForTesting |
| 4574 | String getPreNDatabaseName(int userId) { |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 4575 | File systemDir = Environment.getDataSystemDirectory(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4576 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 4577 | PRE_N_DATABASE_NAME); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4578 | if (userId == 0) { |
Amith Yamasani | a23bb38 | 2012-04-11 15:32:07 -0700 | [diff] [blame] | 4579 | // Migrate old file, if it exists, to the new location. |
| 4580 | // Make sure the new file doesn't already exist. A dummy file could have been |
| 4581 | // accidentally created in the old location, causing the new one to become corrupted |
| 4582 | // as well. |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4583 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
Amith Yamasani | a23bb38 | 2012-04-11 15:32:07 -0700 | [diff] [blame] | 4584 | if (oldFile.exists() && !databaseFile.exists()) { |
Marc Blank | c6b0f99 | 2012-03-18 19:16:41 -0700 | [diff] [blame] | 4585 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 4586 | File userDir = Environment.getUserSystemDirectory(userId); |
Marc Blank | c6b0f99 | 2012-03-18 19:16:41 -0700 | [diff] [blame] | 4587 | if (!userDir.exists()) { |
| 4588 | if (!userDir.mkdirs()) { |
| 4589 | throw new IllegalStateException("User dir cannot be created: " + userDir); |
| 4590 | } |
| 4591 | } |
| 4592 | if (!oldFile.renameTo(databaseFile)) { |
| 4593 | throw new IllegalStateException("User dir cannot be migrated: " + databaseFile); |
| 4594 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4595 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 4596 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4597 | return databaseFile.getPath(); |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 4598 | } |
| 4599 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 4600 | @VisibleForTesting |
| 4601 | String getDeDatabaseName(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4602 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 4603 | DE_DATABASE_NAME); |
| 4604 | return databaseFile.getPath(); |
| 4605 | } |
| 4606 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 4607 | @VisibleForTesting |
| 4608 | String getCeDatabaseName(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4609 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 4610 | CE_DATABASE_NAME); |
| 4611 | return databaseFile.getPath(); |
| 4612 | } |
| 4613 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4614 | private static class DebugDbHelper{ |
| 4615 | private DebugDbHelper() { |
| 4616 | } |
| 4617 | |
| 4618 | private static String TABLE_DEBUG = "debug_table"; |
| 4619 | |
| 4620 | // Columns for the table |
| 4621 | private static String ACTION_TYPE = "action_type"; |
| 4622 | private static String TIMESTAMP = "time"; |
| 4623 | private static String CALLER_UID = "caller_uid"; |
| 4624 | private static String TABLE_NAME = "table_name"; |
| 4625 | private static String KEY = "primary_key"; |
| 4626 | |
| 4627 | // These actions correspond to the occurrence of real actions. Since |
| 4628 | // these are called by the authenticators, the uid associated will be |
| 4629 | // of the authenticator. |
| 4630 | private static String ACTION_SET_PASSWORD = "action_set_password"; |
| 4631 | private static String ACTION_CLEAR_PASSWORD = "action_clear_password"; |
| 4632 | private static String ACTION_ACCOUNT_ADD = "action_account_add"; |
| 4633 | private static String ACTION_ACCOUNT_REMOVE = "action_account_remove"; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 4634 | private static String ACTION_ACCOUNT_REMOVE_DE = "action_account_remove_de"; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4635 | private static String ACTION_AUTHENTICATOR_REMOVE = "action_authenticator_remove"; |
| 4636 | private static String ACTION_ACCOUNT_RENAME = "action_account_rename"; |
| 4637 | |
| 4638 | // These actions don't necessarily correspond to any action on |
| 4639 | // accountDb taking place. As an example, there might be a request for |
| 4640 | // addingAccount, which might not lead to addition of account on grounds |
| 4641 | // of bad authentication. We will still be logging it to keep track of |
| 4642 | // who called. |
| 4643 | private static String ACTION_CALLED_ACCOUNT_ADD = "action_called_account_add"; |
| 4644 | private static String ACTION_CALLED_ACCOUNT_REMOVE = "action_called_account_remove"; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 4645 | private static String ACTION_SYNC_DE_CE_ACCOUNTS = "action_sync_de_ce_accounts"; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4646 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 4647 | //This action doesn't add account to accountdb. Account is only |
| 4648 | // added in finishSession which may be in a different user profile. |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 4649 | private static String ACTION_CALLED_START_ACCOUNT_ADD = "action_called_start_account_add"; |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 4650 | private static String ACTION_CALLED_ACCOUNT_SESSION_FINISH = |
| 4651 | "action_called_account_session_finish"; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 4652 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4653 | private static SimpleDateFormat dateFromat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 4654 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4655 | private static void createDebugTable(SQLiteDatabase db) { |
| 4656 | db.execSQL("CREATE TABLE " + TABLE_DEBUG + " ( " |
| 4657 | + ACCOUNTS_ID + " INTEGER," |
| 4658 | + ACTION_TYPE + " TEXT NOT NULL, " |
| 4659 | + TIMESTAMP + " DATETIME," |
| 4660 | + CALLER_UID + " INTEGER NOT NULL," |
| 4661 | + TABLE_NAME + " TEXT NOT NULL," |
| 4662 | + KEY + " INTEGER PRIMARY KEY)"); |
| 4663 | db.execSQL("CREATE INDEX timestamp_index ON " + TABLE_DEBUG + " (" + TIMESTAMP + ")"); |
| 4664 | } |
| 4665 | } |
| 4666 | |
| 4667 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
| 4668 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 4669 | logRecord(db, action, tableName, -1, accounts); |
| 4670 | } |
| 4671 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4672 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
| 4673 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 4674 | logRecord(db, action, tableName, -1, accounts, uid); |
| 4675 | } |
| 4676 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4677 | /* |
| 4678 | * This function receives an opened writable database. |
| 4679 | */ |
| 4680 | private void logRecord(SQLiteDatabase db, String action, String tableName, long accountId, |
| 4681 | UserAccounts userAccount) { |
| 4682 | logRecord(db, action, tableName, accountId, userAccount, getCallingUid()); |
| 4683 | } |
| 4684 | |
| 4685 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4686 | * 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] | 4687 | */ |
| 4688 | private void logRecord(SQLiteDatabase db, String action, String tableName, long accountId, |
| 4689 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4690 | |
| 4691 | class LogRecordTask implements Runnable { |
| 4692 | private final String action; |
| 4693 | private final String tableName; |
| 4694 | private final long accountId; |
| 4695 | private final UserAccounts userAccount; |
| 4696 | private final int callingUid; |
| 4697 | private final long userDebugDbInsertionPoint; |
| 4698 | |
| 4699 | LogRecordTask(final String action, |
| 4700 | final String tableName, |
| 4701 | final long accountId, |
| 4702 | final UserAccounts userAccount, |
| 4703 | final int callingUid, |
| 4704 | final long userDebugDbInsertionPoint) { |
| 4705 | this.action = action; |
| 4706 | this.tableName = tableName; |
| 4707 | this.accountId = accountId; |
| 4708 | this.userAccount = userAccount; |
| 4709 | this.callingUid = callingUid; |
| 4710 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 4711 | } |
| 4712 | |
| 4713 | public void run() { |
| 4714 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 4715 | logStatement.bindLong(1, accountId); |
| 4716 | logStatement.bindString(2, action); |
| 4717 | logStatement.bindString(3, DebugDbHelper.dateFromat.format(new Date())); |
| 4718 | logStatement.bindLong(4, callingUid); |
| 4719 | logStatement.bindString(5, tableName); |
| 4720 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 4721 | logStatement.execute(); |
| 4722 | logStatement.clearBindings(); |
| 4723 | } |
| 4724 | } |
| 4725 | |
| 4726 | mLogRecordRunnables.add(new LogRecordTask(action, tableName, accountId, userAccount, |
| 4727 | callingUid, userAccount.debugDbInsertionPoint)); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4728 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
| 4729 | % MAX_DEBUG_DB_SIZE; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4730 | |
| 4731 | |
| 4732 | if(mLogRecordThread == null || !mLogRecordThread.isAlive()) { |
| 4733 | mLogRecordThread = new Thread(new Runnable() { |
| 4734 | public void run() { |
| 4735 | while(mLogRecordRunnables.size() > 0) { |
| 4736 | mLogRecordRunnables.pollFirst().run(); |
| 4737 | } |
| 4738 | } |
| 4739 | }); |
| 4740 | mLogRecordThread.start(); |
| 4741 | } |
| 4742 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4743 | } |
| 4744 | |
| 4745 | /* |
| 4746 | * This should only be called once to compile the sql statement for logging |
| 4747 | * and to find the insertion point. |
| 4748 | */ |
| 4749 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(SQLiteDatabase db, |
| 4750 | UserAccounts userAccount) { |
| 4751 | // Initialize the count if not done earlier. |
| 4752 | int size = (int) getDebugTableRowCount(db); |
| 4753 | if (size >= MAX_DEBUG_DB_SIZE) { |
| 4754 | // Table is full, and we need to find the point where to insert. |
| 4755 | userAccount.debugDbInsertionPoint = (int) getDebugTableInsertionPoint(db); |
| 4756 | } else { |
| 4757 | userAccount.debugDbInsertionPoint = size; |
| 4758 | } |
| 4759 | compileSqlStatementForLogging(db, userAccount); |
| 4760 | } |
| 4761 | |
| 4762 | private void compileSqlStatementForLogging(SQLiteDatabase db, UserAccounts userAccount) { |
| 4763 | String sql = "INSERT OR REPLACE INTO " + DebugDbHelper.TABLE_DEBUG |
| 4764 | + " VALUES (?,?,?,?,?,?)"; |
| 4765 | userAccount.statementForLogging = db.compileStatement(sql); |
| 4766 | } |
| 4767 | |
| 4768 | private long getDebugTableRowCount(SQLiteDatabase db) { |
| 4769 | String queryCountDebugDbRows = "SELECT COUNT(*) FROM " + DebugDbHelper.TABLE_DEBUG; |
| 4770 | return DatabaseUtils.longForQuery(db, queryCountDebugDbRows, null); |
| 4771 | } |
| 4772 | |
| 4773 | /* |
| 4774 | * Finds the row key where the next insertion should take place. This should |
| 4775 | * be invoked only if the table has reached its full capacity. |
| 4776 | */ |
| 4777 | private long getDebugTableInsertionPoint(SQLiteDatabase db) { |
| 4778 | // This query finds the smallest timestamp value (and if 2 records have |
| 4779 | // same timestamp, the choose the lower id). |
| 4780 | String queryCountDebugDbRows = new StringBuilder() |
| 4781 | .append("SELECT ").append(DebugDbHelper.KEY) |
| 4782 | .append(" FROM ").append(DebugDbHelper.TABLE_DEBUG) |
| 4783 | .append(" ORDER BY ") |
| 4784 | .append(DebugDbHelper.TIMESTAMP).append(",").append(DebugDbHelper.KEY) |
| 4785 | .append(" LIMIT 1") |
| 4786 | .toString(); |
| 4787 | return DatabaseUtils.longForQuery(db, queryCountDebugDbRows, null); |
| 4788 | } |
| 4789 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4790 | static class PreNDatabaseHelper extends SQLiteOpenHelper { |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 4791 | private final Context mContext; |
| 4792 | private final int mUserId; |
| 4793 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 4794 | public PreNDatabaseHelper(Context context, int userId, String preNDatabaseName) { |
| 4795 | super(context, preNDatabaseName, null, PRE_N_DATABASE_VERSION); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 4796 | mContext = context; |
| 4797 | mUserId = userId; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4798 | } |
| 4799 | |
| 4800 | @Override |
| 4801 | public void onCreate(SQLiteDatabase db) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4802 | // We use PreNDatabaseHelper only if pre-N db exists |
| 4803 | throw new IllegalStateException("Legacy database cannot be created - only upgraded!"); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4804 | } |
| 4805 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4806 | private void createSharedAccountsTable(SQLiteDatabase db) { |
| 4807 | db.execSQL("CREATE TABLE " + TABLE_SHARED_ACCOUNTS + " ( " |
| 4808 | + ACCOUNTS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 4809 | + ACCOUNTS_NAME + " TEXT NOT NULL, " |
| 4810 | + ACCOUNTS_TYPE + " TEXT NOT NULL, " |
| 4811 | + "UNIQUE(" + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + "))"); |
| 4812 | } |
| 4813 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4814 | private void addLastSuccessfullAuthenticatedTimeColumn(SQLiteDatabase db) { |
| 4815 | db.execSQL("ALTER TABLE " + TABLE_ACCOUNTS + " ADD COLUMN " |
| 4816 | + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS + " DEFAULT 0"); |
| 4817 | } |
| 4818 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4819 | private void addOldAccountNameColumn(SQLiteDatabase db) { |
| 4820 | db.execSQL("ALTER TABLE " + TABLE_ACCOUNTS + " ADD COLUMN " + ACCOUNTS_PREVIOUS_NAME); |
| 4821 | } |
| 4822 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4823 | private void addDebugTable(SQLiteDatabase db) { |
| 4824 | DebugDbHelper.createDebugTable(db); |
| 4825 | } |
| 4826 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4827 | private void createAccountsDeletionTrigger(SQLiteDatabase db) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4828 | db.execSQL("" |
| 4829 | + " CREATE TRIGGER " + TABLE_ACCOUNTS + "Delete DELETE ON " + TABLE_ACCOUNTS |
| 4830 | + " BEGIN" |
| 4831 | + " DELETE FROM " + TABLE_AUTHTOKENS |
| 4832 | + " WHERE " + AUTHTOKENS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
| 4833 | + " DELETE FROM " + TABLE_EXTRAS |
| 4834 | + " WHERE " + EXTRAS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4835 | + " DELETE FROM " + TABLE_GRANTS |
| 4836 | + " WHERE " + GRANTS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4837 | + " END"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4838 | } |
| 4839 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4840 | private void createGrantsTable(SQLiteDatabase db) { |
| 4841 | db.execSQL("CREATE TABLE " + TABLE_GRANTS + " ( " |
| 4842 | + GRANTS_ACCOUNTS_ID + " INTEGER NOT NULL, " |
| 4843 | + GRANTS_AUTH_TOKEN_TYPE + " STRING NOT NULL, " |
| 4844 | + GRANTS_GRANTEE_UID + " INTEGER NOT NULL, " |
| 4845 | + "UNIQUE (" + GRANTS_ACCOUNTS_ID + "," + GRANTS_AUTH_TOKEN_TYPE |
| 4846 | + "," + GRANTS_GRANTEE_UID + "))"); |
| 4847 | } |
| 4848 | |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4849 | private void populateMetaTableWithAuthTypeAndUID(SQLiteDatabase db, |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 4850 | Map<String, Integer> authTypeAndUIDMap) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4851 | for (Entry<String, Integer> entry : authTypeAndUIDMap.entrySet()) { |
| 4852 | AccountsDbUtils.insertMetaAuthTypeAndUid(db, entry.getKey(), entry.getValue()); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 4853 | } |
| 4854 | } |
| 4855 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4856 | /** |
| 4857 | * Pre-N database may need an upgrade before splitting |
| 4858 | */ |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4859 | @Override |
| 4860 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4861 | Log.e(TAG, "upgrade from version " + oldVersion + " to version " + newVersion); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4862 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4863 | if (oldVersion == 1) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4864 | // no longer need to do anything since the work is done |
| 4865 | // when upgrading from version 2 |
| 4866 | oldVersion++; |
| 4867 | } |
| 4868 | |
| 4869 | if (oldVersion == 2) { |
| 4870 | createGrantsTable(db); |
| 4871 | db.execSQL("DROP TRIGGER " + TABLE_ACCOUNTS + "Delete"); |
| 4872 | createAccountsDeletionTrigger(db); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4873 | oldVersion++; |
| 4874 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4875 | |
| 4876 | if (oldVersion == 3) { |
| 4877 | db.execSQL("UPDATE " + TABLE_ACCOUNTS + " SET " + ACCOUNTS_TYPE + |
| 4878 | " = 'com.google' WHERE " + ACCOUNTS_TYPE + " == 'com.google.GAIA'"); |
| 4879 | oldVersion++; |
| 4880 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4881 | |
| 4882 | if (oldVersion == 4) { |
| 4883 | createSharedAccountsTable(db); |
| 4884 | oldVersion++; |
| 4885 | } |
| 4886 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4887 | if (oldVersion == 5) { |
| 4888 | addOldAccountNameColumn(db); |
| 4889 | oldVersion++; |
| 4890 | } |
| 4891 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4892 | if (oldVersion == 6) { |
| 4893 | addLastSuccessfullAuthenticatedTimeColumn(db); |
| 4894 | oldVersion++; |
| 4895 | } |
| 4896 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4897 | if (oldVersion == 7) { |
| 4898 | addDebugTable(db); |
| 4899 | oldVersion++; |
| 4900 | } |
| 4901 | |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 4902 | if (oldVersion == 8) { |
| 4903 | populateMetaTableWithAuthTypeAndUID( |
| 4904 | db, |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4905 | getAuthenticatorTypeAndUIDForUser(mContext, mUserId)); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 4906 | oldVersion++; |
| 4907 | } |
| 4908 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4909 | if (oldVersion != newVersion) { |
| 4910 | Log.e(TAG, "failed to upgrade version " + oldVersion + " to version " + newVersion); |
| 4911 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | @Override |
| 4915 | public void onOpen(SQLiteDatabase db) { |
| 4916 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "opened database " + DATABASE_NAME); |
| 4917 | } |
| 4918 | } |
| 4919 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4920 | static class DeDatabaseHelper extends SQLiteOpenHelper { |
| 4921 | |
| 4922 | private final int mUserId; |
| 4923 | private volatile boolean mCeAttached; |
| 4924 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 4925 | private DeDatabaseHelper(Context context, int userId, String deDatabaseName) { |
| 4926 | super(context, deDatabaseName, null, DE_DATABASE_VERSION); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4927 | mUserId = userId; |
| 4928 | } |
| 4929 | |
| 4930 | /** |
| 4931 | * This call needs to be made while the mCacheLock is held. The way to |
| 4932 | * ensure this is to get the lock any time a method is called ont the DatabaseHelper |
| 4933 | * @param db The database. |
| 4934 | */ |
| 4935 | @Override |
| 4936 | public void onCreate(SQLiteDatabase db) { |
| 4937 | Log.i(TAG, "Creating DE database for user " + mUserId); |
| 4938 | db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + " ( " |
| 4939 | + ACCOUNTS_ID + " INTEGER PRIMARY KEY, " |
| 4940 | + ACCOUNTS_NAME + " TEXT NOT NULL, " |
| 4941 | + ACCOUNTS_TYPE + " TEXT NOT NULL, " |
| 4942 | + ACCOUNTS_PREVIOUS_NAME + " TEXT, " |
| 4943 | + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS + " INTEGER DEFAULT 0, " |
| 4944 | + "UNIQUE(" + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + "))"); |
| 4945 | |
| 4946 | db.execSQL("CREATE TABLE " + TABLE_META + " ( " |
| 4947 | + META_KEY + " TEXT PRIMARY KEY NOT NULL, " |
| 4948 | + META_VALUE + " TEXT)"); |
| 4949 | |
| 4950 | createGrantsTable(db); |
| 4951 | createSharedAccountsTable(db); |
| 4952 | createAccountsDeletionTrigger(db); |
| 4953 | DebugDbHelper.createDebugTable(db); |
| 4954 | } |
| 4955 | |
| 4956 | private void createSharedAccountsTable(SQLiteDatabase db) { |
| 4957 | db.execSQL("CREATE TABLE " + TABLE_SHARED_ACCOUNTS + " ( " |
| 4958 | + ACCOUNTS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 4959 | + ACCOUNTS_NAME + " TEXT NOT NULL, " |
| 4960 | + ACCOUNTS_TYPE + " TEXT NOT NULL, " |
| 4961 | + "UNIQUE(" + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + "))"); |
| 4962 | } |
| 4963 | |
| 4964 | private void createAccountsDeletionTrigger(SQLiteDatabase db) { |
| 4965 | db.execSQL("" |
| 4966 | + " CREATE TRIGGER " + TABLE_ACCOUNTS + "Delete DELETE ON " + TABLE_ACCOUNTS |
| 4967 | + " BEGIN" |
| 4968 | + " DELETE FROM " + TABLE_GRANTS |
| 4969 | + " WHERE " + GRANTS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
| 4970 | + " END"); |
| 4971 | } |
| 4972 | |
| 4973 | private void createGrantsTable(SQLiteDatabase db) { |
| 4974 | db.execSQL("CREATE TABLE " + TABLE_GRANTS + " ( " |
| 4975 | + GRANTS_ACCOUNTS_ID + " INTEGER NOT NULL, " |
| 4976 | + GRANTS_AUTH_TOKEN_TYPE + " STRING NOT NULL, " |
| 4977 | + GRANTS_GRANTEE_UID + " INTEGER NOT NULL, " |
| 4978 | + "UNIQUE (" + GRANTS_ACCOUNTS_ID + "," + GRANTS_AUTH_TOKEN_TYPE |
| 4979 | + "," + GRANTS_GRANTEE_UID + "))"); |
| 4980 | } |
| 4981 | |
| 4982 | @Override |
| 4983 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 4984 | Log.i(TAG, "upgrade from version " + oldVersion + " to version " + newVersion); |
| 4985 | |
| 4986 | if (oldVersion != newVersion) { |
| 4987 | Log.e(TAG, "failed to upgrade version " + oldVersion + " to version " + newVersion); |
| 4988 | } |
| 4989 | } |
| 4990 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 4991 | public void attachCeDatabase(File ceDbFile) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4992 | SQLiteDatabase db = getWritableDatabase(); |
| 4993 | db.execSQL("ATTACH DATABASE '" + ceDbFile.getPath()+ "' AS ceDb"); |
| 4994 | mCeAttached = true; |
| 4995 | } |
| 4996 | |
| 4997 | public boolean isCeDatabaseAttached() { |
| 4998 | return mCeAttached; |
| 4999 | } |
| 5000 | |
| 5001 | |
| 5002 | public SQLiteDatabase getReadableDatabaseUserIsUnlocked() { |
| 5003 | if(!mCeAttached) { |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5004 | Log.wtf(TAG, "getReadableDatabaseUserIsUnlocked called while user " + mUserId |
| 5005 | + " is still locked. CE database is not yet available.", new Throwable()); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5006 | } |
| 5007 | return super.getReadableDatabase(); |
| 5008 | } |
| 5009 | |
| 5010 | public SQLiteDatabase getWritableDatabaseUserIsUnlocked() { |
| 5011 | if(!mCeAttached) { |
| 5012 | Log.wtf(TAG, "getWritableDatabaseUserIsUnlocked called while user " + mUserId |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5013 | + " is still locked. CE database is not yet available.", new Throwable()); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5014 | } |
| 5015 | return super.getWritableDatabase(); |
| 5016 | } |
| 5017 | |
| 5018 | @Override |
| 5019 | public void onOpen(SQLiteDatabase db) { |
| 5020 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "opened database " + DE_DATABASE_NAME); |
| 5021 | } |
| 5022 | |
| 5023 | private void migratePreNDbToDe(File preNDbFile) { |
| 5024 | Log.i(TAG, "Migrate pre-N database to DE preNDbFile=" + preNDbFile); |
| 5025 | SQLiteDatabase db = getWritableDatabase(); |
| 5026 | db.execSQL("ATTACH DATABASE '" + preNDbFile.getPath() + "' AS preNDb"); |
| 5027 | db.beginTransaction(); |
| 5028 | // Copy accounts fields |
| 5029 | db.execSQL("INSERT INTO " + TABLE_ACCOUNTS |
| 5030 | + "(" + ACCOUNTS_ID + "," + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + ", " |
| 5031 | + ACCOUNTS_PREVIOUS_NAME + ", " + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS |
| 5032 | + ") " |
| 5033 | + "SELECT " + ACCOUNTS_ID + "," + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + ", " |
| 5034 | + ACCOUNTS_PREVIOUS_NAME + ", " + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS |
| 5035 | + " FROM preNDb." + TABLE_ACCOUNTS); |
| 5036 | // Copy SHARED_ACCOUNTS |
| 5037 | db.execSQL("INSERT INTO " + TABLE_SHARED_ACCOUNTS |
| 5038 | + "(" + SHARED_ACCOUNTS_ID + "," + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + ") " + |
| 5039 | "SELECT " + SHARED_ACCOUNTS_ID + "," + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE |
| 5040 | + " FROM preNDb." + TABLE_SHARED_ACCOUNTS); |
| 5041 | // Copy DEBUG_TABLE |
| 5042 | db.execSQL("INSERT INTO " + DebugDbHelper.TABLE_DEBUG |
| 5043 | + "(" + ACCOUNTS_ID + "," + DebugDbHelper.ACTION_TYPE + "," |
| 5044 | + DebugDbHelper.TIMESTAMP + "," + DebugDbHelper.CALLER_UID + "," |
| 5045 | + DebugDbHelper.TABLE_NAME + "," + DebugDbHelper.KEY + ") " + |
| 5046 | "SELECT " + ACCOUNTS_ID + "," + DebugDbHelper.ACTION_TYPE + "," |
| 5047 | + DebugDbHelper.TIMESTAMP + "," + DebugDbHelper.CALLER_UID + "," |
| 5048 | + DebugDbHelper.TABLE_NAME + "," + DebugDbHelper.KEY |
| 5049 | + " FROM preNDb." + DebugDbHelper.TABLE_DEBUG); |
| 5050 | // Copy GRANTS |
| 5051 | db.execSQL("INSERT INTO " + TABLE_GRANTS |
| 5052 | + "(" + GRANTS_ACCOUNTS_ID + "," + GRANTS_AUTH_TOKEN_TYPE + "," |
| 5053 | + GRANTS_GRANTEE_UID + ") " + |
| 5054 | "SELECT " + GRANTS_ACCOUNTS_ID + "," + GRANTS_AUTH_TOKEN_TYPE + "," |
| 5055 | + GRANTS_GRANTEE_UID + " FROM preNDb." + TABLE_GRANTS); |
| 5056 | // Copy META |
| 5057 | db.execSQL("INSERT INTO " + TABLE_META |
| 5058 | + "(" + META_KEY + "," + META_VALUE + ") " |
| 5059 | + "SELECT " + META_KEY + "," + META_VALUE + " FROM preNDb." + TABLE_META); |
| 5060 | db.setTransactionSuccessful(); |
| 5061 | db.endTransaction(); |
| 5062 | |
| 5063 | db.execSQL("DETACH DATABASE preNDb"); |
| 5064 | } |
| 5065 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5066 | static DeDatabaseHelper create( |
| 5067 | Context context, |
| 5068 | int userId, |
| 5069 | File preNDatabaseFile, |
| 5070 | File deDatabaseFile) { |
| 5071 | boolean newDbExists = deDatabaseFile.exists(); |
| 5072 | DeDatabaseHelper deDatabaseHelper = new DeDatabaseHelper(context, userId, |
| 5073 | deDatabaseFile.getPath()); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5074 | // If the db just created, and there is a legacy db, migrate it |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5075 | if (!newDbExists && preNDatabaseFile.exists()) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5076 | // Migrate legacy db to the latest version - PRE_N_DATABASE_VERSION |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5077 | PreNDatabaseHelper preNDatabaseHelper = new PreNDatabaseHelper(context, userId, |
| 5078 | preNDatabaseFile.getPath()); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5079 | // Open the database to force upgrade if required |
| 5080 | preNDatabaseHelper.getWritableDatabase(); |
| 5081 | preNDatabaseHelper.close(); |
| 5082 | // Move data without SPII to DE |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5083 | deDatabaseHelper.migratePreNDbToDe(preNDatabaseFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5084 | } |
| 5085 | return deDatabaseHelper; |
| 5086 | } |
| 5087 | } |
| 5088 | |
| 5089 | static class CeDatabaseHelper extends SQLiteOpenHelper { |
| 5090 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5091 | public CeDatabaseHelper(Context context, String ceDatabaseName) { |
| 5092 | super(context, ceDatabaseName, null, CE_DATABASE_VERSION); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | /** |
| 5096 | * This call needs to be made while the mCacheLock is held. |
| 5097 | * @param db The database. |
| 5098 | */ |
| 5099 | @Override |
| 5100 | public void onCreate(SQLiteDatabase db) { |
| 5101 | Log.i(TAG, "Creating CE database " + getDatabaseName()); |
| 5102 | db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + " ( " |
| 5103 | + ACCOUNTS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 5104 | + ACCOUNTS_NAME + " TEXT NOT NULL, " |
| 5105 | + ACCOUNTS_TYPE + " TEXT NOT NULL, " |
| 5106 | + ACCOUNTS_PASSWORD + " TEXT, " |
| 5107 | + "UNIQUE(" + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + "))"); |
| 5108 | |
| 5109 | db.execSQL("CREATE TABLE " + TABLE_AUTHTOKENS + " ( " |
| 5110 | + AUTHTOKENS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 5111 | + AUTHTOKENS_ACCOUNTS_ID + " INTEGER NOT NULL, " |
| 5112 | + AUTHTOKENS_TYPE + " TEXT NOT NULL, " |
| 5113 | + AUTHTOKENS_AUTHTOKEN + " TEXT, " |
| 5114 | + "UNIQUE (" + AUTHTOKENS_ACCOUNTS_ID + "," + AUTHTOKENS_TYPE + "))"); |
| 5115 | |
| 5116 | db.execSQL("CREATE TABLE " + TABLE_EXTRAS + " ( " |
| 5117 | + EXTRAS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 5118 | + EXTRAS_ACCOUNTS_ID + " INTEGER, " |
| 5119 | + EXTRAS_KEY + " TEXT NOT NULL, " |
| 5120 | + EXTRAS_VALUE + " TEXT, " |
| 5121 | + "UNIQUE(" + EXTRAS_ACCOUNTS_ID + "," + EXTRAS_KEY + "))"); |
| 5122 | |
| 5123 | createAccountsDeletionTrigger(db); |
| 5124 | } |
| 5125 | |
| 5126 | private void createAccountsDeletionTrigger(SQLiteDatabase db) { |
| 5127 | db.execSQL("" |
| 5128 | + " CREATE TRIGGER " + TABLE_ACCOUNTS + "Delete DELETE ON " + TABLE_ACCOUNTS |
| 5129 | + " BEGIN" |
| 5130 | + " DELETE FROM " + TABLE_AUTHTOKENS |
| 5131 | + " WHERE " + AUTHTOKENS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
| 5132 | + " DELETE FROM " + TABLE_EXTRAS |
| 5133 | + " WHERE " + EXTRAS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
| 5134 | + " END"); |
| 5135 | } |
| 5136 | |
| 5137 | @Override |
| 5138 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 5139 | Log.i(TAG, "Upgrade CE from version " + oldVersion + " to version " + newVersion); |
| 5140 | |
| 5141 | if (oldVersion == 9) { |
| 5142 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5143 | Log.v(TAG, "onUpgrade upgrading to v10"); |
| 5144 | } |
| 5145 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_META); |
| 5146 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_SHARED_ACCOUNTS); |
| 5147 | // Recreate the trigger, since the old one references the table to be removed |
| 5148 | db.execSQL("DROP TRIGGER IF EXISTS " + TABLE_ACCOUNTS + "Delete"); |
| 5149 | createAccountsDeletionTrigger(db); |
| 5150 | db.execSQL("DROP TABLE IF EXISTS " + TABLE_GRANTS); |
| 5151 | db.execSQL("DROP TABLE IF EXISTS " + DebugDbHelper.TABLE_DEBUG); |
| 5152 | oldVersion ++; |
| 5153 | } |
| 5154 | |
| 5155 | if (oldVersion != newVersion) { |
| 5156 | Log.e(TAG, "failed to upgrade version " + oldVersion + " to version " + newVersion); |
| 5157 | } |
| 5158 | } |
| 5159 | |
| 5160 | @Override |
| 5161 | public void onOpen(SQLiteDatabase db) { |
| 5162 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "opened database " + CE_DATABASE_NAME); |
| 5163 | } |
| 5164 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 5165 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5166 | /** |
| 5167 | * Creates a new {@code CeDatabaseHelper}. If pre-N db file is present at the old location, |
| 5168 | * it also performs migration to the new CE database. |
| 5169 | * @param context |
| 5170 | * @param userId id of the user where the database is located |
| 5171 | */ |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5172 | static CeDatabaseHelper create( |
| 5173 | Context context, |
| 5174 | int userId, |
| 5175 | File preNDatabaseFile, |
| 5176 | File ceDatabaseFile) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5177 | boolean newDbExists = ceDatabaseFile.exists(); |
| 5178 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5179 | Log.v(TAG, "CeDatabaseHelper.create userId=" + userId + " oldDbExists=" |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5180 | + preNDatabaseFile.exists() + " newDbExists=" + newDbExists); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5181 | } |
| 5182 | boolean removeOldDb = false; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5183 | if (!newDbExists && preNDatabaseFile.exists()) { |
| 5184 | removeOldDb = migratePreNDbToCe(preNDatabaseFile, ceDatabaseFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5185 | } |
| 5186 | // Try to open and upgrade if necessary |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5187 | CeDatabaseHelper ceHelper = new CeDatabaseHelper(context, ceDatabaseFile.getPath()); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5188 | ceHelper.getWritableDatabase(); |
| 5189 | ceHelper.close(); |
| 5190 | if (removeOldDb) { |
Fyodor Kupolov | 27bd37f | 2016-04-21 11:26:14 -0700 | [diff] [blame] | 5191 | Slog.i(TAG, "Migration complete - removing pre-N db " + preNDatabaseFile); |
| 5192 | if (!SQLiteDatabase.deleteDatabase(preNDatabaseFile)) { |
| 5193 | Slog.e(TAG, "Cannot remove pre-N db " + preNDatabaseFile); |
| 5194 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5195 | } |
| 5196 | return ceHelper; |
| 5197 | } |
| 5198 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5199 | private static boolean migratePreNDbToCe(File oldDbFile, File ceDbFile) { |
Fyodor Kupolov | 27bd37f | 2016-04-21 11:26:14 -0700 | [diff] [blame] | 5200 | Slog.i(TAG, "Moving pre-N DB " + oldDbFile + " to CE " + ceDbFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5201 | try { |
| 5202 | FileUtils.copyFileOrThrow(oldDbFile, ceDbFile); |
| 5203 | } catch (IOException e) { |
Fyodor Kupolov | 27bd37f | 2016-04-21 11:26:14 -0700 | [diff] [blame] | 5204 | Slog.e(TAG, "Cannot copy file to " + ceDbFile + " from " + oldDbFile, e); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5205 | // Try to remove potentially damaged file if I/O error occurred |
| 5206 | deleteDbFileWarnIfFailed(ceDbFile); |
| 5207 | return false; |
| 5208 | } |
| 5209 | return true; |
| 5210 | } |
| 5211 | } |
| 5212 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5213 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5214 | return asBinder(); |
| 5215 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5216 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5217 | /** |
| 5218 | * Searches array of arguments for the specified string |
| 5219 | * @param args array of argument strings |
| 5220 | * @param value value to search for |
| 5221 | * @return true if the value is contained in the array |
| 5222 | */ |
| 5223 | private static boolean scanArgs(String[] args, String value) { |
| 5224 | if (args != null) { |
| 5225 | for (String arg : args) { |
| 5226 | if (value.equals(arg)) { |
| 5227 | return true; |
| 5228 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5229 | } |
| 5230 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5231 | return false; |
| 5232 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5233 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5234 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5235 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 5236 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 5237 | != PackageManager.PERMISSION_GRANTED) { |
| 5238 | fout.println("Permission Denial: can't dump AccountsManager from from pid=" |
| 5239 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 5240 | + " without permission " + android.Manifest.permission.DUMP); |
| 5241 | return; |
| 5242 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5243 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5244 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 5245 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5246 | final List<UserInfo> users = getUserManager().getUsers(); |
| 5247 | for (UserInfo user : users) { |
| 5248 | ipw.println("User " + user + ":"); |
| 5249 | ipw.increaseIndent(); |
| 5250 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 5251 | ipw.println(); |
| 5252 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5253 | } |
| 5254 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5255 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5256 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 5257 | String[] args, boolean isCheckinRequest) { |
| 5258 | synchronized (userAccounts.cacheLock) { |
| 5259 | final SQLiteDatabase db = userAccounts.openHelper.getReadableDatabase(); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5260 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5261 | if (isCheckinRequest) { |
| 5262 | // This is a checkin request. *Only* upload the account types and the count of each. |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5263 | AccountsDbUtils.dumpAccountsTable(db, fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5264 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5265 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5266 | Process.myUid(), null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5267 | fout.println("Accounts: " + accounts.length); |
| 5268 | for (Account account : accounts) { |
| 5269 | fout.println(" " + account); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5270 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 5271 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5272 | // Add debug information. |
| 5273 | fout.println(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5274 | AccountsDbUtils.dumpDebugTable(db, fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5275 | fout.println(); |
| 5276 | synchronized (mSessions) { |
| 5277 | final long now = SystemClock.elapsedRealtime(); |
| 5278 | fout.println("Active Sessions: " + mSessions.size()); |
| 5279 | for (Session session : mSessions.values()) { |
| 5280 | fout.println(" " + session.toDebugString(now)); |
| 5281 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5282 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5283 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5284 | fout.println(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5285 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5286 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5287 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5288 | } |
| 5289 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5290 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 5291 | Intent intent, int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5292 | long identityToken = clearCallingIdentity(); |
| 5293 | try { |
| 5294 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5295 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 5296 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5297 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5298 | if (intent.getComponent() != null && |
| 5299 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 5300 | intent.getComponent().getClassName())) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 5301 | createNoCredentialsPermissionNotification(account, intent, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5302 | } else { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5303 | final Integer notificationId = getSigninRequiredNotificationId(accounts, account); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 5304 | intent.addCategory(String.valueOf(notificationId)); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5305 | UserHandle user = new UserHandle(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5306 | Context contextForUser = getContextForUser(user); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 5307 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5308 | contextForUser.getText(R.string.notification_title).toString(); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 5309 | Notification n = new Notification.Builder(contextForUser) |
| 5310 | .setWhen(0) |
| 5311 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 5312 | .setColor(contextForUser.getColor( |
| 5313 | com.android.internal.R.color.system_notification_accent_color)) |
| 5314 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 5315 | .setContentText(message) |
| 5316 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 5317 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
| 5318 | null, user)) |
| 5319 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5320 | installNotification(notificationId, n, user); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5321 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5322 | } finally { |
| 5323 | restoreCallingIdentity(identityToken); |
| 5324 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5325 | } |
| 5326 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5327 | @VisibleForTesting |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5328 | protected void installNotification(final int notificationId, final Notification n, |
| 5329 | UserHandle user) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5330 | ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)) |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5331 | .notifyAsUser(null, notificationId, n, user); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5332 | } |
| 5333 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5334 | @VisibleForTesting |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5335 | protected void cancelNotification(int id, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5336 | long identityToken = clearCallingIdentity(); |
| 5337 | try { |
| 5338 | ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)) |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5339 | .cancelAsUser(null, id, user); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5340 | } finally { |
| 5341 | restoreCallingIdentity(identityToken); |
| 5342 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5343 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5344 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5345 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 5346 | for (String perm : permissions) { |
| 5347 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 5348 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5349 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 5350 | } |
| 5351 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 5352 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 5353 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 5354 | return true; |
| 5355 | } |
| 5356 | } |
| 5357 | } |
| 5358 | return false; |
| 5359 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5360 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5361 | private int handleIncomingUser(int userId) { |
| 5362 | try { |
| 5363 | return ActivityManagerNative.getDefault().handleIncomingUser( |
| 5364 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 5365 | } catch (RemoteException re) { |
| 5366 | // Shouldn't happen, local. |
| 5367 | } |
| 5368 | return userId; |
| 5369 | } |
| 5370 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 5371 | private boolean isPrivileged(int callingUid) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5372 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 5373 | |
| 5374 | final PackageManager userPackageManager; |
| 5375 | try { |
| 5376 | userPackageManager = mContext.createPackageContextAsUser( |
| 5377 | "android", 0, new UserHandle(callingUserId)).getPackageManager(); |
| 5378 | } catch (NameNotFoundException e) { |
| 5379 | return false; |
| 5380 | } |
| 5381 | |
| 5382 | String[] packages = userPackageManager.getPackagesForUid(callingUid); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5383 | for (String name : packages) { |
| 5384 | try { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5385 | PackageInfo packageInfo = userPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5386 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 5387 | && (packageInfo.applicationInfo.privateFlags |
| 5388 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5389 | return true; |
| 5390 | } |
| 5391 | } catch (PackageManager.NameNotFoundException e) { |
| 5392 | return false; |
| 5393 | } |
| 5394 | } |
| 5395 | return false; |
| 5396 | } |
| 5397 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5398 | private boolean permissionIsGranted( |
| 5399 | Account account, String authTokenType, int callerUid, int userId) { |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 5400 | final boolean isPrivileged = isPrivileged(callerUid); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5401 | final boolean fromAuthenticator = account != null |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5402 | && isAccountManagedByCaller(account.type, callerUid, userId); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5403 | final boolean hasExplicitGrants = account != null |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5404 | && hasExplicitlyGrantedPermission(account, authTokenType, callerUid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5405 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5406 | Log.v(TAG, "checkGrantsOrCallingUidAgainstAuthenticator: caller uid " |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5407 | + callerUid + ", " + account |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5408 | + ": is authenticator? " + fromAuthenticator |
| 5409 | + ", has explicit permission? " + hasExplicitGrants); |
| 5410 | } |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 5411 | return fromAuthenticator || hasExplicitGrants || isPrivileged; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5414 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5415 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5416 | if (accountType == null) { |
| 5417 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5418 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5419 | return getTypesVisibleToCaller(callingUid, userId, |
| 5420 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5421 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5425 | if (accountType == null) { |
| 5426 | return false; |
| 5427 | } else { |
| 5428 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5429 | } |
| 5430 | } |
| 5431 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5432 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5433 | String opPackageName) { |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5434 | boolean isPermitted = |
| 5435 | isPermitted(opPackageName, callingUid, Manifest.permission.GET_ACCOUNTS, |
| 5436 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5437 | return getTypesForCaller(callingUid, userId, isPermitted); |
| 5438 | } |
| 5439 | |
| 5440 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
| 5441 | return getTypesForCaller(callingUid, userId, false); |
| 5442 | } |
| 5443 | |
| 5444 | private List<String> getTypesForCaller( |
| 5445 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5446 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5447 | long identityToken = Binder.clearCallingIdentity(); |
| 5448 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5449 | try { |
| 5450 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5451 | } finally { |
| 5452 | Binder.restoreCallingIdentity(identityToken); |
| 5453 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5454 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5455 | serviceInfos) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5456 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 5457 | if (isOtherwisePermitted || sigChk == PackageManager.SIGNATURE_MATCH) { |
| 5458 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5459 | } |
| 5460 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5461 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5462 | } |
| 5463 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5464 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5465 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5466 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5467 | if (account.name.equals(accountName)) { |
| 5468 | return true; |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | return false; |
| 5473 | } |
| 5474 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5475 | private static void checkManageUsersPermission(String message) { |
| 5476 | if (ActivityManager.checkComponentPermission( |
| 5477 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5478 | != PackageManager.PERMISSION_GRANTED) { |
| 5479 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5480 | } |
| 5481 | } |
| 5482 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5483 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5484 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5485 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5486 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5487 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5488 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5489 | + message); |
| 5490 | } |
| 5491 | } |
| 5492 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5493 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5494 | int callerUid) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5495 | if (callerUid == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5496 | return true; |
| 5497 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5498 | UserAccounts accounts = getUserAccountsForCaller(); |
| 5499 | synchronized (accounts.cacheLock) { |
| 5500 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5501 | final boolean permissionGranted = AccountsDbUtils.findMatchingGrantsCount(db, callerUid, |
| 5502 | authTokenType, account) != 0; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5503 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5504 | // TODO: Skip this check when running automated tests. Replace this |
| 5505 | // with a more general solution. |
| 5506 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5507 | + authTokenType + " by uid " + callerUid |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5508 | + " but ignoring since device is in test harness."); |
| 5509 | return true; |
| 5510 | } |
| 5511 | return permissionGranted; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5512 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5513 | } |
| 5514 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5515 | private boolean isSystemUid(int callingUid) { |
| 5516 | String[] packages = null; |
| 5517 | long ident = Binder.clearCallingIdentity(); |
| 5518 | try { |
| 5519 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5520 | } finally { |
| 5521 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5522 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5523 | if (packages != null) { |
| 5524 | for (String name : packages) { |
| 5525 | try { |
| 5526 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5527 | if (packageInfo != null |
| 5528 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5529 | != 0) { |
| 5530 | return true; |
| 5531 | } |
| 5532 | } catch (PackageManager.NameNotFoundException e) { |
| 5533 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5534 | } |
| 5535 | } |
| 5536 | } else { |
| 5537 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5538 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5539 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5542 | /** Succeeds if any of the specified permissions are granted. */ |
| 5543 | private void checkReadAccountsPermitted( |
| 5544 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5545 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5546 | int userId, |
| 5547 | String opPackageName) { |
| 5548 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5549 | String msg = String.format( |
| 5550 | "caller uid %s cannot access %s accounts", |
| 5551 | callingUid, |
| 5552 | accountType); |
| 5553 | Log.w(TAG, " " + msg); |
| 5554 | throw new SecurityException(msg); |
| 5555 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5556 | } |
| 5557 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5558 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5559 | // the managing app can always modify accounts |
| 5560 | if (isProfileOwner(callingUid)) { |
| 5561 | return true; |
| 5562 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5563 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5564 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5565 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5566 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5567 | return true; |
| 5568 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5569 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5570 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5571 | // the managing app can always modify accounts |
| 5572 | if (isProfileOwner(callingUid)) { |
| 5573 | return true; |
| 5574 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5575 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5576 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5577 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5578 | if (typesArray == null) { |
| 5579 | return true; |
| 5580 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5581 | for (String forbiddenType : typesArray) { |
| 5582 | if (forbiddenType.equals(accountType)) { |
| 5583 | return false; |
| 5584 | } |
| 5585 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5586 | return true; |
| 5587 | } |
| 5588 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5589 | private boolean isProfileOwner(int uid) { |
| 5590 | final DevicePolicyManagerInternal dpmi = |
| 5591 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5592 | return (dpmi != null) |
| 5593 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5594 | } |
| 5595 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5596 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5597 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5598 | throws RemoteException { |
| 5599 | final int callingUid = getCallingUid(); |
| 5600 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5601 | if (callingUid != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5602 | throw new SecurityException(); |
| 5603 | } |
| 5604 | |
| 5605 | if (value) { |
| 5606 | grantAppPermission(account, authTokenType, uid); |
| 5607 | } else { |
| 5608 | revokeAppPermission(account, authTokenType, uid); |
| 5609 | } |
| 5610 | } |
| 5611 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5612 | /** |
| 5613 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5614 | * <p> |
| 5615 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5616 | * which is in the system. This means we don't need to protect it with permissions. |
| 5617 | * @hide |
| 5618 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5619 | private void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5620 | if (account == null || authTokenType == null) { |
| 5621 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5622 | return; |
| 5623 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5624 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5625 | synchronized (accounts.cacheLock) { |
| 5626 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5627 | long accountId = AccountsDbUtils.findAccountId(db, account); |
| 5628 | if (accountId >= 0) { |
| 5629 | AccountsDbUtils.insertGrant(db, accountId, authTokenType, uid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5630 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5631 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5632 | UserHandle.of(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5633 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5634 | } |
| 5635 | |
| 5636 | /** |
| 5637 | * Don't allow callers with the given uid permission to get credentials for |
| 5638 | * account/authTokenType. |
| 5639 | * <p> |
| 5640 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5641 | * which is in the system. This means we don't need to protect it with permissions. |
| 5642 | * @hide |
| 5643 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5644 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5645 | if (account == null || authTokenType == null) { |
| 5646 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5647 | return; |
| 5648 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5649 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5650 | synchronized (accounts.cacheLock) { |
| 5651 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5652 | db.beginTransaction(); |
| 5653 | try { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5654 | long accountId = AccountsDbUtils.findAccountId(db, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5655 | if (accountId >= 0) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5656 | AccountsDbUtils.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5657 | db, accountId, authTokenType, uid); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5658 | db.setTransactionSuccessful(); |
| 5659 | } |
| 5660 | } finally { |
| 5661 | db.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5662 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5663 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5664 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5665 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5666 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5667 | |
| 5668 | static final private String stringArrayToString(String[] value) { |
| 5669 | return value != null ? ("[" + TextUtils.join(",", value) + "]") : null; |
| 5670 | } |
| 5671 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5672 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5673 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5674 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5675 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5676 | for (Account curAccount : oldAccountsForType) { |
| 5677 | if (!curAccount.equals(account)) { |
| 5678 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5679 | } |
| 5680 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5681 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5682 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5683 | } else { |
| 5684 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5685 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5686 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5687 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5688 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5689 | accounts.userDataCache.remove(account); |
| 5690 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5691 | accounts.previousNameCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | /** |
| 5695 | * This assumes that the caller has already checked that the account is not already present. |
| 5696 | */ |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5697 | private void insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
| 5698 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5699 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5700 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5701 | if (accountsForType != null) { |
| 5702 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5703 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5704 | newAccountsForType[oldLength] = account; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5705 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5706 | } |
| 5707 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5708 | private Account[] filterSharedAccounts(UserAccounts userAccounts, Account[] unfiltered, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5709 | int callingUid, String callingPackage) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5710 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5711 | || callingUid == Process.myUid()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5712 | return unfiltered; |
| 5713 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5714 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5715 | if (user != null && user.isRestricted()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5716 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5717 | // 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] | 5718 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5719 | // This might be a temporary way to specify a visible list |
| 5720 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5721 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5722 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5723 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5724 | return unfiltered; |
| 5725 | } |
| 5726 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5727 | ArrayList<Account> allowed = new ArrayList<>(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5728 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
| 5729 | if (sharedAccounts == null || sharedAccounts.length == 0) return unfiltered; |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5730 | String requiredAccountType = ""; |
| 5731 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5732 | // If there's an explicit callingPackage specified, check if that package |
| 5733 | // opted in to see restricted accounts. |
| 5734 | if (callingPackage != null) { |
| 5735 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5736 | if (pi != null && pi.restrictedAccountType != null) { |
| 5737 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5738 | } |
| 5739 | } else { |
| 5740 | // Otherwise check if the callingUid has a package that has opted in |
| 5741 | for (String packageName : packages) { |
| 5742 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5743 | if (pi != null && pi.restrictedAccountType != null) { |
| 5744 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5745 | break; |
| 5746 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5747 | } |
| 5748 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5749 | } catch (NameNotFoundException nnfe) { |
| 5750 | } |
| 5751 | for (Account account : unfiltered) { |
| 5752 | if (account.type.equals(requiredAccountType)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5753 | allowed.add(account); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5754 | } else { |
| 5755 | boolean found = false; |
| 5756 | for (Account shared : sharedAccounts) { |
| 5757 | if (shared.equals(account)) { |
| 5758 | found = true; |
| 5759 | break; |
| 5760 | } |
| 5761 | } |
| 5762 | if (!found) { |
| 5763 | allowed.add(account); |
| 5764 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5765 | } |
| 5766 | } |
| 5767 | Account[] filtered = new Account[allowed.size()]; |
| 5768 | allowed.toArray(filtered); |
| 5769 | return filtered; |
| 5770 | } else { |
| 5771 | return unfiltered; |
| 5772 | } |
| 5773 | } |
| 5774 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5775 | /* |
| 5776 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5777 | * that the package is not allowed to access. |
| 5778 | */ |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5779 | protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5780 | int callingUid, String callingPackage) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5781 | if (accountType != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5782 | final Account[] accounts = userAccounts.accountCache.get(accountType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5783 | if (accounts == null) { |
| 5784 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5785 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5786 | return filterSharedAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5787 | callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5788 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5789 | } else { |
| 5790 | int totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5791 | for (Account[] accounts : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5792 | totalLength += accounts.length; |
| 5793 | } |
| 5794 | if (totalLength == 0) { |
| 5795 | return EMPTY_ACCOUNT_ARRAY; |
| 5796 | } |
| 5797 | Account[] accounts = new Account[totalLength]; |
| 5798 | totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5799 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5800 | System.arraycopy(accountsOfType, 0, accounts, totalLength, |
| 5801 | accountsOfType.length); |
| 5802 | totalLength += accountsOfType.length; |
| 5803 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5804 | return filterSharedAccounts(userAccounts, accounts, callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5805 | } |
| 5806 | } |
| 5807 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5808 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db, |
| 5809 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5810 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5811 | if (userDataForAccount == null) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5812 | userDataForAccount = AccountsDbUtils.findUserExtrasForAccount(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5813 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5814 | } |
| 5815 | if (value == null) { |
| 5816 | userDataForAccount.remove(key); |
| 5817 | } else { |
| 5818 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5819 | } |
| 5820 | } |
| 5821 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5822 | protected String readCachedTokenInternal( |
| 5823 | UserAccounts accounts, |
| 5824 | Account account, |
| 5825 | String tokenType, |
| 5826 | String callingPackage, |
| 5827 | byte[] pkgSigDigest) { |
| 5828 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5829 | return accounts.accountTokenCaches.get( |
| 5830 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5831 | } |
| 5832 | } |
| 5833 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5834 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db, |
| 5835 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5836 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5837 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5838 | authTokensForAccount = AccountsDbUtils.findAuthTokensByAccount(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5839 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5840 | } |
| 5841 | if (value == null) { |
| 5842 | authTokensForAccount.remove(key); |
| 5843 | } else { |
| 5844 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5845 | } |
| 5846 | } |
| 5847 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5848 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 5849 | String authTokenType) { |
| 5850 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5851 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5852 | if (authTokensForAccount == null) { |
| 5853 | // need to populate the cache for this account |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5854 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabaseUserIsUnlocked(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5855 | authTokensForAccount = AccountsDbUtils |
| 5856 | .findAuthTokensByAccount(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5857 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5858 | } |
| 5859 | return authTokensForAccount.get(authTokenType); |
| 5860 | } |
| 5861 | } |
| 5862 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5863 | protected String readUserDataInternalLocked( |
| 5864 | UserAccounts accounts, Account account, String key) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5865 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5866 | if (userDataForAccount == null) { |
| 5867 | // need to populate the cache for this account |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5868 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabaseUserIsUnlocked(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5869 | userDataForAccount = AccountsDbUtils.findUserExtrasForAccount(db, account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5870 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5871 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5872 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5873 | } |
| 5874 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5875 | private Context getContextForUser(UserHandle user) { |
| 5876 | try { |
| 5877 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 5878 | } catch (NameNotFoundException e) { |
| 5879 | // Default to mContext, not finding the package system is running as is unlikely. |
| 5880 | return mContext; |
| 5881 | } |
| 5882 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 5883 | |
| 5884 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 5885 | try { |
| 5886 | response.onResult(result); |
| 5887 | } catch (RemoteException e) { |
| 5888 | // if the caller is dead then there is no one to care about remote |
| 5889 | // exceptions |
| 5890 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5891 | Log.v(TAG, "failure while notifying response", e); |
| 5892 | } |
| 5893 | } |
| 5894 | } |
| 5895 | |
| 5896 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 5897 | String errorMessage) { |
| 5898 | try { |
| 5899 | response.onError(errorCode, errorMessage); |
| 5900 | } catch (RemoteException e) { |
| 5901 | // if the caller is dead then there is no one to care about remote |
| 5902 | // exceptions |
| 5903 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5904 | Log.v(TAG, "failure while notifying response", e); |
| 5905 | } |
| 5906 | } |
| 5907 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5908 | |
| 5909 | static class AccountsDbUtils { |
| 5910 | |
| 5911 | static String findAccountPasswordByNameAndType(SQLiteDatabase db, String name, |
| 5912 | String type) { |
| 5913 | Cursor cursor = db.query(CE_TABLE_ACCOUNTS, new String[]{ACCOUNTS_PASSWORD}, |
| 5914 | ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE + "=?", |
| 5915 | new String[]{name, type}, null, null, null); |
| 5916 | try { |
| 5917 | if (cursor.moveToNext()) { |
| 5918 | return cursor.getString(0); |
| 5919 | } |
| 5920 | return null; |
| 5921 | } finally { |
| 5922 | cursor.close(); |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | static Map<Long, Account> findAllAccounts(SQLiteDatabase db) { |
| 5927 | LinkedHashMap<Long, Account> map = new LinkedHashMap<>(); |
| 5928 | Cursor cursor = db.query(TABLE_ACCOUNTS, |
| 5929 | new String[] {ACCOUNTS_ID, ACCOUNTS_TYPE, ACCOUNTS_NAME}, |
| 5930 | null, null, null, null, ACCOUNTS_ID); |
| 5931 | try { |
| 5932 | while (cursor.moveToNext()) { |
| 5933 | final long accountId = cursor.getLong(0); |
| 5934 | final String accountType = cursor.getString(1); |
| 5935 | final String accountName = cursor.getString(2); |
| 5936 | |
| 5937 | final Account account = new Account(accountName, accountType); |
| 5938 | map.put(accountId, account); |
| 5939 | } |
| 5940 | } finally { |
| 5941 | cursor.close(); |
| 5942 | } |
| 5943 | return map; |
| 5944 | } |
| 5945 | |
| 5946 | static String findAccountPreviousName(SQLiteDatabase db, Account account) { |
| 5947 | Cursor cursor = db.query( |
| 5948 | TABLE_ACCOUNTS, |
| 5949 | new String[]{ ACCOUNTS_PREVIOUS_NAME }, |
| 5950 | ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 5951 | new String[] { account.name, account.type }, |
| 5952 | null, |
| 5953 | null, |
| 5954 | null); |
| 5955 | try { |
| 5956 | if (cursor.moveToNext()) { |
| 5957 | return cursor.getString(0); |
| 5958 | } |
| 5959 | } finally { |
| 5960 | cursor.close(); |
| 5961 | } |
| 5962 | return null; |
| 5963 | } |
| 5964 | |
| 5965 | static List<Account> findCeAccountsNotInDe(SQLiteDatabase db) { |
| 5966 | // Select accounts from CE that do not exist in DE |
| 5967 | Cursor cursor = db.rawQuery( |
| 5968 | "SELECT " + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE |
| 5969 | + " FROM " + CE_TABLE_ACCOUNTS |
| 5970 | + " WHERE NOT EXISTS " |
| 5971 | + " (SELECT " + ACCOUNTS_ID + " FROM " + TABLE_ACCOUNTS |
| 5972 | + " WHERE " + ACCOUNTS_ID + "=" + CE_TABLE_ACCOUNTS + "." + ACCOUNTS_ID |
| 5973 | + " )", null); |
| 5974 | try { |
| 5975 | List<Account> accounts = new ArrayList<>(cursor.getCount()); |
| 5976 | while (cursor.moveToNext()) { |
| 5977 | String accountName = cursor.getString(0); |
| 5978 | String accountType = cursor.getString(1); |
| 5979 | accounts.add(new Account(accountName, accountType)); |
| 5980 | } |
| 5981 | return accounts; |
| 5982 | } finally { |
| 5983 | cursor.close(); |
| 5984 | } |
| 5985 | } |
| 5986 | |
| 5987 | static boolean deleteAccount(SQLiteDatabase db, long accountId) { |
| 5988 | return db.delete(TABLE_ACCOUNTS, ACCOUNTS_ID + "=" + accountId, null) > 0; |
| 5989 | } |
| 5990 | |
| 5991 | static boolean deleteCeAccount(SQLiteDatabase db, long accountId) { |
| 5992 | return db.delete(CE_TABLE_ACCOUNTS, ACCOUNTS_ID + "=" + accountId, null) > 0; |
| 5993 | } |
| 5994 | |
| 5995 | /** |
| 5996 | * Returns information about auth tokens and their account for the specified query parameters. |
| 5997 | * Output is in the format: |
| 5998 | * <pre><code> | AUTHTOKEN_ID | ACCOUNT_NAME | AUTH_TOKEN_TYPE |</code></pre> |
| 5999 | */ |
| 6000 | static Cursor findAuthtokenForAllAccounts(SQLiteDatabase db, String accountType, |
| 6001 | String authToken) { |
| 6002 | return db.rawQuery( |
| 6003 | "SELECT " + CE_TABLE_AUTHTOKENS + "." + AUTHTOKENS_ID |
| 6004 | + ", " + CE_TABLE_ACCOUNTS + "." + ACCOUNTS_NAME |
| 6005 | + ", " + CE_TABLE_AUTHTOKENS + "." + AUTHTOKENS_TYPE |
| 6006 | + " FROM " + CE_TABLE_ACCOUNTS |
| 6007 | + " JOIN " + CE_TABLE_AUTHTOKENS |
| 6008 | + " ON " + CE_TABLE_ACCOUNTS + "." + ACCOUNTS_ID |
| 6009 | + " = " + CE_TABLE_AUTHTOKENS + "." + AUTHTOKENS_ACCOUNTS_ID |
| 6010 | + " WHERE " + CE_TABLE_AUTHTOKENS + "." + AUTHTOKENS_AUTHTOKEN |
| 6011 | + " = ? AND " + CE_TABLE_ACCOUNTS + "." + ACCOUNTS_TYPE + " = ?", |
| 6012 | new String[]{authToken, accountType}); |
| 6013 | } |
| 6014 | |
| 6015 | static boolean deleteAuthtokensByAccountIdAndType(SQLiteDatabase db, long accountId, |
| 6016 | String authtokenType) { |
| 6017 | return db.delete(CE_TABLE_AUTHTOKENS, |
| 6018 | AUTHTOKENS_ACCOUNTS_ID + "=?" + accountId + " AND " + AUTHTOKENS_TYPE + "=?", |
| 6019 | new String[] {String.valueOf(accountId), authtokenType}) > 0; |
| 6020 | } |
| 6021 | |
| 6022 | static boolean deleteAuthToken(SQLiteDatabase db, String authTokenId) { |
| 6023 | return db.delete(CE_TABLE_AUTHTOKENS, AUTHTOKENS_ID + "= ?", |
| 6024 | new String[] {authTokenId}) > 0; |
| 6025 | } |
| 6026 | |
| 6027 | static long insertAuthToken(SQLiteDatabase db, long accountId, String authTokenType, |
| 6028 | String authToken) { |
| 6029 | ContentValues values = new ContentValues(); |
| 6030 | values.put(AUTHTOKENS_ACCOUNTS_ID, accountId); |
| 6031 | values.put(AUTHTOKENS_TYPE, authTokenType); |
| 6032 | values.put(AUTHTOKENS_AUTHTOKEN, authToken); |
| 6033 | return db.insert(CE_TABLE_AUTHTOKENS, AUTHTOKENS_AUTHTOKEN, values); |
| 6034 | } |
| 6035 | |
| 6036 | static Map<String, String> findAuthTokensByAccount(final SQLiteDatabase db, Account account) { |
| 6037 | HashMap<String, String> authTokensForAccount = new HashMap<>(); |
| 6038 | Cursor cursor = db.query(CE_TABLE_AUTHTOKENS, |
| 6039 | COLUMNS_AUTHTOKENS_TYPE_AND_AUTHTOKEN, |
| 6040 | SELECTION_AUTHTOKENS_BY_ACCOUNT, |
| 6041 | new String[]{account.name, account.type}, |
| 6042 | null, null, null); |
| 6043 | try { |
| 6044 | while (cursor.moveToNext()) { |
| 6045 | final String type = cursor.getString(0); |
| 6046 | final String authToken = cursor.getString(1); |
| 6047 | authTokensForAccount.put(type, authToken); |
| 6048 | } |
| 6049 | } finally { |
| 6050 | cursor.close(); |
| 6051 | } |
| 6052 | return authTokensForAccount; |
| 6053 | } |
| 6054 | |
| 6055 | static int updateAccountPassword(SQLiteDatabase db, long accountId, String password) { |
| 6056 | final ContentValues values = new ContentValues(); |
| 6057 | values.put(ACCOUNTS_PASSWORD, password); |
| 6058 | return db.update(CE_TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", |
| 6059 | new String[] {String.valueOf(accountId)}); |
| 6060 | } |
| 6061 | |
| 6062 | static boolean deleteAuthTokensByAccountId(SQLiteDatabase db, long accountId) { |
| 6063 | return db.delete(CE_TABLE_AUTHTOKENS, AUTHTOKENS_ACCOUNTS_ID + "=?", |
| 6064 | new String[] {String.valueOf(accountId)}) > 0; |
| 6065 | } |
| 6066 | |
| 6067 | static long insertSharedAccount(SQLiteDatabase db, Account account) { |
| 6068 | ContentValues values = new ContentValues(); |
| 6069 | values.put(ACCOUNTS_NAME, account.name); |
| 6070 | values.put(ACCOUNTS_TYPE, account.type); |
| 6071 | return db.insert(TABLE_SHARED_ACCOUNTS, ACCOUNTS_NAME, values); |
| 6072 | } |
| 6073 | |
| 6074 | static boolean deleteSharedAccount(SQLiteDatabase db, Account account) { |
| 6075 | return db.delete(TABLE_SHARED_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE + "=?", |
| 6076 | new String[] {account.name, account.type}) > 0; |
| 6077 | } |
| 6078 | |
| 6079 | static int renameSharedAccount(SQLiteDatabase db, Account account, String newName) { |
| 6080 | final ContentValues values = new ContentValues(); |
| 6081 | values.put(ACCOUNTS_NAME, newName); |
| 6082 | return db.update(TABLE_SHARED_ACCOUNTS, |
| 6083 | values, |
| 6084 | ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 6085 | new String[] { account.name, account.type }); |
| 6086 | } |
| 6087 | |
| 6088 | static List<Account> getSharedAccounts(SQLiteDatabase db) { |
| 6089 | ArrayList<Account> accountList = new ArrayList<>(); |
| 6090 | Cursor cursor = null; |
| 6091 | try { |
| 6092 | cursor = db.query(TABLE_SHARED_ACCOUNTS, new String[] {ACCOUNTS_NAME, ACCOUNTS_TYPE}, |
| 6093 | null, null, null, null, null); |
| 6094 | if (cursor != null && cursor.moveToFirst()) { |
| 6095 | int nameIndex = cursor.getColumnIndex(ACCOUNTS_NAME); |
| 6096 | int typeIndex = cursor.getColumnIndex(ACCOUNTS_TYPE); |
| 6097 | do { |
| 6098 | accountList.add(new Account(cursor.getString(nameIndex), |
| 6099 | cursor.getString(typeIndex))); |
| 6100 | } while (cursor.moveToNext()); |
| 6101 | } |
| 6102 | } finally { |
| 6103 | if (cursor != null) { |
| 6104 | cursor.close(); |
| 6105 | } |
| 6106 | } |
| 6107 | return accountList; |
| 6108 | } |
| 6109 | |
| 6110 | static long findSharedAccountId(SQLiteDatabase db, Account account) { |
| 6111 | Cursor cursor = db.query(TABLE_SHARED_ACCOUNTS, new String[]{ACCOUNTS_ID}, |
| 6112 | "name=? AND type=?", new String[]{account.name, account.type}, null, null, null); |
| 6113 | try { |
| 6114 | if (cursor.moveToNext()) { |
| 6115 | return cursor.getLong(0); |
| 6116 | } |
| 6117 | return -1; |
| 6118 | } finally { |
| 6119 | cursor.close(); |
| 6120 | } |
| 6121 | } |
| 6122 | |
| 6123 | static long findAccountId(SQLiteDatabase db, Account account) { |
| 6124 | Cursor cursor = db.query( |
| 6125 | TABLE_ACCOUNTS, new String[] {ACCOUNTS_ID}, |
| 6126 | "name=? AND type=?", new String[]{account.name, account.type}, null, null, null); |
| 6127 | try { |
| 6128 | if (cursor.moveToNext()) { |
| 6129 | return cursor.getLong(0); |
| 6130 | } |
| 6131 | return -1; |
| 6132 | } finally { |
| 6133 | cursor.close(); |
| 6134 | } |
| 6135 | } |
| 6136 | |
| 6137 | static long findExtrasIdByAccountId(SQLiteDatabase db, long accountId, String key) { |
| 6138 | Cursor cursor = db.query( |
| 6139 | CE_TABLE_EXTRAS, new String[] {EXTRAS_ID}, |
| 6140 | EXTRAS_ACCOUNTS_ID + "=" + accountId + " AND " + EXTRAS_KEY + "=?", |
| 6141 | new String[]{key}, null, null, null); |
| 6142 | try { |
| 6143 | if (cursor.moveToNext()) { |
| 6144 | return cursor.getLong(0); |
| 6145 | } |
| 6146 | return -1; |
| 6147 | } finally { |
| 6148 | cursor.close(); |
| 6149 | } |
| 6150 | } |
| 6151 | |
| 6152 | static boolean updateExtra(SQLiteDatabase db, long extrasId, String value) { |
| 6153 | ContentValues values = new ContentValues(); |
| 6154 | values.put(EXTRAS_VALUE, value); |
| 6155 | int rows = db.update(TABLE_EXTRAS, values, EXTRAS_ID + "=?", |
| 6156 | new String[]{String.valueOf(extrasId)}); |
| 6157 | return rows == 1; |
| 6158 | } |
| 6159 | |
| 6160 | static long insertExtra(SQLiteDatabase db, long accountId, String key, String value) { |
| 6161 | ContentValues values = new ContentValues(); |
| 6162 | values.put(EXTRAS_KEY, key); |
| 6163 | values.put(EXTRAS_ACCOUNTS_ID, accountId); |
| 6164 | values.put(EXTRAS_VALUE, value); |
| 6165 | return db.insert(CE_TABLE_EXTRAS, EXTRAS_KEY, values); |
| 6166 | } |
| 6167 | |
| 6168 | static Map<String, String> findUserExtrasForAccount(SQLiteDatabase db, Account account) { |
| 6169 | Map<String, String> userExtrasForAccount = new HashMap<>(); |
| 6170 | Cursor cursor = db.query(CE_TABLE_EXTRAS, |
| 6171 | COLUMNS_EXTRAS_KEY_AND_VALUE, |
| 6172 | SELECTION_USERDATA_BY_ACCOUNT, |
| 6173 | new String[] {account.name, account.type}, |
| 6174 | null, null, null); |
| 6175 | try { |
| 6176 | while (cursor.moveToNext()) { |
| 6177 | final String tmpkey = cursor.getString(0); |
| 6178 | final String value = cursor.getString(1); |
| 6179 | userExtrasForAccount.put(tmpkey, value); |
| 6180 | } |
| 6181 | } finally { |
| 6182 | cursor.close(); |
| 6183 | } |
| 6184 | return userExtrasForAccount; |
| 6185 | } |
| 6186 | |
| 6187 | static long insertGrant(SQLiteDatabase db, long accountId, String authTokenType, int uid) { |
| 6188 | ContentValues values = new ContentValues(); |
| 6189 | values.put(GRANTS_ACCOUNTS_ID, accountId); |
| 6190 | values.put(GRANTS_AUTH_TOKEN_TYPE, authTokenType); |
| 6191 | values.put(GRANTS_GRANTEE_UID, uid); |
| 6192 | return db.insert(TABLE_GRANTS, GRANTS_ACCOUNTS_ID, values); |
| 6193 | } |
| 6194 | |
| 6195 | static boolean deleteGrantsByUid(SQLiteDatabase db, int uid) { |
| 6196 | return db.delete(TABLE_GRANTS, GRANTS_GRANTEE_UID + "=?", |
| 6197 | new String[] {Integer.toString(uid)}) > 0; |
| 6198 | } |
| 6199 | |
| 6200 | static boolean deleteGrantsByAccountIdAuthTokenTypeAndUid(SQLiteDatabase db, long accountId, String authTokenType, long uid) { |
| 6201 | return db.delete(TABLE_GRANTS, |
| 6202 | GRANTS_ACCOUNTS_ID + "=? AND " + GRANTS_AUTH_TOKEN_TYPE + "=? AND " |
| 6203 | + GRANTS_GRANTEE_UID + "=?", |
| 6204 | new String[] {String.valueOf(accountId), authTokenType, String.valueOf(uid)}) > 0; |
| 6205 | } |
| 6206 | |
| 6207 | static List<Integer> findAllUidGrants(SQLiteDatabase db) { |
| 6208 | List<Integer> result = new ArrayList<>(); |
| 6209 | final Cursor cursor = db.query(TABLE_GRANTS, |
| 6210 | new String[] {GRANTS_GRANTEE_UID}, |
| 6211 | null, null, GRANTS_GRANTEE_UID, null, null); |
| 6212 | try { |
| 6213 | while (cursor.moveToNext()) { |
| 6214 | final int uid = cursor.getInt(0); |
| 6215 | result.add(uid); |
| 6216 | } |
| 6217 | } finally { |
| 6218 | cursor.close(); |
| 6219 | } |
| 6220 | return result; |
| 6221 | } |
| 6222 | |
| 6223 | static long findMatchingGrantsCount(SQLiteDatabase db, |
| 6224 | int uid, String authTokenType, Account account) { |
| 6225 | String[] args = { String.valueOf(uid), authTokenType, |
| 6226 | account.name, account.type}; |
| 6227 | return DatabaseUtils.longForQuery(db, COUNT_OF_MATCHING_GRANTS, args); |
| 6228 | } |
| 6229 | |
| 6230 | static long insertMetaAuthTypeAndUid(SQLiteDatabase db, String authenticatorType, int uid) { |
| 6231 | ContentValues values = new ContentValues(); |
| 6232 | values.put(META_KEY, |
| 6233 | META_KEY_FOR_AUTHENTICATOR_UID_FOR_TYPE_PREFIX + authenticatorType); |
| 6234 | values.put(META_VALUE, uid); |
| 6235 | return db.insert(TABLE_META, null, values); |
| 6236 | } |
| 6237 | |
| 6238 | static long insertOrReplaceMetaAuthTypeAndUid(SQLiteDatabase db, String authenticatorType, |
| 6239 | int uid) { |
| 6240 | ContentValues values = new ContentValues(); |
| 6241 | values.put(META_KEY, |
| 6242 | META_KEY_FOR_AUTHENTICATOR_UID_FOR_TYPE_PREFIX + authenticatorType); |
| 6243 | values.put(META_VALUE, uid); |
| 6244 | return db.insertWithOnConflict(TABLE_META, null, values, |
| 6245 | SQLiteDatabase.CONFLICT_REPLACE); |
| 6246 | } |
| 6247 | |
| 6248 | |
| 6249 | static Map<String, Integer> findMetaAuthUid(SQLiteDatabase db) { |
| 6250 | Cursor metaCursor = db.query( |
| 6251 | TABLE_META, |
| 6252 | new String[] {META_KEY, META_VALUE}, |
| 6253 | SELECTION_META_BY_AUTHENTICATOR_TYPE, |
| 6254 | new String[] {META_KEY_FOR_AUTHENTICATOR_UID_FOR_TYPE_PREFIX + "%"}, |
| 6255 | null /* groupBy */, |
| 6256 | null /* having */, |
| 6257 | META_KEY); |
| 6258 | Map<String, Integer> map = new LinkedHashMap<>(); |
| 6259 | try { |
| 6260 | while (metaCursor.moveToNext()) { |
| 6261 | String type = TextUtils.split(metaCursor.getString(0), META_KEY_DELIMITER)[1]; |
| 6262 | String uidStr = metaCursor.getString(1); |
| 6263 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(uidStr)) { |
| 6264 | // Should never happen. |
| 6265 | Slog.e(TAG, "Auth type empty: " + TextUtils.isEmpty(type) |
| 6266 | + ", uid empty: " + TextUtils.isEmpty(uidStr)); |
| 6267 | continue; |
| 6268 | } |
| 6269 | int uid = Integer.parseInt(metaCursor.getString(1)); |
| 6270 | map.put(type, uid); |
| 6271 | } |
| 6272 | } finally { |
| 6273 | metaCursor.close(); |
| 6274 | } |
| 6275 | return map; |
| 6276 | } |
| 6277 | |
| 6278 | static boolean deleteMetaByAuthTypeAndUid(SQLiteDatabase db, String type, int uid) { |
| 6279 | return db.delete( |
| 6280 | TABLE_META, |
| 6281 | META_KEY + "=? AND " + META_VALUE + "=?", |
| 6282 | new String[] { |
| 6283 | META_KEY_FOR_AUTHENTICATOR_UID_FOR_TYPE_PREFIX + type, |
| 6284 | String.valueOf(uid)} |
| 6285 | ) > 0; |
| 6286 | } |
| 6287 | |
| 6288 | static void dumpAccountsTable(SQLiteDatabase db, PrintWriter pw) { |
| 6289 | Cursor cursor = db.query(TABLE_ACCOUNTS, ACCOUNT_TYPE_COUNT_PROJECTION, |
| 6290 | null, null, ACCOUNTS_TYPE, null, null); |
| 6291 | try { |
| 6292 | while (cursor.moveToNext()) { |
| 6293 | // print type,count |
| 6294 | pw.println(cursor.getString(0) + "," + cursor.getString(1)); |
| 6295 | } |
| 6296 | } finally { |
| 6297 | if (cursor != null) { |
| 6298 | cursor.close(); |
| 6299 | } |
| 6300 | } |
| 6301 | } |
| 6302 | |
| 6303 | static void dumpDebugTable(SQLiteDatabase db, PrintWriter pw) { |
| 6304 | Cursor cursor = db.query(DebugDbHelper.TABLE_DEBUG, null, |
| 6305 | null, null, null, null, DebugDbHelper.TIMESTAMP); |
| 6306 | pw.println("AccountId, Action_Type, timestamp, UID, TableName, Key"); |
| 6307 | pw.println("Accounts History"); |
| 6308 | try { |
| 6309 | while (cursor.moveToNext()) { |
| 6310 | // print type,count |
| 6311 | pw.println(cursor.getString(0) + "," + cursor.getString(1) + "," + |
| 6312 | cursor.getString(2) + "," + cursor.getString(3) + "," |
| 6313 | + cursor.getString(4) + "," + cursor.getString(5)); |
| 6314 | } |
| 6315 | } finally { |
| 6316 | cursor.close(); |
| 6317 | } |
| 6318 | } |
| 6319 | |
| 6320 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 6321 | } |