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; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 25 | import android.accounts.AccountManagerInternal; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 26 | import android.accounts.AuthenticatorDescription; |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 27 | import android.accounts.CantAddAccountActivity; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 28 | import android.accounts.GrantCredentialsPermissionActivity; |
| 29 | import android.accounts.IAccountAuthenticator; |
| 30 | import android.accounts.IAccountAuthenticatorResponse; |
| 31 | import android.accounts.IAccountManager; |
| 32 | import android.accounts.IAccountManagerResponse; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 33 | import android.annotation.IntRange; |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 34 | import android.annotation.NonNull; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 35 | import android.annotation.Nullable; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 36 | import android.app.ActivityManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 37 | import android.app.ActivityManagerNative; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 38 | import android.app.ActivityThread; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 39 | import android.app.AppGlobals; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 40 | import android.app.AppOpsManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 41 | import android.app.INotificationManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 42 | import android.app.Notification; |
| 43 | import android.app.NotificationManager; |
| 44 | import android.app.PendingIntent; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 45 | import android.app.admin.DeviceAdminInfo; |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 46 | import android.app.admin.DevicePolicyManager; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 47 | import android.app.admin.DevicePolicyManagerInternal; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 48 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 49 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 50 | import android.content.Context; |
| 51 | import android.content.Intent; |
| 52 | import android.content.IntentFilter; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 53 | import android.content.IntentSender; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 54 | import android.content.ServiceConnection; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 55 | import android.content.pm.ActivityInfo; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 56 | import android.content.pm.ApplicationInfo; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 57 | import android.content.pm.IPackageManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 58 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 59 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 60 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 61 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 62 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 63 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 64 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 65 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 66 | import android.database.Cursor; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 67 | import android.database.sqlite.SQLiteDatabase; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 68 | import android.database.sqlite.SQLiteStatement; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 69 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 70 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 71 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 72 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 73 | import android.os.IBinder; |
| 74 | import android.os.Looper; |
| 75 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 76 | import android.os.Parcel; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 77 | import android.os.Process; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 78 | import android.os.RemoteCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 79 | import android.os.RemoteException; |
| 80 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 81 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 82 | import android.os.UserManager; |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 83 | import android.os.storage.StorageManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 84 | import android.text.TextUtils; |
| 85 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 86 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 87 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 88 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 89 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 90 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 91 | import com.android.internal.R; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 92 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 93 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 94 | import com.android.internal.content.PackageMonitor; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 95 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 96 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 97 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 98 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 99 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 100 | import com.android.server.SystemService; |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 101 | import com.android.server.accounts.AccountsDb.CeDatabaseHelper; |
| 102 | import com.android.server.accounts.AccountsDb.DeDatabaseHelper; |
| 103 | import com.android.server.accounts.AccountsDb.DebugDbHelper; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 104 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 105 | import com.google.android.collect.Lists; |
| 106 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 107 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 108 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 109 | import java.io.FileDescriptor; |
| 110 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 111 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 112 | import java.security.MessageDigest; |
| 113 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 114 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 115 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 116 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 117 | import java.util.Collection; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 118 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 119 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 120 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 121 | import java.util.LinkedHashMap; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 122 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 123 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 124 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 125 | import java.util.Objects; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 126 | import java.util.Set; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 127 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 128 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 129 | import java.util.concurrent.atomic.AtomicInteger; |
| 130 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 131 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 132 | /** |
| 133 | * A system service that provides account, password, and authtoken management for all |
| 134 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 135 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 136 | * 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] | 137 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 138 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 139 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 140 | public class AccountManagerService |
| 141 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 142 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 143 | private static final String TAG = "AccountManagerService"; |
| 144 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 145 | public static class Lifecycle extends SystemService { |
| 146 | private AccountManagerService mService; |
| 147 | |
| 148 | public Lifecycle(Context context) { |
| 149 | super(context); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void onStart() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 154 | mService = new AccountManagerService(new Injector(getContext())); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 155 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 156 | } |
| 157 | |
| 158 | @Override |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 159 | public void onUnlockUser(int userHandle) { |
| 160 | mService.onUnlockUser(userHandle); |
| 161 | } |
| 162 | } |
| 163 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 164 | private static final int MAX_DEBUG_DB_SIZE = 64; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 165 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 166 | final Context mContext; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 167 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 168 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 169 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 170 | private UserManager mUserManager; |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 171 | private final Injector mInjector; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 172 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 173 | final MessageHandler mHandler; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 174 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 175 | // Messages that can be sent on mHandler |
| 176 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 177 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 178 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 179 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 180 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 181 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2d | 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 | |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 188 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 189 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 190 | private final AtomicInteger mNotificationIds = new AtomicInteger(1); |
| 191 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 192 | private static final String NEW_ACCOUNT_VISIBLE = "android.accounts.NEW_ACCOUNT_VISIBLE"; |
| 193 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 194 | static class UserAccounts { |
| 195 | private final int userId; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 196 | final DeDatabaseHelper openHelper; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 197 | private final HashMap<Pair<Pair<Account, String>, Integer>, Integer> |
| 198 | credentialsPermissionNotificationIds = |
| 199 | new HashMap<Pair<Pair<Account, String>, Integer>, Integer>(); |
| 200 | private final HashMap<Account, Integer> signinRequiredNotificationIds = |
| 201 | new HashMap<Account, Integer>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 202 | final Object cacheLock = new Object(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 203 | /** protected by the {@link #cacheLock} */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 204 | final HashMap<String, Account[]> accountCache = |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 205 | new LinkedHashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 206 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 207 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 208 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 209 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 210 | |
| 211 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 212 | private final TokenCache accountTokenCaches = new TokenCache(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 213 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 214 | /** protected by the {@link #cacheLock} */ |
| 215 | private final Map<String, ArrayList<Integer>> mApplicationAccountRequestMappings = |
| 216 | new HashMap<>(); |
| 217 | |
| 218 | /* Together the below two Sparse Arrays serve as visible list. One maps UID to account |
| 219 | number. Another maps Account number to Account.*/ |
| 220 | |
| 221 | /** protected by the {@link #cacheLock} */ |
| 222 | private final SparseArray<ArrayList<Integer>> mVisibleListUidToMockAccountNumbers = |
| 223 | new SparseArray<>(); |
| 224 | |
| 225 | //TODO: Instead of using Mock Account IDs, use the actual account IDs. |
| 226 | /** protected by the {@link #cacheLock} */ |
| 227 | private final SparseArray<Account> mMockAccountIdToAccount = new SparseArray<>(); |
| 228 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 229 | /** |
| 230 | * protected by the {@link #cacheLock} |
| 231 | * |
| 232 | * Caches the previous names associated with an account. Previous names |
| 233 | * should be cached because we expect that when an Account is renamed, |
| 234 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 235 | * want to know if the accounts they care about have been renamed. |
| 236 | * |
| 237 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 238 | * we can distinguish between those accounts with no previous names and |
| 239 | * those whose previous names haven't been cached (yet). |
| 240 | */ |
| 241 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 242 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 243 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 244 | private int debugDbInsertionPoint = -1; |
| 245 | private SQLiteStatement statementForLogging; |
| 246 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 247 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 248 | this.userId = userId; |
| 249 | synchronized (cacheLock) { |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 250 | openHelper = DeDatabaseHelper.create(context, userId, preNDbFile, deDbFile); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 255 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 256 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 257 | // Not thread-safe. Only use in synchronized context |
| 258 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 259 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 260 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 261 | |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 262 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 263 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 264 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 265 | /** |
| 266 | * This should only be called by system code. One should only call this after the service |
| 267 | * has started. |
| 268 | * @return a reference to the AccountManagerService instance |
| 269 | * @hide |
| 270 | */ |
| 271 | public static AccountManagerService getSingleton() { |
| 272 | return sThis.get(); |
| 273 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 274 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 275 | public AccountManagerService(Injector injector) { |
| 276 | mInjector = injector; |
| 277 | mContext = injector.getContext(); |
| 278 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 279 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 280 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 281 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 282 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 283 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 284 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 285 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 286 | addRequestsForPreInstalledApplications(); |
| 287 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 288 | IntentFilter intentFilter = new IntentFilter(); |
| 289 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 290 | intentFilter.addDataScheme("package"); |
| 291 | mContext.registerReceiver(new BroadcastReceiver() { |
| 292 | @Override |
| 293 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 294 | // Don't delete accounts when updating a authenticator's |
| 295 | // package. |
| 296 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 297 | /* Purging data requires file io, don't block the main thread. This is probably |
| 298 | * less than ideal because we are introducing a race condition where old grants |
| 299 | * could be exercised until they are purged. But that race condition existed |
| 300 | * anyway with the broadcast receiver. |
| 301 | * |
| 302 | * Ideally, we would completely clear the cache, purge data from the database, |
| 303 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 304 | * large at this point. |
| 305 | */ |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 306 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 307 | @Override |
| 308 | public void run() { |
| 309 | purgeOldGrantsAll(); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 310 | |
| 311 | /* clears application request's for account types supported */ |
| 312 | int uidOfUninstalledApplication = |
| 313 | intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 314 | if(uidOfUninstalledApplication != -1) { |
| 315 | clearRequestedAccountVisibility(uidOfUninstalledApplication, |
| 316 | getUserAccounts(UserHandle.getUserId( |
| 317 | uidOfUninstalledApplication))); |
| 318 | } |
| 319 | |
| 320 | /* removes visibility of previous UID of this uninstalled application*/ |
| 321 | removeAccountVisibilityAllAccounts(uidOfUninstalledApplication, |
| 322 | getUserAccounts(UserHandle.getUserId( |
| 323 | uidOfUninstalledApplication))); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 324 | } |
| 325 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 326 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 327 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 328 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 329 | } |
| 330 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 331 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 332 | IntentFilter packageAddedOrChangedFilter = new IntentFilter(); |
| 333 | intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 334 | packageAddedOrChangedFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 335 | packageAddedOrChangedFilter.addDataScheme("package"); |
| 336 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 337 | @Override |
| 338 | public void onReceive(Context context1, Intent intent) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 339 | mHandler.post(new Runnable() { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 340 | @Override |
| 341 | public void run() { |
| 342 | int uidOfInstalledApplication = |
| 343 | intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 344 | if(uidOfInstalledApplication != -1) { |
| 345 | registerAccountTypesSupported( |
| 346 | uidOfInstalledApplication, |
| 347 | getUserAccounts( |
| 348 | UserHandle.getUserId(uidOfInstalledApplication))); |
| 349 | } |
| 350 | } |
| 351 | }); |
| 352 | } |
| 353 | }, UserHandle.ALL, packageAddedOrChangedFilter, null, null); |
| 354 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 355 | IntentFilter userFilter = new IntentFilter(); |
| 356 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 357 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 358 | @Override |
| 359 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 360 | String action = intent.getAction(); |
| 361 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 362 | onUserRemoved(intent); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 363 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 364 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 365 | }, UserHandle.ALL, userFilter, null, null); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 366 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 367 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 368 | |
| 369 | // Need to cancel account request notifications if the update/install can access the account |
| 370 | new PackageMonitor() { |
| 371 | @Override |
| 372 | public void onPackageAdded(String packageName, int uid) { |
| 373 | // Called on a handler, and running as the system |
| 374 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 379 | // Called on a handler, and running as the system |
| 380 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 381 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 382 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 383 | |
| 384 | // Cancel account request notification if an app op was preventing the account access |
| 385 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 386 | new AppOpsManager.OnOpChangedInternalListener() { |
| 387 | @Override |
| 388 | public void onOpChanged(int op, String packageName) { |
| 389 | try { |
| 390 | final int userId = ActivityManager.getCurrentUser(); |
| 391 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 392 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 393 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 394 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 395 | final long identity = Binder.clearCallingIdentity(); |
| 396 | try { |
| 397 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 398 | } finally { |
| 399 | Binder.restoreCallingIdentity(identity); |
| 400 | } |
| 401 | } |
| 402 | } catch (NameNotFoundException e) { |
| 403 | /* ignore */ |
| 404 | } |
| 405 | } |
| 406 | }); |
| 407 | |
| 408 | // Cancel account request notification if a permission was preventing the account access |
| 409 | mPackageManager.addOnPermissionsChangeListener( |
| 410 | (int uid) -> { |
| 411 | Account[] accounts = null; |
| 412 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 413 | if (packageNames != null) { |
| 414 | final int userId = UserHandle.getUserId(uid); |
| 415 | final long identity = Binder.clearCallingIdentity(); |
| 416 | try { |
| 417 | for (String packageName : packageNames) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 418 | if (mPackageManager.checkPermission( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 419 | Manifest.permission.GET_ACCOUNTS, packageName) |
| 420 | != PackageManager.PERMISSION_GRANTED) { |
| 421 | continue; |
| 422 | } |
| 423 | |
| 424 | if (accounts == null) { |
| 425 | accounts = getAccountsAsUser(null, userId, "android"); |
| 426 | if (ArrayUtils.isEmpty(accounts)) { |
| 427 | return; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | for (Account account : accounts) { |
| 432 | cancelAccountAccessRequestNotificationIfNeeded( |
| 433 | account, uid, packageName, true); |
| 434 | } |
| 435 | } |
| 436 | } finally { |
| 437 | Binder.restoreCallingIdentity(identity); |
| 438 | } |
| 439 | } |
| 440 | }); |
| 441 | } |
| 442 | |
| 443 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 444 | boolean checkAccess) { |
| 445 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 446 | for (Account account : accounts) { |
| 447 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 452 | boolean checkAccess) { |
| 453 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 454 | for (Account account : accounts) { |
| 455 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 460 | boolean checkAccess) { |
| 461 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 462 | if (packageNames != null) { |
| 463 | for (String packageName : packageNames) { |
| 464 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 465 | packageName, checkAccess); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 471 | int uid, String packageName, boolean checkAccess) { |
| 472 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 473 | UserHandle.getUserHandleForUid(uid))) { |
| 474 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 475 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 476 | UserHandle.getUserHandleForUid(uid)); |
| 477 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 480 | @Override |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 481 | public boolean addAccountExplicitlyWithUid(Account account, String password, Bundle extras, |
| 482 | int[] selectedUids) { |
| 483 | if(addAccountExplicitly(account,password,extras)) { |
| 484 | for(int thisUid : selectedUids) { |
| 485 | makeAccountVisible(account, thisUid); |
| 486 | } |
| 487 | return true; |
| 488 | } |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public int[] getRequestingUidsForType(String accountType) { |
| 494 | int callingUid = Binder.getCallingUid(); |
| 495 | if (!isAccountManagedByCaller(accountType, callingUid, UserHandle.getUserId(callingUid))) { |
| 496 | String msg = String.format( |
| 497 | "uid %s cannot get secrets for accounts of type: %s", |
| 498 | callingUid, |
| 499 | accountType); |
| 500 | throw new SecurityException(msg); |
| 501 | } |
| 502 | return getRequestingUidsForType(accountType, getUserAccounts( |
| 503 | UserHandle.getUserId(callingUid))); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Returns all UIDs for applications that requested the account type. This method |
| 508 | * is called indirectly by the Authenticator and AccountManager |
| 509 | * |
| 510 | * @param accountType authenticator would like to know the requesting apps of |
| 511 | * @param ua UserAccount that currently hosts the account and application |
| 512 | * |
| 513 | * @return ArrayList of all UIDs that support accounts of this |
| 514 | * account type that seek approval (to be used to know which accounts for |
| 515 | * the authenticator to include in addAccountExplicitly). Null if none. |
| 516 | */ |
| 517 | private int[] getRequestingUidsForType(String accountType, UserAccounts ua) { |
| 518 | synchronized(ua.cacheLock) { |
| 519 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 520 | ua.mApplicationAccountRequestMappings; |
| 521 | ArrayList<Integer> allUidsForAccountType = userApplicationAccountRequestMappings.get( |
| 522 | accountType); |
| 523 | if(allUidsForAccountType == null) { |
| 524 | return null; |
| 525 | } |
| 526 | int[] toReturn = new int[allUidsForAccountType.size()]; |
| 527 | for(int i = 0 ; i < toReturn.length ; i++) { |
| 528 | toReturn[i] = allUidsForAccountType.get(i); |
| 529 | } |
| 530 | return toReturn; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public boolean isAccountVisible(Account a, int uid) { |
| 536 | int callingUid = Binder.getCallingUid(); |
| 537 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 538 | String msg = String.format( |
| 539 | "uid %s cannot get secrets for accounts of type: %s", |
| 540 | callingUid, |
| 541 | a.type); |
| 542 | throw new SecurityException(msg); |
| 543 | } |
| 544 | return isAccountVisible(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Checks visibility of certain account of a process identified |
| 549 | * by a given UID. This is called by the Authenticator indirectly. |
| 550 | * |
| 551 | * @param a The account to check visibility of |
| 552 | * @param uid UID to check visibility of |
| 553 | * @param ua UserAccount that currently hosts the account and application |
| 554 | * |
| 555 | * @return True if application has access to the account |
| 556 | * |
| 557 | */ |
| 558 | private boolean isAccountVisible(Account a, int uid, UserAccounts ua) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 559 | if(isAccountManagedByCaller(a.type, uid, UserHandle.getUserId(uid))) { |
| 560 | return true; |
| 561 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 562 | int accountMapping = getMockAccountNumber(a, ua); |
| 563 | if(accountMapping < 0) { |
| 564 | return true; |
| 565 | } |
| 566 | synchronized(ua.cacheLock) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 567 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 568 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 569 | ua.mVisibleListUidToMockAccountNumbers; |
| 570 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 571 | int indexOfAccountMapping = userAcctIdToAcctMap.indexOfValueByValue(a); |
| 572 | return indexOfAccountMapping == -1 || (linkedAccountsToUid != null |
| 573 | && linkedAccountsToUid.contains(accountMapping)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | @Override |
| 578 | public boolean makeAccountVisible(Account a, int uid) { |
| 579 | int callingUid = Binder.getCallingUid(); |
| 580 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 581 | String msg = String.format( |
| 582 | "uid %s cannot get secrets for accounts of type: %s", |
| 583 | callingUid, |
| 584 | a.type); |
| 585 | throw new SecurityException(msg); |
| 586 | } |
| 587 | return makeAccountVisible(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Gives a certain UID, represented a application, access to an account. This method |
| 592 | * is called indirectly by the Authenticator. |
| 593 | * |
| 594 | * @param a Account to make visible |
| 595 | * @param uid to add visibility of the Account from |
| 596 | * @param ua UserAccount that currently hosts the account and application |
| 597 | * |
| 598 | * @return True if account made visible to application and was not previously visible. |
| 599 | */ |
| 600 | private boolean makeAccountVisible(Account a, int uid, UserAccounts ua) { |
| 601 | int accountMapping = getMockAccountNumber(a, ua); |
| 602 | if(accountMapping < 0) { |
| 603 | accountMapping = makeAccountNumber(a, ua); |
| 604 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 605 | synchronized(ua.cacheLock) { |
| 606 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 607 | ua.mVisibleListUidToMockAccountNumbers; |
| 608 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
| 609 | if(linkedAccountsToUid == null) { |
| 610 | linkedAccountsToUid = new ArrayList<>(); |
| 611 | linkedAccountsToUid.add(accountMapping); |
| 612 | userWlUidToMockAccountNums.put(uid, linkedAccountsToUid); |
| 613 | } else if(!linkedAccountsToUid.contains(accountMapping)) { |
| 614 | linkedAccountsToUid.add(accountMapping); |
| 615 | } else { |
| 616 | return false; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | String[] subPackages = mPackageManager.getPackagesForUid(uid); |
| 621 | if(subPackages != null) { |
| 622 | for(String subPackage : subPackages) { |
| 623 | sendNotification(subPackage, a); |
| 624 | } |
| 625 | } |
| 626 | return true; |
| 627 | } |
| 628 | |
| 629 | @Override |
| 630 | public boolean removeAccountVisibility(Account a, int uid) { |
| 631 | int callingUid = Binder.getCallingUid(); |
| 632 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 633 | String msg = String.format( |
| 634 | "uid %s cannot get secrets for accounts of type: %s", |
| 635 | callingUid, |
| 636 | a.type); |
| 637 | throw new SecurityException(msg); |
| 638 | } |
| 639 | return removeAccountVisibility(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Removes visibility of certain account of a process identified |
| 644 | * by a given UID to an application. This is called directly by the |
| 645 | * AccountManager and indirectly by the Authenticator. |
| 646 | * |
| 647 | * @param a Account to remove visibility from |
| 648 | * @param uid UID to remove visibility of the Account from |
| 649 | * @param ua UserAccount that hosts the account and application |
| 650 | * |
| 651 | * @return True if application access to account removed and was previously visible. |
| 652 | */ |
| 653 | private boolean removeAccountVisibility(Account a, int uid, UserAccounts ua) { |
| 654 | int accountMapping = getMockAccountNumber(a, ua); |
| 655 | if(accountMapping < 0) { |
| 656 | return false; |
| 657 | } |
| 658 | synchronized(ua.cacheLock) { |
| 659 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 660 | ua.mVisibleListUidToMockAccountNumbers; |
| 661 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
| 662 | if(linkedAccountsToUid != null) { |
| 663 | boolean toReturn = linkedAccountsToUid.remove((Integer) accountMapping); |
| 664 | if(linkedAccountsToUid.size() == 0) { |
| 665 | userWlUidToMockAccountNums.remove(uid); |
| 666 | } |
| 667 | return toReturn; |
| 668 | } |
| 669 | } |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Registers an application's preferences for supported account types for login. This is |
| 675 | * a helper method of requestAccountVisibility and indirectly called by AccountManager. |
| 676 | * |
| 677 | * @param accountTypes account types third party app is willing to support |
| 678 | * @param uid of application requesting account visibility |
| 679 | * @param ua UserAccount that hosts the account and application |
| 680 | */ |
| 681 | private void addRequestedAccountsVisibility(String[] accountTypes, int uid, UserAccounts ua) { |
| 682 | synchronized(ua.cacheLock) { |
| 683 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 684 | ua.mApplicationAccountRequestMappings; |
| 685 | for(String accountType : accountTypes) { |
| 686 | ArrayList<Integer> accountUidAppList = userApplicationAccountRequestMappings |
| 687 | .get(accountType); |
| 688 | if(accountUidAppList == null) { |
| 689 | accountUidAppList = new ArrayList<>(); |
| 690 | accountUidAppList.add(uid); |
| 691 | userApplicationAccountRequestMappings.put(accountType, accountUidAppList); |
| 692 | } else if (!accountUidAppList.contains(uid)) { |
| 693 | accountUidAppList.add(uid); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Registers the requested login account types requested by all the applications already |
| 701 | * installed on the device. |
| 702 | */ |
| 703 | private void addRequestsForPreInstalledApplications() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 704 | List<PackageInfo> allInstalledPackages = mPackageManager.getInstalledPackages(0); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 705 | for(PackageInfo pi : allInstalledPackages) { |
| 706 | int currentUid = pi.applicationInfo.uid; |
| 707 | if(currentUid != -1) { |
| 708 | registerAccountTypesSupported(currentUid, |
| 709 | getUserAccounts(UserHandle.getUserId(currentUid))); |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Clears all preferences an application had for login account types it offered |
| 716 | * support for. This method is used by AccountManager after application is |
| 717 | * uninstalled. |
| 718 | * |
| 719 | * @param uid Uid of the application to clear account type preferences |
| 720 | * @param ua UserAccount that hosted the account and application |
| 721 | * |
| 722 | * @return true if any previous settings were overridden. |
| 723 | */ |
| 724 | private boolean clearRequestedAccountVisibility(int uid, UserAccounts ua) { |
| 725 | boolean accountsDeleted = false; |
| 726 | ArrayList<String> accountTypesToRemove = new ArrayList<>(); |
| 727 | synchronized(ua.cacheLock) { |
| 728 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 729 | ua.mApplicationAccountRequestMappings; |
| 730 | Set<Entry<String, ArrayList<Integer>>> accountTypeAppListEntries = |
| 731 | userApplicationAccountRequestMappings.entrySet(); |
| 732 | |
| 733 | for(Entry<String, ArrayList<Integer>> entry : accountTypeAppListEntries) { |
| 734 | ArrayList<Integer> supportedApps = entry.getValue(); |
| 735 | if(supportedApps.remove((Integer) uid)) { |
| 736 | accountsDeleted = true; |
| 737 | } |
| 738 | |
| 739 | if(supportedApps.isEmpty()) { |
| 740 | accountTypesToRemove.add(entry.getKey()); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | for(String s : accountTypesToRemove) { |
| 745 | userApplicationAccountRequestMappings.remove(s); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | return accountsDeleted; |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * Retrieves the mock account number associated with an Account in order to later retrieve |
| 754 | * the account from the Integer-Account Mapping. An account number is not the same as |
| 755 | * accountId in the database. This method can be indirectly called by AccountManager and |
| 756 | * indirectly by the Authenticator. |
| 757 | * |
| 758 | * @param a account to retrieve account number mapping |
| 759 | * @param ua UserAccount that currently hosts the account and application |
| 760 | * |
| 761 | * @return account number affiliated with the Account in question. Negative number if none. |
| 762 | */ |
| 763 | private int getMockAccountNumber(Account a, UserAccounts ua) { |
| 764 | //TODO: Each account is linked to AccountId rather than generated mock account numbers |
| 765 | SparseArray<Account> userAcctIdToAcctMap = |
| 766 | ua.mMockAccountIdToAccount; |
| 767 | synchronized(ua.cacheLock) { |
| 768 | int indexOfAccount = userAcctIdToAcctMap.indexOfValueByValue(a); |
| 769 | if(indexOfAccount < 0) { |
| 770 | return -1; |
| 771 | } |
| 772 | return userAcctIdToAcctMap.keyAt(indexOfAccount); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Returns a full list of accounts that a certain UID is allowed access |
| 778 | * based on the visible list entries. |
| 779 | * |
| 780 | * @param uid of application to retrieve visible listed accounts for |
| 781 | * @param ua UserAccount that currently hosts the account and application |
| 782 | * |
| 783 | * @return array of Account values that are accessible by the given uids |
| 784 | */ |
| 785 | private Account[] getVisibleListedAccounts(int uid, UserAccounts ua) { |
| 786 | ArrayList<Account> visibleListedAccounts = new ArrayList<>(); |
| 787 | synchronized(ua.cacheLock) { |
| 788 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 789 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 790 | ua.mVisibleListUidToMockAccountNumbers; |
| 791 | ArrayList<Integer> visibleListedUidAccountNumbers = |
| 792 | userWlUidToMockAccountNums.get(uid); |
| 793 | if(visibleListedUidAccountNumbers != null) { |
| 794 | for(Integer accountNumber : visibleListedUidAccountNumbers) { |
| 795 | Account currentAccount = userAcctIdToAcctMap.get(accountNumber); |
| 796 | visibleListedAccounts.add(currentAccount); |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | Account[] arrVisibleListedAccounts = new Account[visibleListedAccounts.size()]; |
| 801 | return visibleListedAccounts.toArray(arrVisibleListedAccounts); |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Makes an account number for a given Account to be mapped to. |
| 806 | * This method is called by makeVisible if an Account does not have |
| 807 | * a mapping for the visible list. This method is thus indirectly |
| 808 | * called by the Authenticator. |
| 809 | * |
| 810 | * @param a account to make an account number mapping of |
| 811 | * @param ua UserAccount that currently hosts the account and application |
| 812 | * |
| 813 | * @return account number created to map to the given account |
| 814 | */ |
| 815 | // TODO: Remove this method and use accountId from DB. |
| 816 | private int makeAccountNumber(Account a, UserAccounts ua) { |
| 817 | synchronized(ua.cacheLock) { |
| 818 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 819 | int newAccountMapping = 0; |
| 820 | while(userAcctIdToAcctMap.get(newAccountMapping) != null) { |
| 821 | newAccountMapping++; |
| 822 | } |
| 823 | userAcctIdToAcctMap.put(newAccountMapping, a); |
| 824 | return newAccountMapping; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | |
| 829 | |
| 830 | /** |
| 831 | * Registers an application, represented by a UID, to support account types detailed in |
| 832 | * the applications manifest as well as allowing it to opt for notifications. |
| 833 | * |
| 834 | * @param uid UID of application |
| 835 | * @param ua UserAccount that currently hosts the account and application |
| 836 | */ |
| 837 | private void registerAccountTypesSupported(int uid, UserAccounts ua) { |
| 838 | /* Account types supported are drawn from the Android Manifest of the Application */ |
| 839 | String interestedPackages = null; |
| 840 | try { |
| 841 | String[] allPackages = mPackageManager.getPackagesForUid(uid); |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 842 | if (allPackages != null) { |
| 843 | for(String aPackage : allPackages) { |
| 844 | ApplicationInfo ai = mPackageManager.getApplicationInfo(aPackage, |
| 845 | PackageManager.GET_META_DATA); |
| 846 | Bundle b = ai.metaData; |
| 847 | if(b == null) { |
| 848 | return; |
| 849 | } |
| 850 | interestedPackages = b.getString("android.accounts.SupportedLoginTypes"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 851 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 852 | } |
| 853 | } catch (PackageManager.NameNotFoundException e) { |
| 854 | Log.d("NameNotFoundException", e.getMessage()); |
| 855 | } |
| 856 | if(interestedPackages != null) { |
| 857 | /* request remote account types directly from here. Reads from Android Manifest */ |
| 858 | requestAccountVisibility(interestedPackages.split(";"), uid, ua); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /** |
| 863 | * Allows AccountManager to register account types that an application has login |
| 864 | * support for. This method over-writes all of the application's previous settings |
| 865 | * for accounts it supported. |
| 866 | * |
| 867 | * @param accountTypes array of account types application wishes to support |
| 868 | * @param uid of application registering requested account types |
| 869 | * @param ua UserAccount that hosts the account and application |
| 870 | */ |
| 871 | private void requestAccountVisibility(String[] accountTypes, int uid, UserAccounts ua) { |
| 872 | if(accountTypes.length > 0) { |
| 873 | clearRequestedAccountVisibility(uid, ua); |
| 874 | addRequestedAccountsVisibility(accountTypes, uid, ua); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Removes visibility of all Accounts to this particular UID. This is called when an |
| 880 | * application is uninstalled so another application that is installed with the same |
| 881 | * UID cannot access Accounts. This is called by AccountManager. |
| 882 | * |
| 883 | * @param uid of application to remove all Account visibility to |
| 884 | * @param ua UserAccount that hosts the current Account |
| 885 | */ |
| 886 | private void removeAccountVisibilityAllAccounts(int uid, UserAccounts ua) { |
| 887 | synchronized(ua.cacheLock) { |
| 888 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 889 | ua.mVisibleListUidToMockAccountNumbers; |
| 890 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 891 | ArrayList<Integer> allAccountNumbersList = userWlUidToMockAccountNums.get(uid); |
| 892 | if(allAccountNumbersList != null) { |
| 893 | Integer[] allAccountNumbers = allAccountNumbersList.toArray( |
| 894 | new Integer[allAccountNumbersList.size()]); |
| 895 | for(int accountNum : allAccountNumbers) { |
| 896 | removeAccountVisibility(userAcctIdToAcctMap.get(accountNum), uid, ua); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * Removes visible list functionality of a certain Account. |
| 904 | * This method is currently called by (1) addAccountExplicitly (as opposed to |
| 905 | * addAccountExplicitlyWithUid) and (2) removeAccountExplicitly. |
| 906 | * |
| 907 | * @param a the account to clear the visible list functionality for |
| 908 | * @param ua currently UserAccounts profile containing Account |
| 909 | * |
| 910 | * @return true if account previously had visible list functionality |
| 911 | */ |
| 912 | private boolean removeVisibleListFunctionality(Account a, UserAccounts ua) { |
| 913 | int mockAccountNum = getMockAccountNumber(a, ua); |
| 914 | if(mockAccountNum < 0) { |
| 915 | return false; |
| 916 | } |
| 917 | synchronized(ua.cacheLock) { |
| 918 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 919 | ua.mVisibleListUidToMockAccountNumbers; |
| 920 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 921 | |
| 922 | /* Removing mapping from account number to account removes visible list functionality*/ |
| 923 | userAcctIdToAcctMap.remove(mockAccountNum); |
| 924 | |
| 925 | for(int i = userWlUidToMockAccountNums.size() - 1 ; i >= 0 ; i--) { |
| 926 | int uidKey = userWlUidToMockAccountNums.keyAt(i); |
| 927 | ArrayList<Integer> allAccountNumbers = userWlUidToMockAccountNums.get(uidKey); |
| 928 | if(allAccountNumbers != null) { |
| 929 | allAccountNumbers.remove(mockAccountNum); |
| 930 | if(allAccountNumbers.isEmpty()) { |
| 931 | userWlUidToMockAccountNums.remove(uidKey); |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | return true; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Sends a direct intent to a package, notifying it of a visible account. This |
| 941 | * method is a helper method of makeAccountVisible. |
| 942 | * |
| 943 | * @param desiredPackage to send Account to |
| 944 | * @param visibleAccount to send to package |
| 945 | */ |
| 946 | private void sendNotification(String desiredPackage, Account visibleAccount) { |
| 947 | Intent intent = new Intent(); |
| 948 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 949 | intent.setAction(NEW_ACCOUNT_VISIBLE); |
| 950 | intent.setPackage(desiredPackage); |
| 951 | intent.putExtra("android.accounts.KEY_ACCOUNT", (Account) visibleAccount); |
| 952 | mContext.sendBroadcast(intent); |
| 953 | } |
| 954 | |
| 955 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 956 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 957 | throws RemoteException { |
| 958 | try { |
| 959 | return super.onTransact(code, data, reply, flags); |
| 960 | } catch (RuntimeException e) { |
| 961 | // The account manager only throws security exceptions, so let's |
| 962 | // log all others. |
| 963 | if (!(e instanceof SecurityException)) { |
| 964 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 965 | } |
| 966 | throw e; |
| 967 | } |
| 968 | } |
| 969 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 970 | private UserManager getUserManager() { |
| 971 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 972 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 973 | } |
| 974 | return mUserManager; |
| 975 | } |
| 976 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 977 | /** |
| 978 | * Validate internal set of accounts against installed authenticators for |
| 979 | * given user. Clears cached authenticators before validating. |
| 980 | */ |
| 981 | public void validateAccounts(int userId) { |
| 982 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 983 | // Invalidate user-specific cache to make sure we catch any |
| 984 | // removed authenticators. |
| 985 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * Validate internal set of accounts against installed authenticators for |
| 990 | * given user. Clear cached authenticators before validating when requested. |
| 991 | */ |
| 992 | private void validateAccountsInternal( |
| 993 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 994 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 995 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
| 996 | + " isCeDatabaseAttached=" + accounts.openHelper.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 997 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 998 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 999 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1000 | if (invalidateAuthenticatorCache) { |
| 1001 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 1002 | } |
| 1003 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1004 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 1005 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1006 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1007 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1008 | synchronized (accounts.cacheLock) { |
| 1009 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1010 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1011 | |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1012 | // Get a map of stored authenticator types to UID |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1013 | Map<String, Integer> metaAuthUid = DeDatabaseHelper.findMetaAuthUid(db); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1014 | // Create a list of authenticator type whose previous uid no longer exists |
| 1015 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1016 | SparseBooleanArray knownUids = null; |
| 1017 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 1018 | String type = authToUidEntry.getKey(); |
| 1019 | int uid = authToUidEntry.getValue(); |
| 1020 | Integer knownUid = knownAuth.get(type); |
| 1021 | if (knownUid != null && uid == knownUid) { |
| 1022 | // Remove it from the knownAuth list if it's unchanged. |
| 1023 | knownAuth.remove(type); |
| 1024 | } else { |
| 1025 | /* |
| 1026 | * The authenticator is presently not cached and should only be triggered |
| 1027 | * when we think an authenticator has been removed (or is being updated). |
| 1028 | * But we still want to check if any data with the associated uid is |
| 1029 | * around. This is an (imperfect) signal that the package may be updating. |
| 1030 | * |
| 1031 | * A side effect of this is that an authenticator sharing a uid with |
| 1032 | * multiple apps won't get its credentials wiped as long as some app with |
| 1033 | * that uid is still on the device. But I suspect that this is a rare case. |
| 1034 | * And it isn't clear to me how an attacker could really exploit that |
| 1035 | * feature. |
| 1036 | * |
| 1037 | * The upshot is that we don't have to worry about accounts getting |
| 1038 | * uninstalled while the authenticator's package is being updated. |
| 1039 | * |
| 1040 | */ |
| 1041 | if (knownUids == null) { |
| 1042 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1043 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1044 | if (!knownUids.get(uid)) { |
| 1045 | // The authenticator is not presently available to the cache. And the |
| 1046 | // package no longer has a data directory (so we surmise it isn't updating). |
| 1047 | // So purge its data from the account databases. |
| 1048 | obsoleteAuthType.add(type); |
| 1049 | // And delete it from the TABLE_META |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1050 | DeDatabaseHelper.deleteMetaByAuthTypeAndUid(db, type, uid); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1051 | } |
| 1052 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1055 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1056 | // been re-enabled (after being updated for example), then we just overwrite the old |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1057 | // values. |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1058 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1059 | DeDatabaseHelper.insertOrReplaceMetaAuthTypeAndUid(db, entry.getKey(), |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1060 | entry.getValue()); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1063 | final Map<Long, Account> accountsMap = DeDatabaseHelper.findAllAccounts(db); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1064 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1065 | accounts.accountCache.clear(); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1066 | final HashMap<String, ArrayList<String>> accountNamesByType = new LinkedHashMap<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1067 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1068 | final long accountId = accountEntry.getKey(); |
| 1069 | final Account account = accountEntry.getValue(); |
| 1070 | if (obsoleteAuthType.contains(account.type)) { |
| 1071 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 1072 | + account.type + "'s registered authenticator no longer exist."); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1073 | db.beginTransaction(); |
| 1074 | try { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1075 | DeDatabaseHelper.deleteAccount(db, accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1076 | // Also delete from CE table if user is unlocked; if user is currently |
| 1077 | // locked the account will be removed later by syncDeCeAccountsLocked |
| 1078 | if (userUnlocked) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1079 | AccountsDb.deleteCeAccount(db, accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1080 | } |
| 1081 | db.setTransactionSuccessful(); |
| 1082 | } finally { |
| 1083 | db.endTransaction(); |
| 1084 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1085 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1086 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1087 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1088 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1089 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1090 | accounts.userDataCache.remove(account); |
| 1091 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1092 | accounts.accountTokenCaches.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1093 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1094 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1095 | if (accountNames == null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1096 | accountNames = new ArrayList<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1097 | accountNamesByType.put(account.type, accountNames); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1098 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1099 | accountNames.add(account.name); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1100 | } |
| 1101 | } |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1102 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1103 | final String accountType = cur.getKey(); |
| 1104 | final ArrayList<String> accountNames = cur.getValue(); |
| 1105 | final Account[] accountsForType = new Account[accountNames.size()]; |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1106 | for (int i = 0; i < accountsForType.length; i++) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1107 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1108 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1109 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1110 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1111 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1112 | } finally { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1113 | if (accountDeleted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1114 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1115 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1116 | } |
| 1117 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1120 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1121 | // Get the UIDs of all apps that might have data on the device. We want |
| 1122 | // to preserve user data if the app might otherwise be storing data. |
| 1123 | List<PackageInfo> pkgsWithData = |
| 1124 | mPackageManager.getInstalledPackagesAsUser( |
| 1125 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1126 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1127 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1128 | if (pkgInfo.applicationInfo != null |
| 1129 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1130 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1131 | } |
| 1132 | } |
| 1133 | return knownUids; |
| 1134 | } |
| 1135 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1136 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1137 | Context context, |
| 1138 | int userId) { |
| 1139 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1140 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1141 | } |
| 1142 | |
| 1143 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1144 | IAccountAuthenticatorCache authCache, |
| 1145 | int userId) { |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1146 | HashMap<String, Integer> knownAuth = new HashMap<>(); |
| 1147 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1148 | .getAllServices(userId)) { |
| 1149 | knownAuth.put(service.type.type, service.uid); |
| 1150 | } |
| 1151 | return knownAuth; |
| 1152 | } |
| 1153 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1154 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1155 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | protected UserAccounts getUserAccounts(int userId) { |
| 1159 | synchronized (mUsers) { |
| 1160 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1161 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1162 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 1163 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1164 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1165 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1166 | initializeDebugDbSizeAndCompileSqlStatementForLogging( |
| 1167 | accounts.openHelper.getWritableDatabase(), accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1168 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1169 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1170 | validateAccounts = true; |
| 1171 | } |
| 1172 | // open CE database if necessary |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1173 | if (!accounts.openHelper.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1174 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
| 1175 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 1176 | File preNDatabaseFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1177 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1178 | CeDatabaseHelper.create(mContext, userId, preNDatabaseFile, ceDatabaseFile); |
| 1179 | accounts.openHelper.attachCeDatabase(ceDatabaseFile); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1180 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1181 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1182 | } |
| 1183 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1184 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1185 | } |
| 1186 | return accounts; |
| 1187 | } |
| 1188 | } |
| 1189 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1190 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1191 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
| 1192 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabaseUserIsUnlocked(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1193 | List<Account> accountsToRemove = AccountsDb.findCeAccountsNotInDe(db); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1194 | if (!accountsToRemove.isEmpty()) { |
| 1195 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1196 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1197 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1198 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1199 | |
| 1200 | for (Account account : accountsToRemove) { |
| 1201 | removeAccountInternal(accounts, account, Process.myUid()); |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1206 | private void purgeOldGrantsAll() { |
| 1207 | synchronized (mUsers) { |
| 1208 | for (int i = 0; i < mUsers.size(); i++) { |
| 1209 | purgeOldGrants(mUsers.valueAt(i)); |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | private void purgeOldGrants(UserAccounts accounts) { |
| 1215 | synchronized (accounts.cacheLock) { |
| 1216 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1217 | List<Integer> uids = DeDatabaseHelper.findAllUidGrants(db); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1218 | for (int uid : uids) { |
| 1219 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1220 | if (packageExists) { |
| 1221 | continue; |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1222 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1223 | Log.d(TAG, "deleting grants for UID " + uid |
| 1224 | + " because its package is no longer installed"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1225 | DeDatabaseHelper.deleteGrantsByUid(db, uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1230 | private void onUserRemoved(Intent intent) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1231 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1232 | if (userId < 1) return; |
| 1233 | |
| 1234 | UserAccounts accounts; |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1235 | boolean userUnlocked; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1236 | synchronized (mUsers) { |
| 1237 | accounts = mUsers.get(userId); |
| 1238 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1239 | userUnlocked = mLocalUnlockedUsers.get(userId); |
| 1240 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1241 | } |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1242 | if (accounts != null) { |
| 1243 | synchronized (accounts.cacheLock) { |
| 1244 | accounts.openHelper.close(); |
| 1245 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1246 | } |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1247 | Log.i(TAG, "Removing database files for user " + userId); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 1248 | File dbFile = new File(mInjector.getDeDatabaseName(userId)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1249 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1250 | AccountsDb.deleteDbFileWarnIfFailed(dbFile); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1251 | // Remove CE file if user is unlocked, or FBE is not enabled |
| 1252 | boolean fbeEnabled = StorageManager.isFileEncryptedNativeOrEmulated(); |
| 1253 | if (!fbeEnabled || userUnlocked) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 1254 | File ceDb = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1255 | if (ceDb.exists()) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1256 | AccountsDb.deleteDbFileWarnIfFailed(ceDb); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1261 | @VisibleForTesting |
| 1262 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1263 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1264 | } |
| 1265 | |
| 1266 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1267 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1268 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1269 | } |
| 1270 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1271 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1272 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1273 | if (userId < 1) return; |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1274 | syncSharedAccounts(userId); |
| 1275 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1276 | |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1277 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1278 | // Check if there's a shared account that needs to be created as an account |
| 1279 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1280 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1281 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1282 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1283 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1284 | : UserHandle.USER_SYSTEM; |
| 1285 | if (parentUserId < 0) { |
| 1286 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1287 | return; |
| 1288 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1289 | for (Account sa : sharedAccounts) { |
| 1290 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1291 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1292 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1296 | @Override |
| 1297 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1298 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1301 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1302 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1303 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1304 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1305 | Log.v(TAG, "getPassword: " + account |
| 1306 | + ", caller's uid " + Binder.getCallingUid() |
| 1307 | + ", pid " + Binder.getCallingPid()); |
| 1308 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1309 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1310 | int userId = UserHandle.getCallingUserId(); |
| 1311 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1312 | String msg = String.format( |
| 1313 | "uid %s cannot get secrets for accounts of type: %s", |
| 1314 | callingUid, |
| 1315 | account.type); |
| 1316 | throw new SecurityException(msg); |
| 1317 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1318 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1319 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1320 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1321 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1322 | } finally { |
| 1323 | restoreCallingIdentity(identityToken); |
| 1324 | } |
| 1325 | } |
| 1326 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1327 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1328 | if (account == null) { |
| 1329 | return null; |
| 1330 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1331 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1332 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1333 | return null; |
| 1334 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1335 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1336 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1337 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabaseUserIsUnlocked(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1338 | return CeDatabaseHelper.findAccountPasswordByNameAndType(db, account.name, |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1339 | account.type); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1343 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1344 | public String getPreviousName(Account account) { |
| 1345 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1346 | Log.v(TAG, "getPreviousName: " + account |
| 1347 | + ", caller's uid " + Binder.getCallingUid() |
| 1348 | + ", pid " + Binder.getCallingPid()); |
| 1349 | } |
| 1350 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1351 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1352 | long identityToken = clearCallingIdentity(); |
| 1353 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1354 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1355 | return readPreviousNameInternal(accounts, account); |
| 1356 | } finally { |
| 1357 | restoreCallingIdentity(identityToken); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1362 | if (account == null) { |
| 1363 | return null; |
| 1364 | } |
| 1365 | synchronized (accounts.cacheLock) { |
| 1366 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1367 | if (previousNameRef == null) { |
| 1368 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1369 | String previousName = DeDatabaseHelper.findAccountPreviousName(db, account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1370 | previousNameRef = new AtomicReference<>(previousName); |
| 1371 | accounts.previousNameCache.put(account, previousNameRef); |
| 1372 | return previousName; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1373 | } else { |
| 1374 | return previousNameRef.get(); |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1380 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1381 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1382 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1383 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1384 | account, key, callingUid, Binder.getCallingPid()); |
| 1385 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1386 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1387 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1388 | if (key == null) throw new IllegalArgumentException("key is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1389 | int userId = UserHandle.getCallingUserId(); |
| 1390 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1391 | String msg = String.format( |
| 1392 | "uid %s cannot get user data for accounts of type: %s", |
| 1393 | callingUid, |
| 1394 | account.type); |
| 1395 | throw new SecurityException(msg); |
| 1396 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1397 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1398 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1399 | return null; |
| 1400 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1401 | long identityToken = clearCallingIdentity(); |
| 1402 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1403 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1404 | synchronized (accounts.cacheLock) { |
| 1405 | if (!accountExistsCacheLocked(accounts, account)) { |
| 1406 | return null; |
| 1407 | } |
| 1408 | return readUserDataInternalLocked(accounts, account, key); |
| 1409 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1410 | } finally { |
| 1411 | restoreCallingIdentity(identityToken); |
| 1412 | } |
| 1413 | } |
| 1414 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1415 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1416 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1417 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1418 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1419 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1420 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1421 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1422 | + ", pid " + Binder.getCallingPid()); |
| 1423 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1424 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1425 | if (isCrossUser(callingUid, userId)) { |
| 1426 | throw new SecurityException( |
| 1427 | String.format( |
| 1428 | "User %s tying to get authenticator types for %s" , |
| 1429 | UserHandle.getCallingUserId(), |
| 1430 | userId)); |
| 1431 | } |
| 1432 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1433 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1434 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1435 | return getAuthenticatorTypesInternal(userId); |
| 1436 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1437 | } finally { |
| 1438 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1439 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1442 | /** |
| 1443 | * Should only be called inside of a clearCallingIdentity block. |
| 1444 | */ |
| 1445 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1446 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1447 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1448 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1449 | AuthenticatorDescription[] types = |
| 1450 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1451 | int i = 0; |
| 1452 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1453 | : authenticatorCollection) { |
| 1454 | types[i] = authenticator.type; |
| 1455 | i++; |
| 1456 | } |
| 1457 | return types; |
| 1458 | } |
| 1459 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1460 | private boolean isCrossUser(int callingUid, int userId) { |
| 1461 | return (userId != UserHandle.getCallingUserId() |
| 1462 | && callingUid != Process.myUid() |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1463 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1464 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1465 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1468 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1469 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1470 | Bundle.setDefusable(extras, true); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1471 | // clears the visible list functionality for this account because this method allows |
| 1472 | // default account access to all applications for account. |
| 1473 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1474 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1475 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1476 | Log.v(TAG, "addAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1477 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1478 | + ", pid " + Binder.getCallingPid()); |
| 1479 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1480 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1481 | int userId = UserHandle.getCallingUserId(); |
| 1482 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1483 | String msg = String.format( |
| 1484 | "uid %s cannot explicitly add accounts of type: %s", |
| 1485 | callingUid, |
| 1486 | account.type); |
| 1487 | throw new SecurityException(msg); |
| 1488 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1489 | removeVisibleListFunctionality(account, getUserAccounts(UserHandle.getUserId(callingUid))); |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1490 | /* |
| 1491 | * Child users are not allowed to add accounts. Only the accounts that are |
| 1492 | * shared by the parent profile can be added to child profile. |
| 1493 | * |
| 1494 | * TODO: Only allow accounts that were shared to be added by |
| 1495 | * a limited user. |
| 1496 | */ |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1497 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1498 | // fails if the account already exists |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1499 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1500 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1501 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1502 | return addAccountInternal(accounts, account, password, extras, callingUid); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1503 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1504 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1508 | @Override |
| 1509 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1510 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1511 | int callingUid = Binder.getCallingUid(); |
| 1512 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1513 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1514 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1515 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1516 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1517 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1518 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1519 | if (response != null) { |
| 1520 | Bundle result = new Bundle(); |
| 1521 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1522 | try { |
| 1523 | response.onResult(result); |
| 1524 | } catch (RemoteException e) { |
| 1525 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1526 | } |
| 1527 | } |
| 1528 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1531 | Slog.d(TAG, "Copying account " + account.name |
| 1532 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1533 | long identityToken = clearCallingIdentity(); |
| 1534 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1535 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1536 | false /* stripAuthTokenFromResult */, account.name, |
| 1537 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1538 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1539 | protected String toDebugString(long now) { |
| 1540 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1541 | + ", " + account.type; |
| 1542 | } |
| 1543 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1544 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1545 | public void run() throws RemoteException { |
| 1546 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1547 | } |
| 1548 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1549 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1550 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1551 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1552 | if (result != null |
| 1553 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1554 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1555 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1556 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1557 | super.onResult(result); |
| 1558 | } |
| 1559 | } |
| 1560 | }.bind(); |
| 1561 | } finally { |
| 1562 | restoreCallingIdentity(identityToken); |
| 1563 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1564 | } |
| 1565 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1566 | @Override |
| 1567 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1568 | final int callingUid = Binder.getCallingUid(); |
| 1569 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1570 | String msg = String.format( |
| 1571 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1572 | account, |
| 1573 | callingUid); |
| 1574 | Log.v(TAG, msg); |
| 1575 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1576 | if (account == null) { |
| 1577 | throw new IllegalArgumentException("account is null"); |
| 1578 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1579 | int userId = UserHandle.getCallingUserId(); |
| 1580 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1581 | String msg = String.format( |
| 1582 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1583 | callingUid, |
| 1584 | account.type); |
| 1585 | throw new SecurityException(msg); |
| 1586 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1587 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1588 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1589 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1590 | return false; |
| 1591 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1592 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1593 | long identityToken = clearCallingIdentity(); |
| 1594 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1595 | UserAccounts accounts = getUserAccounts(userId); |
| 1596 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1597 | } finally { |
| 1598 | restoreCallingIdentity(identityToken); |
| 1599 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1603 | final UserAccounts accounts = getUserAccountsForCaller(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1604 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1605 | return DeDatabaseHelper.updateAccountLastAuthenticatedTime( |
| 1606 | accounts.openHelper.getWritableDatabase(), account); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1607 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1610 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1611 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1612 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1613 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1614 | long id = clearCallingIdentity(); |
| 1615 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1616 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1617 | false /* stripAuthTokenFromResult */, account.name, |
| 1618 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1619 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1620 | protected String toDebugString(long now) { |
| 1621 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1622 | + ", " + account.type; |
| 1623 | } |
| 1624 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1625 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1626 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1627 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1628 | UserAccounts owner = getUserAccounts(parentUserId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1629 | synchronized (owner.cacheLock) { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1630 | for (Account acc : getAccounts(parentUserId, |
| 1631 | mContext.getOpPackageName())) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1632 | if (acc.equals(account)) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1633 | mAuthenticator.addAccountFromCredentials( |
| 1634 | this, account, accountCredentials); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1639 | } |
| 1640 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1641 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1642 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1643 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1644 | // TODO: Anything to do if if succedded? |
| 1645 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1646 | // account to avoid retries? |
| 1647 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1648 | } |
| 1649 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1650 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1651 | public void onError(int errorCode, String errorMessage) { |
| 1652 | super.onError(errorCode, errorMessage); |
| 1653 | // TODO: Show error notification to user |
| 1654 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1655 | } |
| 1656 | |
| 1657 | }.bind(); |
| 1658 | } finally { |
| 1659 | restoreCallingIdentity(id); |
| 1660 | } |
| 1661 | } |
| 1662 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1663 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1664 | Bundle extras, int callingUid) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1665 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1666 | if (account == null) { |
| 1667 | return false; |
| 1668 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1669 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1670 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1671 | + " is locked. callingUid=" + callingUid); |
| 1672 | return false; |
| 1673 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1674 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1675 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1676 | db.beginTransaction(); |
| 1677 | try { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1678 | if (CeDatabaseHelper.findAccountId(db, account) >= 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1679 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1680 | + ", skipping since the account already exists"); |
| 1681 | return false; |
| 1682 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1683 | long accountId = CeDatabaseHelper.insertAccount(db, account, password); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1684 | if (accountId < 0) { |
| 1685 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1686 | + ", skipping the DB insert failed"); |
| 1687 | return false; |
| 1688 | } |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1689 | // Insert into DE table |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1690 | if (DeDatabaseHelper.insertAccount(db, account, accountId) < 0) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1691 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1692 | + ", skipping the DB insert failed"); |
| 1693 | return false; |
| 1694 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1695 | if (extras != null) { |
| 1696 | for (String key : extras.keySet()) { |
| 1697 | final String value = extras.getString(key); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1698 | if (CeDatabaseHelper.insertExtra(db, accountId, key, value) < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1699 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1700 | + ", skipping since insertExtra failed for key " + key); |
| 1701 | return false; |
| 1702 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1703 | } |
| 1704 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1705 | db.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1706 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1707 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 1708 | accountId, accounts, callingUid); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1709 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1710 | insertAccountIntoCacheLocked(accounts, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1711 | } finally { |
| 1712 | db.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1713 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1714 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1715 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1716 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1717 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1718 | |
| 1719 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1720 | sendAccountsChangedBroadcast(accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1721 | return true; |
| 1722 | } |
| 1723 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1724 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1725 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1726 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1727 | } |
| 1728 | } |
| 1729 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1730 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1731 | * 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] | 1732 | * running, then clone the account too. |
| 1733 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1734 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1735 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1736 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1737 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1738 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1739 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1740 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1741 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1742 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1743 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1744 | } |
| 1745 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1746 | } |
| 1747 | } |
| 1748 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1749 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1750 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1751 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1752 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1753 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1754 | Log.v(TAG, "hasFeatures: " + account |
| 1755 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1756 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1757 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1758 | + ", pid " + Binder.getCallingPid()); |
| 1759 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1760 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1761 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1762 | if (features == null) throw new IllegalArgumentException("features is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1763 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1764 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1765 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1766 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1767 | long identityToken = clearCallingIdentity(); |
| 1768 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1769 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1770 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1771 | } finally { |
| 1772 | restoreCallingIdentity(identityToken); |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | private class TestFeaturesSession extends Session { |
| 1777 | private final String[] mFeatures; |
| 1778 | private final Account mAccount; |
| 1779 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1780 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1781 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1782 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1783 | true /* stripAuthTokenFromResult */, account.name, |
| 1784 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1785 | mFeatures = features; |
| 1786 | mAccount = account; |
| 1787 | } |
| 1788 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1789 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1790 | public void run() throws RemoteException { |
| 1791 | try { |
| 1792 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1793 | } catch (RemoteException e) { |
| 1794 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1795 | } |
| 1796 | } |
| 1797 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1798 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1799 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1800 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1801 | IAccountManagerResponse response = getResponseAndClose(); |
| 1802 | if (response != null) { |
| 1803 | try { |
| 1804 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1805 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1806 | return; |
| 1807 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1808 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1809 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1810 | + response); |
| 1811 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1812 | final Bundle newResult = new Bundle(); |
| 1813 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1814 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1815 | response.onResult(newResult); |
| 1816 | } catch (RemoteException e) { |
| 1817 | // if the caller is dead then there is no one to care about remote exceptions |
| 1818 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1819 | Log.v(TAG, "failure while notifying response", e); |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1825 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1826 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1827 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1828 | + ", " + mAccount |
| 1829 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1830 | } |
| 1831 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1832 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1833 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1834 | public void renameAccount( |
| 1835 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1836 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1837 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1838 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1839 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1840 | + ", pid " + Binder.getCallingPid()); |
| 1841 | } |
| 1842 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1843 | int userId = UserHandle.getCallingUserId(); |
| 1844 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1845 | String msg = String.format( |
| 1846 | "uid %s cannot rename accounts of type: %s", |
| 1847 | callingUid, |
| 1848 | accountToRename.type); |
| 1849 | throw new SecurityException(msg); |
| 1850 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1851 | long identityToken = clearCallingIdentity(); |
| 1852 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1853 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1854 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1855 | Bundle result = new Bundle(); |
| 1856 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1857 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1858 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1859 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1860 | try { |
| 1861 | response.onResult(result); |
| 1862 | } catch (RemoteException e) { |
| 1863 | Log.w(TAG, e.getMessage()); |
| 1864 | } |
| 1865 | } finally { |
| 1866 | restoreCallingIdentity(identityToken); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1871 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1872 | Account resultAccount = null; |
| 1873 | /* |
| 1874 | * Cancel existing notifications. Let authenticators |
| 1875 | * re-post notifications as required. But we don't know if |
| 1876 | * the authenticators have bound their notifications to |
| 1877 | * now stale account name data. |
| 1878 | * |
| 1879 | * With a rename api, we might not need to do this anymore but it |
| 1880 | * shouldn't hurt. |
| 1881 | */ |
| 1882 | cancelNotification( |
| 1883 | getSigninRequiredNotificationId(accounts, accountToRename), |
| 1884 | new UserHandle(accounts.userId)); |
| 1885 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1886 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1887 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1888 | if (accountToRename.equals(pair.first.first)) { |
| 1889 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
| 1890 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1895 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1896 | db.beginTransaction(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1897 | Account renamedAccount = new Account(newName, accountToRename.type); |
| 1898 | try { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1899 | final long accountId = DeDatabaseHelper.findAccountId(db, accountToRename); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1900 | if (accountId >= 0) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1901 | CeDatabaseHelper.renameAccount(db, accountId, newName); |
| 1902 | DeDatabaseHelper.renameAccount(db, accountId, newName, accountToRename.name); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1903 | } |
| 1904 | } finally { |
| 1905 | db.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1906 | } |
| 1907 | /* |
| 1908 | * Database transaction was successful. Clean up cached |
| 1909 | * data associated with the account in the user profile. |
| 1910 | */ |
Svet Ganov | 5c4a512 | 2016-09-23 19:29:11 -0700 | [diff] [blame] | 1911 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1912 | /* |
| 1913 | * Extract the data and token caches before removing the |
| 1914 | * old account to preserve the user data associated with |
| 1915 | * the account. |
| 1916 | */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1917 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1918 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1919 | removeAccountFromCacheLocked(accounts, accountToRename); |
| 1920 | /* |
| 1921 | * Update the cached data associated with the renamed |
| 1922 | * account. |
| 1923 | */ |
| 1924 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1925 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 1926 | accounts.previousNameCache.put( |
| 1927 | renamedAccount, |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1928 | new AtomicReference<>(accountToRename.name)); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1929 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1930 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1931 | int parentUserId = accounts.userId; |
| 1932 | if (canHaveProfile(parentUserId)) { |
| 1933 | /* |
| 1934 | * Owner or system user account was renamed, rename the account for |
| 1935 | * those users with which the account was shared. |
| 1936 | */ |
| 1937 | List<UserInfo> users = getUserManager().getUsers(true); |
| 1938 | for (UserInfo user : users) { |
| 1939 | if (user.isRestricted() |
| 1940 | && (user.restrictedProfileParentId == parentUserId)) { |
| 1941 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1942 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1943 | } |
| 1944 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1945 | sendAccountsChangedBroadcast(accounts.userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1946 | } |
| 1947 | return resultAccount; |
| 1948 | } |
| 1949 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1950 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1951 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1952 | return userInfo != null && userInfo.canHaveProfile(); |
| 1953 | } |
| 1954 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1955 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1956 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1957 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1958 | removeAccountAsUser( |
| 1959 | response, |
| 1960 | account, |
| 1961 | expectActivityLaunch, |
| 1962 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | @Override |
| 1966 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1967 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1968 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1969 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1970 | Log.v(TAG, "removeAccount: " + account |
| 1971 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1972 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1973 | + ", pid " + Binder.getCallingPid() |
| 1974 | + ", for user id " + userId); |
| 1975 | } |
| 1976 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1977 | if (account == null) throw new IllegalArgumentException("account is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1978 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1979 | if (isCrossUser(callingUid, userId)) { |
| 1980 | throw new SecurityException( |
| 1981 | String.format( |
| 1982 | "User %s tying remove account for %s" , |
| 1983 | UserHandle.getCallingUserId(), |
| 1984 | userId)); |
| 1985 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1986 | /* |
| 1987 | * Only the system or authenticator should be allowed to remove accounts for that |
| 1988 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 1989 | * arbitrary applications (like competing authenticators). |
| 1990 | */ |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1991 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 1992 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 1993 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1994 | String msg = String.format( |
| 1995 | "uid %s cannot remove accounts of type: %s", |
| 1996 | callingUid, |
| 1997 | account.type); |
| 1998 | throw new SecurityException(msg); |
| 1999 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2000 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2001 | try { |
| 2002 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2003 | "User cannot modify accounts"); |
| 2004 | } catch (RemoteException re) { |
| 2005 | } |
| 2006 | return; |
| 2007 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2008 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2009 | try { |
| 2010 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2011 | "User cannot modify accounts of this type (policy)."); |
| 2012 | } catch (RemoteException re) { |
| 2013 | } |
| 2014 | return; |
| 2015 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2016 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2017 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2018 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2019 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2020 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2021 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2022 | if (account.equals(pair.first.first)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2023 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2024 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | } |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2028 | SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2029 | final long accountId = DeDatabaseHelper.findAccountId(db, account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2030 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2031 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2032 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2033 | accountId, |
| 2034 | accounts, |
| 2035 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2036 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2037 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2038 | } finally { |
| 2039 | restoreCallingIdentity(identityToken); |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | @Override |
| 2044 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2045 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2046 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2047 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2048 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2049 | + ", pid " + Binder.getCallingPid()); |
| 2050 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2051 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2052 | if (account == null) { |
| 2053 | /* |
| 2054 | * Null accounts should result in returning false, as per |
| 2055 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2056 | */ |
| 2057 | Log.e(TAG, "account is null"); |
| 2058 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2059 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2060 | String msg = String.format( |
| 2061 | "uid %s cannot explicitly add accounts of type: %s", |
| 2062 | callingUid, |
| 2063 | account.type); |
| 2064 | throw new SecurityException(msg); |
| 2065 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 2066 | removeVisibleListFunctionality(account, getUserAccounts(UserHandle.getUserId(callingUid))); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2067 | UserAccounts accounts = getUserAccountsForCaller(); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2068 | SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2069 | final long accountId = DeDatabaseHelper.findAccountId(db, account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2070 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2071 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2072 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2073 | accountId, |
| 2074 | accounts, |
| 2075 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2076 | long identityToken = clearCallingIdentity(); |
| 2077 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2078 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2079 | } finally { |
| 2080 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2081 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2084 | private class RemoveAccountSession extends Session { |
| 2085 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2086 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2087 | Account account, boolean expectActivityLaunch) { |
| 2088 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2089 | true /* stripAuthTokenFromResult */, account.name, |
| 2090 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2091 | mAccount = account; |
| 2092 | } |
| 2093 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2094 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2095 | protected String toDebugString(long now) { |
| 2096 | return super.toDebugString(now) + ", removeAccount" |
| 2097 | + ", account " + mAccount; |
| 2098 | } |
| 2099 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2100 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2101 | public void run() throws RemoteException { |
| 2102 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2103 | } |
| 2104 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2105 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2106 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2107 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2108 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2109 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2110 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2111 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2112 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2113 | } |
| 2114 | IAccountManagerResponse response = getResponseAndClose(); |
| 2115 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2116 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2117 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2118 | + response); |
| 2119 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2120 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2121 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2122 | try { |
| 2123 | response.onResult(result2); |
| 2124 | } catch (RemoteException e) { |
| 2125 | // ignore |
| 2126 | } |
| 2127 | } |
| 2128 | } |
| 2129 | super.onResult(result); |
| 2130 | } |
| 2131 | } |
| 2132 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2133 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2134 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2135 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2136 | } |
| 2137 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2138 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2139 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2140 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2141 | if (!userUnlocked) { |
| 2142 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2143 | + " is still locked. CE data will be removed later"); |
| 2144 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2145 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2146 | final SQLiteDatabase db = userUnlocked |
| 2147 | ? accounts.openHelper.getWritableDatabaseUserIsUnlocked() |
| 2148 | : accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2149 | db.beginTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2150 | // Set to a dummy value, this will only be used if the database |
| 2151 | // transaction succeeds. |
| 2152 | long accountId = -1; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2153 | try { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2154 | accountId = DeDatabaseHelper.findAccountId(db, account); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2155 | if (accountId >= 0) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2156 | DeDatabaseHelper.deleteAccount(db, accountId); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2157 | if (userUnlocked) { |
| 2158 | // Delete from CE table |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2159 | AccountsDb.deleteCeAccount(db, accountId); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2160 | } |
| 2161 | db.setTransactionSuccessful(); |
| 2162 | isChanged = true; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2163 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2164 | } finally { |
| 2165 | db.endTransaction(); |
| 2166 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2167 | if (isChanged) { |
| 2168 | removeAccountFromCacheLocked(accounts, account); |
| 2169 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occured. |
| 2170 | sendAccountsChangedBroadcast(accounts.userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2171 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2172 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2173 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2174 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2175 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2176 | long id = Binder.clearCallingIdentity(); |
| 2177 | try { |
| 2178 | int parentUserId = accounts.userId; |
| 2179 | if (canHaveProfile(parentUserId)) { |
| 2180 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2181 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2182 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2183 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2184 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2185 | } |
| 2186 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2187 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2188 | } finally { |
| 2189 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2190 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2191 | |
| 2192 | if (isChanged) { |
| 2193 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2194 | for (Pair<Pair<Account, String>, Integer> key |
| 2195 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2196 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2197 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2198 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2199 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2200 | account, uid, false)); |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2206 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2207 | } |
| 2208 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2209 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2210 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2211 | int callerUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2212 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2213 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2214 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2215 | + ", pid " + Binder.getCallingPid()); |
| 2216 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2217 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 2218 | if (authToken == null) throw new IllegalArgumentException("authToken is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2219 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2220 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2221 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2222 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2223 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2224 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2225 | db.beginTransaction(); |
| 2226 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2227 | invalidateAuthTokenLocked(accounts, db, accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2228 | invalidateCustomTokenLocked(accounts, accountType, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2229 | db.setTransactionSuccessful(); |
| 2230 | } finally { |
| 2231 | db.endTransaction(); |
| 2232 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2233 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2234 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2235 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2239 | private void invalidateCustomTokenLocked( |
| 2240 | UserAccounts accounts, |
| 2241 | String accountType, |
| 2242 | String authToken) { |
| 2243 | if (authToken == null || accountType == null) { |
| 2244 | return; |
| 2245 | } |
| 2246 | // Also wipe out cached token in memory. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2247 | accounts.accountTokenCaches.remove(accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2250 | private void invalidateAuthTokenLocked(UserAccounts accounts, SQLiteDatabase db, |
| 2251 | String accountType, String authToken) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2252 | if (authToken == null || accountType == null) { |
| 2253 | return; |
| 2254 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2255 | Cursor cursor = CeDatabaseHelper.findAuthtokenForAllAccounts(db, accountType, authToken); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2256 | try { |
| 2257 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2258 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2259 | String accountName = cursor.getString(1); |
| 2260 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2261 | CeDatabaseHelper.deleteAuthToken(db, authTokenId); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2262 | writeAuthTokenIntoCacheLocked( |
| 2263 | accounts, |
| 2264 | db, |
| 2265 | new Account(accountName, accountType), |
| 2266 | authTokenType, |
| 2267 | null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2268 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2269 | } finally { |
| 2270 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2274 | private void saveCachedToken( |
| 2275 | UserAccounts accounts, |
| 2276 | Account account, |
| 2277 | String callerPkg, |
| 2278 | byte[] callerSigDigest, |
| 2279 | String tokenType, |
| 2280 | String token, |
| 2281 | long expiryMillis) { |
| 2282 | |
| 2283 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2284 | return; |
| 2285 | } |
| 2286 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2287 | UserHandle.of(accounts.userId)); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2288 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2289 | accounts.accountTokenCaches.put( |
| 2290 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2291 | } |
| 2292 | } |
| 2293 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2294 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2295 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2296 | if (account == null || type == null) { |
| 2297 | return false; |
| 2298 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2299 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2300 | UserHandle.of(accounts.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2301 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2302 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2303 | db.beginTransaction(); |
| 2304 | try { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2305 | long accountId = DeDatabaseHelper.findAccountId(db, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2306 | if (accountId < 0) { |
| 2307 | return false; |
| 2308 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2309 | CeDatabaseHelper.deleteAuthtokensByAccountIdAndType(db, accountId, type); |
| 2310 | if (CeDatabaseHelper.insertAuthToken(db, accountId, type, authToken) >= 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2311 | db.setTransactionSuccessful(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2312 | writeAuthTokenIntoCacheLocked(accounts, db, account, type, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2313 | return true; |
| 2314 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2315 | return false; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2316 | } finally { |
| 2317 | db.endTransaction(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2318 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2322 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2323 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2324 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2325 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2326 | Log.v(TAG, "peekAuthToken: " + account |
| 2327 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2328 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2329 | + ", pid " + Binder.getCallingPid()); |
| 2330 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2331 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2332 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2333 | int userId = UserHandle.getCallingUserId(); |
| 2334 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2335 | String msg = String.format( |
| 2336 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2337 | callingUid, |
| 2338 | account.type); |
| 2339 | throw new SecurityException(msg); |
| 2340 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2341 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2342 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2343 | + callingUid); |
| 2344 | return null; |
| 2345 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2346 | long identityToken = clearCallingIdentity(); |
| 2347 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2348 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2349 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2350 | } finally { |
| 2351 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2352 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2353 | } |
| 2354 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2355 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2356 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2357 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2358 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2359 | Log.v(TAG, "setAuthToken: " + account |
| 2360 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2361 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2362 | + ", pid " + Binder.getCallingPid()); |
| 2363 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2364 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2365 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2366 | int userId = UserHandle.getCallingUserId(); |
| 2367 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2368 | String msg = String.format( |
| 2369 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2370 | callingUid, |
| 2371 | account.type); |
| 2372 | throw new SecurityException(msg); |
| 2373 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2374 | long identityToken = clearCallingIdentity(); |
| 2375 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2376 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2377 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2378 | } finally { |
| 2379 | restoreCallingIdentity(identityToken); |
| 2380 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2383 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2384 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2385 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2386 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2387 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2388 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2389 | + ", pid " + Binder.getCallingPid()); |
| 2390 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2391 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2392 | int userId = UserHandle.getCallingUserId(); |
| 2393 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2394 | String msg = String.format( |
| 2395 | "uid %s cannot set secrets for accounts of type: %s", |
| 2396 | callingUid, |
| 2397 | account.type); |
| 2398 | throw new SecurityException(msg); |
| 2399 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2400 | long identityToken = clearCallingIdentity(); |
| 2401 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2402 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2403 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2404 | } finally { |
| 2405 | restoreCallingIdentity(identityToken); |
| 2406 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2407 | } |
| 2408 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2409 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2410 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2411 | if (account == null) { |
| 2412 | return; |
| 2413 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2414 | boolean isChanged = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2415 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2416 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2417 | db.beginTransaction(); |
| 2418 | try { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2419 | final long accountId = DeDatabaseHelper.findAccountId(db, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2420 | if (accountId >= 0) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2421 | CeDatabaseHelper.updateAccountPassword(db, accountId, password); |
| 2422 | CeDatabaseHelper.deleteAuthTokensByAccountId(db, accountId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2423 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2424 | accounts.accountTokenCaches.remove(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2425 | db.setTransactionSuccessful(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2426 | // If there is an account whose password will be updated and the database |
| 2427 | // transactions succeed, then we say that a change has occured. Even if the |
| 2428 | // new password is the same as the old and there were no authtokens to delete. |
| 2429 | isChanged = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2430 | String action = (password == null || password.length() == 0) ? |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2431 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2432 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2433 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, callingUid); |
Costin Manolache | f5ffe89 | 2011-01-19 09:35:32 -0800 | [diff] [blame] | 2434 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2435 | } finally { |
| 2436 | db.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2437 | if (isChanged) { |
| 2438 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
| 2439 | sendAccountsChangedBroadcast(accounts.userId); |
| 2440 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2441 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2442 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2443 | } |
| 2444 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2445 | private void sendAccountsChangedBroadcast(int userId) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2446 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 2447 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 2448 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2451 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2452 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2453 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2454 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2455 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2456 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2457 | + ", pid " + Binder.getCallingPid()); |
| 2458 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2459 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2460 | int userId = UserHandle.getCallingUserId(); |
| 2461 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2462 | String msg = String.format( |
| 2463 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2464 | callingUid, |
| 2465 | account.type); |
| 2466 | throw new SecurityException(msg); |
| 2467 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2468 | long identityToken = clearCallingIdentity(); |
| 2469 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2470 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2471 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2472 | } finally { |
| 2473 | restoreCallingIdentity(identityToken); |
| 2474 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2477 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2478 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2479 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2480 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2481 | Log.v(TAG, "setUserData: " + account |
| 2482 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2483 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2484 | + ", pid " + Binder.getCallingPid()); |
| 2485 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2486 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2487 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2488 | int userId = UserHandle.getCallingUserId(); |
| 2489 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2490 | String msg = String.format( |
| 2491 | "uid %s cannot set user data for accounts of type: %s", |
| 2492 | callingUid, |
| 2493 | account.type); |
| 2494 | throw new SecurityException(msg); |
| 2495 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2496 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2497 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2498 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2499 | synchronized (accounts.cacheLock) { |
| 2500 | if (!accountExistsCacheLocked(accounts, account)) { |
| 2501 | return; |
| 2502 | } |
| 2503 | setUserdataInternalLocked(accounts, account, key, value); |
| 2504 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2505 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2506 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2510 | private boolean accountExistsCacheLocked(UserAccounts accounts, Account account) { |
| 2511 | if (accounts.accountCache.containsKey(account.type)) { |
| 2512 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2513 | if (acc.name.equals(account.name)) { |
| 2514 | return true; |
| 2515 | } |
| 2516 | } |
| 2517 | } |
| 2518 | return false; |
| 2519 | } |
| 2520 | |
| 2521 | private void setUserdataInternalLocked(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2522 | String value) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2523 | if (account == null || key == null) { |
| 2524 | return; |
| 2525 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2526 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
| 2527 | db.beginTransaction(); |
| 2528 | try { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2529 | long accountId = DeDatabaseHelper.findAccountId(db, account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2530 | if (accountId < 0) { |
| 2531 | return; |
| 2532 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2533 | long extrasId = CeDatabaseHelper.findExtrasIdByAccountId(db, accountId, key); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2534 | if (extrasId < 0) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2535 | extrasId = CeDatabaseHelper.insertExtra(db, accountId, key, value); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2536 | if (extrasId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2537 | return; |
| 2538 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2539 | } else if (!CeDatabaseHelper.updateExtra(db, extrasId, value)) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2540 | return; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2541 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2542 | writeUserDataIntoCacheLocked(accounts, db, account, key, value); |
| 2543 | db.setTransactionSuccessful(); |
| 2544 | } finally { |
| 2545 | db.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2546 | } |
| 2547 | } |
| 2548 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2549 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2550 | if (result == null) { |
| 2551 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2552 | } |
| 2553 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2554 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2555 | + response); |
| 2556 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2557 | try { |
| 2558 | response.onResult(result); |
| 2559 | } catch (RemoteException e) { |
| 2560 | // if the caller is dead then there is no one to care about remote |
| 2561 | // exceptions |
| 2562 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2563 | Log.v(TAG, "failure while notifying response", e); |
| 2564 | } |
| 2565 | } |
| 2566 | } |
| 2567 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2568 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2569 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2570 | final String authTokenType) |
| 2571 | throws RemoteException { |
| 2572 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2573 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 2574 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2575 | final int callingUid = getCallingUid(); |
| 2576 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2577 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2578 | throw new SecurityException("can only call from system"); |
| 2579 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2580 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2581 | long identityToken = clearCallingIdentity(); |
| 2582 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2583 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2584 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2585 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2586 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2587 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2588 | protected String toDebugString(long now) { |
| 2589 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2590 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2591 | + ", authTokenType " + authTokenType; |
| 2592 | } |
| 2593 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2594 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2595 | public void run() throws RemoteException { |
| 2596 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2597 | } |
| 2598 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2599 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2600 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2601 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2602 | if (result != null) { |
| 2603 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2604 | Bundle bundle = new Bundle(); |
| 2605 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2606 | super.onResult(bundle); |
| 2607 | return; |
| 2608 | } else { |
| 2609 | super.onResult(result); |
| 2610 | } |
| 2611 | } |
| 2612 | }.bind(); |
| 2613 | } finally { |
| 2614 | restoreCallingIdentity(identityToken); |
| 2615 | } |
| 2616 | } |
| 2617 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2618 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2619 | public void getAuthToken( |
| 2620 | IAccountManagerResponse response, |
| 2621 | final Account account, |
| 2622 | final String authTokenType, |
| 2623 | final boolean notifyOnAuthFailure, |
| 2624 | final boolean expectActivityLaunch, |
| 2625 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2626 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2627 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2628 | Log.v(TAG, "getAuthToken: " + account |
| 2629 | + ", response " + response |
| 2630 | + ", authTokenType " + authTokenType |
| 2631 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2632 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2633 | + ", caller's uid " + Binder.getCallingUid() |
| 2634 | + ", pid " + Binder.getCallingPid()); |
| 2635 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2636 | if (response == null) throw new IllegalArgumentException("response is null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2637 | try { |
| 2638 | if (account == null) { |
| 2639 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2640 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2641 | return; |
| 2642 | } |
| 2643 | if (authTokenType == null) { |
| 2644 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2645 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2646 | return; |
| 2647 | } |
| 2648 | } catch (RemoteException e) { |
| 2649 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2650 | return; |
| 2651 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2652 | int userId = UserHandle.getCallingUserId(); |
| 2653 | long ident = Binder.clearCallingIdentity(); |
| 2654 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2655 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2656 | try { |
| 2657 | accounts = getUserAccounts(userId); |
| 2658 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2659 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2660 | } finally { |
| 2661 | Binder.restoreCallingIdentity(ident); |
| 2662 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2663 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2664 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2665 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2666 | |
| 2667 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2668 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2669 | final boolean permissionGranted = |
| 2670 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2671 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2672 | // Get the calling package. We will use it for the purpose of caching. |
| 2673 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2674 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2675 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2676 | try { |
| 2677 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2678 | } finally { |
| 2679 | Binder.restoreCallingIdentity(ident); |
| 2680 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2681 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2682 | String msg = String.format( |
| 2683 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2684 | callerUid, |
| 2685 | callerPkg); |
| 2686 | throw new SecurityException(msg); |
| 2687 | } |
| 2688 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2689 | // let authenticator know the identity of the caller |
| 2690 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2691 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2692 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2693 | if (notifyOnAuthFailure) { |
| 2694 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2695 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2696 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2697 | long identityToken = clearCallingIdentity(); |
| 2698 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2699 | // Distill the caller's package signatures into a single digest. |
| 2700 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2701 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2702 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2703 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2704 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2705 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2706 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2707 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2708 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2709 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2710 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2711 | onResult(response, result); |
| 2712 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2713 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2716 | if (customTokens) { |
| 2717 | /* |
| 2718 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2719 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2720 | * ANDORID_PACKAGE_NAME. |
| 2721 | */ |
| 2722 | String token = readCachedTokenInternal( |
| 2723 | accounts, |
| 2724 | account, |
| 2725 | authTokenType, |
| 2726 | callerPkg, |
| 2727 | callerPkgSigDigest); |
| 2728 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2729 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2730 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2731 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2732 | Bundle result = new Bundle(); |
| 2733 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2734 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2735 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2736 | onResult(response, result); |
| 2737 | return; |
| 2738 | } |
| 2739 | } |
| 2740 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2741 | new Session( |
| 2742 | accounts, |
| 2743 | response, |
| 2744 | account.type, |
| 2745 | expectActivityLaunch, |
| 2746 | false /* stripAuthTokenFromResult */, |
| 2747 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2748 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2749 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2750 | protected String toDebugString(long now) { |
| 2751 | if (loginOptions != null) loginOptions.keySet(); |
| 2752 | return super.toDebugString(now) + ", getAuthToken" |
| 2753 | + ", " + account |
| 2754 | + ", authTokenType " + authTokenType |
| 2755 | + ", loginOptions " + loginOptions |
| 2756 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2757 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2758 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2759 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2760 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2761 | // If the caller doesn't have permission then create and return the |
| 2762 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2763 | if (!permissionGranted) { |
| 2764 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2765 | } else { |
| 2766 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2767 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2770 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2771 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2772 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2773 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2774 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2775 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2776 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2777 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2778 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2779 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2780 | authTokenType, |
| 2781 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2782 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2783 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2784 | onResult(bundle); |
| 2785 | return; |
| 2786 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2787 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2788 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2789 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2790 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2791 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2792 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2793 | "the type and name should not be empty"); |
| 2794 | return; |
| 2795 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2796 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2797 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2798 | saveAuthTokenToDatabase( |
| 2799 | mAccounts, |
| 2800 | resultAccount, |
| 2801 | authTokenType, |
| 2802 | authToken); |
| 2803 | } |
| 2804 | long expiryMillis = result.getLong( |
| 2805 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2806 | if (customTokens |
| 2807 | && expiryMillis > System.currentTimeMillis()) { |
| 2808 | saveCachedToken( |
| 2809 | mAccounts, |
| 2810 | account, |
| 2811 | callerPkg, |
| 2812 | callerPkgSigDigest, |
| 2813 | authTokenType, |
| 2814 | authToken, |
| 2815 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2816 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2817 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2818 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2819 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2820 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2821 | /* |
| 2822 | * Make sure that the supplied intent is owned by the authenticator |
| 2823 | * giving it to the system. Otherwise a malicious authenticator could |
| 2824 | * have users launching arbitrary activities by tricking users to |
| 2825 | * interact with malicious notifications. |
| 2826 | */ |
| 2827 | checkKeyIntent( |
| 2828 | Binder.getCallingUid(), |
| 2829 | intent); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2830 | doNotification(mAccounts, |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2831 | account, result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2832 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2833 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2834 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2835 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2836 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2837 | }.bind(); |
| 2838 | } finally { |
| 2839 | restoreCallingIdentity(identityToken); |
| 2840 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2843 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2844 | MessageDigest digester; |
| 2845 | try { |
| 2846 | digester = MessageDigest.getInstance("SHA-256"); |
| 2847 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2848 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2849 | for (Signature sig : pkgInfo.signatures) { |
| 2850 | digester.update(sig.toByteArray()); |
| 2851 | } |
| 2852 | } catch (NoSuchAlgorithmException x) { |
| 2853 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2854 | digester = null; |
| 2855 | } catch (NameNotFoundException e) { |
| 2856 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2857 | digester = null; |
| 2858 | } |
| 2859 | return (digester == null) ? null : digester.digest(); |
| 2860 | } |
| 2861 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2862 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2863 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2864 | int uid = intent.getIntExtra( |
| 2865 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2866 | String authTokenType = intent.getStringExtra( |
| 2867 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2868 | final String titleAndSubtitle = |
| 2869 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2870 | account.name); |
| 2871 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2872 | String title = titleAndSubtitle; |
| 2873 | String subtitle = ""; |
| 2874 | if (index > 0) { |
| 2875 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2876 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2877 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2878 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2879 | Context contextForUser = getContextForUser(user); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 2880 | Notification n = new Notification.Builder(contextForUser) |
| 2881 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2882 | .setWhen(0) |
| 2883 | .setColor(contextForUser.getColor( |
| 2884 | com.android.internal.R.color.system_notification_accent_color)) |
| 2885 | .setContentTitle(title) |
| 2886 | .setContentText(subtitle) |
| 2887 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2888 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2889 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2890 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2891 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2892 | } |
| 2893 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2894 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 2895 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 2896 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2897 | |
| 2898 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2899 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2900 | if (startInNewTask) { |
| 2901 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 2902 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2903 | // the intent from a non-Activity context. This is the default behavior. |
| 2904 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2905 | } |
| 2906 | intent.addCategory(String.valueOf(getCredentialPermissionNotificationId(account, |
| 2907 | authTokenType, uid) + (packageName != null ? packageName : ""))); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2908 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2909 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2910 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2911 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2912 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2913 | return intent; |
| 2914 | } |
| 2915 | |
| 2916 | private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, |
| 2917 | int uid) { |
| 2918 | Integer id; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2919 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2920 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2921 | final Pair<Pair<Account, String>, Integer> key = |
| 2922 | new Pair<Pair<Account, String>, Integer>( |
| 2923 | new Pair<Account, String>(account, authTokenType), uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2924 | id = accounts.credentialsPermissionNotificationIds.get(key); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2925 | if (id == null) { |
| 2926 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2927 | accounts.credentialsPermissionNotificationIds.put(key, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | return id; |
| 2931 | } |
| 2932 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2933 | private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2934 | Integer id; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2935 | synchronized (accounts.signinRequiredNotificationIds) { |
| 2936 | id = accounts.signinRequiredNotificationIds.get(account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2937 | if (id == null) { |
| 2938 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2939 | accounts.signinRequiredNotificationIds.put(account, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2940 | } |
| 2941 | } |
| 2942 | return id; |
| 2943 | } |
| 2944 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2945 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2946 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2947 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2948 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2949 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2950 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2951 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2952 | + ", response " + response |
| 2953 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2954 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2955 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2956 | + ", caller's uid " + Binder.getCallingUid() |
| 2957 | + ", pid " + Binder.getCallingPid()); |
| 2958 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2959 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2960 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2961 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2962 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2963 | final int uid = Binder.getCallingUid(); |
| 2964 | final int userId = UserHandle.getUserId(uid); |
| 2965 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2966 | try { |
| 2967 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2968 | "User is not allowed to add an account!"); |
| 2969 | } catch (RemoteException re) { |
| 2970 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2971 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2972 | return; |
| 2973 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2974 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2975 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2976 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2977 | "User cannot modify accounts of this type (policy)."); |
| 2978 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2979 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2980 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2981 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2982 | return; |
| 2983 | } |
| 2984 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2985 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2986 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2987 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2988 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2989 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2990 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2991 | long identityToken = clearCallingIdentity(); |
| 2992 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2993 | UserAccounts accounts = getUserAccounts(usrId); |
| 2994 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2995 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 2996 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2997 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2998 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2999 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3000 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3001 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 3002 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3003 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3004 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3005 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3006 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3007 | protected String toDebugString(long now) { |
| 3008 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3009 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3010 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3011 | } |
| 3012 | }.bind(); |
| 3013 | } finally { |
| 3014 | restoreCallingIdentity(identityToken); |
| 3015 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3016 | } |
| 3017 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3018 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3019 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 3020 | final String authTokenType, final String[] requiredFeatures, |
| 3021 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3022 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3023 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3024 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3025 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3026 | + ", response " + response |
| 3027 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3028 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3029 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3030 | + ", caller's uid " + Binder.getCallingUid() |
| 3031 | + ", pid " + Binder.getCallingPid() |
| 3032 | + ", for user id " + userId); |
| 3033 | } |
| 3034 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3035 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3036 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3037 | if (isCrossUser(callingUid, userId)) { |
| 3038 | throw new SecurityException( |
| 3039 | String.format( |
| 3040 | "User %s trying to add account for %s" , |
| 3041 | UserHandle.getCallingUserId(), |
| 3042 | userId)); |
| 3043 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3044 | |
| 3045 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3046 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3047 | try { |
| 3048 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3049 | "User is not allowed to add an account!"); |
| 3050 | } catch (RemoteException re) { |
| 3051 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3052 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3053 | return; |
| 3054 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3055 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3056 | try { |
| 3057 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3058 | "User cannot modify accounts of this type (policy)."); |
| 3059 | } catch (RemoteException re) { |
| 3060 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3061 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3062 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3063 | return; |
| 3064 | } |
| 3065 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3066 | final int pid = Binder.getCallingPid(); |
| 3067 | final int uid = Binder.getCallingUid(); |
| 3068 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3069 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3070 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3071 | |
| 3072 | long identityToken = clearCallingIdentity(); |
| 3073 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3074 | UserAccounts accounts = getUserAccounts(userId); |
| 3075 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3076 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3077 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3078 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3079 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3080 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3081 | @Override |
| 3082 | public void run() throws RemoteException { |
| 3083 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3084 | options); |
| 3085 | } |
| 3086 | |
| 3087 | @Override |
| 3088 | protected String toDebugString(long now) { |
| 3089 | return super.toDebugString(now) + ", addAccount" |
| 3090 | + ", accountType " + accountType |
| 3091 | + ", requiredFeatures " |
| 3092 | + (requiredFeatures != null |
| 3093 | ? TextUtils.join(",", requiredFeatures) |
| 3094 | : null); |
| 3095 | } |
| 3096 | }.bind(); |
| 3097 | } finally { |
| 3098 | restoreCallingIdentity(identityToken); |
| 3099 | } |
| 3100 | } |
| 3101 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3102 | @Override |
Sandra Kwan | e68c37ee | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3103 | public void startAddAccountSession( |
| 3104 | final IAccountManagerResponse response, |
| 3105 | final String accountType, |
| 3106 | final String authTokenType, |
| 3107 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3108 | final boolean expectActivityLaunch, |
| 3109 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3110 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3111 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3112 | Log.v(TAG, |
| 3113 | "startAddAccountSession: accountType " + accountType |
| 3114 | + ", response " + response |
| 3115 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3116 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3117 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3118 | + ", caller's uid " + Binder.getCallingUid() |
| 3119 | + ", pid " + Binder.getCallingPid()); |
| 3120 | } |
| 3121 | if (response == null) { |
| 3122 | throw new IllegalArgumentException("response is null"); |
| 3123 | } |
| 3124 | if (accountType == null) { |
| 3125 | throw new IllegalArgumentException("accountType is null"); |
| 3126 | } |
| 3127 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3128 | final int uid = Binder.getCallingUid(); |
| 3129 | final int userId = UserHandle.getUserId(uid); |
| 3130 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3131 | try { |
| 3132 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3133 | "User is not allowed to add an account!"); |
| 3134 | } catch (RemoteException re) { |
| 3135 | } |
| 3136 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3137 | return; |
| 3138 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3139 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3140 | try { |
| 3141 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3142 | "User cannot modify accounts of this type (policy)."); |
| 3143 | } catch (RemoteException re) { |
| 3144 | } |
| 3145 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3146 | userId); |
| 3147 | return; |
| 3148 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3149 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3150 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3151 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3152 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3153 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3154 | // Check to see if the Password should be included to the caller. |
| 3155 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3156 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3157 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3158 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3159 | long identityToken = clearCallingIdentity(); |
| 3160 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3161 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3162 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3163 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3164 | new StartAccountSession( |
| 3165 | accounts, |
| 3166 | response, |
| 3167 | accountType, |
| 3168 | expectActivityLaunch, |
| 3169 | null /* accountName */, |
| 3170 | false /* authDetailsRequired */, |
| 3171 | true /* updateLastAuthenticationTime */, |
| 3172 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3173 | @Override |
| 3174 | public void run() throws RemoteException { |
| 3175 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3176 | requiredFeatures, options); |
| 3177 | } |
| 3178 | |
| 3179 | @Override |
| 3180 | protected String toDebugString(long now) { |
| 3181 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3182 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3183 | + accountType + ", requiredFeatures " |
| 3184 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3185 | } |
| 3186 | }.bind(); |
| 3187 | } finally { |
| 3188 | restoreCallingIdentity(identityToken); |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3193 | private abstract class StartAccountSession extends Session { |
| 3194 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3195 | private final boolean mIsPasswordForwardingAllowed; |
| 3196 | |
| 3197 | public StartAccountSession( |
| 3198 | UserAccounts accounts, |
| 3199 | IAccountManagerResponse response, |
| 3200 | String accountType, |
| 3201 | boolean expectActivityLaunch, |
| 3202 | String accountName, |
| 3203 | boolean authDetailsRequired, |
| 3204 | boolean updateLastAuthenticationTime, |
| 3205 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3206 | super(accounts, response, accountType, expectActivityLaunch, |
| 3207 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3208 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3209 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | @Override |
| 3213 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3214 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3215 | mNumResults++; |
| 3216 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3217 | if (result != null |
| 3218 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3219 | checkKeyIntent( |
| 3220 | Binder.getCallingUid(), |
| 3221 | intent); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3222 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3223 | IAccountManagerResponse response; |
| 3224 | if (mExpectActivityLaunch && result != null |
| 3225 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3226 | response = mResponse; |
| 3227 | } else { |
| 3228 | response = getResponseAndClose(); |
| 3229 | } |
| 3230 | if (response == null) { |
| 3231 | return; |
| 3232 | } |
| 3233 | if (result == null) { |
| 3234 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3235 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3236 | + response); |
| 3237 | } |
| 3238 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3239 | "null bundle returned"); |
| 3240 | return; |
| 3241 | } |
| 3242 | |
| 3243 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3244 | // All AccountManager error codes are greater |
| 3245 | // than 0 |
| 3246 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3247 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3248 | return; |
| 3249 | } |
| 3250 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3251 | // Omit passwords if the caller isn't permitted to see them. |
| 3252 | if (!mIsPasswordForwardingAllowed) { |
| 3253 | result.remove(AccountManager.KEY_PASSWORD); |
| 3254 | } |
| 3255 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3256 | // Strip auth token from result. |
| 3257 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3258 | |
| 3259 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3260 | Log.v(TAG, |
| 3261 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3262 | } |
| 3263 | |
| 3264 | // Get the session bundle created by authenticator. The |
| 3265 | // bundle contains data necessary for finishing the session |
| 3266 | // later. The session bundle will be encrypted here and |
| 3267 | // decrypted later when trying to finish the session. |
| 3268 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3269 | if (sessionBundle != null) { |
| 3270 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3271 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3272 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3273 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3274 | } |
| 3275 | // Add accountType info to session bundle. This will |
| 3276 | // override any value set by authenticator. |
| 3277 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3278 | |
| 3279 | // Encrypt session bundle before returning to caller. |
| 3280 | try { |
| 3281 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3282 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3283 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3284 | } catch (GeneralSecurityException e) { |
| 3285 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3286 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3287 | } |
| 3288 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3289 | "failed to encrypt session bundle"); |
| 3290 | return; |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | sendResponse(response, result); |
| 3295 | } |
| 3296 | } |
| 3297 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3298 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3299 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3300 | @NonNull Bundle sessionBundle, |
| 3301 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3302 | Bundle appInfo, |
| 3303 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3304 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3305 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3306 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3307 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3308 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3309 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3310 | + ", caller's uid " + callingUid |
| 3311 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3312 | + ", pid " + Binder.getCallingPid() |
| 3313 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3314 | } |
| 3315 | if (response == null) { |
| 3316 | throw new IllegalArgumentException("response is null"); |
| 3317 | } |
| 3318 | |
| 3319 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3320 | // Account type is added to it before encryption. |
| 3321 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3322 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3323 | } |
| 3324 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3325 | // Only allow the system process to finish session for other users |
| 3326 | if (isCrossUser(callingUid, userId)) { |
| 3327 | throw new SecurityException( |
| 3328 | String.format( |
| 3329 | "User %s trying to finish session for %s without cross user permission", |
| 3330 | UserHandle.getCallingUserId(), |
| 3331 | userId)); |
| 3332 | } |
| 3333 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3334 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3335 | sendErrorResponse(response, |
| 3336 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3337 | "User is not allowed to add an account!"); |
| 3338 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3339 | return; |
| 3340 | } |
| 3341 | |
| 3342 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3343 | final Bundle decryptedBundle; |
| 3344 | final String accountType; |
| 3345 | // First decrypt session bundle to get account type for checking permission. |
| 3346 | try { |
| 3347 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3348 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3349 | if (decryptedBundle == null) { |
| 3350 | sendErrorResponse( |
| 3351 | response, |
| 3352 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3353 | "failed to decrypt session bundle"); |
| 3354 | return; |
| 3355 | } |
| 3356 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3357 | // Account type cannot be null. This should not happen if session bundle was created |
| 3358 | // properly by #StartAccountSession. |
| 3359 | if (TextUtils.isEmpty(accountType)) { |
| 3360 | sendErrorResponse( |
| 3361 | response, |
| 3362 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3363 | "accountType is empty"); |
| 3364 | return; |
| 3365 | } |
| 3366 | |
| 3367 | // If by any chances, decryptedBundle contains colliding keys with |
| 3368 | // system info |
| 3369 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3370 | // update credentials flow, we should replace with the new values of the current call. |
| 3371 | if (appInfo != null) { |
| 3372 | decryptedBundle.putAll(appInfo); |
| 3373 | } |
| 3374 | |
| 3375 | // 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] | 3376 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3377 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3378 | } catch (GeneralSecurityException e) { |
| 3379 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3380 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3381 | } |
| 3382 | sendErrorResponse( |
| 3383 | response, |
| 3384 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3385 | "failed to decrypt session bundle"); |
| 3386 | return; |
| 3387 | } |
| 3388 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3389 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3390 | sendErrorResponse( |
| 3391 | response, |
| 3392 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3393 | "User cannot modify accounts of this type (policy)."); |
| 3394 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3395 | userId); |
| 3396 | return; |
| 3397 | } |
| 3398 | |
| 3399 | long identityToken = clearCallingIdentity(); |
| 3400 | try { |
| 3401 | UserAccounts accounts = getUserAccounts(userId); |
| 3402 | logRecordWithUid( |
| 3403 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3404 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3405 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3406 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3407 | new Session( |
| 3408 | accounts, |
| 3409 | response, |
| 3410 | accountType, |
| 3411 | expectActivityLaunch, |
| 3412 | true /* stripAuthTokenFromResult */, |
| 3413 | null /* accountName */, |
| 3414 | false /* authDetailsRequired */, |
| 3415 | true /* updateLastAuthenticationTime */) { |
| 3416 | @Override |
| 3417 | public void run() throws RemoteException { |
| 3418 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3419 | } |
| 3420 | |
| 3421 | @Override |
| 3422 | protected String toDebugString(long now) { |
| 3423 | return super.toDebugString(now) |
| 3424 | + ", finishSession" |
| 3425 | + ", accountType " + accountType; |
| 3426 | } |
| 3427 | }.bind(); |
| 3428 | } finally { |
| 3429 | restoreCallingIdentity(identityToken); |
| 3430 | } |
| 3431 | } |
| 3432 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3433 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3434 | final DevicePolicyManagerInternal dpmi = |
| 3435 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3436 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3437 | if (dpmi == null) { |
| 3438 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3439 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3440 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3441 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3442 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3443 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3444 | } |
| 3445 | if (intent == null) { |
| 3446 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3447 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3448 | long identityToken = clearCallingIdentity(); |
| 3449 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3450 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3451 | } finally { |
| 3452 | restoreCallingIdentity(identityToken); |
| 3453 | } |
| 3454 | } |
| 3455 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3456 | /** |
| 3457 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3458 | */ |
| 3459 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3460 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3461 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3462 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3463 | return cantAddAccount; |
| 3464 | } |
| 3465 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3466 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3467 | public void confirmCredentialsAsUser( |
| 3468 | IAccountManagerResponse response, |
| 3469 | final Account account, |
| 3470 | final Bundle options, |
| 3471 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3472 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3473 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3474 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3475 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3476 | Log.v(TAG, "confirmCredentials: " + account |
| 3477 | + ", response " + response |
| 3478 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3479 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3480 | + ", pid " + Binder.getCallingPid()); |
| 3481 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3482 | // Only allow the system process to read accounts of other users |
| 3483 | if (isCrossUser(callingUid, userId)) { |
| 3484 | throw new SecurityException( |
| 3485 | String.format( |
| 3486 | "User %s trying to confirm account credentials for %s" , |
| 3487 | UserHandle.getCallingUserId(), |
| 3488 | userId)); |
| 3489 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3490 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3491 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3492 | long identityToken = clearCallingIdentity(); |
| 3493 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3494 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3495 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3496 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3497 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3498 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3499 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3500 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3501 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3502 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3503 | protected String toDebugString(long now) { |
| 3504 | return super.toDebugString(now) + ", confirmCredentials" |
| 3505 | + ", " + account; |
| 3506 | } |
| 3507 | }.bind(); |
| 3508 | } finally { |
| 3509 | restoreCallingIdentity(identityToken); |
| 3510 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3511 | } |
| 3512 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3513 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3514 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3515 | final String authTokenType, final boolean expectActivityLaunch, |
| 3516 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3517 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3518 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3519 | Log.v(TAG, "updateCredentials: " + account |
| 3520 | + ", response " + response |
| 3521 | + ", authTokenType " + authTokenType |
| 3522 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3523 | + ", caller's uid " + Binder.getCallingUid() |
| 3524 | + ", pid " + Binder.getCallingPid()); |
| 3525 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3526 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3527 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3528 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3529 | long identityToken = clearCallingIdentity(); |
| 3530 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3531 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3532 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3533 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3534 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3535 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3536 | public void run() throws RemoteException { |
| 3537 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3538 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3539 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3540 | protected String toDebugString(long now) { |
| 3541 | if (loginOptions != null) loginOptions.keySet(); |
| 3542 | return super.toDebugString(now) + ", updateCredentials" |
| 3543 | + ", " + account |
| 3544 | + ", authTokenType " + authTokenType |
| 3545 | + ", loginOptions " + loginOptions; |
| 3546 | } |
| 3547 | }.bind(); |
| 3548 | } finally { |
| 3549 | restoreCallingIdentity(identityToken); |
| 3550 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3551 | } |
| 3552 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3553 | @Override |
Sandra Kwan | e68c37ee | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3554 | public void startUpdateCredentialsSession( |
| 3555 | IAccountManagerResponse response, |
| 3556 | final Account account, |
| 3557 | final String authTokenType, |
| 3558 | final boolean expectActivityLaunch, |
| 3559 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3560 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37ee | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3561 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3562 | Log.v(TAG, |
| 3563 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3564 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3565 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3566 | + ", pid " + Binder.getCallingPid()); |
| 3567 | } |
| 3568 | if (response == null) { |
| 3569 | throw new IllegalArgumentException("response is null"); |
| 3570 | } |
| 3571 | if (account == null) { |
| 3572 | throw new IllegalArgumentException("account is null"); |
| 3573 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3574 | |
| 3575 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37ee | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3576 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3577 | |
| 3578 | // Check to see if the Password should be included to the caller. |
| 3579 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3580 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3581 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3582 | |
Sandra Kwan | e68c37ee | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3583 | long identityToken = clearCallingIdentity(); |
| 3584 | try { |
| 3585 | UserAccounts accounts = getUserAccounts(userId); |
| 3586 | new StartAccountSession( |
| 3587 | accounts, |
| 3588 | response, |
| 3589 | account.type, |
| 3590 | expectActivityLaunch, |
| 3591 | account.name, |
| 3592 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3593 | true /* updateLastCredentialTime */, |
| 3594 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37ee | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3595 | @Override |
| 3596 | public void run() throws RemoteException { |
| 3597 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3598 | loginOptions); |
| 3599 | } |
| 3600 | |
| 3601 | @Override |
| 3602 | protected String toDebugString(long now) { |
| 3603 | if (loginOptions != null) |
| 3604 | loginOptions.keySet(); |
| 3605 | return super.toDebugString(now) |
| 3606 | + ", startUpdateCredentialsSession" |
| 3607 | + ", " + account |
| 3608 | + ", authTokenType " + authTokenType |
| 3609 | + ", loginOptions " + loginOptions; |
| 3610 | } |
| 3611 | }.bind(); |
| 3612 | } finally { |
| 3613 | restoreCallingIdentity(identityToken); |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | @Override |
Sandra Kwan | 390c9d2d | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3618 | public void isCredentialsUpdateSuggested( |
| 3619 | IAccountManagerResponse response, |
| 3620 | final Account account, |
| 3621 | final String statusToken) { |
| 3622 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3623 | Log.v(TAG, |
| 3624 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3625 | + ", caller's uid " + Binder.getCallingUid() |
| 3626 | + ", pid " + Binder.getCallingPid()); |
| 3627 | } |
| 3628 | if (response == null) { |
| 3629 | throw new IllegalArgumentException("response is null"); |
| 3630 | } |
| 3631 | if (account == null) { |
| 3632 | throw new IllegalArgumentException("account is null"); |
| 3633 | } |
| 3634 | if (TextUtils.isEmpty(statusToken)) { |
| 3635 | throw new IllegalArgumentException("status token is empty"); |
| 3636 | } |
| 3637 | |
Sandra Kwan | 390c9d2d | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3638 | int usrId = UserHandle.getCallingUserId(); |
| 3639 | long identityToken = clearCallingIdentity(); |
| 3640 | try { |
| 3641 | UserAccounts accounts = getUserAccounts(usrId); |
| 3642 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3643 | false /* stripAuthTokenFromResult */, account.name, |
| 3644 | false /* authDetailsRequired */) { |
| 3645 | @Override |
| 3646 | protected String toDebugString(long now) { |
| 3647 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3648 | + ", " + account; |
| 3649 | } |
| 3650 | |
| 3651 | @Override |
| 3652 | public void run() throws RemoteException { |
| 3653 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3654 | } |
| 3655 | |
| 3656 | @Override |
| 3657 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3658 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2d | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3659 | IAccountManagerResponse response = getResponseAndClose(); |
| 3660 | if (response == null) { |
| 3661 | return; |
| 3662 | } |
| 3663 | |
| 3664 | if (result == null) { |
| 3665 | sendErrorResponse( |
| 3666 | response, |
| 3667 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3668 | "null bundle"); |
| 3669 | return; |
| 3670 | } |
| 3671 | |
| 3672 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3673 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3674 | + response); |
| 3675 | } |
| 3676 | // Check to see if an error occurred. We know if an error occurred because all |
| 3677 | // error codes are greater than 0. |
| 3678 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3679 | sendErrorResponse(response, |
| 3680 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3681 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3682 | return; |
| 3683 | } |
| 3684 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3685 | sendErrorResponse( |
| 3686 | response, |
| 3687 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3688 | "no result in response"); |
| 3689 | return; |
| 3690 | } |
| 3691 | final Bundle newResult = new Bundle(); |
| 3692 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3693 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3694 | sendResponse(response, newResult); |
| 3695 | } |
| 3696 | }.bind(); |
| 3697 | } finally { |
| 3698 | restoreCallingIdentity(identityToken); |
| 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3703 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3704 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3705 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3706 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3707 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3708 | + ", response " + response |
| 3709 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3710 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3711 | + ", pid " + Binder.getCallingPid()); |
| 3712 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3713 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3714 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3715 | int userId = UserHandle.getCallingUserId(); |
| 3716 | if (!isAccountManagedByCaller(accountType, callingUid, userId) && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3717 | String msg = String.format( |
| 3718 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3719 | callingUid, |
| 3720 | accountType); |
| 3721 | throw new SecurityException(msg); |
| 3722 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3723 | long identityToken = clearCallingIdentity(); |
| 3724 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3725 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3726 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3727 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3728 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3729 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3730 | public void run() throws RemoteException { |
| 3731 | mAuthenticator.editProperties(this, mAccountType); |
| 3732 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3733 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3734 | protected String toDebugString(long now) { |
| 3735 | return super.toDebugString(now) + ", editProperties" |
| 3736 | + ", accountType " + accountType; |
| 3737 | } |
| 3738 | }.bind(); |
| 3739 | } finally { |
| 3740 | restoreCallingIdentity(identityToken); |
| 3741 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3742 | } |
| 3743 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3744 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3745 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3746 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3747 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3748 | throw new SecurityException("Can be called only by system UID"); |
| 3749 | } |
| 3750 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3751 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3752 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3753 | |
| 3754 | final int userId = userHandle.getIdentifier(); |
| 3755 | |
| 3756 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3757 | |
| 3758 | try { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3759 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3760 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3761 | } catch (NameNotFoundException e) { |
| 3762 | return false; |
| 3763 | } |
| 3764 | } |
| 3765 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3766 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3767 | int uid) { |
| 3768 | if (packageName == null) { |
| 3769 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3770 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3771 | return false; |
| 3772 | } |
| 3773 | // For app op checks related to permissions all packages in the UID |
| 3774 | // have the same app op state, so doesn't matter which one we pick. |
| 3775 | packageName = packageNames[0]; |
| 3776 | } |
| 3777 | |
| 3778 | // Use null token which means any token. Having a token means the package |
| 3779 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3780 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3781 | return true; |
| 3782 | } |
| 3783 | // In addition to the permissions required to get an auth token we also allow |
| 3784 | // the account to be accessed by holders of the get accounts permissions. |
| 3785 | return checkUidPermission(Manifest.permission.GET_ACCOUNTS_PRIVILEGED, uid, packageName) |
| 3786 | || checkUidPermission(Manifest.permission.GET_ACCOUNTS, uid, packageName); |
| 3787 | } |
| 3788 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3789 | private boolean checkUidPermission(String permission, int uid, String opPackageName) { |
| 3790 | final long identity = Binder.clearCallingIdentity(); |
| 3791 | try { |
| 3792 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 3793 | if (pm.checkUidPermission(permission, uid) != PackageManager.PERMISSION_GRANTED) { |
| 3794 | return false; |
| 3795 | } |
| 3796 | final int opCode = AppOpsManager.permissionToOpCode(permission); |
| 3797 | return (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOpNoThrow( |
| 3798 | opCode, uid, opPackageName) == AppOpsManager.MODE_ALLOWED); |
| 3799 | } catch (RemoteException e) { |
| 3800 | /* ignore - local call */ |
| 3801 | } finally { |
| 3802 | Binder.restoreCallingIdentity(identity); |
| 3803 | } |
| 3804 | return false; |
| 3805 | } |
| 3806 | |
| 3807 | @Override |
| 3808 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3809 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3810 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3811 | throw new SecurityException("Can be called only by system UID"); |
| 3812 | } |
| 3813 | |
| 3814 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3815 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3816 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3817 | |
| 3818 | final int userId = userHandle.getIdentifier(); |
| 3819 | |
| 3820 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3821 | |
| 3822 | final int uid; |
| 3823 | try { |
| 3824 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 3825 | } catch (NameNotFoundException e) { |
| 3826 | Slog.e(TAG, "Unknown package " + packageName); |
| 3827 | return null; |
| 3828 | } |
| 3829 | |
| 3830 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 3831 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3832 | final long identity = Binder.clearCallingIdentity(); |
| 3833 | try { |
| 3834 | return PendingIntent.getActivityAsUser( |
| 3835 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 3836 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 3837 | null, new UserHandle(userId)).getIntentSender(); |
| 3838 | } finally { |
| 3839 | Binder.restoreCallingIdentity(identity); |
| 3840 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 3844 | int uid, RemoteCallback callback) { |
| 3845 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 3846 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 3847 | @Override |
| 3848 | public void onResult(Bundle value) throws RemoteException { |
| 3849 | handleAuthenticatorResponse(true); |
| 3850 | } |
| 3851 | |
| 3852 | @Override |
| 3853 | public void onRequestContinued() { |
| 3854 | /* ignore */ |
| 3855 | } |
| 3856 | |
| 3857 | @Override |
| 3858 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 3859 | handleAuthenticatorResponse(false); |
| 3860 | } |
| 3861 | |
| 3862 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 3863 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3864 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3865 | UserHandle.getUserHandleForUid(uid)); |
| 3866 | if (callback != null) { |
| 3867 | Bundle result = new Bundle(); |
| 3868 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 3869 | callback.sendResult(result); |
| 3870 | } |
| 3871 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3872 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3876 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 3877 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 3878 | throw new SecurityException("Only system can check for accounts across users"); |
| 3879 | } |
| 3880 | final long token = Binder.clearCallingIdentity(); |
| 3881 | try { |
| 3882 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 3883 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 3884 | if (allAccounts[i].account.equals(account)) { |
| 3885 | return true; |
| 3886 | } |
| 3887 | } |
| 3888 | return false; |
| 3889 | } finally { |
| 3890 | Binder.restoreCallingIdentity(token); |
| 3891 | } |
| 3892 | } |
| 3893 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3894 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 3895 | private final String[] mFeatures; |
| 3896 | private volatile Account[] mAccountsOfType = null; |
| 3897 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 3898 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3899 | private final int mCallingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3900 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3901 | public GetAccountsByTypeAndFeatureSession(UserAccounts accounts, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3902 | IAccountManagerResponse response, String type, String[] features, int callingUid) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3903 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3904 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3905 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3906 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3907 | mFeatures = features; |
| 3908 | } |
| 3909 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3910 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3911 | public void run() throws RemoteException { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3912 | synchronized (mAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3913 | mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType, mCallingUid, |
| 3914 | null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3915 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3916 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 3917 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3918 | mCurrentAccount = 0; |
| 3919 | |
| 3920 | checkAccount(); |
| 3921 | } |
| 3922 | |
| 3923 | public void checkAccount() { |
| 3924 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 3925 | sendResult(); |
| 3926 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3927 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3928 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3929 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 3930 | if (accountAuthenticator == null) { |
| 3931 | // It is possible that the authenticator has died, which is indicated by |
| 3932 | // mAuthenticator being set to null. If this happens then just abort. |
| 3933 | // There is no need to send back a result or error in this case since |
| 3934 | // that already happened when mAuthenticator was cleared. |
| 3935 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3936 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 3937 | + " connected to the authenticator, " + toDebugString()); |
| 3938 | } |
| 3939 | return; |
| 3940 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3941 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3942 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3943 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3944 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3945 | } |
| 3946 | } |
| 3947 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3948 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3949 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3950 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3951 | mNumResults++; |
| 3952 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3953 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3954 | return; |
| 3955 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3956 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3957 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 3958 | } |
| 3959 | mCurrentAccount++; |
| 3960 | checkAccount(); |
| 3961 | } |
| 3962 | |
| 3963 | public void sendResult() { |
| 3964 | IAccountManagerResponse response = getResponseAndClose(); |
| 3965 | if (response != null) { |
| 3966 | try { |
| 3967 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 3968 | for (int i = 0; i < accounts.length; i++) { |
| 3969 | accounts[i] = mAccountsWithFeatures.get(i); |
| 3970 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3971 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3972 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3973 | + response); |
| 3974 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3975 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3976 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3977 | response.onResult(result); |
| 3978 | } catch (RemoteException e) { |
| 3979 | // if the caller is dead then there is no one to care about remote exceptions |
| 3980 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3981 | Log.v(TAG, "failure while notifying response", e); |
| 3982 | } |
| 3983 | } |
| 3984 | } |
| 3985 | } |
| 3986 | |
| 3987 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3988 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3989 | protected String toDebugString(long now) { |
| 3990 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 3991 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 3992 | } |
| 3993 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 3994 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3995 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3996 | * 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] | 3997 | * @hide |
| 3998 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3999 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4000 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4001 | int callingUid = Binder.getCallingUid(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4002 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4003 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4004 | if (visibleAccountTypes.isEmpty()) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4005 | return new Account[0]; |
| 4006 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4007 | long identityToken = clearCallingIdentity(); |
| 4008 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4009 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4010 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4011 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4012 | callingUid, |
| 4013 | null, // packageName |
| 4014 | visibleAccountTypes); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4015 | } finally { |
| 4016 | restoreCallingIdentity(identityToken); |
| 4017 | } |
| 4018 | } |
| 4019 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4020 | /** |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4021 | * Returns accounts for all running users. |
| 4022 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4023 | * @hide |
| 4024 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4025 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4026 | public AccountAndUser[] getRunningAccounts() { |
| 4027 | final int[] runningUserIds; |
| 4028 | try { |
| 4029 | runningUserIds = ActivityManagerNative.getDefault().getRunningUserIds(); |
| 4030 | } catch (RemoteException e) { |
| 4031 | // Running in system_server; should never happen |
| 4032 | throw new RuntimeException(e); |
| 4033 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4034 | return getAccounts(runningUserIds); |
| 4035 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4036 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4037 | /** {@hide} */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4038 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4039 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 4040 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4041 | final int[] userIds = new int[users.size()]; |
| 4042 | for (int i = 0; i < userIds.length; i++) { |
| 4043 | userIds[i] = users.get(i).id; |
| 4044 | } |
| 4045 | return getAccounts(userIds); |
| 4046 | } |
| 4047 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4048 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4049 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4050 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4051 | for (int userId : userIds) { |
| 4052 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4053 | if (userAccounts == null) continue; |
| 4054 | synchronized (userAccounts.cacheLock) { |
| 4055 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null, |
| 4056 | Binder.getCallingUid(), null); |
| 4057 | for (int a = 0; a < accounts.length; a++) { |
| 4058 | runningAccounts.add(new AccountAndUser(accounts[a], userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4059 | } |
| 4060 | } |
| 4061 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4062 | |
| 4063 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4064 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4065 | } |
| 4066 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4067 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4068 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4069 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
| 4070 | return getAccountsAsUser(type, userId, null, -1, opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4071 | } |
| 4072 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4073 | @NonNull |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4074 | private Account[] getAccountsAsUser( |
| 4075 | String type, |
| 4076 | int userId, |
| 4077 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4078 | int packageUid, |
| 4079 | String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4080 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4081 | // Only allow the system process to read accounts of other users |
| 4082 | if (userId != UserHandle.getCallingUserId() |
Amith Yamasani | bb49e85 | 2013-03-30 19:20:18 -0700 | [diff] [blame] | 4083 | && callingUid != Process.myUid() |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4084 | && mContext.checkCallingOrSelfPermission( |
| 4085 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4086 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4087 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4088 | + " trying to get account for " + userId); |
| 4089 | } |
| 4090 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4091 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4092 | Log.v(TAG, "getAccounts: accountType " + type |
| 4093 | + ", caller's uid " + Binder.getCallingUid() |
| 4094 | + ", pid " + Binder.getCallingPid()); |
| 4095 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4096 | // If the original calling app was using the framework account chooser activity, we'll |
| 4097 | // be passed in the original caller's uid here, which is what should be used for filtering. |
| 4098 | if (packageUid != -1 && UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 4099 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4100 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4101 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4102 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4103 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4104 | if (visibleAccountTypes.isEmpty() |
| 4105 | || (type != null && !visibleAccountTypes.contains(type))) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4106 | return new Account[0]; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4107 | } else if (visibleAccountTypes.contains(type)) { |
| 4108 | // Prune the list down to just the requested type. |
| 4109 | visibleAccountTypes = new ArrayList<>(); |
| 4110 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4111 | } // else aggregate all the visible accounts (it won't matter if the |
| 4112 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4113 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4114 | long identityToken = clearCallingIdentity(); |
| 4115 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4116 | UserAccounts accounts = getUserAccounts(userId); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4117 | Account[] accountsToReturn = getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4118 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4119 | callingUid, |
| 4120 | callingPackage, |
| 4121 | visibleAccountTypes); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4122 | ArrayList<Account> accountsToReturnList = new |
| 4123 | ArrayList<Account>(Arrays.asList(accountsToReturn)); |
| 4124 | for(int i = accountsToReturnList.size() - 1; i >= 0 ; i--) { |
| 4125 | // if account not visible to caller or managed by caller, remove from |
| 4126 | // accounts to return. Note that all accounts visible by default unless |
| 4127 | // visible list functionality implemented |
| 4128 | if(!(isAccountVisible(accountsToReturnList.get(i), callingUid, |
| 4129 | getUserAccounts(userId)))) { |
| 4130 | accountsToReturnList.remove(i); |
| 4131 | } |
| 4132 | } |
| 4133 | return accountsToReturnList.toArray(new Account[accountsToReturnList.size()]); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4134 | } finally { |
| 4135 | restoreCallingIdentity(identityToken); |
| 4136 | } |
| 4137 | } |
| 4138 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4139 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4140 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4141 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4142 | int callingUid, |
| 4143 | String callingPackage, |
| 4144 | List<String> visibleAccountTypes) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4145 | synchronized (userAccounts.cacheLock) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4146 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4147 | for (String visibleType : visibleAccountTypes) { |
| 4148 | Account[] accountsForType = getAccountsFromCacheLocked( |
| 4149 | userAccounts, visibleType, callingUid, callingPackage); |
| 4150 | if (accountsForType != null) { |
| 4151 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
| 4152 | } |
| 4153 | } |
| 4154 | Account[] result = new Account[visibleAccounts.size()]; |
| 4155 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4156 | result[i] = visibleAccounts.get(i); |
| 4157 | } |
| 4158 | return result; |
| 4159 | } |
| 4160 | } |
| 4161 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4162 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4163 | public void addSharedAccountsFromParentUser(int parentUserId, int userId) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4164 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4165 | Account[] accounts = getAccountsAsUser(null, parentUserId, mContext.getOpPackageName()); |
| 4166 | for (Account account : accounts) { |
| 4167 | addSharedAccountAsUser(account, userId); |
| 4168 | } |
| 4169 | } |
| 4170 | |
| 4171 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4172 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4173 | UserAccounts accounts = getUserAccounts(userId); |
| 4174 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4175 | DeDatabaseHelper.deleteSharedAccount(db, account); |
| 4176 | long accountId = DeDatabaseHelper.insertSharedAccount(db, account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4177 | if (accountId < 0) { |
| 4178 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4179 | + ", skipping the DB insert failed"); |
| 4180 | return false; |
| 4181 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4182 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4183 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4184 | return true; |
| 4185 | } |
| 4186 | |
| 4187 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4188 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4189 | userId = handleIncomingUser(userId); |
| 4190 | UserAccounts accounts = getUserAccounts(userId); |
| 4191 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4192 | long sharedTableAccountId = DeDatabaseHelper.findSharedAccountId(db, account); |
| 4193 | int r = DeDatabaseHelper.renameSharedAccount(db, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4194 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4195 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4196 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4197 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4198 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4199 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4200 | } |
| 4201 | return r > 0; |
| 4202 | } |
| 4203 | |
| 4204 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4205 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4206 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4207 | } |
| 4208 | |
| 4209 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4210 | userId = handleIncomingUser(userId); |
| 4211 | UserAccounts accounts = getUserAccounts(userId); |
| 4212 | SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4213 | long sharedTableAccountId = DeDatabaseHelper.findSharedAccountId(db, account); |
| 4214 | boolean deleted = DeDatabaseHelper.deleteSharedAccount(db, account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4215 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4216 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4217 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4218 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4219 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4220 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | @Override |
| 4224 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4225 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4226 | SQLiteDatabase db = getUserAccounts(userId).openHelper.getReadableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4227 | List<Account> accountList = DeDatabaseHelper.getSharedAccounts(db); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4228 | Account[] accountArray = new Account[accountList.size()]; |
| 4229 | accountList.toArray(accountArray); |
| 4230 | return accountArray; |
| 4231 | } |
| 4232 | |
| 4233 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4234 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4235 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4236 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4237 | } |
| 4238 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4239 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4240 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4241 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4242 | int callingUid = Binder.getCallingUid(); |
| 4243 | if (!UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 4244 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4245 | + callingUid + " with uid=" + uid); |
| 4246 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4247 | return getAccountsAsUser(null, UserHandle.getCallingUserId(), packageName, uid, |
| 4248 | opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4251 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4252 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4253 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4254 | String opPackageName) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4255 | int packageUid = -1; |
| 4256 | try { |
| 4257 | packageUid = AppGlobals.getPackageManager().getPackageUid( |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 4258 | packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 4259 | UserHandle.getCallingUserId()); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4260 | } catch (RemoteException re) { |
| 4261 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
| 4262 | return new Account[0]; |
| 4263 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4264 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), packageName, |
| 4265 | packageUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4266 | } |
| 4267 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4268 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4269 | public void getAccountsByFeatures( |
| 4270 | IAccountManagerResponse response, |
| 4271 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4272 | String[] features, |
| 4273 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4274 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4275 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4276 | Log.v(TAG, "getAccounts: accountType " + type |
| 4277 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4278 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4279 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4280 | + ", pid " + Binder.getCallingPid()); |
| 4281 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4282 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4283 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4284 | int userId = UserHandle.getCallingUserId(); |
| 4285 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4286 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4287 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4288 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4289 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4290 | // Need to return just the accounts that are from matching signatures. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4291 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, new Account[0]); |
| 4292 | try { |
| 4293 | response.onResult(result); |
| 4294 | } catch (RemoteException e) { |
| 4295 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4296 | } |
| 4297 | return; |
| 4298 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4299 | long identityToken = clearCallingIdentity(); |
| 4300 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4301 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4302 | if (features == null || features.length == 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4303 | Account[] accounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4304 | synchronized (userAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4305 | accounts = getAccountsFromCacheLocked(userAccounts, type, callingUid, null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4306 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4307 | Bundle result = new Bundle(); |
| 4308 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4309 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4310 | return; |
| 4311 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4312 | new GetAccountsByTypeAndFeatureSession( |
| 4313 | userAccounts, |
| 4314 | response, |
| 4315 | type, |
| 4316 | features, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4317 | callingUid).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4318 | } finally { |
| 4319 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4320 | } |
| 4321 | } |
| 4322 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4323 | @Override |
| 4324 | public void onAccountAccessed(String token) throws RemoteException { |
| 4325 | final int uid = Binder.getCallingUid(); |
| 4326 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4327 | return; |
| 4328 | } |
| 4329 | final int userId = UserHandle.getCallingUserId(); |
| 4330 | final long identity = Binder.clearCallingIdentity(); |
| 4331 | try { |
| 4332 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4333 | if (Objects.equals(account.getAccessId(), token)) { |
| 4334 | // An app just accessed the account. At this point it knows about |
| 4335 | // it and there is not need to hide this account from the app. |
| 4336 | if (!hasAccountAccess(account, null, uid)) { |
| 4337 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4338 | uid, true); |
| 4339 | } |
| 4340 | } |
| 4341 | } |
| 4342 | } finally { |
| 4343 | Binder.restoreCallingIdentity(identity); |
| 4344 | } |
| 4345 | } |
| 4346 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4347 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4348 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4349 | IAccountManagerResponse mResponse; |
| 4350 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4351 | final boolean mExpectActivityLaunch; |
| 4352 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4353 | final String mAccountName; |
| 4354 | // Indicates if we need to add auth details(like last credential time) |
| 4355 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4356 | // If set, we need to update the last authenticated time. This is |
| 4357 | // currently |
| 4358 | // used on |
| 4359 | // successful confirming credentials. |
| 4360 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4361 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4362 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4363 | private int mNumRequestContinued = 0; |
| 4364 | private int mNumErrors = 0; |
| 4365 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4366 | IAccountAuthenticator mAuthenticator = null; |
| 4367 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4368 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4369 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4370 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4371 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4372 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4373 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4374 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4375 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4376 | } |
| 4377 | |
| 4378 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4379 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4380 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4381 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4382 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4383 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4384 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4385 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4386 | mResponse = response; |
| 4387 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4388 | mExpectActivityLaunch = expectActivityLaunch; |
| 4389 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4390 | mAccountName = accountName; |
| 4391 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4392 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4393 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4394 | synchronized (mSessions) { |
| 4395 | mSessions.put(toString(), this); |
| 4396 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4397 | if (response != null) { |
| 4398 | try { |
| 4399 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4400 | } catch (RemoteException e) { |
| 4401 | mResponse = null; |
| 4402 | binderDied(); |
| 4403 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4404 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4405 | } |
| 4406 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4407 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4408 | if (mResponse == null) { |
| 4409 | // this session has already been closed |
| 4410 | return null; |
| 4411 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4412 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4413 | 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] | 4414 | return response; |
| 4415 | } |
| 4416 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4417 | /** |
| 4418 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4419 | * security policy. |
| 4420 | * |
| 4421 | * In particular we want to make sure that the Authenticator doesn't try to trick users |
| 4422 | * into launching aribtrary intents on the device via by tricking to click authenticator |
| 4423 | * supplied entries in the system Settings app. |
| 4424 | */ |
| 4425 | protected void checkKeyIntent( |
| 4426 | int authUid, |
| 4427 | Intent intent) throws SecurityException { |
| 4428 | long bid = Binder.clearCallingIdentity(); |
| 4429 | try { |
| 4430 | PackageManager pm = mContext.getPackageManager(); |
| 4431 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 4432 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4433 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Sandra Kwan | 0e961a1 | 2016-06-30 14:34:01 -0700 | [diff] [blame] | 4434 | if (!GrantCredentialsPermissionActivity.class.getName().equals( |
| 4435 | targetActivityInfo.getClass().getName()) |
| 4436 | && !CantAddAccountActivity.class |
| 4437 | .equals(targetActivityInfo.getClass().getName()) |
| 4438 | && PackageManager.SIGNATURE_MATCH != pm.checkSignatures(authUid, |
| 4439 | targetUid)) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4440 | String pkgName = targetActivityInfo.packageName; |
| 4441 | String activityName = targetActivityInfo.name; |
| 4442 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4443 | + "does not share a signature with the supplying authenticator (%s)."; |
| 4444 | throw new SecurityException( |
| 4445 | String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4446 | } |
| 4447 | } finally { |
| 4448 | Binder.restoreCallingIdentity(bid); |
| 4449 | } |
| 4450 | } |
| 4451 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4452 | private void close() { |
| 4453 | synchronized (mSessions) { |
| 4454 | if (mSessions.remove(toString()) == null) { |
| 4455 | // the session was already closed, so bail out now |
| 4456 | return; |
| 4457 | } |
| 4458 | } |
| 4459 | if (mResponse != null) { |
| 4460 | // stop listening for response deaths |
| 4461 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4462 | |
| 4463 | // clear this so that we don't accidentally send any further results |
| 4464 | mResponse = null; |
| 4465 | } |
| 4466 | cancelTimeout(); |
| 4467 | unbind(); |
| 4468 | } |
| 4469 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4470 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4471 | public void binderDied() { |
| 4472 | mResponse = null; |
| 4473 | close(); |
| 4474 | } |
| 4475 | |
| 4476 | protected String toDebugString() { |
| 4477 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4478 | } |
| 4479 | |
| 4480 | protected String toDebugString(long now) { |
| 4481 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4482 | + ", connected " + (mAuthenticator != null) |
| 4483 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4484 | + "/" + mNumErrors + ")" |
| 4485 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4486 | } |
| 4487 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4488 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4489 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4490 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4491 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4492 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4493 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4494 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | private void unbind() { |
| 4499 | if (mAuthenticator != null) { |
| 4500 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4501 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4502 | } |
| 4503 | } |
| 4504 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4505 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4506 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4507 | } |
| 4508 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4509 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4510 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4511 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4512 | try { |
| 4513 | run(); |
| 4514 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4515 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4516 | "remote exception"); |
| 4517 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4518 | } |
| 4519 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4520 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4521 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4522 | mAuthenticator = null; |
| 4523 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4524 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4525 | try { |
| 4526 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4527 | "disconnected"); |
| 4528 | } catch (RemoteException e) { |
| 4529 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4530 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4531 | + "caught RemoteException while responding", e); |
| 4532 | } |
| 4533 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4537 | public abstract void run() throws RemoteException; |
| 4538 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4539 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4540 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4541 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4542 | try { |
| 4543 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4544 | "timeout"); |
| 4545 | } catch (RemoteException e) { |
| 4546 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4547 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4548 | e); |
| 4549 | } |
| 4550 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4551 | } |
| 4552 | } |
| 4553 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4554 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4555 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4556 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4557 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4558 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4559 | if (result != null) { |
| 4560 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4561 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4562 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4563 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4564 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4565 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4566 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4567 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4568 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4569 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4570 | if (needUpdate || mAuthDetailsRequired) { |
| 4571 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4572 | if (needUpdate && accountPresent) { |
| 4573 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4574 | } |
| 4575 | if (mAuthDetailsRequired) { |
| 4576 | long lastAuthenticatedTime = -1; |
| 4577 | if (accountPresent) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4578 | lastAuthenticatedTime = DeDatabaseHelper |
| 4579 | .findAccountLastAuthenticatedTime( |
| 4580 | mAccounts.openHelper.getReadableDatabase(), |
| 4581 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4582 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4583 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4584 | lastAuthenticatedTime); |
| 4585 | } |
| 4586 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4587 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4588 | if (result != null |
| 4589 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4590 | checkKeyIntent( |
| 4591 | Binder.getCallingUid(), |
| 4592 | intent); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4593 | } |
| 4594 | if (result != null |
| 4595 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4596 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4597 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4598 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4599 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4600 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4601 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4602 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4603 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4604 | IAccountManagerResponse response; |
| 4605 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4606 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4607 | response = mResponse; |
| 4608 | } else { |
| 4609 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4610 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4611 | if (response != null) { |
| 4612 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4613 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4614 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4615 | Log.v(TAG, getClass().getSimpleName() |
| 4616 | + " calling onError() on response " + response); |
| 4617 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4618 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4619 | "null bundle returned"); |
| 4620 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4621 | if (mStripAuthTokenFromResult) { |
| 4622 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4623 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4624 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4625 | Log.v(TAG, getClass().getSimpleName() |
| 4626 | + " calling onResult() on response " + response); |
| 4627 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4628 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4629 | (intent == null)) { |
| 4630 | // All AccountManager error codes are greater than 0 |
| 4631 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4632 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4633 | } else { |
| 4634 | response.onResult(result); |
| 4635 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4636 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4637 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4638 | // if the caller is dead then there is no one to care about remote exceptions |
| 4639 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4640 | Log.v(TAG, "failure while notifying response", e); |
| 4641 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4642 | } |
| 4643 | } |
| 4644 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4645 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4646 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4647 | public void onRequestContinued() { |
| 4648 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4649 | } |
| 4650 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4651 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4652 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4653 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4654 | IAccountManagerResponse response = getResponseAndClose(); |
| 4655 | if (response != null) { |
| 4656 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4657 | Log.v(TAG, getClass().getSimpleName() |
| 4658 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4659 | } |
| 4660 | try { |
| 4661 | response.onError(errorCode, errorMessage); |
| 4662 | } catch (RemoteException e) { |
| 4663 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4664 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4665 | } |
| 4666 | } |
| 4667 | } else { |
| 4668 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4669 | Log.v(TAG, "Session.onError: already closed"); |
| 4670 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4671 | } |
| 4672 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4673 | |
| 4674 | /** |
| 4675 | * find the component name for the authenticator and initiate a bind |
| 4676 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4677 | */ |
| 4678 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4679 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4680 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4681 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4682 | if (authenticatorInfo == null) { |
| 4683 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4684 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 4685 | + ", bailing out"); |
| 4686 | } |
| 4687 | return false; |
| 4688 | } |
| 4689 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 4690 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 4691 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 4692 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 4693 | + " which isn't encryption aware"); |
| 4694 | return false; |
| 4695 | } |
| 4696 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4697 | Intent intent = new Intent(); |
| 4698 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 4699 | intent.setComponent(authenticatorInfo.componentName); |
| 4700 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4701 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 4702 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 4703 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4704 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4705 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4706 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 4707 | } |
| 4708 | return false; |
| 4709 | } |
| 4710 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4711 | return true; |
| 4712 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 4715 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4716 | MessageHandler(Looper looper) { |
| 4717 | super(looper); |
| 4718 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4719 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4720 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4721 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4722 | switch (msg.what) { |
| 4723 | case MESSAGE_TIMED_OUT: |
| 4724 | Session session = (Session)msg.obj; |
| 4725 | session.onTimedOut(); |
| 4726 | break; |
| 4727 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4728 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 4729 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4730 | break; |
| 4731 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4732 | default: |
| 4733 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 4734 | } |
| 4735 | } |
| 4736 | } |
| 4737 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4738 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4739 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4740 | } |
| 4741 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4742 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4743 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4744 | } |
| 4745 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4746 | /* |
| 4747 | * This function receives an opened writable database. |
| 4748 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4749 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4750 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4751 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4755 | * This function receives an opened writable database and writes to it in a separate thread. |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4756 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4757 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4758 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4759 | |
| 4760 | class LogRecordTask implements Runnable { |
| 4761 | private final String action; |
| 4762 | private final String tableName; |
| 4763 | private final long accountId; |
| 4764 | private final UserAccounts userAccount; |
| 4765 | private final int callingUid; |
| 4766 | private final long userDebugDbInsertionPoint; |
| 4767 | |
| 4768 | LogRecordTask(final String action, |
| 4769 | final String tableName, |
| 4770 | final long accountId, |
| 4771 | final UserAccounts userAccount, |
| 4772 | final int callingUid, |
| 4773 | final long userDebugDbInsertionPoint) { |
| 4774 | this.action = action; |
| 4775 | this.tableName = tableName; |
| 4776 | this.accountId = accountId; |
| 4777 | this.userAccount = userAccount; |
| 4778 | this.callingUid = callingUid; |
| 4779 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 4780 | } |
| 4781 | |
| 4782 | public void run() { |
| 4783 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 4784 | logStatement.bindLong(1, accountId); |
| 4785 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4786 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4787 | logStatement.bindLong(4, callingUid); |
| 4788 | logStatement.bindString(5, tableName); |
| 4789 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 4790 | logStatement.execute(); |
| 4791 | logStatement.clearBindings(); |
| 4792 | } |
| 4793 | } |
| 4794 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4795 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 4796 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4797 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
| 4798 | % MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4799 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | /* |
| 4803 | * This should only be called once to compile the sql statement for logging |
| 4804 | * and to find the insertion point. |
| 4805 | */ |
| 4806 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(SQLiteDatabase db, |
| 4807 | UserAccounts userAccount) { |
| 4808 | // Initialize the count if not done earlier. |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4809 | int size = DebugDbHelper.getDebugTableRowCount(db); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4810 | if (size >= MAX_DEBUG_DB_SIZE) { |
| 4811 | // Table is full, and we need to find the point where to insert. |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4812 | userAccount.debugDbInsertionPoint = DebugDbHelper.getDebugTableInsertionPoint(db); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4813 | } else { |
| 4814 | userAccount.debugDbInsertionPoint = size; |
| 4815 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4816 | userAccount.statementForLogging = DebugDbHelper.compileSqlStatementForLogging(db); |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4817 | } |
| 4818 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4819 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4820 | return asBinder(); |
| 4821 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4822 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4823 | /** |
| 4824 | * Searches array of arguments for the specified string |
| 4825 | * @param args array of argument strings |
| 4826 | * @param value value to search for |
| 4827 | * @return true if the value is contained in the array |
| 4828 | */ |
| 4829 | private static boolean scanArgs(String[] args, String value) { |
| 4830 | if (args != null) { |
| 4831 | for (String arg : args) { |
| 4832 | if (value.equals(arg)) { |
| 4833 | return true; |
| 4834 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4835 | } |
| 4836 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4837 | return false; |
| 4838 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4839 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4840 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4841 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4842 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 4843 | != PackageManager.PERMISSION_GRANTED) { |
| 4844 | fout.println("Permission Denial: can't dump AccountsManager from from pid=" |
| 4845 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 4846 | + " without permission " + android.Manifest.permission.DUMP); |
| 4847 | return; |
| 4848 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4849 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4850 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4851 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4852 | final List<UserInfo> users = getUserManager().getUsers(); |
| 4853 | for (UserInfo user : users) { |
| 4854 | ipw.println("User " + user + ":"); |
| 4855 | ipw.increaseIndent(); |
| 4856 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 4857 | ipw.println(); |
| 4858 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4859 | } |
| 4860 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4861 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4862 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 4863 | String[] args, boolean isCheckinRequest) { |
| 4864 | synchronized (userAccounts.cacheLock) { |
| 4865 | final SQLiteDatabase db = userAccounts.openHelper.getReadableDatabase(); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4866 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4867 | if (isCheckinRequest) { |
| 4868 | // This is a checkin request. *Only* upload the account types and the count of each. |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4869 | DeDatabaseHelper.dumpAccountsTable(db, fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4870 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4871 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4872 | Process.myUid(), null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4873 | fout.println("Accounts: " + accounts.length); |
| 4874 | for (Account account : accounts) { |
| 4875 | fout.println(" " + account); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4876 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 4877 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4878 | // Add debug information. |
| 4879 | fout.println(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4880 | DebugDbHelper.dumpDebugTable(db, fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4881 | fout.println(); |
| 4882 | synchronized (mSessions) { |
| 4883 | final long now = SystemClock.elapsedRealtime(); |
| 4884 | fout.println("Active Sessions: " + mSessions.size()); |
| 4885 | for (Session session : mSessions.values()) { |
| 4886 | fout.println(" " + session.toDebugString(now)); |
| 4887 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4888 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4889 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4890 | fout.println(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4891 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4892 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4893 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4894 | } |
| 4895 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4896 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4897 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4898 | long identityToken = clearCallingIdentity(); |
| 4899 | try { |
| 4900 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4901 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 4902 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4903 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4904 | if (intent.getComponent() != null && |
| 4905 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 4906 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4907 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4908 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4909 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4910 | final Integer notificationId = getSigninRequiredNotificationId(accounts, account); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4911 | intent.addCategory(String.valueOf(notificationId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4912 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4913 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4914 | contextForUser.getText(R.string.notification_title).toString(); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4915 | Notification n = new Notification.Builder(contextForUser) |
| 4916 | .setWhen(0) |
| 4917 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 4918 | .setColor(contextForUser.getColor( |
| 4919 | com.android.internal.R.color.system_notification_accent_color)) |
| 4920 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 4921 | .setContentText(message) |
| 4922 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 4923 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4924 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4925 | .build(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4926 | installNotification(notificationId, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4927 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4928 | } finally { |
| 4929 | restoreCallingIdentity(identityToken); |
| 4930 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4931 | } |
| 4932 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4933 | private void installNotification(int notificationId, final Notification notification, |
| 4934 | String packageName, int userId) { |
| 4935 | final long token = clearCallingIdentity(); |
| 4936 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 4937 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4938 | try { |
| 4939 | notificationManager.enqueueNotificationWithTag(packageName, packageName, null, |
| 4940 | notificationId, notification, new int[1], userId); |
| 4941 | } catch (RemoteException e) { |
| 4942 | /* ignore - local call */ |
| 4943 | } |
| 4944 | } finally { |
| 4945 | Binder.restoreCallingIdentity(token); |
| 4946 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4947 | } |
| 4948 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 4949 | private void cancelNotification(int id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4950 | cancelNotification(id, mContext.getPackageName(), user); |
| 4951 | } |
| 4952 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 4953 | private void cancelNotification(int id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4954 | long identityToken = clearCallingIdentity(); |
| 4955 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 4956 | INotificationManager service = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4957 | service.cancelNotificationWithTag(packageName, null, id, user.getIdentifier()); |
| 4958 | } catch (RemoteException e) { |
| 4959 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4960 | } finally { |
| 4961 | restoreCallingIdentity(identityToken); |
| 4962 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4963 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4964 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 4965 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 4966 | for (String perm : permissions) { |
| 4967 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 4968 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4969 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 4970 | } |
| 4971 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 4972 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 4973 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 4974 | return true; |
| 4975 | } |
| 4976 | } |
| 4977 | } |
| 4978 | return false; |
| 4979 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4980 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4981 | private int handleIncomingUser(int userId) { |
| 4982 | try { |
| 4983 | return ActivityManagerNative.getDefault().handleIncomingUser( |
| 4984 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 4985 | } catch (RemoteException re) { |
| 4986 | // Shouldn't happen, local. |
| 4987 | } |
| 4988 | return userId; |
| 4989 | } |
| 4990 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 4991 | private boolean isPrivileged(int callingUid) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4992 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 4993 | |
| 4994 | final PackageManager userPackageManager; |
| 4995 | try { |
| 4996 | userPackageManager = mContext.createPackageContextAsUser( |
| 4997 | "android", 0, new UserHandle(callingUserId)).getPackageManager(); |
| 4998 | } catch (NameNotFoundException e) { |
| 4999 | return false; |
| 5000 | } |
| 5001 | |
| 5002 | String[] packages = userPackageManager.getPackagesForUid(callingUid); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5003 | for (String name : packages) { |
| 5004 | try { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5005 | PackageInfo packageInfo = userPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5006 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 5007 | && (packageInfo.applicationInfo.privateFlags |
| 5008 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5009 | return true; |
| 5010 | } |
| 5011 | } catch (PackageManager.NameNotFoundException e) { |
| 5012 | return false; |
| 5013 | } |
| 5014 | } |
| 5015 | return false; |
| 5016 | } |
| 5017 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5018 | private boolean permissionIsGranted( |
| 5019 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5020 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 5021 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5022 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 5023 | } |
| 5024 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5025 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5026 | |
| 5027 | if (isPrivileged(callerUid)) { |
| 5028 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5029 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5030 | + callerUid + " privileged"); |
| 5031 | } |
| 5032 | return true; |
| 5033 | } |
| 5034 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 5035 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5036 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5037 | + callerUid + " manages the account"); |
| 5038 | } |
| 5039 | return true; |
| 5040 | } |
| 5041 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5042 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5043 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5044 | + callerUid + " user granted access"); |
| 5045 | } |
| 5046 | return true; |
| 5047 | } |
| 5048 | |
| 5049 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5050 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5051 | } |
| 5052 | |
| 5053 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5054 | } |
| 5055 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5056 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5057 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5058 | if (accountType == null) { |
| 5059 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5060 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5061 | return getTypesVisibleToCaller(callingUid, userId, |
| 5062 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5063 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5067 | if (accountType == null) { |
| 5068 | return false; |
| 5069 | } else { |
| 5070 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5071 | } |
| 5072 | } |
| 5073 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5074 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5075 | String opPackageName) { |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5076 | boolean isPermitted = |
| 5077 | isPermitted(opPackageName, callingUid, Manifest.permission.GET_ACCOUNTS, |
| 5078 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5079 | return getTypesForCaller(callingUid, userId, isPermitted); |
| 5080 | } |
| 5081 | |
| 5082 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
| 5083 | return getTypesForCaller(callingUid, userId, false); |
| 5084 | } |
| 5085 | |
| 5086 | private List<String> getTypesForCaller( |
| 5087 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5088 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5089 | long identityToken = Binder.clearCallingIdentity(); |
| 5090 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5091 | try { |
| 5092 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5093 | } finally { |
| 5094 | Binder.restoreCallingIdentity(identityToken); |
| 5095 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5096 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5097 | serviceInfos) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5098 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 5099 | if (isOtherwisePermitted || sigChk == PackageManager.SIGNATURE_MATCH) { |
| 5100 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5101 | } |
| 5102 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5103 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5104 | } |
| 5105 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5106 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5107 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5108 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5109 | if (account.name.equals(accountName)) { |
| 5110 | return true; |
| 5111 | } |
| 5112 | } |
| 5113 | } |
| 5114 | return false; |
| 5115 | } |
| 5116 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5117 | private static void checkManageUsersPermission(String message) { |
| 5118 | if (ActivityManager.checkComponentPermission( |
| 5119 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5120 | != PackageManager.PERMISSION_GRANTED) { |
| 5121 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5122 | } |
| 5123 | } |
| 5124 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5125 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5126 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5127 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5128 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5129 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5130 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5131 | + message); |
| 5132 | } |
| 5133 | } |
| 5134 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5135 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5136 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5137 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5138 | return true; |
| 5139 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5140 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5141 | synchronized (accounts.cacheLock) { |
| 5142 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5143 | long grantsCount; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5144 | if (authTokenType != null) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5145 | grantsCount = DeDatabaseHelper.findMatchingGrantsCount(db, callerUid, authTokenType, |
| 5146 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5147 | } else { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5148 | grantsCount = DeDatabaseHelper.findMatchingGrantsCountAnyToken(db, callerUid, |
| 5149 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5150 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5151 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5152 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5153 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5154 | // TODO: Skip this check when running automated tests. Replace this |
| 5155 | // with a more general solution. |
| 5156 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5157 | + authTokenType + " by uid " + callerUid |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5158 | + " but ignoring since device is in test harness."); |
| 5159 | return true; |
| 5160 | } |
| 5161 | return permissionGranted; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5162 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5163 | } |
| 5164 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5165 | private boolean isSystemUid(int callingUid) { |
| 5166 | String[] packages = null; |
| 5167 | long ident = Binder.clearCallingIdentity(); |
| 5168 | try { |
| 5169 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5170 | } finally { |
| 5171 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5172 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5173 | if (packages != null) { |
| 5174 | for (String name : packages) { |
| 5175 | try { |
| 5176 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5177 | if (packageInfo != null |
| 5178 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5179 | != 0) { |
| 5180 | return true; |
| 5181 | } |
| 5182 | } catch (PackageManager.NameNotFoundException e) { |
| 5183 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5184 | } |
| 5185 | } |
| 5186 | } else { |
| 5187 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5188 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5189 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5192 | /** Succeeds if any of the specified permissions are granted. */ |
| 5193 | private void checkReadAccountsPermitted( |
| 5194 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5195 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5196 | int userId, |
| 5197 | String opPackageName) { |
| 5198 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5199 | String msg = String.format( |
| 5200 | "caller uid %s cannot access %s accounts", |
| 5201 | callingUid, |
| 5202 | accountType); |
| 5203 | Log.w(TAG, " " + msg); |
| 5204 | throw new SecurityException(msg); |
| 5205 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5206 | } |
| 5207 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5208 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5209 | // the managing app can always modify accounts |
| 5210 | if (isProfileOwner(callingUid)) { |
| 5211 | return true; |
| 5212 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5213 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5214 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5215 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5216 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5217 | return true; |
| 5218 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5219 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5220 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5221 | // the managing app can always modify accounts |
| 5222 | if (isProfileOwner(callingUid)) { |
| 5223 | return true; |
| 5224 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5225 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5226 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5227 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5228 | if (typesArray == null) { |
| 5229 | return true; |
| 5230 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5231 | for (String forbiddenType : typesArray) { |
| 5232 | if (forbiddenType.equals(accountType)) { |
| 5233 | return false; |
| 5234 | } |
| 5235 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5236 | return true; |
| 5237 | } |
| 5238 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5239 | private boolean isProfileOwner(int uid) { |
| 5240 | final DevicePolicyManagerInternal dpmi = |
| 5241 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5242 | return (dpmi != null) |
| 5243 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5244 | } |
| 5245 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5246 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5247 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5248 | throws RemoteException { |
| 5249 | final int callingUid = getCallingUid(); |
| 5250 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5251 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5252 | throw new SecurityException(); |
| 5253 | } |
| 5254 | |
| 5255 | if (value) { |
| 5256 | grantAppPermission(account, authTokenType, uid); |
| 5257 | } else { |
| 5258 | revokeAppPermission(account, authTokenType, uid); |
| 5259 | } |
| 5260 | } |
| 5261 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5262 | /** |
| 5263 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5264 | * <p> |
| 5265 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5266 | * which is in the system. This means we don't need to protect it with permissions. |
| 5267 | * @hide |
| 5268 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5269 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5270 | if (account == null || authTokenType == null) { |
| 5271 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5272 | return; |
| 5273 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5274 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5275 | synchronized (accounts.cacheLock) { |
| 5276 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5277 | long accountId = DeDatabaseHelper.findAccountId(db, account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5278 | if (accountId >= 0) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5279 | DeDatabaseHelper.insertGrant(db, accountId, authTokenType, uid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5280 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5281 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5282 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5283 | |
| 5284 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5285 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5286 | |
| 5287 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5288 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5289 | : mAppPermissionChangeListeners) { |
| 5290 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5291 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5292 | } |
| 5293 | |
| 5294 | /** |
| 5295 | * Don't allow callers with the given uid permission to get credentials for |
| 5296 | * account/authTokenType. |
| 5297 | * <p> |
| 5298 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5299 | * which is in the system. This means we don't need to protect it with permissions. |
| 5300 | * @hide |
| 5301 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5302 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5303 | if (account == null || authTokenType == null) { |
| 5304 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5305 | return; |
| 5306 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5307 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5308 | synchronized (accounts.cacheLock) { |
| 5309 | final SQLiteDatabase db = accounts.openHelper.getWritableDatabase(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5310 | db.beginTransaction(); |
| 5311 | try { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5312 | long accountId = DeDatabaseHelper.findAccountId(db, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5313 | if (accountId >= 0) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5314 | DeDatabaseHelper.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5315 | db, accountId, authTokenType, uid); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5316 | db.setTransactionSuccessful(); |
| 5317 | } |
| 5318 | } finally { |
| 5319 | db.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5320 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5321 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5322 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5323 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5324 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5325 | |
| 5326 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5327 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5328 | : mAppPermissionChangeListeners) { |
| 5329 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5330 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5331 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5332 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5333 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5334 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5335 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5336 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5337 | for (Account curAccount : oldAccountsForType) { |
| 5338 | if (!curAccount.equals(account)) { |
| 5339 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5340 | } |
| 5341 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5342 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5343 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5344 | } else { |
| 5345 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5346 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5347 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5348 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5349 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5350 | accounts.userDataCache.remove(account); |
| 5351 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5352 | accounts.previousNameCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | /** |
| 5356 | * This assumes that the caller has already checked that the account is not already present. |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5357 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5358 | * processes and if you will return this account to apps you should return the result. |
| 5359 | * @return The inserted account which is a new instance that is being tracked. |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5360 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5361 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5362 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5363 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5364 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5365 | if (accountsForType != null) { |
| 5366 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5367 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5368 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5369 | : UUID.randomUUID().toString(); |
| 5370 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5371 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5372 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5373 | } |
| 5374 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5375 | private Account[] filterSharedAccounts(UserAccounts userAccounts, Account[] unfiltered, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5376 | int callingUid, String callingPackage) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5377 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5378 | || callingUid == Process.myUid()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5379 | return unfiltered; |
| 5380 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5381 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5382 | if (user != null && user.isRestricted()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5383 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5384 | // If any of the packages is a visible listed package, return the full set, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5385 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5386 | // This might be a temporary way to specify a visible list |
| 5387 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5388 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5389 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5390 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5391 | return unfiltered; |
| 5392 | } |
| 5393 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5394 | ArrayList<Account> allowed = new ArrayList<>(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5395 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
| 5396 | if (sharedAccounts == null || sharedAccounts.length == 0) return unfiltered; |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5397 | String requiredAccountType = ""; |
| 5398 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5399 | // If there's an explicit callingPackage specified, check if that package |
| 5400 | // opted in to see restricted accounts. |
| 5401 | if (callingPackage != null) { |
| 5402 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5403 | if (pi != null && pi.restrictedAccountType != null) { |
| 5404 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5405 | } |
| 5406 | } else { |
| 5407 | // Otherwise check if the callingUid has a package that has opted in |
| 5408 | for (String packageName : packages) { |
| 5409 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5410 | if (pi != null && pi.restrictedAccountType != null) { |
| 5411 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5412 | break; |
| 5413 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5414 | } |
| 5415 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5416 | } catch (NameNotFoundException nnfe) { |
| 5417 | } |
| 5418 | for (Account account : unfiltered) { |
| 5419 | if (account.type.equals(requiredAccountType)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5420 | allowed.add(account); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5421 | } else { |
| 5422 | boolean found = false; |
| 5423 | for (Account shared : sharedAccounts) { |
| 5424 | if (shared.equals(account)) { |
| 5425 | found = true; |
| 5426 | break; |
| 5427 | } |
| 5428 | } |
| 5429 | if (!found) { |
| 5430 | allowed.add(account); |
| 5431 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5432 | } |
| 5433 | } |
| 5434 | Account[] filtered = new Account[allowed.size()]; |
| 5435 | allowed.toArray(filtered); |
| 5436 | return filtered; |
| 5437 | } else { |
| 5438 | return unfiltered; |
| 5439 | } |
| 5440 | } |
| 5441 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5442 | /* |
| 5443 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5444 | * that the package is not allowed to access. |
| 5445 | */ |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5446 | protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5447 | int callingUid, String callingPackage) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5448 | if (accountType != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5449 | final Account[] accounts = userAccounts.accountCache.get(accountType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5450 | if (accounts == null) { |
| 5451 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5452 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5453 | return filterSharedAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5454 | callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5455 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5456 | } else { |
| 5457 | int totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5458 | for (Account[] accounts : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5459 | totalLength += accounts.length; |
| 5460 | } |
| 5461 | if (totalLength == 0) { |
| 5462 | return EMPTY_ACCOUNT_ARRAY; |
| 5463 | } |
| 5464 | Account[] accounts = new Account[totalLength]; |
| 5465 | totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5466 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5467 | System.arraycopy(accountsOfType, 0, accounts, totalLength, |
| 5468 | accountsOfType.length); |
| 5469 | totalLength += accountsOfType.length; |
| 5470 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5471 | return filterSharedAccounts(userAccounts, accounts, callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5472 | } |
| 5473 | } |
| 5474 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5475 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db, |
| 5476 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5477 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5478 | if (userDataForAccount == null) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5479 | userDataForAccount = CeDatabaseHelper.findUserExtrasForAccount(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5480 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5481 | } |
| 5482 | if (value == null) { |
| 5483 | userDataForAccount.remove(key); |
| 5484 | } else { |
| 5485 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5486 | } |
| 5487 | } |
| 5488 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5489 | protected String readCachedTokenInternal( |
| 5490 | UserAccounts accounts, |
| 5491 | Account account, |
| 5492 | String tokenType, |
| 5493 | String callingPackage, |
| 5494 | byte[] pkgSigDigest) { |
| 5495 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5496 | return accounts.accountTokenCaches.get( |
| 5497 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5498 | } |
| 5499 | } |
| 5500 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5501 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, final SQLiteDatabase db, |
| 5502 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5503 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5504 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5505 | authTokensForAccount = CeDatabaseHelper.findAuthTokensByAccount(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5506 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5507 | } |
| 5508 | if (value == null) { |
| 5509 | authTokensForAccount.remove(key); |
| 5510 | } else { |
| 5511 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5512 | } |
| 5513 | } |
| 5514 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5515 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 5516 | String authTokenType) { |
| 5517 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5518 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5519 | if (authTokensForAccount == null) { |
| 5520 | // need to populate the cache for this account |
Fyodor Kupolov | ef73aaa3 | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5521 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabaseUserIsUnlocked(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5522 | authTokensForAccount = CeDatabaseHelper.findAuthTokensByAccount(db, account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5523 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5524 | } |
| 5525 | return authTokensForAccount.get(authTokenType); |
| 5526 | } |
| 5527 | } |
| 5528 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5529 | protected String readUserDataInternalLocked( |
| 5530 | UserAccounts accounts, Account account, String key) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5531 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5532 | if (userDataForAccount == null) { |
| 5533 | // need to populate the cache for this account |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 5534 | final SQLiteDatabase db = accounts.openHelper.getReadableDatabaseUserIsUnlocked(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5535 | userDataForAccount = CeDatabaseHelper.findUserExtrasForAccount(db, account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5536 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5537 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5538 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5539 | } |
| 5540 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5541 | private Context getContextForUser(UserHandle user) { |
| 5542 | try { |
| 5543 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 5544 | } catch (NameNotFoundException e) { |
| 5545 | // Default to mContext, not finding the package system is running as is unlikely. |
| 5546 | return mContext; |
| 5547 | } |
| 5548 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 5549 | |
| 5550 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 5551 | try { |
| 5552 | response.onResult(result); |
| 5553 | } catch (RemoteException e) { |
| 5554 | // if the caller is dead then there is no one to care about remote |
| 5555 | // exceptions |
| 5556 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5557 | Log.v(TAG, "failure while notifying response", e); |
| 5558 | } |
| 5559 | } |
| 5560 | } |
| 5561 | |
| 5562 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 5563 | String errorMessage) { |
| 5564 | try { |
| 5565 | response.onError(errorCode, errorMessage); |
| 5566 | } catch (RemoteException e) { |
| 5567 | // if the caller is dead then there is no one to care about remote |
| 5568 | // exceptions |
| 5569 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5570 | Log.v(TAG, "failure while notifying response", e); |
| 5571 | } |
| 5572 | } |
| 5573 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5574 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5575 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5576 | private final Object mLock = new Object(); |
| 5577 | |
| 5578 | @GuardedBy("mLock") |
| 5579 | private AccountManagerBackupHelper mBackupHelper; |
| 5580 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5581 | @Override |
| 5582 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 5583 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 5584 | if (account == null) { |
| 5585 | Slog.w(TAG, "account cannot be null"); |
| 5586 | return; |
| 5587 | } |
| 5588 | if (packageName == null) { |
| 5589 | Slog.w(TAG, "packageName cannot be null"); |
| 5590 | return; |
| 5591 | } |
| 5592 | if (userId < UserHandle.USER_SYSTEM) { |
| 5593 | Slog.w(TAG, "user id must be concrete"); |
| 5594 | return; |
| 5595 | } |
| 5596 | if (callback == null) { |
| 5597 | Slog.w(TAG, "callback cannot be null"); |
| 5598 | return; |
| 5599 | } |
| 5600 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5601 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 5602 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5603 | Bundle result = new Bundle(); |
| 5604 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 5605 | callback.sendResult(result); |
| 5606 | return; |
| 5607 | } |
| 5608 | |
| 5609 | final int uid; |
| 5610 | try { |
| 5611 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 5612 | } catch (NameNotFoundException e) { |
| 5613 | Slog.e(TAG, "Unknown package " + packageName); |
| 5614 | return; |
| 5615 | } |
| 5616 | |
| 5617 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5618 | final UserAccounts userAccounts; |
| 5619 | synchronized (mUsers) { |
| 5620 | userAccounts = mUsers.get(userId); |
| 5621 | } |
| 5622 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 5623 | } |
| 5624 | |
| 5625 | @Override |
| 5626 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 5627 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5628 | mAppPermissionChangeListeners.add(listener); |
| 5629 | } |
| 5630 | |
| 5631 | @Override |
| 5632 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 5633 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5634 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5635 | |
| 5636 | @Override |
| 5637 | public byte[] backupAccountAccessPermissions(int userId) { |
| 5638 | synchronized (mLock) { |
| 5639 | if (mBackupHelper == null) { |
| 5640 | mBackupHelper = new AccountManagerBackupHelper( |
| 5641 | AccountManagerService.this, this); |
| 5642 | } |
| 5643 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 5644 | } |
| 5645 | } |
| 5646 | |
| 5647 | @Override |
| 5648 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 5649 | synchronized (mLock) { |
| 5650 | if (mBackupHelper == null) { |
| 5651 | mBackupHelper = new AccountManagerBackupHelper( |
| 5652 | AccountManagerService.this, this); |
| 5653 | } |
| 5654 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 5655 | } |
| 5656 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5657 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame^] | 5658 | |
| 5659 | @VisibleForTesting |
| 5660 | static class Injector { |
| 5661 | private final Context mContext; |
| 5662 | |
| 5663 | public Injector(Context context) { |
| 5664 | mContext = context; |
| 5665 | } |
| 5666 | |
| 5667 | Looper getMessageHandlerLooper() { |
| 5668 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 5669 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 5670 | serviceThread.start(); |
| 5671 | return serviceThread.getLooper(); |
| 5672 | } |
| 5673 | |
| 5674 | Context getContext() { |
| 5675 | return mContext; |
| 5676 | } |
| 5677 | |
| 5678 | void addLocalService(AccountManagerInternal service) { |
| 5679 | LocalServices.addService(AccountManagerInternal.class, service); |
| 5680 | } |
| 5681 | |
| 5682 | String getDeDatabaseName(int userId) { |
| 5683 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 5684 | AccountsDb.DE_DATABASE_NAME); |
| 5685 | return databaseFile.getPath(); |
| 5686 | } |
| 5687 | |
| 5688 | String getCeDatabaseName(int userId) { |
| 5689 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 5690 | AccountsDb.CE_DATABASE_NAME); |
| 5691 | return databaseFile.getPath(); |
| 5692 | } |
| 5693 | |
| 5694 | String getPreNDatabaseName(int userId) { |
| 5695 | File systemDir = Environment.getDataSystemDirectory(); |
| 5696 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 5697 | PRE_N_DATABASE_NAME); |
| 5698 | if (userId == 0) { |
| 5699 | // Migrate old file, if it exists, to the new location. |
| 5700 | // Make sure the new file doesn't already exist. A dummy file could have been |
| 5701 | // accidentally created in the old location, causing the new one to become corrupted |
| 5702 | // as well. |
| 5703 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 5704 | if (oldFile.exists() && !databaseFile.exists()) { |
| 5705 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 5706 | File userDir = Environment.getUserSystemDirectory(userId); |
| 5707 | if (!userDir.exists()) { |
| 5708 | if (!userDir.mkdirs()) { |
| 5709 | throw new IllegalStateException("User dir cannot be created: " + userDir); |
| 5710 | } |
| 5711 | } |
| 5712 | if (!oldFile.renameTo(databaseFile)) { |
| 5713 | throw new IllegalStateException("User dir cannot be migrated: " + databaseFile); |
| 5714 | } |
| 5715 | } |
| 5716 | } |
| 5717 | return databaseFile.getPath(); |
| 5718 | } |
| 5719 | |
| 5720 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 5721 | return new AccountAuthenticatorCache(mContext); |
| 5722 | } |
| 5723 | |
| 5724 | INotificationManager getNotificationManager() { |
| 5725 | return NotificationManager.getService(); |
| 5726 | } |
| 5727 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5728 | } |