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