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; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 32 | import android.app.ActivityManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 33 | import android.app.ActivityManagerNative; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 34 | import android.app.AppGlobals; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 35 | import android.app.AppOpsManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 36 | import android.app.Notification; |
| 37 | import android.app.NotificationManager; |
| 38 | import android.app.PendingIntent; |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 39 | import android.app.admin.DevicePolicyManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 41 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 42 | import android.content.ContentValues; |
| 43 | import android.content.Context; |
| 44 | import android.content.Intent; |
| 45 | import android.content.IntentFilter; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 46 | import android.content.ServiceConnection; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 47 | import android.content.pm.ApplicationInfo; |
| 48 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 49 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 50 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 51 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 52 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 53 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 54 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 55 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 56 | import android.database.Cursor; |
| 57 | import android.database.DatabaseUtils; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 58 | import android.database.sqlite.SQLiteDatabase; |
| 59 | import android.database.sqlite.SQLiteOpenHelper; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 60 | import android.database.sqlite.SQLiteStatement; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 61 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 62 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 63 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 64 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 65 | import android.os.IBinder; |
| 66 | import android.os.Looper; |
| 67 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 68 | import android.os.Parcel; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 69 | import android.os.Process; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 70 | import android.os.RemoteException; |
| 71 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 72 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 73 | import android.os.UserManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 74 | import android.text.TextUtils; |
| 75 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 76 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 77 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 78 | import android.util.SparseArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 79 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 80 | import com.android.internal.R; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 81 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 82 | import com.android.internal.util.IndentingPrintWriter; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 83 | import com.android.server.FgThread; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 84 | import com.google.android.collect.Lists; |
| 85 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 86 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 87 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 88 | import java.io.FileDescriptor; |
| 89 | import java.io.PrintWriter; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 90 | import java.security.MessageDigest; |
| 91 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 92 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 93 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 94 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 95 | import java.util.Collection; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 96 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 97 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 98 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 99 | import java.util.LinkedHashMap; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 100 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 101 | import java.util.Map; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 102 | import java.util.concurrent.atomic.AtomicInteger; |
| 103 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 104 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 105 | /** |
| 106 | * A system service that provides account, password, and authtoken management for all |
| 107 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 108 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 109 | * 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] | 110 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 111 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 112 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 113 | public class AccountManagerService |
| 114 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 115 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 116 | private static final String TAG = "AccountManagerService"; |
| 117 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 118 | private static final String DATABASE_NAME = "accounts.db"; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 119 | private static final int DATABASE_VERSION = 8; |
| 120 | |
| 121 | private static final int MAX_DEBUG_DB_SIZE = 64; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 122 | |
| 123 | private final Context mContext; |
| 124 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 125 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 126 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 127 | private UserManager mUserManager; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 128 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 129 | private final MessageHandler mMessageHandler; |
| 130 | |
| 131 | // Messages that can be sent on mHandler |
| 132 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 133 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 134 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 135 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 136 | |
| 137 | private static final String TABLE_ACCOUNTS = "accounts"; |
| 138 | private static final String ACCOUNTS_ID = "_id"; |
| 139 | private static final String ACCOUNTS_NAME = "name"; |
| 140 | private static final String ACCOUNTS_TYPE = "type"; |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 141 | private static final String ACCOUNTS_TYPE_COUNT = "count(type)"; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 142 | private static final String ACCOUNTS_PASSWORD = "password"; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 143 | private static final String ACCOUNTS_PREVIOUS_NAME = "previous_name"; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 144 | private static final String ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS = |
| 145 | "last_password_entry_time_millis_epoch"; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 146 | |
| 147 | private static final String TABLE_AUTHTOKENS = "authtokens"; |
| 148 | private static final String AUTHTOKENS_ID = "_id"; |
| 149 | private static final String AUTHTOKENS_ACCOUNTS_ID = "accounts_id"; |
| 150 | private static final String AUTHTOKENS_TYPE = "type"; |
| 151 | private static final String AUTHTOKENS_AUTHTOKEN = "authtoken"; |
| 152 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 153 | private static final String TABLE_GRANTS = "grants"; |
| 154 | private static final String GRANTS_ACCOUNTS_ID = "accounts_id"; |
| 155 | private static final String GRANTS_AUTH_TOKEN_TYPE = "auth_token_type"; |
| 156 | private static final String GRANTS_GRANTEE_UID = "uid"; |
| 157 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 158 | private static final String TABLE_EXTRAS = "extras"; |
| 159 | private static final String EXTRAS_ID = "_id"; |
| 160 | private static final String EXTRAS_ACCOUNTS_ID = "accounts_id"; |
| 161 | private static final String EXTRAS_KEY = "key"; |
| 162 | private static final String EXTRAS_VALUE = "value"; |
| 163 | |
| 164 | private static final String TABLE_META = "meta"; |
| 165 | private static final String META_KEY = "key"; |
| 166 | private static final String META_VALUE = "value"; |
| 167 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 168 | private static final String TABLE_SHARED_ACCOUNTS = "shared_accounts"; |
| 169 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 170 | private static final String[] ACCOUNT_TYPE_COUNT_PROJECTION = |
| 171 | new String[] { ACCOUNTS_TYPE, ACCOUNTS_TYPE_COUNT}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 172 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 173 | static { |
| 174 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
| 175 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 176 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 177 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 178 | private static final String COUNT_OF_MATCHING_GRANTS = "" |
| 179 | + "SELECT COUNT(*) FROM " + TABLE_GRANTS + ", " + TABLE_ACCOUNTS |
| 180 | + " WHERE " + GRANTS_ACCOUNTS_ID + "=" + ACCOUNTS_ID |
| 181 | + " AND " + GRANTS_GRANTEE_UID + "=?" |
| 182 | + " AND " + GRANTS_AUTH_TOKEN_TYPE + "=?" |
| 183 | + " AND " + ACCOUNTS_NAME + "=?" |
| 184 | + " AND " + ACCOUNTS_TYPE + "=?"; |
| 185 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 186 | private static final String SELECTION_AUTHTOKENS_BY_ACCOUNT = |
| 187 | AUTHTOKENS_ACCOUNTS_ID + "=(select _id FROM accounts WHERE name=? AND type=?)"; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 188 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 189 | private static final String[] COLUMNS_AUTHTOKENS_TYPE_AND_AUTHTOKEN = {AUTHTOKENS_TYPE, |
| 190 | AUTHTOKENS_AUTHTOKEN}; |
| 191 | |
| 192 | private static final String SELECTION_USERDATA_BY_ACCOUNT = |
| 193 | EXTRAS_ACCOUNTS_ID + "=(select _id FROM accounts WHERE name=? AND type=?)"; |
| 194 | private static final String[] COLUMNS_EXTRAS_KEY_AND_VALUE = {EXTRAS_KEY, EXTRAS_VALUE}; |
| 195 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 196 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 197 | private final AtomicInteger mNotificationIds = new AtomicInteger(1); |
| 198 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 199 | static class UserAccounts { |
| 200 | private final int userId; |
| 201 | private final DatabaseHelper openHelper; |
| 202 | private final HashMap<Pair<Pair<Account, String>, Integer>, Integer> |
| 203 | credentialsPermissionNotificationIds = |
| 204 | new HashMap<Pair<Pair<Account, String>, Integer>, Integer>(); |
| 205 | private final HashMap<Account, Integer> signinRequiredNotificationIds = |
| 206 | new HashMap<Account, Integer>(); |
| 207 | private final Object cacheLock = new Object(); |
| 208 | /** protected by the {@link #cacheLock} */ |
Amith Yamasani | b483a99 | 2012-05-22 13:14:25 -0700 | [diff] [blame] | 209 | private final HashMap<String, Account[]> accountCache = |
| 210 | new LinkedHashMap<String, Account[]>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 211 | /** protected by the {@link #cacheLock} */ |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 212 | private final HashMap<Account, HashMap<String, String>> userDataCache = |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 213 | new HashMap<Account, HashMap<String, String>>(); |
| 214 | /** protected by the {@link #cacheLock} */ |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 215 | private final HashMap<Account, HashMap<String, String>> authTokenCache = |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 216 | new HashMap<Account, HashMap<String, String>>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 217 | |
| 218 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 219 | private final TokenCache accountTokenCaches = new TokenCache(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 220 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 221 | /** |
| 222 | * protected by the {@link #cacheLock} |
| 223 | * |
| 224 | * Caches the previous names associated with an account. Previous names |
| 225 | * should be cached because we expect that when an Account is renamed, |
| 226 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 227 | * want to know if the accounts they care about have been renamed. |
| 228 | * |
| 229 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 230 | * we can distinguish between those accounts with no previous names and |
| 231 | * those whose previous names haven't been cached (yet). |
| 232 | */ |
| 233 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 234 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 235 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 236 | private int debugDbInsertionPoint = -1; |
| 237 | private SQLiteStatement statementForLogging; |
| 238 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 239 | UserAccounts(Context context, int userId) { |
| 240 | this.userId = userId; |
| 241 | synchronized (cacheLock) { |
| 242 | openHelper = new DatabaseHelper(context, userId); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private final SparseArray<UserAccounts> mUsers = new SparseArray<UserAccounts>(); |
| 248 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 249 | private static AtomicReference<AccountManagerService> sThis = |
| 250 | new AtomicReference<AccountManagerService>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 251 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 252 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 253 | /** |
| 254 | * This should only be called by system code. One should only call this after the service |
| 255 | * has started. |
| 256 | * @return a reference to the AccountManagerService instance |
| 257 | * @hide |
| 258 | */ |
| 259 | public static AccountManagerService getSingleton() { |
| 260 | return sThis.get(); |
| 261 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 262 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 263 | public AccountManagerService(Context context) { |
| 264 | this(context, context.getPackageManager(), new AccountAuthenticatorCache(context)); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 267 | public AccountManagerService(Context context, PackageManager packageManager, |
| 268 | IAccountAuthenticatorCache authenticatorCache) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 269 | mContext = context; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 270 | mPackageManager = packageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 271 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 272 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 273 | mMessageHandler = new MessageHandler(FgThread.get().getLooper()); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 274 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 275 | mAuthenticatorCache = authenticatorCache; |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 276 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 277 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 278 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 279 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 280 | IntentFilter intentFilter = new IntentFilter(); |
| 281 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 282 | intentFilter.addDataScheme("package"); |
| 283 | mContext.registerReceiver(new BroadcastReceiver() { |
| 284 | @Override |
| 285 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 286 | // Don't delete accounts when updating a authenticator's |
| 287 | // package. |
| 288 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 289 | /* Purging data requires file io, don't block the main thread. This is probably |
| 290 | * less than ideal because we are introducing a race condition where old grants |
| 291 | * could be exercised until they are purged. But that race condition existed |
| 292 | * anyway with the broadcast receiver. |
| 293 | * |
| 294 | * Ideally, we would completely clear the cache, purge data from the database, |
| 295 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 296 | * large at this point. |
| 297 | */ |
| 298 | Runnable r = new Runnable() { |
| 299 | @Override |
| 300 | public void run() { |
| 301 | purgeOldGrantsAll(); |
| 302 | } |
| 303 | }; |
| 304 | new Thread(r).start(); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 305 | } |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 306 | } |
| 307 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 308 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 309 | IntentFilter userFilter = new IntentFilter(); |
| 310 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 311 | userFilter.addAction(Intent.ACTION_USER_STARTED); |
| 312 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 313 | @Override |
| 314 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 315 | String action = intent.getAction(); |
| 316 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 317 | onUserRemoved(intent); |
| 318 | } else if (Intent.ACTION_USER_STARTED.equals(action)) { |
| 319 | onUserStarted(intent); |
| 320 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 321 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 322 | }, UserHandle.ALL, userFilter, null, null); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 325 | @Override |
| 326 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 327 | throws RemoteException { |
| 328 | try { |
| 329 | return super.onTransact(code, data, reply, flags); |
| 330 | } catch (RuntimeException e) { |
| 331 | // The account manager only throws security exceptions, so let's |
| 332 | // log all others. |
| 333 | if (!(e instanceof SecurityException)) { |
| 334 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 335 | } |
| 336 | throw e; |
| 337 | } |
| 338 | } |
| 339 | |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 340 | public void systemReady() { |
Kenny Root | 26ff662 | 2012-07-30 12:58:03 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 343 | private UserManager getUserManager() { |
| 344 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 345 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 346 | } |
| 347 | return mUserManager; |
| 348 | } |
| 349 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 350 | /** |
| 351 | * Validate internal set of accounts against installed authenticators for |
| 352 | * given user. Clears cached authenticators before validating. |
| 353 | */ |
| 354 | public void validateAccounts(int userId) { |
| 355 | final UserAccounts accounts = getUserAccounts(userId); |
| 356 | |
| 357 | // Invalidate user-specific cache to make sure we catch any |
| 358 | // removed authenticators. |
| 359 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Validate internal set of accounts against installed authenticators for |
| 364 | * given user. Clear cached authenticators before validating when requested. |
| 365 | */ |
| 366 | private void validateAccountsInternal( |
| 367 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
| 368 | if (invalidateAuthenticatorCache) { |
| 369 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 370 | } |
| 371 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 372 | final HashSet<AuthenticatorDescription> knownAuth = Sets.newHashSet(); |
| 373 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : |
| 374 | mAuthenticatorCache.getAllServices(accounts.userId)) { |
| 375 | knownAuth.add(service.type); |
| 376 | } |
| 377 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 378 | synchronized (accounts.cacheLock) { |
| 379 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 380 | boolean accountDeleted = false; |
| 381 | Cursor cursor = db.query(TABLE_ACCOUNTS, |
| 382 | new String[]{ACCOUNTS_ID, ACCOUNTS_TYPE, ACCOUNTS_NAME}, |
Marvin Paul | 48fcd4e | 2014-12-01 18:26:07 -0800 | [diff] [blame] | 383 | null, null, null, null, ACCOUNTS_ID); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 384 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 385 | accounts.accountCache.clear(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 386 | final HashMap<String, ArrayList<String>> accountNamesByType = |
Amith Yamasani | b483a99 | 2012-05-22 13:14:25 -0700 | [diff] [blame] | 387 | new LinkedHashMap<String, ArrayList<String>>(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 388 | while (cursor.moveToNext()) { |
| 389 | final long accountId = cursor.getLong(0); |
| 390 | final String accountType = cursor.getString(1); |
| 391 | final String accountName = cursor.getString(2); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 392 | |
| 393 | if (!knownAuth.contains(AuthenticatorDescription.newKey(accountType))) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 394 | Slog.w(TAG, "deleting account " + accountName + " because type " |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 395 | + accountType + " no longer has a registered authenticator"); |
| 396 | db.delete(TABLE_ACCOUNTS, ACCOUNTS_ID + "=" + accountId, null); |
| 397 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 398 | |
| 399 | logRecord(db, DebugDbHelper.ACTION_AUTHENTICATOR_REMOVE, TABLE_ACCOUNTS, |
| 400 | accountId, accounts); |
| 401 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 402 | final Account account = new Account(accountName, accountType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 403 | accounts.userDataCache.remove(account); |
| 404 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 405 | accounts.accountTokenCaches.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 406 | } else { |
| 407 | ArrayList<String> accountNames = accountNamesByType.get(accountType); |
| 408 | if (accountNames == null) { |
| 409 | accountNames = new ArrayList<String>(); |
| 410 | accountNamesByType.put(accountType, accountNames); |
| 411 | } |
| 412 | accountNames.add(accountName); |
| 413 | } |
| 414 | } |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 415 | for (Map.Entry<String, ArrayList<String>> cur |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 416 | : accountNamesByType.entrySet()) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 417 | final String accountType = cur.getKey(); |
| 418 | final ArrayList<String> accountNames = cur.getValue(); |
| 419 | final Account[] accountsForType = new Account[accountNames.size()]; |
| 420 | int i = 0; |
| 421 | for (String accountName : accountNames) { |
| 422 | accountsForType[i] = new Account(accountName, accountType); |
| 423 | ++i; |
| 424 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 425 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 426 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 427 | } finally { |
| 428 | cursor.close(); |
| 429 | if (accountDeleted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 430 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 431 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 436 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 437 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | protected UserAccounts getUserAccounts(int userId) { |
| 441 | synchronized (mUsers) { |
| 442 | UserAccounts accounts = mUsers.get(userId); |
| 443 | if (accounts == null) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 444 | accounts = new UserAccounts(mContext, userId); |
| 445 | initializeDebugDbSizeAndCompileSqlStatementForLogging( |
| 446 | accounts.openHelper.getWritableDatabase(), accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 447 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 448 | purgeOldGrants(accounts); |
| 449 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 450 | } |
| 451 | return accounts; |
| 452 | } |
| 453 | } |
| 454 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 455 | private void purgeOldGrantsAll() { |
| 456 | synchronized (mUsers) { |
| 457 | for (int i = 0; i < mUsers.size(); i++) { |
| 458 | purgeOldGrants(mUsers.valueAt(i)); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | private void purgeOldGrants(UserAccounts accounts) { |
| 464 | synchronized (accounts.cacheLock) { |
| 465 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 466 | final Cursor cursor = db.query(TABLE_GRANTS, |
| 467 | new String[]{GRANTS_GRANTEE_UID}, |
| 468 | null, null, GRANTS_GRANTEE_UID, null, null); |
| 469 | try { |
| 470 | while (cursor.moveToNext()) { |
| 471 | final int uid = cursor.getInt(0); |
| 472 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 473 | if (packageExists) { |
| 474 | continue; |
| 475 | } |
| 476 | Log.d(TAG, "deleting grants for UID " + uid |
| 477 | + " because its package is no longer installed"); |
| 478 | db.delete(TABLE_GRANTS, GRANTS_GRANTEE_UID + "=?", |
| 479 | new String[]{Integer.toString(uid)}); |
| 480 | } |
| 481 | } finally { |
| 482 | cursor.close(); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 487 | private void onUserRemoved(Intent intent) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 488 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 489 | if (userId < 1) return; |
| 490 | |
| 491 | UserAccounts accounts; |
| 492 | synchronized (mUsers) { |
| 493 | accounts = mUsers.get(userId); |
| 494 | mUsers.remove(userId); |
| 495 | } |
| 496 | if (accounts == null) { |
| 497 | File dbFile = new File(getDatabaseName(userId)); |
| 498 | dbFile.delete(); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | synchronized (accounts.cacheLock) { |
| 503 | accounts.openHelper.close(); |
| 504 | File dbFile = new File(getDatabaseName(userId)); |
| 505 | dbFile.delete(); |
| 506 | } |
| 507 | } |
| 508 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 509 | private void onUserStarted(Intent intent) { |
| 510 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 511 | if (userId < 1) return; |
| 512 | |
| 513 | // Check if there's a shared account that needs to be created as an account |
| 514 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 515 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 516 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 517 | int parentUserId = UserManager.isSplitSystemUser() |
| 518 | ? mUserManager.getUserInfo(userId).restrictedProfileParentId |
| 519 | : UserHandle.USER_SYSTEM; |
| 520 | if (parentUserId < 0) { |
| 521 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 522 | return; |
| 523 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 524 | for (Account sa : sharedAccounts) { |
| 525 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 526 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 527 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 531 | @Override |
| 532 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 533 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 536 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 537 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 538 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 539 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 540 | Log.v(TAG, "getPassword: " + account |
| 541 | + ", caller's uid " + Binder.getCallingUid() |
| 542 | + ", pid " + Binder.getCallingPid()); |
| 543 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 544 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 545 | int userId = UserHandle.getCallingUserId(); |
| 546 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 547 | String msg = String.format( |
| 548 | "uid %s cannot get secrets for accounts of type: %s", |
| 549 | callingUid, |
| 550 | account.type); |
| 551 | throw new SecurityException(msg); |
| 552 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 553 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 554 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 555 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 556 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 557 | } finally { |
| 558 | restoreCallingIdentity(identityToken); |
| 559 | } |
| 560 | } |
| 561 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 562 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 563 | if (account == null) { |
| 564 | return null; |
| 565 | } |
| 566 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 567 | synchronized (accounts.cacheLock) { |
| 568 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 569 | Cursor cursor = db.query(TABLE_ACCOUNTS, new String[]{ACCOUNTS_PASSWORD}, |
| 570 | ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 571 | new String[]{account.name, account.type}, null, null, null); |
| 572 | try { |
| 573 | if (cursor.moveToNext()) { |
| 574 | return cursor.getString(0); |
| 575 | } |
| 576 | return null; |
| 577 | } finally { |
| 578 | cursor.close(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 579 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 583 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 584 | public String getPreviousName(Account account) { |
| 585 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 586 | Log.v(TAG, "getPreviousName: " + account |
| 587 | + ", caller's uid " + Binder.getCallingUid() |
| 588 | + ", pid " + Binder.getCallingPid()); |
| 589 | } |
| 590 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 591 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 592 | long identityToken = clearCallingIdentity(); |
| 593 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 594 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 595 | return readPreviousNameInternal(accounts, account); |
| 596 | } finally { |
| 597 | restoreCallingIdentity(identityToken); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 602 | if (account == null) { |
| 603 | return null; |
| 604 | } |
| 605 | synchronized (accounts.cacheLock) { |
| 606 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 607 | if (previousNameRef == null) { |
| 608 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
| 609 | Cursor cursor = db.query( |
| 610 | TABLE_ACCOUNTS, |
| 611 | new String[]{ ACCOUNTS_PREVIOUS_NAME }, |
| 612 | ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 613 | new String[] { account.name, account.type }, |
| 614 | null, |
| 615 | null, |
| 616 | null); |
| 617 | try { |
| 618 | if (cursor.moveToNext()) { |
| 619 | String previousName = cursor.getString(0); |
| 620 | previousNameRef = new AtomicReference<String>(previousName); |
| 621 | accounts.previousNameCache.put(account, previousNameRef); |
| 622 | return previousName; |
| 623 | } else { |
| 624 | return null; |
| 625 | } |
| 626 | } finally { |
| 627 | cursor.close(); |
| 628 | } |
| 629 | } else { |
| 630 | return previousNameRef.get(); |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 636 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 637 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 638 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 639 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 640 | account, key, callingUid, Binder.getCallingPid()); |
| 641 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 642 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 643 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 644 | if (key == null) throw new IllegalArgumentException("key is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 645 | int userId = UserHandle.getCallingUserId(); |
| 646 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 647 | String msg = String.format( |
| 648 | "uid %s cannot get user data for accounts of type: %s", |
| 649 | callingUid, |
| 650 | account.type); |
| 651 | throw new SecurityException(msg); |
| 652 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 653 | long identityToken = clearCallingIdentity(); |
| 654 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 655 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 656 | return readUserDataInternal(accounts, account, key); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 657 | } finally { |
| 658 | restoreCallingIdentity(identityToken); |
| 659 | } |
| 660 | } |
| 661 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 662 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 663 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 664 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 665 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 666 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 667 | + "for user id " + userId |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 668 | + "caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 669 | + ", pid " + Binder.getCallingPid()); |
| 670 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 671 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 672 | if (isCrossUser(callingUid, userId)) { |
| 673 | throw new SecurityException( |
| 674 | String.format( |
| 675 | "User %s tying to get authenticator types for %s" , |
| 676 | UserHandle.getCallingUserId(), |
| 677 | userId)); |
| 678 | } |
| 679 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 680 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 681 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 682 | return getAuthenticatorTypesInternal(userId); |
| 683 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 684 | } finally { |
| 685 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 686 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 689 | /** |
| 690 | * Should only be called inside of a clearCallingIdentity block. |
| 691 | */ |
| 692 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
| 693 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 694 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 695 | AuthenticatorDescription[] types = |
| 696 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 697 | int i = 0; |
| 698 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 699 | : authenticatorCollection) { |
| 700 | types[i] = authenticator.type; |
| 701 | i++; |
| 702 | } |
| 703 | return types; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 708 | private boolean isCrossUser(int callingUid, int userId) { |
| 709 | return (userId != UserHandle.getCallingUserId() |
| 710 | && callingUid != Process.myUid() |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 711 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 712 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 713 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 716 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 717 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 718 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 719 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 720 | Log.v(TAG, "addAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 721 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 722 | + ", pid " + Binder.getCallingPid()); |
| 723 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 724 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 725 | int userId = UserHandle.getCallingUserId(); |
| 726 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 727 | String msg = String.format( |
| 728 | "uid %s cannot explicitly add accounts of type: %s", |
| 729 | callingUid, |
| 730 | account.type); |
| 731 | throw new SecurityException(msg); |
| 732 | } |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 733 | /* |
| 734 | * Child users are not allowed to add accounts. Only the accounts that are |
| 735 | * shared by the parent profile can be added to child profile. |
| 736 | * |
| 737 | * TODO: Only allow accounts that were shared to be added by |
| 738 | * a limited user. |
| 739 | */ |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 740 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 741 | // fails if the account already exists |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 742 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 743 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 744 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 745 | return addAccountInternal(accounts, account, password, extras, false, callingUid); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 746 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 747 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 751 | @Override |
| 752 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 753 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 754 | int callingUid = Binder.getCallingUid(); |
| 755 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 756 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 757 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 758 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 759 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 760 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 761 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 762 | if (response != null) { |
| 763 | Bundle result = new Bundle(); |
| 764 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 765 | try { |
| 766 | response.onResult(result); |
| 767 | } catch (RemoteException e) { |
| 768 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 769 | } |
| 770 | } |
| 771 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 774 | Slog.d(TAG, "Copying account " + account.name |
| 775 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 776 | long identityToken = clearCallingIdentity(); |
| 777 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 778 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 779 | false /* stripAuthTokenFromResult */, account.name, |
| 780 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 781 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 782 | protected String toDebugString(long now) { |
| 783 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 784 | + ", " + account.type; |
| 785 | } |
| 786 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 787 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 788 | public void run() throws RemoteException { |
| 789 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 790 | } |
| 791 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 792 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 793 | public void onResult(Bundle result) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 794 | if (result != null |
| 795 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 796 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 797 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 798 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 799 | super.onResult(result); |
| 800 | } |
| 801 | } |
| 802 | }.bind(); |
| 803 | } finally { |
| 804 | restoreCallingIdentity(identityToken); |
| 805 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 808 | @Override |
| 809 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 810 | final int callingUid = Binder.getCallingUid(); |
| 811 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 812 | String msg = String.format( |
| 813 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 814 | account, |
| 815 | callingUid); |
| 816 | Log.v(TAG, msg); |
| 817 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 818 | if (account == null) { |
| 819 | throw new IllegalArgumentException("account is null"); |
| 820 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 821 | int userId = UserHandle.getCallingUserId(); |
| 822 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 823 | String msg = String.format( |
| 824 | "uid %s cannot notify authentication for accounts of type: %s", |
| 825 | callingUid, |
| 826 | account.type); |
| 827 | throw new SecurityException(msg); |
| 828 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 829 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 830 | if (!canUserModifyAccounts(userId) || !canUserModifyAccountsForType(userId, account.type)) { |
| 831 | return false; |
| 832 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 833 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 834 | long identityToken = clearCallingIdentity(); |
| 835 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 836 | UserAccounts accounts = getUserAccounts(userId); |
| 837 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 838 | } finally { |
| 839 | restoreCallingIdentity(identityToken); |
| 840 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | private boolean updateLastAuthenticatedTime(Account account) { |
| 844 | final UserAccounts accounts = getUserAccountsForCaller(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 845 | synchronized (accounts.cacheLock) { |
| 846 | final ContentValues values = new ContentValues(); |
| 847 | values.put(ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS, System.currentTimeMillis()); |
| 848 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 849 | int i = db.update( |
| 850 | TABLE_ACCOUNTS, |
| 851 | values, |
| 852 | ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE + "=?", |
| 853 | new String[] { |
| 854 | account.name, account.type |
| 855 | }); |
| 856 | if (i > 0) { |
| 857 | return true; |
| 858 | } |
| 859 | } |
| 860 | return false; |
| 861 | } |
| 862 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 863 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 864 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 865 | final int parentUserId){ |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 866 | long id = clearCallingIdentity(); |
| 867 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 868 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 869 | false /* stripAuthTokenFromResult */, account.name, |
| 870 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 871 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 872 | protected String toDebugString(long now) { |
| 873 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 874 | + ", " + account.type; |
| 875 | } |
| 876 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 877 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 878 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 879 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 880 | UserAccounts owner = getUserAccounts(parentUserId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 881 | synchronized (owner.cacheLock) { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 882 | for (Account acc : getAccounts(parentUserId, |
| 883 | mContext.getOpPackageName())) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 884 | if (acc.equals(account)) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 885 | mAuthenticator.addAccountFromCredentials( |
| 886 | this, account, accountCredentials); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | } |
| 890 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 893 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 894 | public void onResult(Bundle result) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 895 | // TODO: Anything to do if if succedded? |
| 896 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 897 | // account to avoid retries? |
| 898 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 901 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 902 | public void onError(int errorCode, String errorMessage) { |
| 903 | super.onError(errorCode, errorMessage); |
| 904 | // TODO: Show error notification to user |
| 905 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 906 | } |
| 907 | |
| 908 | }.bind(); |
| 909 | } finally { |
| 910 | restoreCallingIdentity(id); |
| 911 | } |
| 912 | } |
| 913 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 914 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 915 | Bundle extras, boolean restricted, int callingUid) { |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 916 | if (account == null) { |
| 917 | return false; |
| 918 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 919 | synchronized (accounts.cacheLock) { |
| 920 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 921 | db.beginTransaction(); |
| 922 | try { |
| 923 | long numMatches = DatabaseUtils.longForQuery(db, |
| 924 | "select count(*) from " + TABLE_ACCOUNTS |
| 925 | + " WHERE " + ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 926 | new String[]{account.name, account.type}); |
| 927 | if (numMatches > 0) { |
| 928 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 929 | + ", skipping since the account already exists"); |
| 930 | return false; |
| 931 | } |
| 932 | ContentValues values = new ContentValues(); |
| 933 | values.put(ACCOUNTS_NAME, account.name); |
| 934 | values.put(ACCOUNTS_TYPE, account.type); |
| 935 | values.put(ACCOUNTS_PASSWORD, password); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 936 | values.put(ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS, System.currentTimeMillis()); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 937 | long accountId = db.insert(TABLE_ACCOUNTS, ACCOUNTS_NAME, values); |
| 938 | if (accountId < 0) { |
| 939 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 940 | + ", skipping the DB insert failed"); |
| 941 | return false; |
| 942 | } |
| 943 | if (extras != null) { |
| 944 | for (String key : extras.keySet()) { |
| 945 | final String value = extras.getString(key); |
| 946 | if (insertExtraLocked(db, accountId, key, value) < 0) { |
| 947 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 948 | + ", skipping since insertExtra failed for key " + key); |
| 949 | return false; |
| 950 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 953 | db.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 954 | |
| 955 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_ADD, TABLE_ACCOUNTS, accountId, |
| 956 | accounts, callingUid); |
| 957 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 958 | insertAccountIntoCacheLocked(accounts, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 959 | } finally { |
| 960 | db.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 961 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 962 | sendAccountsChangedBroadcast(accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 963 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 964 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 965 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 966 | } |
| 967 | return true; |
| 968 | } |
| 969 | |
| 970 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 971 | * 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] | 972 | * running, then clone the account too. |
| 973 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 974 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 975 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 976 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 977 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 978 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 979 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 980 | addSharedAccountAsUser(account, user.id); |
| 981 | try { |
| 982 | if (ActivityManagerNative.getDefault().isUserRunning(user.id, false)) { |
| 983 | mMessageHandler.sendMessage(mMessageHandler.obtainMessage( |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 984 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 985 | } |
| 986 | } catch (RemoteException re) { |
| 987 | // Shouldn't happen |
| 988 | } |
| 989 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 993 | private long insertExtraLocked(SQLiteDatabase db, long accountId, String key, String value) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 994 | ContentValues values = new ContentValues(); |
| 995 | values.put(EXTRAS_KEY, key); |
| 996 | values.put(EXTRAS_ACCOUNTS_ID, accountId); |
| 997 | values.put(EXTRAS_VALUE, value); |
| 998 | return db.insert(TABLE_EXTRAS, EXTRAS_KEY, values); |
| 999 | } |
| 1000 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1001 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1002 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 1003 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1004 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1005 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1006 | Log.v(TAG, "hasFeatures: " + account |
| 1007 | + ", response " + response |
| 1008 | + ", features " + stringArrayToString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1009 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1010 | + ", pid " + Binder.getCallingPid()); |
| 1011 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1012 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1013 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1014 | if (features == null) throw new IllegalArgumentException("features is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1015 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 1016 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1017 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1018 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1019 | long identityToken = clearCallingIdentity(); |
| 1020 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1021 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1022 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1023 | } finally { |
| 1024 | restoreCallingIdentity(identityToken); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | private class TestFeaturesSession extends Session { |
| 1029 | private final String[] mFeatures; |
| 1030 | private final Account mAccount; |
| 1031 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1032 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1033 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1034 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1035 | true /* stripAuthTokenFromResult */, account.name, |
| 1036 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1037 | mFeatures = features; |
| 1038 | mAccount = account; |
| 1039 | } |
| 1040 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1041 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1042 | public void run() throws RemoteException { |
| 1043 | try { |
| 1044 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1045 | } catch (RemoteException e) { |
| 1046 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1047 | } |
| 1048 | } |
| 1049 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1050 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1051 | public void onResult(Bundle result) { |
| 1052 | IAccountManagerResponse response = getResponseAndClose(); |
| 1053 | if (response != null) { |
| 1054 | try { |
| 1055 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1056 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1057 | return; |
| 1058 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1059 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1060 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1061 | + response); |
| 1062 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1063 | final Bundle newResult = new Bundle(); |
| 1064 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1065 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1066 | response.onResult(newResult); |
| 1067 | } catch (RemoteException e) { |
| 1068 | // if the caller is dead then there is no one to care about remote exceptions |
| 1069 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1070 | Log.v(TAG, "failure while notifying response", e); |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1076 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1077 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1078 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1079 | + ", " + mAccount |
| 1080 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1081 | } |
| 1082 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1083 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1084 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1085 | public void renameAccount( |
| 1086 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1087 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1088 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1089 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1090 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1091 | + ", pid " + Binder.getCallingPid()); |
| 1092 | } |
| 1093 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1094 | int userId = UserHandle.getCallingUserId(); |
| 1095 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1096 | String msg = String.format( |
| 1097 | "uid %s cannot rename accounts of type: %s", |
| 1098 | callingUid, |
| 1099 | accountToRename.type); |
| 1100 | throw new SecurityException(msg); |
| 1101 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1102 | long identityToken = clearCallingIdentity(); |
| 1103 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1104 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1105 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1106 | Bundle result = new Bundle(); |
| 1107 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1108 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
| 1109 | try { |
| 1110 | response.onResult(result); |
| 1111 | } catch (RemoteException e) { |
| 1112 | Log.w(TAG, e.getMessage()); |
| 1113 | } |
| 1114 | } finally { |
| 1115 | restoreCallingIdentity(identityToken); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1120 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1121 | Account resultAccount = null; |
| 1122 | /* |
| 1123 | * Cancel existing notifications. Let authenticators |
| 1124 | * re-post notifications as required. But we don't know if |
| 1125 | * the authenticators have bound their notifications to |
| 1126 | * now stale account name data. |
| 1127 | * |
| 1128 | * With a rename api, we might not need to do this anymore but it |
| 1129 | * shouldn't hurt. |
| 1130 | */ |
| 1131 | cancelNotification( |
| 1132 | getSigninRequiredNotificationId(accounts, accountToRename), |
| 1133 | new UserHandle(accounts.userId)); |
| 1134 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1135 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1136 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1137 | if (accountToRename.equals(pair.first.first)) { |
| 1138 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
| 1139 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | synchronized (accounts.cacheLock) { |
| 1144 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 1145 | db.beginTransaction(); |
| 1146 | boolean isSuccessful = false; |
| 1147 | Account renamedAccount = new Account(newName, accountToRename.type); |
| 1148 | try { |
| 1149 | final ContentValues values = new ContentValues(); |
| 1150 | values.put(ACCOUNTS_NAME, newName); |
| 1151 | values.put(ACCOUNTS_PREVIOUS_NAME, accountToRename.name); |
| 1152 | final long accountId = getAccountIdLocked(db, accountToRename); |
| 1153 | if (accountId >= 0) { |
| 1154 | final String[] argsAccountId = { String.valueOf(accountId) }; |
| 1155 | db.update(TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", argsAccountId); |
| 1156 | db.setTransactionSuccessful(); |
| 1157 | isSuccessful = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1158 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_RENAME, TABLE_ACCOUNTS, accountId, |
| 1159 | accounts); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1160 | } |
| 1161 | } finally { |
| 1162 | db.endTransaction(); |
| 1163 | if (isSuccessful) { |
| 1164 | /* |
| 1165 | * Database transaction was successful. Clean up cached |
| 1166 | * data associated with the account in the user profile. |
| 1167 | */ |
| 1168 | insertAccountIntoCacheLocked(accounts, renamedAccount); |
| 1169 | /* |
| 1170 | * Extract the data and token caches before removing the |
| 1171 | * old account to preserve the user data associated with |
| 1172 | * the account. |
| 1173 | */ |
| 1174 | HashMap<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1175 | HashMap<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
| 1176 | removeAccountFromCacheLocked(accounts, accountToRename); |
| 1177 | /* |
| 1178 | * Update the cached data associated with the renamed |
| 1179 | * account. |
| 1180 | */ |
| 1181 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1182 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 1183 | accounts.previousNameCache.put( |
| 1184 | renamedAccount, |
| 1185 | new AtomicReference<String>(accountToRename.name)); |
| 1186 | resultAccount = renamedAccount; |
| 1187 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1188 | int parentUserId = accounts.userId; |
| 1189 | if (canHaveProfile(parentUserId)) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1190 | /* |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1191 | * Owner or system user account was renamed, rename the account for |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1192 | * those users with which the account was shared. |
| 1193 | */ |
| 1194 | List<UserInfo> users = mUserManager.getUsers(true); |
| 1195 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1196 | if (user.isRestricted() |
| 1197 | && (user.restrictedProfileParentId == parentUserId)) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1198 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | sendAccountsChangedBroadcast(accounts.userId); |
| 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | return resultAccount; |
| 1207 | } |
| 1208 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1209 | private boolean canHaveProfile(final int parentUserId) { |
| 1210 | final UserInfo userInfo = mUserManager.getUserInfo(parentUserId); |
| 1211 | return userInfo != null && userInfo.canHaveProfile(); |
| 1212 | } |
| 1213 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1214 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1215 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1216 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1217 | removeAccountAsUser( |
| 1218 | response, |
| 1219 | account, |
| 1220 | expectActivityLaunch, |
| 1221 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | @Override |
| 1225 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1226 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1227 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1228 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1229 | Log.v(TAG, "removeAccount: " + account |
| 1230 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1231 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1232 | + ", pid " + Binder.getCallingPid() |
| 1233 | + ", for user id " + userId); |
| 1234 | } |
| 1235 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1236 | if (account == null) throw new IllegalArgumentException("account is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1237 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1238 | if (isCrossUser(callingUid, userId)) { |
| 1239 | throw new SecurityException( |
| 1240 | String.format( |
| 1241 | "User %s tying remove account for %s" , |
| 1242 | UserHandle.getCallingUserId(), |
| 1243 | userId)); |
| 1244 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1245 | /* |
| 1246 | * Only the system or authenticator should be allowed to remove accounts for that |
| 1247 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 1248 | * arbitrary applications (like competing authenticators). |
| 1249 | */ |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1250 | UserHandle user = new UserHandle(userId); |
| 1251 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 1252 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1253 | String msg = String.format( |
| 1254 | "uid %s cannot remove accounts of type: %s", |
| 1255 | callingUid, |
| 1256 | account.type); |
| 1257 | throw new SecurityException(msg); |
| 1258 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1259 | if (!canUserModifyAccounts(userId)) { |
| 1260 | try { |
| 1261 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 1262 | "User cannot modify accounts"); |
| 1263 | } catch (RemoteException re) { |
| 1264 | } |
| 1265 | return; |
| 1266 | } |
| 1267 | if (!canUserModifyAccountsForType(userId, account.type)) { |
| 1268 | try { |
| 1269 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 1270 | "User cannot modify accounts of this type (policy)."); |
| 1271 | } catch (RemoteException re) { |
| 1272 | } |
| 1273 | return; |
| 1274 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1275 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1276 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1277 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1278 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1279 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1280 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1281 | if (account.equals(pair.first.first)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1282 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 1283 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1288 | logRecord(accounts, DebugDbHelper.ACTION_CALLED_ACCOUNT_REMOVE, TABLE_ACCOUNTS); |
| 1289 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1290 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1291 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 1292 | } finally { |
| 1293 | restoreCallingIdentity(identityToken); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | @Override |
| 1298 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1299 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1300 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1301 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1302 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1303 | + ", pid " + Binder.getCallingPid()); |
| 1304 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1305 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1306 | if (account == null) { |
| 1307 | /* |
| 1308 | * Null accounts should result in returning false, as per |
| 1309 | * AccountManage.addAccountExplicitly(...) java doc. |
| 1310 | */ |
| 1311 | Log.e(TAG, "account is null"); |
| 1312 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1313 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1314 | String msg = String.format( |
| 1315 | "uid %s cannot explicitly add accounts of type: %s", |
| 1316 | callingUid, |
| 1317 | account.type); |
| 1318 | throw new SecurityException(msg); |
| 1319 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1320 | UserAccounts accounts = getUserAccountsForCaller(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1321 | if (!canUserModifyAccounts(userId) || !canUserModifyAccountsForType(userId, account.type)) { |
| 1322 | return false; |
| 1323 | } |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1324 | logRecord(accounts, DebugDbHelper.ACTION_CALLED_ACCOUNT_REMOVE, TABLE_ACCOUNTS); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1325 | long identityToken = clearCallingIdentity(); |
| 1326 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1327 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1328 | } finally { |
| 1329 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1330 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1333 | private class RemoveAccountSession extends Session { |
| 1334 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1335 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1336 | Account account, boolean expectActivityLaunch) { |
| 1337 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1338 | true /* stripAuthTokenFromResult */, account.name, |
| 1339 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1340 | mAccount = account; |
| 1341 | } |
| 1342 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1343 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1344 | protected String toDebugString(long now) { |
| 1345 | return super.toDebugString(now) + ", removeAccount" |
| 1346 | + ", account " + mAccount; |
| 1347 | } |
| 1348 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1349 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1350 | public void run() throws RemoteException { |
| 1351 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 1352 | } |
| 1353 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1354 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1355 | public void onResult(Bundle result) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1356 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 1357 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 1358 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1359 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1360 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1361 | } |
| 1362 | IAccountManagerResponse response = getResponseAndClose(); |
| 1363 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1364 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1365 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1366 | + response); |
| 1367 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1368 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1369 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1370 | try { |
| 1371 | response.onResult(result2); |
| 1372 | } catch (RemoteException e) { |
| 1373 | // ignore |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | super.onResult(result); |
| 1378 | } |
| 1379 | } |
| 1380 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1381 | /* For testing */ |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1382 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1383 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1384 | } |
| 1385 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1386 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1387 | int deleted; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1388 | synchronized (accounts.cacheLock) { |
| 1389 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1390 | final long accountId = getAccountIdLocked(db, account); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1391 | deleted = db.delete(TABLE_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE |
| 1392 | + "=?", |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1393 | new String[]{account.name, account.type}); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1394 | removeAccountFromCacheLocked(accounts, account); |
| 1395 | sendAccountsChangedBroadcast(accounts.userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1396 | |
| 1397 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_REMOVE, TABLE_ACCOUNTS, accountId, accounts); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1398 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1399 | long id = Binder.clearCallingIdentity(); |
| 1400 | try { |
| 1401 | int parentUserId = accounts.userId; |
| 1402 | if (canHaveProfile(parentUserId)) { |
| 1403 | // Remove from any restricted profiles that are sharing this account. |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1404 | List<UserInfo> users = mUserManager.getUsers(true); |
| 1405 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1406 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1407 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1408 | } |
| 1409 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1410 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1411 | } finally { |
| 1412 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1413 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1414 | return (deleted > 0); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 1417 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1418 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1419 | int callerUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1420 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1421 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1422 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1423 | + ", pid " + Binder.getCallingPid()); |
| 1424 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1425 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 1426 | if (authToken == null) throw new IllegalArgumentException("authToken is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1427 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1428 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1429 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1430 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1431 | synchronized (accounts.cacheLock) { |
| 1432 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1433 | db.beginTransaction(); |
| 1434 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1435 | invalidateAuthTokenLocked(accounts, db, accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1436 | invalidateCustomTokenLocked(accounts, accountType, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1437 | db.setTransactionSuccessful(); |
| 1438 | } finally { |
| 1439 | db.endTransaction(); |
| 1440 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1441 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1442 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1443 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1444 | } |
| 1445 | } |
| 1446 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1447 | private void invalidateCustomTokenLocked( |
| 1448 | UserAccounts accounts, |
| 1449 | String accountType, |
| 1450 | String authToken) { |
| 1451 | if (authToken == null || accountType == null) { |
| 1452 | return; |
| 1453 | } |
| 1454 | // Also wipe out cached token in memory. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1455 | accounts.accountTokenCaches.remove(accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1458 | private void invalidateAuthTokenLocked(UserAccounts accounts, SQLiteDatabase db, |
| 1459 | String accountType, String authToken) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1460 | if (authToken == null || accountType == null) { |
| 1461 | return; |
| 1462 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1463 | Cursor cursor = db.rawQuery( |
| 1464 | "SELECT " + TABLE_AUTHTOKENS + "." + AUTHTOKENS_ID |
| 1465 | + ", " + TABLE_ACCOUNTS + "." + ACCOUNTS_NAME |
| 1466 | + ", " + TABLE_AUTHTOKENS + "." + AUTHTOKENS_TYPE |
| 1467 | + " FROM " + TABLE_ACCOUNTS |
| 1468 | + " JOIN " + TABLE_AUTHTOKENS |
| 1469 | + " ON " + TABLE_ACCOUNTS + "." + ACCOUNTS_ID |
| 1470 | + " = " + AUTHTOKENS_ACCOUNTS_ID |
| 1471 | + " WHERE " + AUTHTOKENS_AUTHTOKEN + " = ? AND " |
| 1472 | + TABLE_ACCOUNTS + "." + ACCOUNTS_TYPE + " = ?", |
| 1473 | new String[]{authToken, accountType}); |
| 1474 | try { |
| 1475 | while (cursor.moveToNext()) { |
| 1476 | long authTokenId = cursor.getLong(0); |
| 1477 | String accountName = cursor.getString(1); |
| 1478 | String authTokenType = cursor.getString(2); |
| 1479 | db.delete(TABLE_AUTHTOKENS, AUTHTOKENS_ID + "=" + authTokenId, null); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1480 | writeAuthTokenIntoCacheLocked( |
| 1481 | accounts, |
| 1482 | db, |
| 1483 | new Account(accountName, accountType), |
| 1484 | authTokenType, |
| 1485 | null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1486 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1487 | } finally { |
| 1488 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1492 | private void saveCachedToken( |
| 1493 | UserAccounts accounts, |
| 1494 | Account account, |
| 1495 | String callerPkg, |
| 1496 | byte[] callerSigDigest, |
| 1497 | String tokenType, |
| 1498 | String token, |
| 1499 | long expiryMillis) { |
| 1500 | |
| 1501 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 1502 | return; |
| 1503 | } |
| 1504 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
| 1505 | new UserHandle(accounts.userId)); |
| 1506 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1507 | accounts.accountTokenCaches.put( |
| 1508 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1512 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 1513 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1514 | if (account == null || type == null) { |
| 1515 | return false; |
| 1516 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 1517 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
| 1518 | new UserHandle(accounts.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1519 | synchronized (accounts.cacheLock) { |
| 1520 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1521 | db.beginTransaction(); |
| 1522 | try { |
| 1523 | long accountId = getAccountIdLocked(db, account); |
| 1524 | if (accountId < 0) { |
| 1525 | return false; |
| 1526 | } |
| 1527 | db.delete(TABLE_AUTHTOKENS, |
| 1528 | AUTHTOKENS_ACCOUNTS_ID + "=" + accountId + " AND " + AUTHTOKENS_TYPE + "=?", |
| 1529 | new String[]{type}); |
| 1530 | ContentValues values = new ContentValues(); |
| 1531 | values.put(AUTHTOKENS_ACCOUNTS_ID, accountId); |
| 1532 | values.put(AUTHTOKENS_TYPE, type); |
| 1533 | values.put(AUTHTOKENS_AUTHTOKEN, authToken); |
| 1534 | if (db.insert(TABLE_AUTHTOKENS, AUTHTOKENS_AUTHTOKEN, values) >= 0) { |
| 1535 | db.setTransactionSuccessful(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1536 | writeAuthTokenIntoCacheLocked(accounts, db, account, type, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1537 | return true; |
| 1538 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1539 | return false; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1540 | } finally { |
| 1541 | db.endTransaction(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1542 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1546 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1547 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1548 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1549 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1550 | Log.v(TAG, "peekAuthToken: " + account |
| 1551 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1552 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1553 | + ", pid " + Binder.getCallingPid()); |
| 1554 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1555 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1556 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1557 | int userId = UserHandle.getCallingUserId(); |
| 1558 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1559 | String msg = String.format( |
| 1560 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 1561 | callingUid, |
| 1562 | account.type); |
| 1563 | throw new SecurityException(msg); |
| 1564 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1565 | long identityToken = clearCallingIdentity(); |
| 1566 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1567 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1568 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1569 | } finally { |
| 1570 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1571 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1574 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1575 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1576 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1577 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1578 | Log.v(TAG, "setAuthToken: " + account |
| 1579 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1580 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1581 | + ", pid " + Binder.getCallingPid()); |
| 1582 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1583 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1584 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1585 | int userId = UserHandle.getCallingUserId(); |
| 1586 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1587 | String msg = String.format( |
| 1588 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 1589 | callingUid, |
| 1590 | account.type); |
| 1591 | throw new SecurityException(msg); |
| 1592 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1593 | long identityToken = clearCallingIdentity(); |
| 1594 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1595 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1596 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1597 | } finally { |
| 1598 | restoreCallingIdentity(identityToken); |
| 1599 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1602 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1603 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1604 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1605 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1606 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1607 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1608 | + ", pid " + Binder.getCallingPid()); |
| 1609 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1610 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1611 | int userId = UserHandle.getCallingUserId(); |
| 1612 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1613 | String msg = String.format( |
| 1614 | "uid %s cannot set secrets for accounts of type: %s", |
| 1615 | callingUid, |
| 1616 | account.type); |
| 1617 | throw new SecurityException(msg); |
| 1618 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1619 | long identityToken = clearCallingIdentity(); |
| 1620 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1621 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1622 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1623 | } finally { |
| 1624 | restoreCallingIdentity(identityToken); |
| 1625 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1628 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 1629 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1630 | if (account == null) { |
| 1631 | return; |
| 1632 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1633 | synchronized (accounts.cacheLock) { |
| 1634 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1635 | db.beginTransaction(); |
| 1636 | try { |
| 1637 | final ContentValues values = new ContentValues(); |
| 1638 | values.put(ACCOUNTS_PASSWORD, password); |
| 1639 | final long accountId = getAccountIdLocked(db, account); |
| 1640 | if (accountId >= 0) { |
| 1641 | final String[] argsAccountId = {String.valueOf(accountId)}; |
| 1642 | db.update(TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", argsAccountId); |
| 1643 | db.delete(TABLE_AUTHTOKENS, AUTHTOKENS_ACCOUNTS_ID + "=?", argsAccountId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1644 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1645 | accounts.accountTokenCaches.remove(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1646 | db.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1647 | |
| 1648 | String action = (password == null || password.length() == 0) ? |
| 1649 | DebugDbHelper.ACTION_CLEAR_PASSWORD |
| 1650 | : DebugDbHelper.ACTION_SET_PASSWORD; |
| 1651 | logRecord(db, action, TABLE_ACCOUNTS, accountId, accounts, callingUid); |
Costin Manolache | f5ffe89 | 2011-01-19 09:35:32 -0800 | [diff] [blame] | 1652 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1653 | } finally { |
| 1654 | db.endTransaction(); |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 1655 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1656 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 1657 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1660 | private void sendAccountsChangedBroadcast(int userId) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1661 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 1662 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1663 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1666 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1667 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1668 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1669 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1670 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1671 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1672 | + ", pid " + Binder.getCallingPid()); |
| 1673 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1674 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1675 | int userId = UserHandle.getCallingUserId(); |
| 1676 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1677 | String msg = String.format( |
| 1678 | "uid %s cannot clear passwords for accounts of type: %s", |
| 1679 | callingUid, |
| 1680 | account.type); |
| 1681 | throw new SecurityException(msg); |
| 1682 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1683 | long identityToken = clearCallingIdentity(); |
| 1684 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1685 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1686 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1687 | } finally { |
| 1688 | restoreCallingIdentity(identityToken); |
| 1689 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1692 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1693 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1694 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1695 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1696 | Log.v(TAG, "setUserData: " + account |
| 1697 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1698 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1699 | + ", pid " + Binder.getCallingPid()); |
| 1700 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1701 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 1702 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1703 | int userId = UserHandle.getCallingUserId(); |
| 1704 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1705 | String msg = String.format( |
| 1706 | "uid %s cannot set user data for accounts of type: %s", |
| 1707 | callingUid, |
| 1708 | account.type); |
| 1709 | throw new SecurityException(msg); |
| 1710 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1711 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1712 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1713 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1714 | setUserdataInternal(accounts, account, key, value); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1715 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1716 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1720 | private void setUserdataInternal(UserAccounts accounts, Account account, String key, |
| 1721 | String value) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1722 | if (account == null || key == null) { |
| 1723 | return; |
| 1724 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1725 | synchronized (accounts.cacheLock) { |
| 1726 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1727 | db.beginTransaction(); |
| 1728 | try { |
| 1729 | long accountId = getAccountIdLocked(db, account); |
| 1730 | if (accountId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1731 | return; |
| 1732 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1733 | long extrasId = getExtrasIdLocked(db, accountId, key); |
| 1734 | if (extrasId < 0 ) { |
| 1735 | extrasId = insertExtraLocked(db, accountId, key, value); |
| 1736 | if (extrasId < 0) { |
| 1737 | return; |
| 1738 | } |
| 1739 | } else { |
| 1740 | ContentValues values = new ContentValues(); |
| 1741 | values.put(EXTRAS_VALUE, value); |
| 1742 | if (1 != db.update(TABLE_EXTRAS, values, EXTRAS_ID + "=" + extrasId, null)) { |
| 1743 | return; |
| 1744 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1745 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1746 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1747 | writeUserDataIntoCacheLocked(accounts, db, account, key, value); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1748 | db.setTransactionSuccessful(); |
| 1749 | } finally { |
| 1750 | db.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1751 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1752 | } |
| 1753 | } |
| 1754 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1755 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1756 | if (result == null) { |
| 1757 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 1758 | } |
| 1759 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1760 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1761 | + response); |
| 1762 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1763 | try { |
| 1764 | response.onResult(result); |
| 1765 | } catch (RemoteException e) { |
| 1766 | // if the caller is dead then there is no one to care about remote |
| 1767 | // exceptions |
| 1768 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1769 | Log.v(TAG, "failure while notifying response", e); |
| 1770 | } |
| 1771 | } |
| 1772 | } |
| 1773 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1774 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 1775 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 1776 | final String authTokenType) |
| 1777 | throws RemoteException { |
| 1778 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1779 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 1780 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 1781 | final int callingUid = getCallingUid(); |
| 1782 | clearCallingIdentity(); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1783 | if (callingUid != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 1784 | throw new SecurityException("can only call from system"); |
| 1785 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1786 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1787 | long identityToken = clearCallingIdentity(); |
| 1788 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1789 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1790 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 1791 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 1792 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1793 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1794 | protected String toDebugString(long now) { |
| 1795 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 1796 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1797 | + ", authTokenType " + authTokenType; |
| 1798 | } |
| 1799 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1800 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1801 | public void run() throws RemoteException { |
| 1802 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 1803 | } |
| 1804 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1805 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 1806 | public void onResult(Bundle result) { |
| 1807 | if (result != null) { |
| 1808 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 1809 | Bundle bundle = new Bundle(); |
| 1810 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 1811 | super.onResult(bundle); |
| 1812 | return; |
| 1813 | } else { |
| 1814 | super.onResult(result); |
| 1815 | } |
| 1816 | } |
| 1817 | }.bind(); |
| 1818 | } finally { |
| 1819 | restoreCallingIdentity(identityToken); |
| 1820 | } |
| 1821 | } |
| 1822 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1823 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1824 | public void getAuthToken( |
| 1825 | IAccountManagerResponse response, |
| 1826 | final Account account, |
| 1827 | final String authTokenType, |
| 1828 | final boolean notifyOnAuthFailure, |
| 1829 | final boolean expectActivityLaunch, |
| 1830 | final Bundle loginOptions) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1831 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1832 | Log.v(TAG, "getAuthToken: " + account |
| 1833 | + ", response " + response |
| 1834 | + ", authTokenType " + authTokenType |
| 1835 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 1836 | + ", expectActivityLaunch " + expectActivityLaunch |
| 1837 | + ", caller's uid " + Binder.getCallingUid() |
| 1838 | + ", pid " + Binder.getCallingPid()); |
| 1839 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1840 | if (response == null) throw new IllegalArgumentException("response is null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1841 | try { |
| 1842 | if (account == null) { |
| 1843 | Slog.w(TAG, "getAuthToken called with null account"); |
| 1844 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 1845 | return; |
| 1846 | } |
| 1847 | if (authTokenType == null) { |
| 1848 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 1849 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 1850 | return; |
| 1851 | } |
| 1852 | } catch (RemoteException e) { |
| 1853 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1854 | return; |
| 1855 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1856 | int userId = UserHandle.getCallingUserId(); |
| 1857 | long ident = Binder.clearCallingIdentity(); |
| 1858 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1859 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1860 | try { |
| 1861 | accounts = getUserAccounts(userId); |
| 1862 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 1863 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 1864 | } finally { |
| 1865 | Binder.restoreCallingIdentity(ident); |
| 1866 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1867 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1868 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1869 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1870 | |
| 1871 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1872 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1873 | final boolean permissionGranted = |
| 1874 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1875 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1876 | // Get the calling package. We will use it for the purpose of caching. |
| 1877 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 1878 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1879 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 1880 | try { |
| 1881 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 1882 | } finally { |
| 1883 | Binder.restoreCallingIdentity(ident); |
| 1884 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1885 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 1886 | String msg = String.format( |
| 1887 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 1888 | callerUid, |
| 1889 | callerPkg); |
| 1890 | throw new SecurityException(msg); |
| 1891 | } |
| 1892 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1893 | // let authenticator know the identity of the caller |
| 1894 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 1895 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1896 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 1897 | if (notifyOnAuthFailure) { |
| 1898 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1899 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1900 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1901 | long identityToken = clearCallingIdentity(); |
| 1902 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 1903 | // Distill the caller's package signatures into a single digest. |
| 1904 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 1905 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1906 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 1907 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1908 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1909 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1910 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1911 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1912 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 1913 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 1914 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1915 | onResult(response, result); |
| 1916 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1917 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1920 | if (customTokens) { |
| 1921 | /* |
| 1922 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 1923 | * outside of those expected to be injected by the AccountManager, e.g. |
| 1924 | * ANDORID_PACKAGE_NAME. |
| 1925 | */ |
| 1926 | String token = readCachedTokenInternal( |
| 1927 | accounts, |
| 1928 | account, |
| 1929 | authTokenType, |
| 1930 | callerPkg, |
| 1931 | callerPkgSigDigest); |
| 1932 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1933 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1934 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 1935 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1936 | Bundle result = new Bundle(); |
| 1937 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 1938 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 1939 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 1940 | onResult(response, result); |
| 1941 | return; |
| 1942 | } |
| 1943 | } |
| 1944 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1945 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1946 | false /* stripAuthTokenFromResult */, account.name, |
| 1947 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1948 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1949 | protected String toDebugString(long now) { |
| 1950 | if (loginOptions != null) loginOptions.keySet(); |
| 1951 | return super.toDebugString(now) + ", getAuthToken" |
| 1952 | + ", " + account |
| 1953 | + ", authTokenType " + authTokenType |
| 1954 | + ", loginOptions " + loginOptions |
| 1955 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 1956 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1957 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1958 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1959 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1960 | // If the caller doesn't have permission then create and return the |
| 1961 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 1962 | if (!permissionGranted) { |
| 1963 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 1964 | } else { |
| 1965 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 1966 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1969 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1970 | public void onResult(Bundle result) { |
| 1971 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1972 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1973 | Intent intent = newGrantCredentialsPermissionIntent( |
| 1974 | account, |
| 1975 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1976 | new AccountAuthenticatorResponse(this), |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1977 | authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1978 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1979 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1980 | onResult(bundle); |
| 1981 | return; |
| 1982 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1983 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1984 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1985 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 1986 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1987 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 1988 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1989 | "the type and name should not be empty"); |
| 1990 | return; |
| 1991 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1992 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 1993 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1994 | saveAuthTokenToDatabase( |
| 1995 | mAccounts, |
| 1996 | resultAccount, |
| 1997 | authTokenType, |
| 1998 | authToken); |
| 1999 | } |
| 2000 | long expiryMillis = result.getLong( |
| 2001 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2002 | if (customTokens |
| 2003 | && expiryMillis > System.currentTimeMillis()) { |
| 2004 | saveCachedToken( |
| 2005 | mAccounts, |
| 2006 | account, |
| 2007 | callerPkg, |
| 2008 | callerPkgSigDigest, |
| 2009 | authTokenType, |
| 2010 | authToken, |
| 2011 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2012 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2013 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2014 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2015 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2016 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2017 | doNotification(mAccounts, |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2018 | account, result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2019 | intent, accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2020 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2021 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2022 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2023 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2024 | }.bind(); |
| 2025 | } finally { |
| 2026 | restoreCallingIdentity(identityToken); |
| 2027 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2030 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2031 | MessageDigest digester; |
| 2032 | try { |
| 2033 | digester = MessageDigest.getInstance("SHA-256"); |
| 2034 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2035 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2036 | for (Signature sig : pkgInfo.signatures) { |
| 2037 | digester.update(sig.toByteArray()); |
| 2038 | } |
| 2039 | } catch (NoSuchAlgorithmException x) { |
| 2040 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2041 | digester = null; |
| 2042 | } catch (NameNotFoundException e) { |
| 2043 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2044 | digester = null; |
| 2045 | } |
| 2046 | return (digester == null) ? null : digester.digest(); |
| 2047 | } |
| 2048 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2049 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
| 2050 | int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2051 | int uid = intent.getIntExtra( |
| 2052 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2053 | String authTokenType = intent.getStringExtra( |
| 2054 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2055 | final String titleAndSubtitle = |
| 2056 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2057 | account.name); |
| 2058 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2059 | String title = titleAndSubtitle; |
| 2060 | String subtitle = ""; |
| 2061 | if (index > 0) { |
| 2062 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2063 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2064 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2065 | UserHandle user = new UserHandle(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2066 | Context contextForUser = getContextForUser(user); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 2067 | Notification n = new Notification.Builder(contextForUser) |
| 2068 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2069 | .setWhen(0) |
| 2070 | .setColor(contextForUser.getColor( |
| 2071 | com.android.internal.R.color.system_notification_accent_color)) |
| 2072 | .setContentTitle(title) |
| 2073 | .setContentText(subtitle) |
| 2074 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2075 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2076 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2077 | installNotification(getCredentialPermissionNotificationId( |
| 2078 | account, authTokenType, uid), n, user); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2081 | private Intent newGrantCredentialsPermissionIntent(Account account, int uid, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2082 | AccountAuthenticatorResponse response, String authTokenType) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2083 | |
| 2084 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 2085 | // 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] | 2086 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2087 | // the intent from a non-Activity context. |
| 2088 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2089 | intent.addCategory( |
| 2090 | String.valueOf(getCredentialPermissionNotificationId(account, authTokenType, uid))); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2091 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2092 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2093 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2094 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2095 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2096 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2097 | return intent; |
| 2098 | } |
| 2099 | |
| 2100 | private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, |
| 2101 | int uid) { |
| 2102 | Integer id; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2103 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2104 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2105 | final Pair<Pair<Account, String>, Integer> key = |
| 2106 | new Pair<Pair<Account, String>, Integer>( |
| 2107 | new Pair<Account, String>(account, authTokenType), uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2108 | id = accounts.credentialsPermissionNotificationIds.get(key); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2109 | if (id == null) { |
| 2110 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2111 | accounts.credentialsPermissionNotificationIds.put(key, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | return id; |
| 2115 | } |
| 2116 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2117 | private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2118 | Integer id; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2119 | synchronized (accounts.signinRequiredNotificationIds) { |
| 2120 | id = accounts.signinRequiredNotificationIds.get(account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2121 | if (id == null) { |
| 2122 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2123 | accounts.signinRequiredNotificationIds.put(account, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | return id; |
| 2127 | } |
| 2128 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2129 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2130 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2131 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2132 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2133 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2134 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2135 | + ", response " + response |
| 2136 | + ", authTokenType " + authTokenType |
| 2137 | + ", requiredFeatures " + stringArrayToString(requiredFeatures) |
| 2138 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2139 | + ", caller's uid " + Binder.getCallingUid() |
| 2140 | + ", pid " + Binder.getCallingPid()); |
| 2141 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2142 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2143 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2144 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2145 | // Is user disallowed from modifying accounts? |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2146 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
| 2147 | if (!canUserModifyAccounts(userId)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2148 | try { |
| 2149 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2150 | "User is not allowed to add an account!"); |
| 2151 | } catch (RemoteException re) { |
| 2152 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2153 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2154 | return; |
| 2155 | } |
| 2156 | if (!canUserModifyAccountsForType(userId, accountType)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2157 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2158 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2159 | "User cannot modify accounts of this type (policy)."); |
| 2160 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2161 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2162 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2163 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2164 | return; |
| 2165 | } |
| 2166 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2167 | final int pid = Binder.getCallingPid(); |
| 2168 | final int uid = Binder.getCallingUid(); |
| 2169 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2170 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2171 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2172 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2173 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2174 | long identityToken = clearCallingIdentity(); |
| 2175 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2176 | UserAccounts accounts = getUserAccounts(usrId); |
| 2177 | logRecordWithUid( |
| 2178 | accounts, DebugDbHelper.ACTION_CALLED_ACCOUNT_ADD, TABLE_ACCOUNTS, uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2179 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2180 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2181 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2182 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2183 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2184 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2185 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2186 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2187 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2188 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2189 | protected String toDebugString(long now) { |
| 2190 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2191 | + ", accountType " + accountType |
| 2192 | + ", requiredFeatures " |
| 2193 | + (requiredFeatures != null |
| 2194 | ? TextUtils.join(",", requiredFeatures) |
| 2195 | : null); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2196 | } |
| 2197 | }.bind(); |
| 2198 | } finally { |
| 2199 | restoreCallingIdentity(identityToken); |
| 2200 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2201 | } |
| 2202 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2203 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2204 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 2205 | final String authTokenType, final String[] requiredFeatures, |
| 2206 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2207 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2208 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2209 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2210 | + ", response " + response |
| 2211 | + ", authTokenType " + authTokenType |
| 2212 | + ", requiredFeatures " + stringArrayToString(requiredFeatures) |
| 2213 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2214 | + ", caller's uid " + Binder.getCallingUid() |
| 2215 | + ", pid " + Binder.getCallingPid() |
| 2216 | + ", for user id " + userId); |
| 2217 | } |
| 2218 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2219 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2220 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2221 | if (isCrossUser(callingUid, userId)) { |
| 2222 | throw new SecurityException( |
| 2223 | String.format( |
| 2224 | "User %s trying to add account for %s" , |
| 2225 | UserHandle.getCallingUserId(), |
| 2226 | userId)); |
| 2227 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2228 | |
| 2229 | // Is user disallowed from modifying accounts? |
| 2230 | if (!canUserModifyAccounts(userId)) { |
| 2231 | try { |
| 2232 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2233 | "User is not allowed to add an account!"); |
| 2234 | } catch (RemoteException re) { |
| 2235 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2236 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2237 | return; |
| 2238 | } |
| 2239 | if (!canUserModifyAccountsForType(userId, accountType)) { |
| 2240 | try { |
| 2241 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2242 | "User cannot modify accounts of this type (policy)."); |
| 2243 | } catch (RemoteException re) { |
| 2244 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2245 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2246 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2247 | return; |
| 2248 | } |
| 2249 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2250 | final int pid = Binder.getCallingPid(); |
| 2251 | final int uid = Binder.getCallingUid(); |
| 2252 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2253 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2254 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2255 | |
| 2256 | long identityToken = clearCallingIdentity(); |
| 2257 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2258 | UserAccounts accounts = getUserAccounts(userId); |
| 2259 | logRecordWithUid( |
| 2260 | accounts, DebugDbHelper.ACTION_CALLED_ACCOUNT_ADD, TABLE_ACCOUNTS, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2261 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2262 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2263 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2264 | @Override |
| 2265 | public void run() throws RemoteException { |
| 2266 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 2267 | options); |
| 2268 | } |
| 2269 | |
| 2270 | @Override |
| 2271 | protected String toDebugString(long now) { |
| 2272 | return super.toDebugString(now) + ", addAccount" |
| 2273 | + ", accountType " + accountType |
| 2274 | + ", requiredFeatures " |
| 2275 | + (requiredFeatures != null |
| 2276 | ? TextUtils.join(",", requiredFeatures) |
| 2277 | : null); |
| 2278 | } |
| 2279 | }.bind(); |
| 2280 | } finally { |
| 2281 | restoreCallingIdentity(identityToken); |
| 2282 | } |
| 2283 | } |
| 2284 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2285 | private void showCantAddAccount(int errorCode, int userId) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2286 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 2287 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 2288 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2289 | long identityToken = clearCallingIdentity(); |
| 2290 | try { |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2291 | mContext.startActivityAsUser(cantAddAccount, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2292 | } finally { |
| 2293 | restoreCallingIdentity(identityToken); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2298 | public void confirmCredentialsAsUser( |
| 2299 | IAccountManagerResponse response, |
| 2300 | final Account account, |
| 2301 | final Bundle options, |
| 2302 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2303 | int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2304 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2305 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2306 | Log.v(TAG, "confirmCredentials: " + account |
| 2307 | + ", response " + response |
| 2308 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2309 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2310 | + ", pid " + Binder.getCallingPid()); |
| 2311 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2312 | // Only allow the system process to read accounts of other users |
| 2313 | if (isCrossUser(callingUid, userId)) { |
| 2314 | throw new SecurityException( |
| 2315 | String.format( |
| 2316 | "User %s trying to confirm account credentials for %s" , |
| 2317 | UserHandle.getCallingUserId(), |
| 2318 | userId)); |
| 2319 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2320 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2321 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2322 | long identityToken = clearCallingIdentity(); |
| 2323 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2324 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2325 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2326 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 2327 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2328 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2329 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2330 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2331 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2332 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2333 | protected String toDebugString(long now) { |
| 2334 | return super.toDebugString(now) + ", confirmCredentials" |
| 2335 | + ", " + account; |
| 2336 | } |
| 2337 | }.bind(); |
| 2338 | } finally { |
| 2339 | restoreCallingIdentity(identityToken); |
| 2340 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2341 | } |
| 2342 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2343 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2344 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 2345 | final String authTokenType, final boolean expectActivityLaunch, |
| 2346 | final Bundle loginOptions) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2347 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2348 | Log.v(TAG, "updateCredentials: " + account |
| 2349 | + ", response " + response |
| 2350 | + ", authTokenType " + authTokenType |
| 2351 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2352 | + ", caller's uid " + Binder.getCallingUid() |
| 2353 | + ", pid " + Binder.getCallingPid()); |
| 2354 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2355 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2356 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2357 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2358 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2359 | long identityToken = clearCallingIdentity(); |
| 2360 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2361 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2362 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2363 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 2364 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2365 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2366 | public void run() throws RemoteException { |
| 2367 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 2368 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2369 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2370 | protected String toDebugString(long now) { |
| 2371 | if (loginOptions != null) loginOptions.keySet(); |
| 2372 | return super.toDebugString(now) + ", updateCredentials" |
| 2373 | + ", " + account |
| 2374 | + ", authTokenType " + authTokenType |
| 2375 | + ", loginOptions " + loginOptions; |
| 2376 | } |
| 2377 | }.bind(); |
| 2378 | } finally { |
| 2379 | restoreCallingIdentity(identityToken); |
| 2380 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2383 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2384 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 2385 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2386 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2387 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2388 | Log.v(TAG, "editProperties: accountType " + accountType |
| 2389 | + ", response " + response |
| 2390 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2391 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2392 | + ", pid " + Binder.getCallingPid()); |
| 2393 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2394 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2395 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2396 | int userId = UserHandle.getCallingUserId(); |
| 2397 | if (!isAccountManagedByCaller(accountType, callingUid, userId) && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2398 | String msg = String.format( |
| 2399 | "uid %s cannot edit authenticator properites for account type: %s", |
| 2400 | callingUid, |
| 2401 | accountType); |
| 2402 | throw new SecurityException(msg); |
| 2403 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2404 | long identityToken = clearCallingIdentity(); |
| 2405 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2406 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2407 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2408 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 2409 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2410 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2411 | public void run() throws RemoteException { |
| 2412 | mAuthenticator.editProperties(this, mAccountType); |
| 2413 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2414 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2415 | protected String toDebugString(long now) { |
| 2416 | return super.toDebugString(now) + ", editProperties" |
| 2417 | + ", accountType " + accountType; |
| 2418 | } |
| 2419 | }.bind(); |
| 2420 | } finally { |
| 2421 | restoreCallingIdentity(identityToken); |
| 2422 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2425 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 2426 | private final String[] mFeatures; |
| 2427 | private volatile Account[] mAccountsOfType = null; |
| 2428 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 2429 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2430 | private final int mCallingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2431 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2432 | public GetAccountsByTypeAndFeatureSession(UserAccounts accounts, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2433 | IAccountManagerResponse response, String type, String[] features, int callingUid) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2434 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2435 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 2436 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2437 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2438 | mFeatures = features; |
| 2439 | } |
| 2440 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2441 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2442 | public void run() throws RemoteException { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2443 | synchronized (mAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2444 | mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType, mCallingUid, |
| 2445 | null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2446 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2447 | // check whether each account matches the requested features |
| 2448 | mAccountsWithFeatures = new ArrayList<Account>(mAccountsOfType.length); |
| 2449 | mCurrentAccount = 0; |
| 2450 | |
| 2451 | checkAccount(); |
| 2452 | } |
| 2453 | |
| 2454 | public void checkAccount() { |
| 2455 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 2456 | sendResult(); |
| 2457 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2458 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2459 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 2460 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 2461 | if (accountAuthenticator == null) { |
| 2462 | // It is possible that the authenticator has died, which is indicated by |
| 2463 | // mAuthenticator being set to null. If this happens then just abort. |
| 2464 | // There is no need to send back a result or error in this case since |
| 2465 | // that already happened when mAuthenticator was cleared. |
| 2466 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2467 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 2468 | + " connected to the authenticator, " + toDebugString()); |
| 2469 | } |
| 2470 | return; |
| 2471 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2472 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 2473 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2474 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2475 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2479 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2480 | public void onResult(Bundle result) { |
| 2481 | mNumResults++; |
| 2482 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2483 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2484 | return; |
| 2485 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2486 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2487 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 2488 | } |
| 2489 | mCurrentAccount++; |
| 2490 | checkAccount(); |
| 2491 | } |
| 2492 | |
| 2493 | public void sendResult() { |
| 2494 | IAccountManagerResponse response = getResponseAndClose(); |
| 2495 | if (response != null) { |
| 2496 | try { |
| 2497 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 2498 | for (int i = 0; i < accounts.length; i++) { |
| 2499 | accounts[i] = mAccountsWithFeatures.get(i); |
| 2500 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2501 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2502 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2503 | + response); |
| 2504 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2505 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2506 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2507 | response.onResult(result); |
| 2508 | } catch (RemoteException e) { |
| 2509 | // if the caller is dead then there is no one to care about remote exceptions |
| 2510 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2511 | Log.v(TAG, "failure while notifying response", e); |
| 2512 | } |
| 2513 | } |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2518 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2519 | protected String toDebugString(long now) { |
| 2520 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 2521 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 2522 | } |
| 2523 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2524 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2525 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2526 | * 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] | 2527 | * @hide |
| 2528 | */ |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2529 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2530 | int callingUid = Binder.getCallingUid(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2531 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 2532 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2533 | if (visibleAccountTypes.isEmpty()) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2534 | return new Account[0]; |
| 2535 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2536 | long identityToken = clearCallingIdentity(); |
| 2537 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 2538 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2539 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 2540 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2541 | callingUid, |
| 2542 | null, // packageName |
| 2543 | visibleAccountTypes); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2544 | } finally { |
| 2545 | restoreCallingIdentity(identityToken); |
| 2546 | } |
| 2547 | } |
| 2548 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 2549 | /** |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2550 | * Returns accounts for all running users. |
| 2551 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 2552 | * @hide |
| 2553 | */ |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2554 | public AccountAndUser[] getRunningAccounts() { |
| 2555 | final int[] runningUserIds; |
| 2556 | try { |
| 2557 | runningUserIds = ActivityManagerNative.getDefault().getRunningUserIds(); |
| 2558 | } catch (RemoteException e) { |
| 2559 | // Running in system_server; should never happen |
| 2560 | throw new RuntimeException(e); |
| 2561 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 2562 | return getAccounts(runningUserIds); |
| 2563 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 2564 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 2565 | /** {@hide} */ |
| 2566 | public AccountAndUser[] getAllAccounts() { |
| 2567 | final List<UserInfo> users = getUserManager().getUsers(); |
| 2568 | final int[] userIds = new int[users.size()]; |
| 2569 | for (int i = 0; i < userIds.length; i++) { |
| 2570 | userIds[i] = users.get(i).id; |
| 2571 | } |
| 2572 | return getAccounts(userIds); |
| 2573 | } |
| 2574 | |
| 2575 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2576 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 2577 | for (int userId : userIds) { |
| 2578 | UserAccounts userAccounts = getUserAccounts(userId); |
| 2579 | if (userAccounts == null) continue; |
| 2580 | synchronized (userAccounts.cacheLock) { |
| 2581 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null, |
| 2582 | Binder.getCallingUid(), null); |
| 2583 | for (int a = 0; a < accounts.length; a++) { |
| 2584 | runningAccounts.add(new AccountAndUser(accounts[a], userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 2585 | } |
| 2586 | } |
| 2587 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2588 | |
| 2589 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 2590 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 2591 | } |
| 2592 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2593 | @Override |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2594 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
| 2595 | return getAccountsAsUser(type, userId, null, -1, opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2596 | } |
| 2597 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2598 | private Account[] getAccountsAsUser( |
| 2599 | String type, |
| 2600 | int userId, |
| 2601 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2602 | int packageUid, |
| 2603 | String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2604 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2605 | // Only allow the system process to read accounts of other users |
| 2606 | if (userId != UserHandle.getCallingUserId() |
Amith Yamasani | bb49e85 | 2013-03-30 19:20:18 -0700 | [diff] [blame] | 2607 | && callingUid != Process.myUid() |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 2608 | && mContext.checkCallingOrSelfPermission( |
| 2609 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 2610 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2611 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 2612 | + " trying to get account for " + userId); |
| 2613 | } |
| 2614 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2615 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2616 | Log.v(TAG, "getAccounts: accountType " + type |
| 2617 | + ", caller's uid " + Binder.getCallingUid() |
| 2618 | + ", pid " + Binder.getCallingPid()); |
| 2619 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2620 | // If the original calling app was using the framework account chooser activity, we'll |
| 2621 | // be passed in the original caller's uid here, which is what should be used for filtering. |
| 2622 | if (packageUid != -1 && UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 2623 | callingUid = packageUid; |
| 2624 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2625 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2626 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 2627 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2628 | if (visibleAccountTypes.isEmpty() |
| 2629 | || (type != null && !visibleAccountTypes.contains(type))) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2630 | return new Account[0]; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2631 | } else if (visibleAccountTypes.contains(type)) { |
| 2632 | // Prune the list down to just the requested type. |
| 2633 | visibleAccountTypes = new ArrayList<>(); |
| 2634 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 2635 | } // else aggregate all the visible accounts (it won't matter if the |
| 2636 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2637 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2638 | long identityToken = clearCallingIdentity(); |
| 2639 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 2640 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2641 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 2642 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2643 | callingUid, |
| 2644 | callingPackage, |
| 2645 | visibleAccountTypes); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2646 | } finally { |
| 2647 | restoreCallingIdentity(identityToken); |
| 2648 | } |
| 2649 | } |
| 2650 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2651 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 2652 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2653 | int callingUid, |
| 2654 | String callingPackage, |
| 2655 | List<String> visibleAccountTypes) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 2656 | synchronized (userAccounts.cacheLock) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2657 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 2658 | for (String visibleType : visibleAccountTypes) { |
| 2659 | Account[] accountsForType = getAccountsFromCacheLocked( |
| 2660 | userAccounts, visibleType, callingUid, callingPackage); |
| 2661 | if (accountsForType != null) { |
| 2662 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
| 2663 | } |
| 2664 | } |
| 2665 | Account[] result = new Account[visibleAccounts.size()]; |
| 2666 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 2667 | result[i] = visibleAccounts.get(i); |
| 2668 | } |
| 2669 | return result; |
| 2670 | } |
| 2671 | } |
| 2672 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2673 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2674 | public boolean addSharedAccountAsUser(Account account, int userId) { |
| 2675 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2676 | UserAccounts accounts = getUserAccounts(userId); |
| 2677 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2678 | ContentValues values = new ContentValues(); |
| 2679 | values.put(ACCOUNTS_NAME, account.name); |
| 2680 | values.put(ACCOUNTS_TYPE, account.type); |
| 2681 | db.delete(TABLE_SHARED_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 2682 | new String[] {account.name, account.type}); |
| 2683 | long accountId = db.insert(TABLE_SHARED_ACCOUNTS, ACCOUNTS_NAME, values); |
| 2684 | if (accountId < 0) { |
| 2685 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 2686 | + ", skipping the DB insert failed"); |
| 2687 | return false; |
| 2688 | } |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2689 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_ADD, TABLE_SHARED_ACCOUNTS, accountId, accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2690 | return true; |
| 2691 | } |
| 2692 | |
| 2693 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2694 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 2695 | userId = handleIncomingUser(userId); |
| 2696 | UserAccounts accounts = getUserAccounts(userId); |
| 2697 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2698 | long sharedTableAccountId = getAccountIdFromSharedTable(db, account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2699 | final ContentValues values = new ContentValues(); |
| 2700 | values.put(ACCOUNTS_NAME, newName); |
| 2701 | values.put(ACCOUNTS_PREVIOUS_NAME, account.name); |
| 2702 | int r = db.update( |
| 2703 | TABLE_SHARED_ACCOUNTS, |
| 2704 | values, |
| 2705 | ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 2706 | new String[] { account.name, account.type }); |
| 2707 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2708 | int callingUid = getCallingUid(); |
| 2709 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_RENAME, TABLE_SHARED_ACCOUNTS, |
| 2710 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2711 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2712 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2713 | } |
| 2714 | return r > 0; |
| 2715 | } |
| 2716 | |
| 2717 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2718 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2719 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 2720 | } |
| 2721 | |
| 2722 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2723 | userId = handleIncomingUser(userId); |
| 2724 | UserAccounts accounts = getUserAccounts(userId); |
| 2725 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2726 | long sharedTableAccountId = getAccountIdFromSharedTable(db, account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2727 | int r = db.delete(TABLE_SHARED_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE+ "=?", |
| 2728 | new String[] {account.name, account.type}); |
| 2729 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2730 | logRecord(db, DebugDbHelper.ACTION_ACCOUNT_REMOVE, TABLE_SHARED_ACCOUNTS, |
| 2731 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2732 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2733 | } |
| 2734 | return r > 0; |
| 2735 | } |
| 2736 | |
| 2737 | @Override |
| 2738 | public Account[] getSharedAccountsAsUser(int userId) { |
| 2739 | userId = handleIncomingUser(userId); |
| 2740 | UserAccounts accounts = getUserAccounts(userId); |
| 2741 | ArrayList<Account> accountList = new ArrayList<Account>(); |
| 2742 | Cursor cursor = null; |
| 2743 | try { |
| 2744 | cursor = accounts.openHelper.getReadableDatabase() |
| 2745 | .query(TABLE_SHARED_ACCOUNTS, new String[]{ACCOUNTS_NAME, ACCOUNTS_TYPE}, |
| 2746 | null, null, null, null, null); |
| 2747 | if (cursor != null && cursor.moveToFirst()) { |
| 2748 | int nameIndex = cursor.getColumnIndex(ACCOUNTS_NAME); |
| 2749 | int typeIndex = cursor.getColumnIndex(ACCOUNTS_TYPE); |
| 2750 | do { |
| 2751 | accountList.add(new Account(cursor.getString(nameIndex), |
| 2752 | cursor.getString(typeIndex))); |
| 2753 | } while (cursor.moveToNext()); |
| 2754 | } |
| 2755 | } finally { |
| 2756 | if (cursor != null) { |
| 2757 | cursor.close(); |
| 2758 | } |
| 2759 | } |
| 2760 | Account[] accountArray = new Account[accountList.size()]; |
| 2761 | accountList.toArray(accountArray); |
| 2762 | return accountArray; |
| 2763 | } |
| 2764 | |
| 2765 | @Override |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2766 | public Account[] getAccounts(String type, String opPackageName) { |
| 2767 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2768 | } |
| 2769 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2770 | @Override |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2771 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2772 | int callingUid = Binder.getCallingUid(); |
| 2773 | if (!UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 2774 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 2775 | + callingUid + " with uid=" + uid); |
| 2776 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2777 | return getAccountsAsUser(null, UserHandle.getCallingUserId(), packageName, uid, |
| 2778 | opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2779 | } |
| 2780 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 2781 | @Override |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2782 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 2783 | String opPackageName) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 2784 | int packageUid = -1; |
| 2785 | try { |
| 2786 | packageUid = AppGlobals.getPackageManager().getPackageUid( |
| 2787 | packageName, UserHandle.getCallingUserId()); |
| 2788 | } catch (RemoteException re) { |
| 2789 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
| 2790 | return new Account[0]; |
| 2791 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2792 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), packageName, |
| 2793 | packageUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 2794 | } |
| 2795 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2796 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2797 | public void getAccountsByFeatures( |
| 2798 | IAccountManagerResponse response, |
| 2799 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2800 | String[] features, |
| 2801 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2802 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2803 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2804 | Log.v(TAG, "getAccounts: accountType " + type |
| 2805 | + ", response " + response |
| 2806 | + ", features " + stringArrayToString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2807 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2808 | + ", pid " + Binder.getCallingPid()); |
| 2809 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2810 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2811 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2812 | int userId = UserHandle.getCallingUserId(); |
| 2813 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 2814 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 2815 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2816 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2817 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2818 | // Need to return just the accounts that are from matching signatures. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2819 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, new Account[0]); |
| 2820 | try { |
| 2821 | response.onResult(result); |
| 2822 | } catch (RemoteException e) { |
| 2823 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 2824 | } |
| 2825 | return; |
| 2826 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2827 | long identityToken = clearCallingIdentity(); |
| 2828 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2829 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2830 | if (features == null || features.length == 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2831 | Account[] accounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2832 | synchronized (userAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2833 | accounts = getAccountsFromCacheLocked(userAccounts, type, callingUid, null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2834 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2835 | Bundle result = new Bundle(); |
| 2836 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 2837 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2838 | return; |
| 2839 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2840 | new GetAccountsByTypeAndFeatureSession( |
| 2841 | userAccounts, |
| 2842 | response, |
| 2843 | type, |
| 2844 | features, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2845 | callingUid).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2846 | } finally { |
| 2847 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2851 | private long getAccountIdFromSharedTable(SQLiteDatabase db, Account account) { |
| 2852 | Cursor cursor = db.query(TABLE_SHARED_ACCOUNTS, new String[]{ACCOUNTS_ID}, |
| 2853 | "name=? AND type=?", new String[]{account.name, account.type}, null, null, null); |
| 2854 | try { |
| 2855 | if (cursor.moveToNext()) { |
| 2856 | return cursor.getLong(0); |
| 2857 | } |
| 2858 | return -1; |
| 2859 | } finally { |
| 2860 | cursor.close(); |
| 2861 | } |
| 2862 | } |
| 2863 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2864 | private long getAccountIdLocked(SQLiteDatabase db, Account account) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2865 | Cursor cursor = db.query(TABLE_ACCOUNTS, new String[]{ACCOUNTS_ID}, |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2866 | "name=? AND type=?", new String[]{account.name, account.type}, null, null, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2867 | try { |
| 2868 | if (cursor.moveToNext()) { |
| 2869 | return cursor.getLong(0); |
| 2870 | } |
| 2871 | return -1; |
| 2872 | } finally { |
| 2873 | cursor.close(); |
| 2874 | } |
| 2875 | } |
| 2876 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2877 | private long getExtrasIdLocked(SQLiteDatabase db, long accountId, String key) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2878 | Cursor cursor = db.query(TABLE_EXTRAS, new String[]{EXTRAS_ID}, |
| 2879 | EXTRAS_ACCOUNTS_ID + "=" + accountId + " AND " + EXTRAS_KEY + "=?", |
| 2880 | new String[]{key}, null, null, null); |
| 2881 | try { |
| 2882 | if (cursor.moveToNext()) { |
| 2883 | return cursor.getLong(0); |
| 2884 | } |
| 2885 | return -1; |
| 2886 | } finally { |
| 2887 | cursor.close(); |
| 2888 | } |
| 2889 | } |
| 2890 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2891 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 2892 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2893 | IAccountManagerResponse mResponse; |
| 2894 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2895 | final boolean mExpectActivityLaunch; |
| 2896 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2897 | final String mAccountName; |
| 2898 | // Indicates if we need to add auth details(like last credential time) |
| 2899 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 2900 | // If set, we need to update the last authenticated time. This is |
| 2901 | // currently |
| 2902 | // used on |
| 2903 | // successful confirming credentials. |
| 2904 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2905 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2906 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2907 | private int mNumRequestContinued = 0; |
| 2908 | private int mNumErrors = 0; |
| 2909 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2910 | IAccountAuthenticator mAuthenticator = null; |
| 2911 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 2912 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2913 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 2914 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2915 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2916 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 2917 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 2918 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 2919 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 2920 | } |
| 2921 | |
| 2922 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 2923 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 2924 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2925 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2926 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2927 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2928 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 2929 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2930 | mResponse = response; |
| 2931 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2932 | mExpectActivityLaunch = expectActivityLaunch; |
| 2933 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2934 | mAccountName = accountName; |
| 2935 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 2936 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2937 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2938 | synchronized (mSessions) { |
| 2939 | mSessions.put(toString(), this); |
| 2940 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2941 | if (response != null) { |
| 2942 | try { |
| 2943 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 2944 | } catch (RemoteException e) { |
| 2945 | mResponse = null; |
| 2946 | binderDied(); |
| 2947 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2948 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2951 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2952 | if (mResponse == null) { |
| 2953 | // this session has already been closed |
| 2954 | return null; |
| 2955 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2956 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2957 | 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] | 2958 | return response; |
| 2959 | } |
| 2960 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2961 | private void close() { |
| 2962 | synchronized (mSessions) { |
| 2963 | if (mSessions.remove(toString()) == null) { |
| 2964 | // the session was already closed, so bail out now |
| 2965 | return; |
| 2966 | } |
| 2967 | } |
| 2968 | if (mResponse != null) { |
| 2969 | // stop listening for response deaths |
| 2970 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 2971 | |
| 2972 | // clear this so that we don't accidentally send any further results |
| 2973 | mResponse = null; |
| 2974 | } |
| 2975 | cancelTimeout(); |
| 2976 | unbind(); |
| 2977 | } |
| 2978 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2979 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2980 | public void binderDied() { |
| 2981 | mResponse = null; |
| 2982 | close(); |
| 2983 | } |
| 2984 | |
| 2985 | protected String toDebugString() { |
| 2986 | return toDebugString(SystemClock.elapsedRealtime()); |
| 2987 | } |
| 2988 | |
| 2989 | protected String toDebugString(long now) { |
| 2990 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 2991 | + ", connected " + (mAuthenticator != null) |
| 2992 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 2993 | + "/" + mNumErrors + ")" |
| 2994 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 2995 | } |
| 2996 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2997 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2998 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2999 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 3000 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 3001 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3002 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3003 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3004 | } |
| 3005 | } |
| 3006 | |
| 3007 | private void unbind() { |
| 3008 | if (mAuthenticator != null) { |
| 3009 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 3010 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3011 | } |
| 3012 | } |
| 3013 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3014 | public void cancelTimeout() { |
| 3015 | mMessageHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
| 3016 | } |
| 3017 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3018 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 3019 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3020 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3021 | try { |
| 3022 | run(); |
| 3023 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3024 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3025 | "remote exception"); |
| 3026 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3029 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 3030 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3031 | mAuthenticator = null; |
| 3032 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3033 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 3034 | try { |
| 3035 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 3036 | "disconnected"); |
| 3037 | } catch (RemoteException e) { |
| 3038 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3039 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 3040 | + "caught RemoteException while responding", e); |
| 3041 | } |
| 3042 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3043 | } |
| 3044 | } |
| 3045 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 3046 | public abstract void run() throws RemoteException; |
| 3047 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3048 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3049 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3050 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 3051 | try { |
| 3052 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 3053 | "timeout"); |
| 3054 | } catch (RemoteException e) { |
| 3055 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3056 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 3057 | e); |
| 3058 | } |
| 3059 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3060 | } |
| 3061 | } |
| 3062 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3063 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3064 | public void onResult(Bundle result) { |
| 3065 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3066 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3067 | if (result != null) { |
| 3068 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 3069 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3070 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3071 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 3072 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 3073 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3074 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 3075 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 3076 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3077 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3078 | if (needUpdate || mAuthDetailsRequired) { |
| 3079 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 3080 | if (needUpdate && accountPresent) { |
| 3081 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 3082 | } |
| 3083 | if (mAuthDetailsRequired) { |
| 3084 | long lastAuthenticatedTime = -1; |
| 3085 | if (accountPresent) { |
| 3086 | lastAuthenticatedTime = DatabaseUtils.longForQuery( |
| 3087 | mAccounts.openHelper.getReadableDatabase(), |
| 3088 | "select " + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS |
| 3089 | + " from " + |
| 3090 | TABLE_ACCOUNTS + " WHERE " + ACCOUNTS_NAME + "=? AND " |
| 3091 | + ACCOUNTS_TYPE + "=?", |
| 3092 | new String[] { |
| 3093 | mAccountName, mAccountType |
| 3094 | }); |
| 3095 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 3096 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3097 | lastAuthenticatedTime); |
| 3098 | } |
| 3099 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3100 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3101 | if (result != null |
| 3102 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
| 3103 | /* |
| 3104 | * The Authenticator API allows third party authenticators to |
| 3105 | * supply arbitrary intents to other apps that they can run, |
| 3106 | * this can be very bad when those apps are in the system like |
| 3107 | * the System Settings. |
| 3108 | */ |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3109 | int authenticatorUid = Binder.getCallingUid(); |
Carlos Valdivia | 416747a | 2013-10-14 17:22:42 -0700 | [diff] [blame] | 3110 | long bid = Binder.clearCallingIdentity(); |
| 3111 | try { |
| 3112 | PackageManager pm = mContext.getPackageManager(); |
| 3113 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 3114 | int targetUid = resolveInfo.activityInfo.applicationInfo.uid; |
| 3115 | if (PackageManager.SIGNATURE_MATCH != |
| 3116 | pm.checkSignatures(authenticatorUid, targetUid)) { |
| 3117 | throw new SecurityException( |
| 3118 | "Activity to be started with KEY_INTENT must " + |
| 3119 | "share Authenticator's signatures"); |
| 3120 | } |
| 3121 | } finally { |
| 3122 | Binder.restoreCallingIdentity(bid); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | if (result != null |
| 3126 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3127 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 3128 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3129 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 3130 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3131 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 3132 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3133 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3134 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3135 | IAccountManagerResponse response; |
| 3136 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3137 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3138 | response = mResponse; |
| 3139 | } else { |
| 3140 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3141 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3142 | if (response != null) { |
| 3143 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3144 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3145 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3146 | Log.v(TAG, getClass().getSimpleName() |
| 3147 | + " calling onError() on response " + response); |
| 3148 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3149 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3150 | "null bundle returned"); |
| 3151 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 3152 | if (mStripAuthTokenFromResult) { |
| 3153 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3154 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3155 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3156 | Log.v(TAG, getClass().getSimpleName() |
| 3157 | + " calling onResult() on response " + response); |
| 3158 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3159 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 3160 | (intent == null)) { |
| 3161 | // All AccountManager error codes are greater than 0 |
| 3162 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3163 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3164 | } else { |
| 3165 | response.onResult(result); |
| 3166 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3167 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3168 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3169 | // if the caller is dead then there is no one to care about remote exceptions |
| 3170 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3171 | Log.v(TAG, "failure while notifying response", e); |
| 3172 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3173 | } |
| 3174 | } |
| 3175 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3176 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3177 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3178 | public void onRequestContinued() { |
| 3179 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3180 | } |
| 3181 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3182 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3183 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3184 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3185 | IAccountManagerResponse response = getResponseAndClose(); |
| 3186 | if (response != null) { |
| 3187 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3188 | Log.v(TAG, getClass().getSimpleName() |
| 3189 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3190 | } |
| 3191 | try { |
| 3192 | response.onError(errorCode, errorMessage); |
| 3193 | } catch (RemoteException e) { |
| 3194 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3195 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 3196 | } |
| 3197 | } |
| 3198 | } else { |
| 3199 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3200 | Log.v(TAG, "Session.onError: already closed"); |
| 3201 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3202 | } |
| 3203 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 3204 | |
| 3205 | /** |
| 3206 | * find the component name for the authenticator and initiate a bind |
| 3207 | * if no authenticator or the bind fails then return false, otherwise return true |
| 3208 | */ |
| 3209 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3210 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 3211 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 3212 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 3213 | if (authenticatorInfo == null) { |
| 3214 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3215 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 3216 | + ", bailing out"); |
| 3217 | } |
| 3218 | return false; |
| 3219 | } |
| 3220 | |
| 3221 | Intent intent = new Intent(); |
| 3222 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 3223 | intent.setComponent(authenticatorInfo.componentName); |
| 3224 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3225 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 3226 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 3227 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
| 3228 | new UserHandle(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 3229 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3230 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 3231 | } |
| 3232 | return false; |
| 3233 | } |
| 3234 | |
| 3235 | |
| 3236 | return true; |
| 3237 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | private class MessageHandler extends Handler { |
| 3241 | MessageHandler(Looper looper) { |
| 3242 | super(looper); |
| 3243 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 3244 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3245 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3246 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3247 | switch (msg.what) { |
| 3248 | case MESSAGE_TIMED_OUT: |
| 3249 | Session session = (Session)msg.obj; |
| 3250 | session.onTimedOut(); |
| 3251 | break; |
| 3252 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 3253 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 3254 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 3255 | break; |
| 3256 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3257 | default: |
| 3258 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 3259 | } |
| 3260 | } |
| 3261 | } |
| 3262 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3263 | private static String getDatabaseName(int userId) { |
| 3264 | File systemDir = Environment.getSystemSecureDirectory(); |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 3265 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), DATABASE_NAME); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3266 | if (userId == 0) { |
Amith Yamasani | a23bb38 | 2012-04-11 15:32:07 -0700 | [diff] [blame] | 3267 | // Migrate old file, if it exists, to the new location. |
| 3268 | // Make sure the new file doesn't already exist. A dummy file could have been |
| 3269 | // accidentally created in the old location, causing the new one to become corrupted |
| 3270 | // as well. |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3271 | File oldFile = new File(systemDir, DATABASE_NAME); |
Amith Yamasani | a23bb38 | 2012-04-11 15:32:07 -0700 | [diff] [blame] | 3272 | if (oldFile.exists() && !databaseFile.exists()) { |
Marc Blank | c6b0f99 | 2012-03-18 19:16:41 -0700 | [diff] [blame] | 3273 | // 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] | 3274 | File userDir = Environment.getUserSystemDirectory(userId); |
Marc Blank | c6b0f99 | 2012-03-18 19:16:41 -0700 | [diff] [blame] | 3275 | if (!userDir.exists()) { |
| 3276 | if (!userDir.mkdirs()) { |
| 3277 | throw new IllegalStateException("User dir cannot be created: " + userDir); |
| 3278 | } |
| 3279 | } |
| 3280 | if (!oldFile.renameTo(databaseFile)) { |
| 3281 | throw new IllegalStateException("User dir cannot be migrated: " + databaseFile); |
| 3282 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3283 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 3284 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3285 | return databaseFile.getPath(); |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 3286 | } |
| 3287 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 3288 | private static class DebugDbHelper{ |
| 3289 | private DebugDbHelper() { |
| 3290 | } |
| 3291 | |
| 3292 | private static String TABLE_DEBUG = "debug_table"; |
| 3293 | |
| 3294 | // Columns for the table |
| 3295 | private static String ACTION_TYPE = "action_type"; |
| 3296 | private static String TIMESTAMP = "time"; |
| 3297 | private static String CALLER_UID = "caller_uid"; |
| 3298 | private static String TABLE_NAME = "table_name"; |
| 3299 | private static String KEY = "primary_key"; |
| 3300 | |
| 3301 | // These actions correspond to the occurrence of real actions. Since |
| 3302 | // these are called by the authenticators, the uid associated will be |
| 3303 | // of the authenticator. |
| 3304 | private static String ACTION_SET_PASSWORD = "action_set_password"; |
| 3305 | private static String ACTION_CLEAR_PASSWORD = "action_clear_password"; |
| 3306 | private static String ACTION_ACCOUNT_ADD = "action_account_add"; |
| 3307 | private static String ACTION_ACCOUNT_REMOVE = "action_account_remove"; |
| 3308 | private static String ACTION_AUTHENTICATOR_REMOVE = "action_authenticator_remove"; |
| 3309 | private static String ACTION_ACCOUNT_RENAME = "action_account_rename"; |
| 3310 | |
| 3311 | // These actions don't necessarily correspond to any action on |
| 3312 | // accountDb taking place. As an example, there might be a request for |
| 3313 | // addingAccount, which might not lead to addition of account on grounds |
| 3314 | // of bad authentication. We will still be logging it to keep track of |
| 3315 | // who called. |
| 3316 | private static String ACTION_CALLED_ACCOUNT_ADD = "action_called_account_add"; |
| 3317 | private static String ACTION_CALLED_ACCOUNT_REMOVE = "action_called_account_remove"; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 3318 | |
| 3319 | private static SimpleDateFormat dateFromat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 3320 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 3321 | private static void createDebugTable(SQLiteDatabase db) { |
| 3322 | db.execSQL("CREATE TABLE " + TABLE_DEBUG + " ( " |
| 3323 | + ACCOUNTS_ID + " INTEGER," |
| 3324 | + ACTION_TYPE + " TEXT NOT NULL, " |
| 3325 | + TIMESTAMP + " DATETIME," |
| 3326 | + CALLER_UID + " INTEGER NOT NULL," |
| 3327 | + TABLE_NAME + " TEXT NOT NULL," |
| 3328 | + KEY + " INTEGER PRIMARY KEY)"); |
| 3329 | db.execSQL("CREATE INDEX timestamp_index ON " + TABLE_DEBUG + " (" + TIMESTAMP + ")"); |
| 3330 | } |
| 3331 | } |
| 3332 | |
| 3333 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
| 3334 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 3335 | logRecord(db, action, tableName, -1, accounts); |
| 3336 | } |
| 3337 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3338 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
| 3339 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 3340 | logRecord(db, action, tableName, -1, accounts, uid); |
| 3341 | } |
| 3342 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 3343 | /* |
| 3344 | * This function receives an opened writable database. |
| 3345 | */ |
| 3346 | private void logRecord(SQLiteDatabase db, String action, String tableName, long accountId, |
| 3347 | UserAccounts userAccount) { |
| 3348 | logRecord(db, action, tableName, accountId, userAccount, getCallingUid()); |
| 3349 | } |
| 3350 | |
| 3351 | /* |
| 3352 | * This function receives an opened writable database. |
| 3353 | */ |
| 3354 | private void logRecord(SQLiteDatabase db, String action, String tableName, long accountId, |
| 3355 | UserAccounts userAccount, int callingUid) { |
| 3356 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 3357 | logStatement.bindLong(1, accountId); |
| 3358 | logStatement.bindString(2, action); |
| 3359 | logStatement.bindString(3, DebugDbHelper.dateFromat.format(new Date())); |
| 3360 | logStatement.bindLong(4, callingUid); |
| 3361 | logStatement.bindString(5, tableName); |
| 3362 | logStatement.bindLong(6, userAccount.debugDbInsertionPoint); |
| 3363 | logStatement.execute(); |
| 3364 | logStatement.clearBindings(); |
| 3365 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
| 3366 | % MAX_DEBUG_DB_SIZE; |
| 3367 | } |
| 3368 | |
| 3369 | /* |
| 3370 | * This should only be called once to compile the sql statement for logging |
| 3371 | * and to find the insertion point. |
| 3372 | */ |
| 3373 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(SQLiteDatabase db, |
| 3374 | UserAccounts userAccount) { |
| 3375 | // Initialize the count if not done earlier. |
| 3376 | int size = (int) getDebugTableRowCount(db); |
| 3377 | if (size >= MAX_DEBUG_DB_SIZE) { |
| 3378 | // Table is full, and we need to find the point where to insert. |
| 3379 | userAccount.debugDbInsertionPoint = (int) getDebugTableInsertionPoint(db); |
| 3380 | } else { |
| 3381 | userAccount.debugDbInsertionPoint = size; |
| 3382 | } |
| 3383 | compileSqlStatementForLogging(db, userAccount); |
| 3384 | } |
| 3385 | |
| 3386 | private void compileSqlStatementForLogging(SQLiteDatabase db, UserAccounts userAccount) { |
| 3387 | String sql = "INSERT OR REPLACE INTO " + DebugDbHelper.TABLE_DEBUG |
| 3388 | + " VALUES (?,?,?,?,?,?)"; |
| 3389 | userAccount.statementForLogging = db.compileStatement(sql); |
| 3390 | } |
| 3391 | |
| 3392 | private long getDebugTableRowCount(SQLiteDatabase db) { |
| 3393 | String queryCountDebugDbRows = "SELECT COUNT(*) FROM " + DebugDbHelper.TABLE_DEBUG; |
| 3394 | return DatabaseUtils.longForQuery(db, queryCountDebugDbRows, null); |
| 3395 | } |
| 3396 | |
| 3397 | /* |
| 3398 | * Finds the row key where the next insertion should take place. This should |
| 3399 | * be invoked only if the table has reached its full capacity. |
| 3400 | */ |
| 3401 | private long getDebugTableInsertionPoint(SQLiteDatabase db) { |
| 3402 | // This query finds the smallest timestamp value (and if 2 records have |
| 3403 | // same timestamp, the choose the lower id). |
| 3404 | String queryCountDebugDbRows = new StringBuilder() |
| 3405 | .append("SELECT ").append(DebugDbHelper.KEY) |
| 3406 | .append(" FROM ").append(DebugDbHelper.TABLE_DEBUG) |
| 3407 | .append(" ORDER BY ") |
| 3408 | .append(DebugDbHelper.TIMESTAMP).append(",").append(DebugDbHelper.KEY) |
| 3409 | .append(" LIMIT 1") |
| 3410 | .toString(); |
| 3411 | return DatabaseUtils.longForQuery(db, queryCountDebugDbRows, null); |
| 3412 | } |
| 3413 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3414 | static class DatabaseHelper extends SQLiteOpenHelper { |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 3415 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3416 | public DatabaseHelper(Context context, int userId) { |
| 3417 | super(context, AccountManagerService.getDatabaseName(userId), null, DATABASE_VERSION); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3418 | } |
| 3419 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3420 | /** |
| 3421 | * This call needs to be made while the mCacheLock is held. The way to |
| 3422 | * ensure this is to get the lock any time a method is called ont the DatabaseHelper |
| 3423 | * @param db The database. |
| 3424 | */ |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3425 | @Override |
| 3426 | public void onCreate(SQLiteDatabase db) { |
| 3427 | db.execSQL("CREATE TABLE " + TABLE_ACCOUNTS + " ( " |
| 3428 | + ACCOUNTS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 3429 | + ACCOUNTS_NAME + " TEXT NOT NULL, " |
| 3430 | + ACCOUNTS_TYPE + " TEXT NOT NULL, " |
| 3431 | + ACCOUNTS_PASSWORD + " TEXT, " |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 3432 | + ACCOUNTS_PREVIOUS_NAME + " TEXT, " |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3433 | + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS + " INTEGER DEFAULT 0, " |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3434 | + "UNIQUE(" + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + "))"); |
| 3435 | |
| 3436 | db.execSQL("CREATE TABLE " + TABLE_AUTHTOKENS + " ( " |
| 3437 | + AUTHTOKENS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 3438 | + AUTHTOKENS_ACCOUNTS_ID + " INTEGER NOT NULL, " |
| 3439 | + AUTHTOKENS_TYPE + " TEXT NOT NULL, " |
| 3440 | + AUTHTOKENS_AUTHTOKEN + " TEXT, " |
| 3441 | + "UNIQUE (" + AUTHTOKENS_ACCOUNTS_ID + "," + AUTHTOKENS_TYPE + "))"); |
| 3442 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3443 | createGrantsTable(db); |
| 3444 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3445 | db.execSQL("CREATE TABLE " + TABLE_EXTRAS + " ( " |
| 3446 | + EXTRAS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 3447 | + EXTRAS_ACCOUNTS_ID + " INTEGER, " |
| 3448 | + EXTRAS_KEY + " TEXT NOT NULL, " |
| 3449 | + EXTRAS_VALUE + " TEXT, " |
| 3450 | + "UNIQUE(" + EXTRAS_ACCOUNTS_ID + "," + EXTRAS_KEY + "))"); |
| 3451 | |
| 3452 | db.execSQL("CREATE TABLE " + TABLE_META + " ( " |
| 3453 | + META_KEY + " TEXT PRIMARY KEY NOT NULL, " |
| 3454 | + META_VALUE + " TEXT)"); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3455 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 3456 | createSharedAccountsTable(db); |
| 3457 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3458 | createAccountsDeletionTrigger(db); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 3459 | |
| 3460 | DebugDbHelper.createDebugTable(db); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3461 | } |
| 3462 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 3463 | private void createSharedAccountsTable(SQLiteDatabase db) { |
| 3464 | db.execSQL("CREATE TABLE " + TABLE_SHARED_ACCOUNTS + " ( " |
| 3465 | + ACCOUNTS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 3466 | + ACCOUNTS_NAME + " TEXT NOT NULL, " |
| 3467 | + ACCOUNTS_TYPE + " TEXT NOT NULL, " |
| 3468 | + "UNIQUE(" + ACCOUNTS_NAME + "," + ACCOUNTS_TYPE + "))"); |
| 3469 | } |
| 3470 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3471 | private void addLastSuccessfullAuthenticatedTimeColumn(SQLiteDatabase db) { |
| 3472 | db.execSQL("ALTER TABLE " + TABLE_ACCOUNTS + " ADD COLUMN " |
| 3473 | + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS + " DEFAULT 0"); |
| 3474 | } |
| 3475 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 3476 | private void addOldAccountNameColumn(SQLiteDatabase db) { |
| 3477 | db.execSQL("ALTER TABLE " + TABLE_ACCOUNTS + " ADD COLUMN " + ACCOUNTS_PREVIOUS_NAME); |
| 3478 | } |
| 3479 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 3480 | private void addDebugTable(SQLiteDatabase db) { |
| 3481 | DebugDbHelper.createDebugTable(db); |
| 3482 | } |
| 3483 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3484 | private void createAccountsDeletionTrigger(SQLiteDatabase db) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3485 | db.execSQL("" |
| 3486 | + " CREATE TRIGGER " + TABLE_ACCOUNTS + "Delete DELETE ON " + TABLE_ACCOUNTS |
| 3487 | + " BEGIN" |
| 3488 | + " DELETE FROM " + TABLE_AUTHTOKENS |
| 3489 | + " WHERE " + AUTHTOKENS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
| 3490 | + " DELETE FROM " + TABLE_EXTRAS |
| 3491 | + " WHERE " + EXTRAS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3492 | + " DELETE FROM " + TABLE_GRANTS |
| 3493 | + " WHERE " + GRANTS_ACCOUNTS_ID + "=OLD." + ACCOUNTS_ID + " ;" |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3494 | + " END"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3497 | private void createGrantsTable(SQLiteDatabase db) { |
| 3498 | db.execSQL("CREATE TABLE " + TABLE_GRANTS + " ( " |
| 3499 | + GRANTS_ACCOUNTS_ID + " INTEGER NOT NULL, " |
| 3500 | + GRANTS_AUTH_TOKEN_TYPE + " STRING NOT NULL, " |
| 3501 | + GRANTS_GRANTEE_UID + " INTEGER NOT NULL, " |
| 3502 | + "UNIQUE (" + GRANTS_ACCOUNTS_ID + "," + GRANTS_AUTH_TOKEN_TYPE |
| 3503 | + "," + GRANTS_GRANTEE_UID + "))"); |
| 3504 | } |
| 3505 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3506 | @Override |
| 3507 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3508 | Log.e(TAG, "upgrade from version " + oldVersion + " to version " + newVersion); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3509 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3510 | if (oldVersion == 1) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3511 | // no longer need to do anything since the work is done |
| 3512 | // when upgrading from version 2 |
| 3513 | oldVersion++; |
| 3514 | } |
| 3515 | |
| 3516 | if (oldVersion == 2) { |
| 3517 | createGrantsTable(db); |
| 3518 | db.execSQL("DROP TRIGGER " + TABLE_ACCOUNTS + "Delete"); |
| 3519 | createAccountsDeletionTrigger(db); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3520 | oldVersion++; |
| 3521 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 3522 | |
| 3523 | if (oldVersion == 3) { |
| 3524 | db.execSQL("UPDATE " + TABLE_ACCOUNTS + " SET " + ACCOUNTS_TYPE + |
| 3525 | " = 'com.google' WHERE " + ACCOUNTS_TYPE + " == 'com.google.GAIA'"); |
| 3526 | oldVersion++; |
| 3527 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 3528 | |
| 3529 | if (oldVersion == 4) { |
| 3530 | createSharedAccountsTable(db); |
| 3531 | oldVersion++; |
| 3532 | } |
| 3533 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 3534 | if (oldVersion == 5) { |
| 3535 | addOldAccountNameColumn(db); |
| 3536 | oldVersion++; |
| 3537 | } |
| 3538 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3539 | if (oldVersion == 6) { |
| 3540 | addLastSuccessfullAuthenticatedTimeColumn(db); |
| 3541 | oldVersion++; |
| 3542 | } |
| 3543 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 3544 | if (oldVersion == 7) { |
| 3545 | addDebugTable(db); |
| 3546 | oldVersion++; |
| 3547 | } |
| 3548 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 3549 | if (oldVersion != newVersion) { |
| 3550 | Log.e(TAG, "failed to upgrade version " + oldVersion + " to version " + newVersion); |
| 3551 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | @Override |
| 3555 | public void onOpen(SQLiteDatabase db) { |
| 3556 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "opened database " + DATABASE_NAME); |
| 3557 | } |
| 3558 | } |
| 3559 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3560 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3561 | return asBinder(); |
| 3562 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3563 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 3564 | /** |
| 3565 | * Searches array of arguments for the specified string |
| 3566 | * @param args array of argument strings |
| 3567 | * @param value value to search for |
| 3568 | * @return true if the value is contained in the array |
| 3569 | */ |
| 3570 | private static boolean scanArgs(String[] args, String value) { |
| 3571 | if (args != null) { |
| 3572 | for (String arg : args) { |
| 3573 | if (value.equals(arg)) { |
| 3574 | return true; |
| 3575 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3576 | } |
| 3577 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 3578 | return false; |
| 3579 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3580 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3581 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 3582 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 3583 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 3584 | != PackageManager.PERMISSION_GRANTED) { |
| 3585 | fout.println("Permission Denial: can't dump AccountsManager from from pid=" |
| 3586 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 3587 | + " without permission " + android.Manifest.permission.DUMP); |
| 3588 | return; |
| 3589 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3590 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3591 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 3592 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3593 | final List<UserInfo> users = getUserManager().getUsers(); |
| 3594 | for (UserInfo user : users) { |
| 3595 | ipw.println("User " + user + ":"); |
| 3596 | ipw.increaseIndent(); |
| 3597 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 3598 | ipw.println(); |
| 3599 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3600 | } |
| 3601 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3602 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3603 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 3604 | String[] args, boolean isCheckinRequest) { |
| 3605 | synchronized (userAccounts.cacheLock) { |
| 3606 | final SQLiteDatabase db = userAccounts.openHelper.getReadableDatabase(); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 3607 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3608 | if (isCheckinRequest) { |
| 3609 | // This is a checkin request. *Only* upload the account types and the count of each. |
| 3610 | Cursor cursor = db.query(TABLE_ACCOUNTS, ACCOUNT_TYPE_COUNT_PROJECTION, |
| 3611 | null, null, ACCOUNTS_TYPE, null, null); |
| 3612 | try { |
| 3613 | while (cursor.moveToNext()) { |
| 3614 | // print type,count |
| 3615 | fout.println(cursor.getString(0) + "," + cursor.getString(1)); |
| 3616 | } |
| 3617 | } finally { |
| 3618 | if (cursor != null) { |
| 3619 | cursor.close(); |
| 3620 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 3621 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3622 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3623 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3624 | Process.myUid(), null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3625 | fout.println("Accounts: " + accounts.length); |
| 3626 | for (Account account : accounts) { |
| 3627 | fout.println(" " + account); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 3628 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 3629 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 3630 | // Add debug information. |
| 3631 | fout.println(); |
| 3632 | Cursor cursor = db.query(DebugDbHelper.TABLE_DEBUG, null, |
| 3633 | null, null, null, null, DebugDbHelper.TIMESTAMP); |
| 3634 | fout.println("AccountId, Action_Type, timestamp, UID, TableName, Key"); |
| 3635 | fout.println("Accounts History"); |
| 3636 | try { |
| 3637 | while (cursor.moveToNext()) { |
| 3638 | // print type,count |
| 3639 | fout.println(cursor.getString(0) + "," + cursor.getString(1) + "," + |
| 3640 | cursor.getString(2) + "," + cursor.getString(3) + "," |
| 3641 | + cursor.getString(4) + "," + cursor.getString(5)); |
| 3642 | } |
| 3643 | } finally { |
| 3644 | cursor.close(); |
| 3645 | } |
| 3646 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3647 | fout.println(); |
| 3648 | synchronized (mSessions) { |
| 3649 | final long now = SystemClock.elapsedRealtime(); |
| 3650 | fout.println("Active Sessions: " + mSessions.size()); |
| 3651 | for (Session session : mSessions.values()) { |
| 3652 | fout.println(" " + session.toDebugString(now)); |
| 3653 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 3654 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 3655 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3656 | fout.println(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3657 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3658 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 3659 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3660 | } |
| 3661 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3662 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3663 | Intent intent, int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3664 | long identityToken = clearCallingIdentity(); |
| 3665 | try { |
| 3666 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3667 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 3668 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3669 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3670 | if (intent.getComponent() != null && |
| 3671 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 3672 | intent.getComponent().getClassName())) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3673 | createNoCredentialsPermissionNotification(account, intent, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3674 | } else { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3675 | final Integer notificationId = getSigninRequiredNotificationId(accounts, account); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 3676 | intent.addCategory(String.valueOf(notificationId)); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3677 | UserHandle user = new UserHandle(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 3678 | Context contextForUser = getContextForUser(user); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 3679 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 3680 | contextForUser.getText(R.string.notification_title).toString(); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 3681 | Notification n = new Notification.Builder(contextForUser) |
| 3682 | .setWhen(0) |
| 3683 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 3684 | .setColor(contextForUser.getColor( |
| 3685 | com.android.internal.R.color.system_notification_accent_color)) |
| 3686 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 3687 | .setContentText(message) |
| 3688 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 3689 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
| 3690 | null, user)) |
| 3691 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3692 | installNotification(notificationId, n, user); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3693 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3694 | } finally { |
| 3695 | restoreCallingIdentity(identityToken); |
| 3696 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3699 | protected void installNotification(final int notificationId, final Notification n, |
| 3700 | UserHandle user) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3701 | ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)) |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3702 | .notifyAsUser(null, notificationId, n, user); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3703 | } |
| 3704 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3705 | protected void cancelNotification(int id, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3706 | long identityToken = clearCallingIdentity(); |
| 3707 | try { |
| 3708 | ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)) |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3709 | .cancelAsUser(null, id, user); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3710 | } finally { |
| 3711 | restoreCallingIdentity(identityToken); |
| 3712 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3713 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3714 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 3715 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 3716 | for (String perm : permissions) { |
| 3717 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 3718 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3719 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 3720 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 3721 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 3722 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 3723 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 3724 | return true; |
| 3725 | } |
Fred Quintana | b38eb14 | 2010-02-24 13:40:54 -0800 | [diff] [blame] | 3726 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3727 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3728 | return false; |
| 3729 | } |
| 3730 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 3731 | private int handleIncomingUser(int userId) { |
| 3732 | try { |
| 3733 | return ActivityManagerNative.getDefault().handleIncomingUser( |
| 3734 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 3735 | } catch (RemoteException re) { |
| 3736 | // Shouldn't happen, local. |
| 3737 | } |
| 3738 | return userId; |
| 3739 | } |
| 3740 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 3741 | private boolean isPrivileged(int callingUid) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3742 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 3743 | |
| 3744 | final PackageManager userPackageManager; |
| 3745 | try { |
| 3746 | userPackageManager = mContext.createPackageContextAsUser( |
| 3747 | "android", 0, new UserHandle(callingUserId)).getPackageManager(); |
| 3748 | } catch (NameNotFoundException e) { |
| 3749 | return false; |
| 3750 | } |
| 3751 | |
| 3752 | String[] packages = userPackageManager.getPackagesForUid(callingUid); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 3753 | for (String name : packages) { |
| 3754 | try { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3755 | PackageInfo packageInfo = userPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3756 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 3757 | && (packageInfo.applicationInfo.privateFlags |
| 3758 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 3759 | return true; |
| 3760 | } |
| 3761 | } catch (PackageManager.NameNotFoundException e) { |
| 3762 | return false; |
| 3763 | } |
| 3764 | } |
| 3765 | return false; |
| 3766 | } |
| 3767 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3768 | private boolean permissionIsGranted( |
| 3769 | Account account, String authTokenType, int callerUid, int userId) { |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 3770 | final boolean isPrivileged = isPrivileged(callerUid); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 3771 | final boolean fromAuthenticator = account != null |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3772 | && isAccountManagedByCaller(account.type, callerUid, userId); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 3773 | final boolean hasExplicitGrants = account != null |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3774 | && hasExplicitlyGrantedPermission(account, authTokenType, callerUid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3775 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3776 | Log.v(TAG, "checkGrantsOrCallingUidAgainstAuthenticator: caller uid " |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3777 | + callerUid + ", " + account |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3778 | + ": is authenticator? " + fromAuthenticator |
| 3779 | + ", has explicit permission? " + hasExplicitGrants); |
| 3780 | } |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 3781 | return fromAuthenticator || hasExplicitGrants || isPrivileged; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3782 | } |
| 3783 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 3784 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 3785 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3786 | if (accountType == null) { |
| 3787 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3788 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 3789 | return getTypesVisibleToCaller(callingUid, userId, |
| 3790 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3791 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 3795 | if (accountType == null) { |
| 3796 | return false; |
| 3797 | } else { |
| 3798 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 3799 | } |
| 3800 | } |
| 3801 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 3802 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 3803 | String opPackageName) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3804 | boolean isPermitted = |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 3805 | isPermitted(opPackageName, callingUid, Manifest.permission.GET_ACCOUNTS, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3806 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 3807 | Log.i(TAG, String.format("getTypesVisibleToCaller: isPermitted? %s", isPermitted)); |
| 3808 | return getTypesForCaller(callingUid, userId, isPermitted); |
| 3809 | } |
| 3810 | |
| 3811 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
| 3812 | return getTypesForCaller(callingUid, userId, false); |
| 3813 | } |
| 3814 | |
| 3815 | private List<String> getTypesForCaller( |
| 3816 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 3817 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 3818 | long identityToken = Binder.clearCallingIdentity(); |
| 3819 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 3820 | try { |
| 3821 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 3822 | } finally { |
| 3823 | Binder.restoreCallingIdentity(identityToken); |
| 3824 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3825 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 3826 | serviceInfos) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3827 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 3828 | if (isOtherwisePermitted || sigChk == PackageManager.SIGNATURE_MATCH) { |
| 3829 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3830 | } |
| 3831 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3832 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3833 | } |
| 3834 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3835 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 3836 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 3837 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 3838 | if (account.name.equals(accountName)) { |
| 3839 | return true; |
| 3840 | } |
| 3841 | } |
| 3842 | } |
| 3843 | return false; |
| 3844 | } |
| 3845 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3846 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 3847 | int callerUid) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3848 | if (callerUid == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3849 | return true; |
| 3850 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3851 | UserAccounts accounts = getUserAccountsForCaller(); |
| 3852 | synchronized (accounts.cacheLock) { |
| 3853 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
| 3854 | String[] args = { String.valueOf(callerUid), authTokenType, |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3855 | account.name, account.type}; |
| 3856 | final boolean permissionGranted = |
| 3857 | DatabaseUtils.longForQuery(db, COUNT_OF_MATCHING_GRANTS, args) != 0; |
| 3858 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 3859 | // TODO: Skip this check when running automated tests. Replace this |
| 3860 | // with a more general solution. |
| 3861 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3862 | + authTokenType + " by uid " + callerUid |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3863 | + " but ignoring since device is in test harness."); |
| 3864 | return true; |
| 3865 | } |
| 3866 | return permissionGranted; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3867 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3868 | } |
| 3869 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3870 | private boolean isSystemUid(int callingUid) { |
| 3871 | String[] packages = null; |
| 3872 | long ident = Binder.clearCallingIdentity(); |
| 3873 | try { |
| 3874 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 3875 | } finally { |
| 3876 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 3877 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3878 | if (packages != null) { |
| 3879 | for (String name : packages) { |
| 3880 | try { |
| 3881 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 3882 | if (packageInfo != null |
| 3883 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 3884 | != 0) { |
| 3885 | return true; |
| 3886 | } |
| 3887 | } catch (PackageManager.NameNotFoundException e) { |
| 3888 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 3889 | } |
| 3890 | } |
| 3891 | } else { |
| 3892 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 3893 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3894 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 3895 | } |
| 3896 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3897 | /** Succeeds if any of the specified permissions are granted. */ |
| 3898 | private void checkReadAccountsPermitted( |
| 3899 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3900 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame^] | 3901 | int userId, |
| 3902 | String opPackageName) { |
| 3903 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3904 | String msg = String.format( |
| 3905 | "caller uid %s cannot access %s accounts", |
| 3906 | callingUid, |
| 3907 | accountType); |
| 3908 | Log.w(TAG, " " + msg); |
| 3909 | throw new SecurityException(msg); |
| 3910 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3913 | private boolean canUserModifyAccounts(int userId) { |
| 3914 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 3915 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 3916 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 3917 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3918 | return true; |
| 3919 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 3920 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3921 | private boolean canUserModifyAccountsForType(int userId, String accountType) { |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 3922 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 3923 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3924 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 3925 | if (typesArray == null) { |
| 3926 | return true; |
| 3927 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 3928 | for (String forbiddenType : typesArray) { |
| 3929 | if (forbiddenType.equals(accountType)) { |
| 3930 | return false; |
| 3931 | } |
| 3932 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 3933 | return true; |
| 3934 | } |
| 3935 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3936 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 3937 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 3938 | throws RemoteException { |
| 3939 | final int callingUid = getCallingUid(); |
| 3940 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3941 | if (callingUid != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 3942 | throw new SecurityException(); |
| 3943 | } |
| 3944 | |
| 3945 | if (value) { |
| 3946 | grantAppPermission(account, authTokenType, uid); |
| 3947 | } else { |
| 3948 | revokeAppPermission(account, authTokenType, uid); |
| 3949 | } |
| 3950 | } |
| 3951 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3952 | /** |
| 3953 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 3954 | * <p> |
| 3955 | * Although this is public it can only be accessed via the AccountManagerService object |
| 3956 | * which is in the system. This means we don't need to protect it with permissions. |
| 3957 | * @hide |
| 3958 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 3959 | private void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3960 | if (account == null || authTokenType == null) { |
| 3961 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 3962 | return; |
| 3963 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3964 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3965 | synchronized (accounts.cacheLock) { |
| 3966 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3967 | db.beginTransaction(); |
| 3968 | try { |
| 3969 | long accountId = getAccountIdLocked(db, account); |
| 3970 | if (accountId >= 0) { |
| 3971 | ContentValues values = new ContentValues(); |
| 3972 | values.put(GRANTS_ACCOUNTS_ID, accountId); |
| 3973 | values.put(GRANTS_AUTH_TOKEN_TYPE, authTokenType); |
| 3974 | values.put(GRANTS_GRANTEE_UID, uid); |
| 3975 | db.insert(TABLE_GRANTS, GRANTS_ACCOUNTS_ID, values); |
| 3976 | db.setTransactionSuccessful(); |
| 3977 | } |
| 3978 | } finally { |
| 3979 | db.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3980 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3981 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 3982 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3983 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | /** |
| 3987 | * Don't allow callers with the given uid permission to get credentials for |
| 3988 | * account/authTokenType. |
| 3989 | * <p> |
| 3990 | * Although this is public it can only be accessed via the AccountManagerService object |
| 3991 | * which is in the system. This means we don't need to protect it with permissions. |
| 3992 | * @hide |
| 3993 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 3994 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3995 | if (account == null || authTokenType == null) { |
| 3996 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 3997 | return; |
| 3998 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3999 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4000 | synchronized (accounts.cacheLock) { |
| 4001 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4002 | db.beginTransaction(); |
| 4003 | try { |
| 4004 | long accountId = getAccountIdLocked(db, account); |
| 4005 | if (accountId >= 0) { |
| 4006 | db.delete(TABLE_GRANTS, |
| 4007 | GRANTS_ACCOUNTS_ID + "=? AND " + GRANTS_AUTH_TOKEN_TYPE + "=? AND " |
| 4008 | + GRANTS_GRANTEE_UID + "=?", |
| 4009 | new String[]{String.valueOf(accountId), authTokenType, |
| 4010 | String.valueOf(uid)}); |
| 4011 | db.setTransactionSuccessful(); |
| 4012 | } |
| 4013 | } finally { |
| 4014 | db.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4015 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4016 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 4017 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4018 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4019 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4020 | |
| 4021 | static final private String stringArrayToString(String[] value) { |
| 4022 | return value != null ? ("[" + TextUtils.join(",", value) + "]") : null; |
| 4023 | } |
| 4024 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4025 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 4026 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4027 | if (oldAccountsForType != null) { |
| 4028 | ArrayList<Account> newAccountsList = new ArrayList<Account>(); |
| 4029 | for (Account curAccount : oldAccountsForType) { |
| 4030 | if (!curAccount.equals(account)) { |
| 4031 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4032 | } |
| 4033 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4034 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4035 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4036 | } else { |
| 4037 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 4038 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4039 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4040 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4041 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4042 | accounts.userDataCache.remove(account); |
| 4043 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4044 | accounts.previousNameCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | /** |
| 4048 | * This assumes that the caller has already checked that the account is not already present. |
| 4049 | */ |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4050 | private void insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
| 4051 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4052 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 4053 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 4054 | if (accountsForType != null) { |
| 4055 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4056 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4057 | newAccountsForType[oldLength] = account; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4058 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4059 | } |
| 4060 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4061 | private Account[] filterSharedAccounts(UserAccounts userAccounts, Account[] unfiltered, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4062 | int callingUid, String callingPackage) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4063 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4064 | || callingUid == Process.myUid()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4065 | return unfiltered; |
| 4066 | } |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4067 | UserInfo user = mUserManager.getUserInfo(userAccounts.userId); |
| 4068 | if (user != null && user.isRestricted()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4069 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 4070 | // If any of the packages is a white listed package, return the full set, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4071 | // otherwise return non-shared accounts only. |
| 4072 | // This might be a temporary way to specify a whitelist |
| 4073 | String whiteList = mContext.getResources().getString( |
| 4074 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 4075 | for (String packageName : packages) { |
| 4076 | if (whiteList.contains(";" + packageName + ";")) { |
| 4077 | return unfiltered; |
| 4078 | } |
| 4079 | } |
| 4080 | ArrayList<Account> allowed = new ArrayList<Account>(); |
| 4081 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
| 4082 | if (sharedAccounts == null || sharedAccounts.length == 0) return unfiltered; |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 4083 | String requiredAccountType = ""; |
| 4084 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 4085 | // If there's an explicit callingPackage specified, check if that package |
| 4086 | // opted in to see restricted accounts. |
| 4087 | if (callingPackage != null) { |
| 4088 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 4089 | if (pi != null && pi.restrictedAccountType != null) { |
| 4090 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 4091 | } |
| 4092 | } else { |
| 4093 | // Otherwise check if the callingUid has a package that has opted in |
| 4094 | for (String packageName : packages) { |
| 4095 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 4096 | if (pi != null && pi.restrictedAccountType != null) { |
| 4097 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4098 | break; |
| 4099 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4100 | } |
| 4101 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 4102 | } catch (NameNotFoundException nnfe) { |
| 4103 | } |
| 4104 | for (Account account : unfiltered) { |
| 4105 | if (account.type.equals(requiredAccountType)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4106 | allowed.add(account); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 4107 | } else { |
| 4108 | boolean found = false; |
| 4109 | for (Account shared : sharedAccounts) { |
| 4110 | if (shared.equals(account)) { |
| 4111 | found = true; |
| 4112 | break; |
| 4113 | } |
| 4114 | } |
| 4115 | if (!found) { |
| 4116 | allowed.add(account); |
| 4117 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4118 | } |
| 4119 | } |
| 4120 | Account[] filtered = new Account[allowed.size()]; |
| 4121 | allowed.toArray(filtered); |
| 4122 | return filtered; |
| 4123 | } else { |
| 4124 | return unfiltered; |
| 4125 | } |
| 4126 | } |
| 4127 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4128 | /* |
| 4129 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 4130 | * that the package is not allowed to access. |
| 4131 | */ |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4132 | protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4133 | int callingUid, String callingPackage) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4134 | if (accountType != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4135 | final Account[] accounts = userAccounts.accountCache.get(accountType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4136 | if (accounts == null) { |
| 4137 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4138 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4139 | return filterSharedAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4140 | callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4141 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4142 | } else { |
| 4143 | int totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4144 | for (Account[] accounts : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4145 | totalLength += accounts.length; |
| 4146 | } |
| 4147 | if (totalLength == 0) { |
| 4148 | return EMPTY_ACCOUNT_ARRAY; |
| 4149 | } |
| 4150 | Account[] accounts = new Account[totalLength]; |
| 4151 | totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4152 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4153 | System.arraycopy(accountsOfType, 0, accounts, totalLength, |
| 4154 | accountsOfType.length); |
| 4155 | totalLength += accountsOfType.length; |
| 4156 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4157 | return filterSharedAccounts(userAccounts, accounts, callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4158 | } |
| 4159 | } |
| 4160 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4161 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db, |
| 4162 | Account account, String key, String value) { |
| 4163 | HashMap<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4164 | if (userDataForAccount == null) { |
| 4165 | userDataForAccount = readUserDataForAccountFromDatabaseLocked(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4166 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4167 | } |
| 4168 | if (value == null) { |
| 4169 | userDataForAccount.remove(key); |
| 4170 | } else { |
| 4171 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4172 | } |
| 4173 | } |
| 4174 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4175 | protected String readCachedTokenInternal( |
| 4176 | UserAccounts accounts, |
| 4177 | Account account, |
| 4178 | String tokenType, |
| 4179 | String callingPackage, |
| 4180 | byte[] pkgSigDigest) { |
| 4181 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4182 | return accounts.accountTokenCaches.get( |
| 4183 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4184 | } |
| 4185 | } |
| 4186 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4187 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db, |
| 4188 | Account account, String key, String value) { |
| 4189 | HashMap<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4190 | if (authTokensForAccount == null) { |
| 4191 | authTokensForAccount = readAuthTokensForAccountFromDatabaseLocked(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4192 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4193 | } |
| 4194 | if (value == null) { |
| 4195 | authTokensForAccount.remove(key); |
| 4196 | } else { |
| 4197 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4198 | } |
| 4199 | } |
| 4200 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4201 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 4202 | String authTokenType) { |
| 4203 | synchronized (accounts.cacheLock) { |
| 4204 | HashMap<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4205 | if (authTokensForAccount == null) { |
| 4206 | // need to populate the cache for this account |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4207 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4208 | authTokensForAccount = readAuthTokensForAccountFromDatabaseLocked(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4209 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4210 | } |
| 4211 | return authTokensForAccount.get(authTokenType); |
| 4212 | } |
| 4213 | } |
| 4214 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4215 | protected String readUserDataInternal(UserAccounts accounts, Account account, String key) { |
| 4216 | synchronized (accounts.cacheLock) { |
| 4217 | HashMap<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4218 | if (userDataForAccount == null) { |
| 4219 | // need to populate the cache for this account |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4220 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4221 | userDataForAccount = readUserDataForAccountFromDatabaseLocked(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4222 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4223 | } |
| 4224 | return userDataForAccount.get(key); |
| 4225 | } |
| 4226 | } |
| 4227 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4228 | protected HashMap<String, String> readUserDataForAccountFromDatabaseLocked( |
| 4229 | final SQLiteDatabase db, Account account) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4230 | HashMap<String, String> userDataForAccount = new HashMap<String, String>(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4231 | Cursor cursor = db.query(TABLE_EXTRAS, |
| 4232 | COLUMNS_EXTRAS_KEY_AND_VALUE, |
| 4233 | SELECTION_USERDATA_BY_ACCOUNT, |
| 4234 | new String[]{account.name, account.type}, |
| 4235 | null, null, null); |
| 4236 | try { |
| 4237 | while (cursor.moveToNext()) { |
| 4238 | final String tmpkey = cursor.getString(0); |
| 4239 | final String value = cursor.getString(1); |
| 4240 | userDataForAccount.put(tmpkey, value); |
| 4241 | } |
| 4242 | } finally { |
| 4243 | cursor.close(); |
| 4244 | } |
| 4245 | return userDataForAccount; |
| 4246 | } |
| 4247 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4248 | protected HashMap<String, String> readAuthTokensForAccountFromDatabaseLocked( |
| 4249 | final SQLiteDatabase db, Account account) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4250 | HashMap<String, String> authTokensForAccount = new HashMap<String, String>(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4251 | Cursor cursor = db.query(TABLE_AUTHTOKENS, |
| 4252 | COLUMNS_AUTHTOKENS_TYPE_AND_AUTHTOKEN, |
| 4253 | SELECTION_AUTHTOKENS_BY_ACCOUNT, |
| 4254 | new String[]{account.name, account.type}, |
| 4255 | null, null, null); |
| 4256 | try { |
| 4257 | while (cursor.moveToNext()) { |
| 4258 | final String type = cursor.getString(0); |
| 4259 | final String authToken = cursor.getString(1); |
| 4260 | authTokensForAccount.put(type, authToken); |
| 4261 | } |
| 4262 | } finally { |
| 4263 | cursor.close(); |
| 4264 | } |
| 4265 | return authTokensForAccount; |
| 4266 | } |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4267 | |
| 4268 | private Context getContextForUser(UserHandle user) { |
| 4269 | try { |
| 4270 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 4271 | } catch (NameNotFoundException e) { |
| 4272 | // Default to mContext, not finding the package system is running as is unlikely. |
| 4273 | return mContext; |
| 4274 | } |
| 4275 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4276 | } |