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