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