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; |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 26 | import android.accounts.AccountManagerResponse; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 27 | import android.accounts.AuthenticatorDescription; |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 28 | import android.accounts.CantAddAccountActivity; |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 29 | import android.accounts.ChooseAccountActivity; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 30 | import android.accounts.GrantCredentialsPermissionActivity; |
| 31 | import android.accounts.IAccountAuthenticator; |
| 32 | import android.accounts.IAccountAuthenticatorResponse; |
| 33 | import android.accounts.IAccountManager; |
| 34 | import android.accounts.IAccountManagerResponse; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 35 | import android.annotation.IntRange; |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 36 | import android.annotation.NonNull; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 37 | import android.annotation.Nullable; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 38 | import android.app.ActivityManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 39 | import android.app.ActivityThread; |
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; |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 61 | import android.content.pm.PackageManagerInternal; |
| 62 | import android.content.pm.PackageParser; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 63 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 64 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 65 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 66 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 67 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 68 | import android.database.Cursor; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 69 | import android.database.sqlite.SQLiteStatement; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 70 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 71 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 72 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 73 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 74 | import android.os.IBinder; |
| 75 | import android.os.Looper; |
| 76 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 77 | import android.os.Parcel; |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 78 | import android.os.Parcelable; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 79 | import android.os.Process; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 80 | import android.os.RemoteCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 81 | import android.os.RemoteException; |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 82 | import android.os.ResultReceiver; |
| 83 | import android.os.ShellCallback; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 84 | import android.os.StrictMode; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 85 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 86 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 87 | import android.os.UserManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 88 | import android.text.TextUtils; |
| 89 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 90 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 91 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 92 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 93 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 94 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 95 | import com.android.internal.R; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 96 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 97 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 98 | import com.android.internal.content.PackageMonitor; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 99 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 100 | import com.android.internal.notification.SystemNotificationChannels; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 101 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 102 | import com.android.internal.util.DumpUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 103 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 104 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 105 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 106 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 107 | import com.android.server.SystemService; |
| 108 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 109 | import com.google.android.collect.Lists; |
| 110 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 111 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 112 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 113 | import java.io.FileDescriptor; |
| 114 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 115 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 116 | import java.security.MessageDigest; |
| 117 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 118 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 119 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 120 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 121 | import java.util.Collection; |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 122 | import java.util.Collections; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 123 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 124 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 125 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 126 | import java.util.LinkedHashMap; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 127 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 128 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 129 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 130 | import java.util.Objects; |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 131 | import java.util.Set; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 132 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 133 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 134 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 135 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 136 | /** |
| 137 | * A system service that provides account, password, and authtoken management for all |
| 138 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 139 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 140 | * 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] | 141 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 142 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 143 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 144 | public class AccountManagerService |
| 145 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 146 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 147 | private static final String TAG = "AccountManagerService"; |
| 148 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 149 | public static class Lifecycle extends SystemService { |
| 150 | private AccountManagerService mService; |
| 151 | |
| 152 | public Lifecycle(Context context) { |
| 153 | super(context); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void onStart() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 158 | mService = new AccountManagerService(new Injector(getContext())); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 159 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 160 | } |
| 161 | |
| 162 | @Override |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 163 | public void onUnlockUser(int userHandle) { |
| 164 | mService.onUnlockUser(userHandle); |
| 165 | } |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 166 | |
| 167 | @Override |
Fyodor Kupolov | ce25ed2 | 2017-05-04 11:44:31 -0700 | [diff] [blame] | 168 | public void onStopUser(int userHandle) { |
Fyodor Kupolov | 945c97e | 2017-06-21 17:45:19 -0700 | [diff] [blame] | 169 | Slog.i(TAG, "onStopUser " + userHandle); |
| 170 | mService.purgeUserData(userHandle); |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 171 | } |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 172 | } |
| 173 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 174 | final Context mContext; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 175 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 176 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 177 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 178 | private UserManager mUserManager; |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 179 | private final Injector mInjector; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 180 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 181 | final MessageHandler mHandler; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 182 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 183 | // Messages that can be sent on mHandler |
| 184 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 185 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 186 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 187 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 188 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 189 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 190 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 191 | private static final int SIGNATURE_CHECK_MISMATCH = 0; |
| 192 | private static final int SIGNATURE_CHECK_MATCH = 1; |
| 193 | private static final int SIGNATURE_CHECK_UID_MATCH = 2; |
| 194 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 195 | static { |
| 196 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
Christopher Tate | bded68f | 2017-02-21 11:41:55 -0800 | [diff] [blame] | 197 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 198 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 199 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 200 | |
| 201 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 202 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 203 | static class UserAccounts { |
| 204 | private final int userId; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 205 | final AccountsDb accountsDb; |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 206 | private final HashMap<Pair<Pair<Account, String>, Integer>, NotificationId> |
| 207 | credentialsPermissionNotificationIds = new HashMap<>(); |
| 208 | private final HashMap<Account, NotificationId> signinRequiredNotificationIds |
| 209 | = new HashMap<>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 210 | final Object cacheLock = new Object(); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 211 | final Object dbLock = new Object(); // if needed, dbLock must be obtained before cacheLock |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 212 | /** protected by the {@link #cacheLock} */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 213 | final HashMap<String, Account[]> accountCache = new LinkedHashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 214 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 215 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 216 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 217 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 218 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 219 | private final TokenCache accountTokenCaches = new TokenCache(); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 220 | /** protected by the {@link #cacheLock} */ |
| 221 | private final Map<Account, Map<String, Integer>> visibilityCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 222 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 223 | /** protected by the {@link #mReceiversForType}, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 224 | * type -> (packageName -> number of active receivers) |
| 225 | * type == null is used to get notifications about all account types |
| 226 | */ |
| 227 | private final Map<String, Map<String, Integer>> mReceiversForType = new HashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 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 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 244 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 245 | this.userId = userId; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 246 | synchronized (dbLock) { |
| 247 | synchronized (cacheLock) { |
| 248 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
| 249 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 254 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 255 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 256 | // Not thread-safe. Only use in synchronized context |
| 257 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 258 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 259 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 260 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 261 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 262 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 263 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 264 | /** |
| 265 | * This should only be called by system code. One should only call this after the service |
| 266 | * has started. |
| 267 | * @return a reference to the AccountManagerService instance |
| 268 | * @hide |
| 269 | */ |
| 270 | public static AccountManagerService getSingleton() { |
| 271 | return sThis.get(); |
| 272 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 273 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 274 | public AccountManagerService(Injector injector) { |
| 275 | mInjector = injector; |
| 276 | mContext = injector.getContext(); |
| 277 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 278 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 279 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 280 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 281 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 282 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 283 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 284 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 285 | IntentFilter intentFilter = new IntentFilter(); |
| 286 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 287 | intentFilter.addDataScheme("package"); |
| 288 | mContext.registerReceiver(new BroadcastReceiver() { |
| 289 | @Override |
| 290 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 291 | // Don't delete accounts when updating a authenticator's |
| 292 | // package. |
| 293 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 294 | /* Purging data requires file io, don't block the main thread. This is probably |
| 295 | * less than ideal because we are introducing a race condition where old grants |
| 296 | * could be exercised until they are purged. But that race condition existed |
| 297 | * anyway with the broadcast receiver. |
| 298 | * |
| 299 | * Ideally, we would completely clear the cache, purge data from the database, |
| 300 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 301 | * large at this point. |
| 302 | */ |
Dmitry Dementyev | 0b67642 | 2017-03-09 11:51:26 -0800 | [diff] [blame] | 303 | final String removedPackageName = intent.getData().getSchemeSpecificPart(); |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 304 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 305 | @Override |
| 306 | public void run() { |
| 307 | purgeOldGrantsAll(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 308 | // Notify authenticator about removed app? |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 309 | removeVisibilityValuesForPackage(removedPackageName); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 310 | } |
| 311 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 312 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 313 | } |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 314 | } |
| 315 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 316 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 317 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 318 | |
Fyodor Kupolov | 945c97e | 2017-06-21 17:45:19 -0700 | [diff] [blame] | 319 | IntentFilter userFilter = new IntentFilter(); |
| 320 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 321 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 322 | @Override |
| 323 | public void onReceive(Context context, Intent intent) { |
| 324 | String action = intent.getAction(); |
| 325 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 326 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 327 | if (userId < 1) return; |
| 328 | Slog.i(TAG, "User " + userId + " removed"); |
| 329 | purgeUserData(userId); |
| 330 | } |
| 331 | } |
| 332 | }, UserHandle.ALL, userFilter, null, null); |
| 333 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 334 | // Need to cancel account request notifications if the update/install can access the account |
| 335 | new PackageMonitor() { |
| 336 | @Override |
| 337 | public void onPackageAdded(String packageName, int uid) { |
| 338 | // Called on a handler, and running as the system |
| 339 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 344 | // Called on a handler, and running as the system |
| 345 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 346 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 347 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 348 | |
| 349 | // Cancel account request notification if an app op was preventing the account access |
| 350 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 351 | new AppOpsManager.OnOpChangedInternalListener() { |
| 352 | @Override |
| 353 | public void onOpChanged(int op, String packageName) { |
| 354 | try { |
| 355 | final int userId = ActivityManager.getCurrentUser(); |
| 356 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 357 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 358 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 359 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 360 | final long identity = Binder.clearCallingIdentity(); |
| 361 | try { |
| 362 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 363 | } finally { |
| 364 | Binder.restoreCallingIdentity(identity); |
| 365 | } |
| 366 | } |
| 367 | } catch (NameNotFoundException e) { |
| 368 | /* ignore */ |
| 369 | } |
| 370 | } |
| 371 | }); |
| 372 | |
| 373 | // Cancel account request notification if a permission was preventing the account access |
| 374 | mPackageManager.addOnPermissionsChangeListener( |
| 375 | (int uid) -> { |
| 376 | Account[] accounts = null; |
| 377 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 378 | if (packageNames != null) { |
| 379 | final int userId = UserHandle.getUserId(uid); |
| 380 | final long identity = Binder.clearCallingIdentity(); |
| 381 | try { |
| 382 | for (String packageName : packageNames) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 383 | // if app asked for permission we need to cancel notification even |
| 384 | // for O+ applications. |
| 385 | if (mPackageManager.checkPermission( |
| 386 | Manifest.permission.GET_ACCOUNTS, |
| 387 | packageName) != PackageManager.PERMISSION_GRANTED) { |
| 388 | continue; |
| 389 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 390 | |
| 391 | if (accounts == null) { |
| 392 | accounts = getAccountsAsUser(null, userId, "android"); |
| 393 | if (ArrayUtils.isEmpty(accounts)) { |
| 394 | return; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | for (Account account : accounts) { |
| 399 | cancelAccountAccessRequestNotificationIfNeeded( |
| 400 | account, uid, packageName, true); |
| 401 | } |
| 402 | } |
| 403 | } finally { |
| 404 | Binder.restoreCallingIdentity(identity); |
| 405 | } |
| 406 | } |
| 407 | }); |
| 408 | } |
| 409 | |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 410 | |
| 411 | boolean getBindInstantServiceAllowed(int userId) { |
| 412 | return mAuthenticatorCache.getBindInstantServiceAllowed(userId); |
| 413 | } |
| 414 | |
| 415 | void setBindInstantServiceAllowed(int userId, boolean allowed) { |
| 416 | mAuthenticatorCache.setBindInstantServiceAllowed(userId, allowed); |
| 417 | } |
| 418 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 419 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 420 | boolean checkAccess) { |
| 421 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 422 | for (Account account : accounts) { |
| 423 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 428 | boolean checkAccess) { |
| 429 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 430 | for (Account account : accounts) { |
| 431 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 436 | boolean checkAccess) { |
| 437 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 438 | if (packageNames != null) { |
| 439 | for (String packageName : packageNames) { |
| 440 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 441 | packageName, checkAccess); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 447 | int uid, String packageName, boolean checkAccess) { |
| 448 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 449 | UserHandle.getUserHandleForUid(uid))) { |
| 450 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 451 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 452 | UserHandle.getUserHandleForUid(uid)); |
| 453 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 456 | @Override |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 457 | public boolean addAccountExplicitlyWithVisibility(Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 458 | Bundle extras, Map packageToVisibility) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 459 | Bundle.setDefusable(extras, true); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 460 | int callingUid = Binder.getCallingUid(); |
| 461 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 462 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 463 | Log.v(TAG, "addAccountExplicitly: " + account + ", caller's uid " + callingUid |
| 464 | + ", pid " + Binder.getCallingPid()); |
| 465 | } |
| 466 | Preconditions.checkNotNull(account, "account cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 467 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
| 468 | String msg = String.format("uid %s cannot explicitly add accounts of type: %s", |
| 469 | callingUid, account.type); |
| 470 | throw new SecurityException(msg); |
| 471 | } |
| 472 | /* |
| 473 | * Child users are not allowed to add accounts. Only the accounts that are shared by the |
| 474 | * parent profile can be added to child profile. |
| 475 | * |
| 476 | * TODO: Only allow accounts that were shared to be added by a limited user. |
| 477 | */ |
| 478 | // fails if the account already exists |
| 479 | long identityToken = clearCallingIdentity(); |
| 480 | try { |
| 481 | UserAccounts accounts = getUserAccounts(userId); |
| 482 | return addAccountInternal(accounts, account, password, extras, callingUid, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 483 | (Map<String, Integer>) packageToVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 484 | } finally { |
| 485 | restoreCallingIdentity(identityToken); |
| 486 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | @Override |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 490 | public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 491 | String accountType) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 492 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 493 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 494 | boolean isSystemUid = UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 495 | List<String> managedTypes = getTypesForCaller(callingUid, userId, isSystemUid); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 496 | |
| 497 | if ((accountType != null && !managedTypes.contains(accountType)) |
| 498 | || (accountType == null && !isSystemUid)) { |
| 499 | throw new SecurityException( |
| 500 | "getAccountsAndVisibilityForPackage() called from unauthorized uid " |
| 501 | + callingUid + " with packageName=" + packageName); |
| 502 | } |
| 503 | if (accountType != null) { |
| 504 | managedTypes = new ArrayList<String>(); |
| 505 | managedTypes.add(accountType); |
| 506 | } |
| 507 | |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 508 | long identityToken = clearCallingIdentity(); |
| 509 | try { |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 510 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 511 | return getAccountsAndVisibilityForPackage(packageName, managedTypes, callingUid, |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 512 | accounts); |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 513 | } finally { |
| 514 | restoreCallingIdentity(identityToken); |
| 515 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* |
| 519 | * accountTypes may not be null |
| 520 | */ |
| 521 | private Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 522 | List<String> accountTypes, Integer callingUid, UserAccounts accounts) { |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 523 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 524 | Log.d(TAG, "Package not found " + packageName); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 525 | return new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 528 | Map<Account, Integer> result = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 529 | for (String accountType : accountTypes) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 530 | synchronized (accounts.dbLock) { |
| 531 | synchronized (accounts.cacheLock) { |
| 532 | final Account[] accountsOfType = accounts.accountCache.get(accountType); |
| 533 | if (accountsOfType != null) { |
| 534 | for (Account account : accountsOfType) { |
| 535 | result.put(account, |
| 536 | resolveAccountVisibility(account, packageName, accounts)); |
| 537 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | return filterSharedAccounts(accounts, result, callingUid, packageName); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | @Override |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 546 | public Map<String, Integer> getPackagesAndVisibilityForAccount(Account account) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 547 | Preconditions.checkNotNull(account, "account cannot be null"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 548 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 549 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 550 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
| 551 | && !isSystemUid(callingUid)) { |
| 552 | String msg = |
| 553 | String.format("uid %s cannot get secrets for account %s", callingUid, account); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 554 | throw new SecurityException(msg); |
| 555 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 556 | |
| 557 | long identityToken = clearCallingIdentity(); |
| 558 | try { |
| 559 | UserAccounts accounts = getUserAccounts(userId); |
| 560 | synchronized (accounts.dbLock) { |
| 561 | synchronized (accounts.cacheLock) { |
| 562 | return getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 563 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 564 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 565 | } finally { |
| 566 | restoreCallingIdentity(identityToken); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 567 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 568 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 572 | * Returns Map with all package names and visibility values for given account. |
| 573 | * The method and returned map must be guarded by accounts.cacheLock |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 574 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 575 | * @param account Account to get visibility values. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 576 | * @param accounts UserAccount that currently hosts the account and application |
| 577 | * |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 578 | * @return Map with cache for package names to visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 579 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 580 | private @NonNull Map<String, Integer> getPackagesAndVisibilityForAccountLocked(Account account, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 581 | UserAccounts accounts) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 582 | Map<String, Integer> accountVisibility = accounts.visibilityCache.get(account); |
| 583 | if (accountVisibility == null) { |
| 584 | Log.d(TAG, "Visibility was not initialized"); |
| 585 | accountVisibility = new HashMap<>(); |
| 586 | accounts.visibilityCache.put(account, accountVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 587 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 588 | return accountVisibility; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 592 | public int getAccountVisibility(Account account, String packageName) { |
| 593 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 594 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 595 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 596 | int userId = UserHandle.getCallingUserId(); |
| 597 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 598 | && !isSystemUid(callingUid)) { |
| 599 | String msg = String.format( |
| 600 | "uid %s cannot get secrets for accounts of type: %s", |
| 601 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 602 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 603 | throw new SecurityException(msg); |
| 604 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 605 | long identityToken = clearCallingIdentity(); |
| 606 | try { |
| 607 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | cbe1bd1 | 2017-04-25 17:02:47 -0700 | [diff] [blame] | 608 | if (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName)) { |
| 609 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
| 610 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 611 | return visibility; |
| 612 | } else { |
| 613 | return AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 614 | } |
| 615 | } |
| 616 | if (AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)) { |
| 617 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
| 618 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 619 | return visibility; |
| 620 | } else { |
| 621 | return AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 622 | } |
| 623 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 624 | return resolveAccountVisibility(account, packageName, accounts); |
| 625 | } finally { |
| 626 | restoreCallingIdentity(identityToken); |
| 627 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 631 | * Method returns visibility for given account and package name. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 632 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 633 | * @param account The account to check visibility. |
| 634 | * @param packageName Package name to check visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 635 | * @param accounts UserAccount that currently hosts the account and application |
| 636 | * |
| 637 | * @return Visibility value, AccountManager.VISIBILITY_UNDEFINED if no value was stored. |
| 638 | * |
| 639 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 640 | private int getAccountVisibilityFromCache(Account account, String packageName, |
| 641 | UserAccounts accounts) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 642 | synchronized (accounts.cacheLock) { |
| 643 | Map<String, Integer> accountVisibility = |
| 644 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 645 | Integer visibility = accountVisibility.get(packageName); |
| 646 | return visibility != null ? visibility : AccountManager.VISIBILITY_UNDEFINED; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Method which handles default values for Account visibility. |
| 652 | * |
| 653 | * @param account The account to check visibility. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 654 | * @param packageName Package name to check visibility |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 655 | * @param accounts UserAccount that currently hosts the account and application |
| 656 | * |
| 657 | * @return Visibility value, the method never returns AccountManager.VISIBILITY_UNDEFINED |
| 658 | * |
| 659 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 660 | private Integer resolveAccountVisibility(Account account, @NonNull String packageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 661 | UserAccounts accounts) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 662 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 663 | int uid = -1; |
| 664 | try { |
| 665 | long identityToken = clearCallingIdentity(); |
| 666 | try { |
| 667 | uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 668 | } finally { |
| 669 | restoreCallingIdentity(identityToken); |
| 670 | } |
| 671 | } catch (NameNotFoundException e) { |
| 672 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 673 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 674 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 675 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 676 | // System visibility can not be restricted. |
| 677 | if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) { |
| 678 | return AccountManager.VISIBILITY_VISIBLE; |
| 679 | } |
| 680 | |
| 681 | int signatureCheckResult = |
| 682 | checkPackageSignature(account.type, uid, accounts.userId); |
| 683 | |
| 684 | // Authenticator can not restrict visibility to itself. |
| 685 | if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) { |
| 686 | return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account |
| 687 | } |
| 688 | |
| 689 | // Return stored value if it was set. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 690 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 691 | |
| 692 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 693 | return visibility; |
| 694 | } |
| 695 | |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 696 | boolean isPrivileged = isPermittedForPackage(packageName, accounts.userId, |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 697 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 698 | |
| 699 | // Device/Profile owner gets visibility by default. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 700 | if (isProfileOwner(uid)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 701 | return AccountManager.VISIBILITY_VISIBLE; |
| 702 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 703 | |
| 704 | boolean preO = isPreOApplication(packageName); |
| 705 | if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH) |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 706 | || (preO && checkGetAccountsPermission(packageName, accounts.userId)) |
| 707 | || (checkReadContactsPermission(packageName, accounts.userId) |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 708 | && accountTypeManagesContacts(account.type, accounts.userId)) |
| 709 | || isPrivileged) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 710 | // Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature |
| 711 | // match. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 712 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 713 | AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 714 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 715 | visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 716 | } |
| 717 | } else { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 718 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 719 | AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 720 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 721 | visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 722 | } |
| 723 | } |
| 724 | return visibility; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Checks targetSdk for a package; |
| 729 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 730 | * @param packageName Package name |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 731 | * |
| 732 | * @return True if package's target SDK is below {@link android.os.Build.VERSION_CODES#O}, or |
| 733 | * undefined |
| 734 | */ |
| 735 | private boolean isPreOApplication(String packageName) { |
| 736 | try { |
| 737 | long identityToken = clearCallingIdentity(); |
| 738 | ApplicationInfo applicationInfo; |
| 739 | try { |
| 740 | applicationInfo = mPackageManager.getApplicationInfo(packageName, 0); |
| 741 | } finally { |
| 742 | restoreCallingIdentity(identityToken); |
| 743 | } |
| 744 | |
| 745 | if (applicationInfo != null) { |
| 746 | int version = applicationInfo.targetSdkVersion; |
| 747 | return version < android.os.Build.VERSION_CODES.O; |
| 748 | } |
| 749 | return true; |
| 750 | } catch (NameNotFoundException e) { |
| 751 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 752 | return true; |
| 753 | } |
Dmitry Dementyev | 58fa8362 | 2016-12-20 18:08:51 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 757 | public boolean setAccountVisibility(Account account, String packageName, int newVisibility) { |
| 758 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 759 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 760 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 761 | int userId = UserHandle.getCallingUserId(); |
| 762 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 763 | && !isSystemUid(callingUid)) { |
| 764 | String msg = String.format( |
| 765 | "uid %s cannot get secrets for accounts of type: %s", |
| 766 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 767 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 768 | throw new SecurityException(msg); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 769 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 770 | long identityToken = clearCallingIdentity(); |
| 771 | try { |
| 772 | UserAccounts accounts = getUserAccounts(userId); |
| 773 | return setAccountVisibility(account, packageName, newVisibility, true /* notify */, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 774 | accounts); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 775 | } finally { |
| 776 | restoreCallingIdentity(identityToken); |
| 777 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 778 | } |
| 779 | |
sunjian | 066aa5e | 2017-06-05 12:16:59 -0700 | [diff] [blame] | 780 | private boolean isVisible(int visibility) { |
| 781 | return visibility == AccountManager.VISIBILITY_VISIBLE || |
| 782 | visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 783 | } |
| 784 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 785 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 786 | * Updates visibility for given account name and package. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 787 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 788 | * @param account Account to update visibility. |
| 789 | * @param packageName Package name for which visibility is updated. |
| 790 | * @param newVisibility New visibility calue |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 791 | * @param notify if the flag is set applications will get notification about visibility change |
| 792 | * @param accounts UserAccount that currently hosts the account and application |
| 793 | * |
| 794 | * @return True if account visibility was changed. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 795 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 796 | private boolean setAccountVisibility(Account account, String packageName, int newVisibility, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 797 | boolean notify, UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 798 | synchronized (accounts.dbLock) { |
| 799 | synchronized (accounts.cacheLock) { |
| 800 | Map<String, Integer> packagesToVisibility; |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 801 | List<String> accountRemovedReceivers; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 802 | if (notify) { |
| 803 | if (isSpecialPackageKey(packageName)) { |
| 804 | packagesToVisibility = |
| 805 | getRequestingPackages(account, accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 806 | accountRemovedReceivers = getAccountRemovedReceivers(account, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 807 | } else { |
| 808 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 809 | return false; // package is not installed. |
| 810 | } |
| 811 | packagesToVisibility = new HashMap<>(); |
| 812 | packagesToVisibility.put(packageName, |
| 813 | resolveAccountVisibility(account, packageName, accounts)); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 814 | accountRemovedReceivers = new ArrayList<>(); |
| 815 | if (shouldNotifyPackageOnAccountRemoval(account, packageName, accounts)) { |
| 816 | accountRemovedReceivers.add(packageName); |
| 817 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 818 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 819 | } else { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 820 | // Notifications will not be send - only used during add account. |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 821 | if (!isSpecialPackageKey(packageName) && |
| 822 | !packageExistsForUser(packageName, accounts.userId)) { |
| 823 | // package is not installed and not meta value. |
| 824 | return false; |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 825 | } |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 826 | packagesToVisibility = Collections.emptyMap(); |
| 827 | accountRemovedReceivers = Collections.emptyList(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 828 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 829 | |
| 830 | if (!updateAccountVisibilityLocked(account, packageName, newVisibility, accounts)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 831 | return false; |
| 832 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 833 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 834 | if (notify) { |
| 835 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 836 | .entrySet()) { |
sunjian | 066aa5e | 2017-06-05 12:16:59 -0700 | [diff] [blame] | 837 | int oldVisibility = packageToVisibility.getValue(); |
| 838 | int currentVisibility = |
| 839 | resolveAccountVisibility(account, packageName, accounts); |
| 840 | if (isVisible(oldVisibility) != isVisible(currentVisibility)) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 841 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 842 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 843 | } |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 844 | for (String packageNameToNotify : accountRemovedReceivers) { |
| 845 | sendAccountRemovedBroadcast(account, packageNameToNotify, accounts.userId); |
| 846 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 847 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 848 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 849 | return true; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 850 | } |
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 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 854 | // Update account visibility in cache and database. |
| 855 | private boolean updateAccountVisibilityLocked(Account account, String packageName, |
| 856 | int newVisibility, UserAccounts accounts) { |
| 857 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 858 | if (accountId < 0) { |
| 859 | return false; |
| 860 | } |
| 861 | |
| 862 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
| 863 | try { |
| 864 | if (!accounts.accountsDb.setAccountVisibility(accountId, packageName, |
| 865 | newVisibility)) { |
| 866 | return false; |
| 867 | } |
| 868 | } finally { |
| 869 | StrictMode.setThreadPolicy(oldPolicy); |
| 870 | } |
| 871 | Map<String, Integer> accountVisibility = |
| 872 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 873 | accountVisibility.put(packageName, newVisibility); |
| 874 | return true; |
| 875 | } |
| 876 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 877 | @Override |
| 878 | public void registerAccountListener(String[] accountTypes, String opPackageName) { |
| 879 | int callingUid = Binder.getCallingUid(); |
| 880 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 881 | |
| 882 | int userId = UserHandle.getCallingUserId(); |
| 883 | long identityToken = clearCallingIdentity(); |
| 884 | try { |
| 885 | UserAccounts accounts = getUserAccounts(userId); |
| 886 | registerAccountListener(accountTypes, opPackageName, accounts); |
| 887 | } finally { |
| 888 | restoreCallingIdentity(identityToken); |
| 889 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 890 | } |
| 891 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 892 | private void registerAccountListener(String[] accountTypes, String opPackageName, |
| 893 | UserAccounts accounts) { |
| 894 | synchronized (accounts.mReceiversForType) { |
| 895 | if (accountTypes == null) { |
| 896 | // null for any type |
| 897 | accountTypes = new String[] {null}; |
| 898 | } |
| 899 | for (String type : accountTypes) { |
| 900 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 901 | if (receivers == null) { |
| 902 | receivers = new HashMap<>(); |
| 903 | accounts.mReceiversForType.put(type, receivers); |
| 904 | } |
| 905 | Integer cnt = receivers.get(opPackageName); |
| 906 | receivers.put(opPackageName, cnt != null ? cnt + 1 : 1); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 911 | @Override |
| 912 | public void unregisterAccountListener(String[] accountTypes, String opPackageName) { |
| 913 | int callingUid = Binder.getCallingUid(); |
| 914 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 915 | int userId = UserHandle.getCallingUserId(); |
| 916 | long identityToken = clearCallingIdentity(); |
| 917 | try { |
| 918 | UserAccounts accounts = getUserAccounts(userId); |
| 919 | unregisterAccountListener(accountTypes, opPackageName, accounts); |
| 920 | } finally { |
| 921 | restoreCallingIdentity(identityToken); |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | private void unregisterAccountListener(String[] accountTypes, String opPackageName, |
| 926 | UserAccounts accounts) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 927 | synchronized (accounts.mReceiversForType) { |
| 928 | if (accountTypes == null) { |
| 929 | // null for any type |
| 930 | accountTypes = new String[] {null}; |
| 931 | } |
| 932 | for (String type : accountTypes) { |
| 933 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 934 | if (receivers == null || receivers.get(opPackageName) == null) { |
| 935 | throw new IllegalArgumentException("attempt to unregister wrong receiver"); |
| 936 | } |
| 937 | Integer cnt = receivers.get(opPackageName); |
| 938 | if (cnt == 1) { |
| 939 | receivers.remove(opPackageName); |
| 940 | } else { |
| 941 | receivers.put(opPackageName, cnt - 1); |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | // Send notification to all packages which can potentially see the account |
| 948 | private void sendNotificationAccountUpdated(Account account, UserAccounts accounts) { |
| 949 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 950 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 951 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 952 | if ((packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) |
| 953 | && (packageToVisibility.getValue() |
| 954 | != AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE)) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 955 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * Sends a direct intent to a package, notifying it of account visibility change. |
| 962 | * |
| 963 | * @param packageName to send Account to |
| 964 | * @param accounts UserAccount that currently hosts the account |
| 965 | */ |
| 966 | private void notifyPackage(String packageName, UserAccounts accounts) { |
| 967 | Intent intent = new Intent(AccountManager.ACTION_VISIBLE_ACCOUNTS_CHANGED); |
| 968 | intent.setPackage(packageName); |
| 969 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 970 | mContext.sendBroadcastAsUser(intent, new UserHandle(accounts.userId)); |
| 971 | } |
| 972 | |
| 973 | // Returns a map from package name to visibility, for packages subscribed |
| 974 | // to notifications about any account type, or type of provided account |
| 975 | // account type or all types. |
| 976 | private Map<String, Integer> getRequestingPackages(Account account, UserAccounts accounts) { |
| 977 | Set<String> packages = new HashSet<>(); |
| 978 | synchronized (accounts.mReceiversForType) { |
| 979 | for (String type : new String[] {account.type, null}) { |
| 980 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 981 | if (receivers != null) { |
| 982 | packages.addAll(receivers.keySet()); |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | Map<String, Integer> result = new HashMap<>(); |
| 987 | for (String packageName : packages) { |
| 988 | result.put(packageName, resolveAccountVisibility(account, packageName, accounts)); |
| 989 | } |
| 990 | return result; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 993 | // Returns a list of packages listening to ACTION_ACCOUNT_REMOVED able to see the account. |
| 994 | private List<String> getAccountRemovedReceivers(Account account, UserAccounts accounts) { |
| 995 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 996 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 997 | List<ResolveInfo> receivers = |
| 998 | mPackageManager.queryBroadcastReceiversAsUser(intent, 0, accounts.userId); |
| 999 | List<String> result = new ArrayList<>(); |
| 1000 | if (receivers == null) { |
| 1001 | return result; |
| 1002 | } |
| 1003 | for (ResolveInfo resolveInfo: receivers) { |
| 1004 | String packageName = resolveInfo.activityInfo.applicationInfo.packageName; |
| 1005 | int visibility = resolveAccountVisibility(account, packageName, accounts); |
| 1006 | if (visibility == AccountManager.VISIBILITY_VISIBLE |
| 1007 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) { |
| 1008 | result.add(packageName); |
| 1009 | } |
| 1010 | } |
| 1011 | return result; |
| 1012 | } |
| 1013 | |
| 1014 | // Returns true if given package is listening to ACTION_ACCOUNT_REMOVED and can see the account. |
| 1015 | private boolean shouldNotifyPackageOnAccountRemoval(Account account, |
| 1016 | String packageName, UserAccounts accounts) { |
| 1017 | int visibility = resolveAccountVisibility(account, packageName, accounts); |
| 1018 | if (visibility != AccountManager.VISIBILITY_VISIBLE |
| 1019 | && visibility != AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) { |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
| 1023 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 1024 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 1025 | intent.setPackage(packageName); |
| 1026 | List<ResolveInfo> receivers = |
| 1027 | mPackageManager.queryBroadcastReceiversAsUser(intent, 0, accounts.userId); |
| 1028 | return (receivers != null && receivers.size() > 0); |
| 1029 | } |
| 1030 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1031 | private boolean packageExistsForUser(String packageName, int userId) { |
| 1032 | try { |
| 1033 | long identityToken = clearCallingIdentity(); |
| 1034 | try { |
| 1035 | mPackageManager.getPackageUidAsUser(packageName, userId); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 1036 | return true; |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1037 | } finally { |
| 1038 | restoreCallingIdentity(identityToken); |
| 1039 | } |
| 1040 | } catch (NameNotFoundException e) { |
| 1041 | return false; |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * Returns true if packageName is one of special values. |
| 1047 | */ |
| 1048 | private boolean isSpecialPackageKey(String packageName) { |
| 1049 | return (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName) |
| 1050 | || AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)); |
| 1051 | } |
| 1052 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1053 | private void sendAccountsChangedBroadcast(int userId) { |
| 1054 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 1055 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
| 1056 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1059 | private void sendAccountRemovedBroadcast(Account account, String packageName, int userId) { |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame] | 1060 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 1061 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1062 | intent.setPackage(packageName); |
| 1063 | intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 1064 | intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame] | 1065 | mContext.sendBroadcastAsUser(intent, new UserHandle(userId)); |
| 1066 | } |
| 1067 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1068 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 1069 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1070 | throws RemoteException { |
| 1071 | try { |
| 1072 | return super.onTransact(code, data, reply, flags); |
| 1073 | } catch (RuntimeException e) { |
| 1074 | // The account manager only throws security exceptions, so let's |
| 1075 | // log all others. |
| 1076 | if (!(e instanceof SecurityException)) { |
| 1077 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 1078 | } |
| 1079 | throw e; |
| 1080 | } |
| 1081 | } |
| 1082 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1083 | private UserManager getUserManager() { |
| 1084 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1085 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1086 | } |
| 1087 | return mUserManager; |
| 1088 | } |
| 1089 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1090 | /** |
| 1091 | * Validate internal set of accounts against installed authenticators for |
| 1092 | * given user. Clears cached authenticators before validating. |
| 1093 | */ |
| 1094 | public void validateAccounts(int userId) { |
| 1095 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1096 | // Invalidate user-specific cache to make sure we catch any |
| 1097 | // removed authenticators. |
| 1098 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * Validate internal set of accounts against installed authenticators for |
| 1103 | * given user. Clear cached authenticators before validating when requested. |
| 1104 | */ |
| 1105 | private void validateAccountsInternal( |
| 1106 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1107 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1108 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1109 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1110 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1111 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1112 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1113 | if (invalidateAuthenticatorCache) { |
| 1114 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 1115 | } |
| 1116 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1117 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 1118 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1119 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1120 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1121 | synchronized (accounts.dbLock) { |
| 1122 | synchronized (accounts.cacheLock) { |
| 1123 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1124 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1125 | // Get a map of stored authenticator types to UID |
| 1126 | final AccountsDb accountsDb = accounts.accountsDb; |
| 1127 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
| 1128 | // Create a list of authenticator type whose previous uid no longer exists |
| 1129 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
| 1130 | SparseBooleanArray knownUids = null; |
| 1131 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 1132 | String type = authToUidEntry.getKey(); |
| 1133 | int uid = authToUidEntry.getValue(); |
| 1134 | Integer knownUid = knownAuth.get(type); |
| 1135 | if (knownUid != null && uid == knownUid) { |
| 1136 | // Remove it from the knownAuth list if it's unchanged. |
| 1137 | knownAuth.remove(type); |
| 1138 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1139 | /* |
| 1140 | * The authenticator is presently not cached and should only be triggered |
| 1141 | * when we think an authenticator has been removed (or is being updated). |
| 1142 | * But we still want to check if any data with the associated uid is |
| 1143 | * around. This is an (imperfect) signal that the package may be updating. |
| 1144 | * |
| 1145 | * A side effect of this is that an authenticator sharing a uid with |
| 1146 | * multiple apps won't get its credentials wiped as long as some app with |
| 1147 | * that uid is still on the device. But I suspect that this is a rare case. |
| 1148 | * And it isn't clear to me how an attacker could really exploit that |
| 1149 | * feature. |
| 1150 | * |
| 1151 | * The upshot is that we don't have to worry about accounts getting |
| 1152 | * uninstalled while the authenticator's package is being updated. |
| 1153 | * |
| 1154 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1155 | if (knownUids == null) { |
| 1156 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
| 1157 | } |
| 1158 | if (!knownUids.get(uid)) { |
| 1159 | // The authenticator is not presently available to the cache. And the |
| 1160 | // package no longer has a data directory (so we surmise it isn't |
| 1161 | // updating). So purge its data from the account databases. |
| 1162 | obsoleteAuthType.add(type); |
| 1163 | // And delete it from the TABLE_META |
| 1164 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
| 1165 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1166 | } |
| 1167 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1168 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1169 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
| 1170 | // been re-enabled (after being updated for example), then we just overwrite the old |
| 1171 | // values. |
| 1172 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
| 1173 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
| 1174 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1175 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1176 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
| 1177 | try { |
| 1178 | accounts.accountCache.clear(); |
| 1179 | final HashMap<String, ArrayList<String>> accountNamesByType |
| 1180 | = new LinkedHashMap<>(); |
| 1181 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1182 | final long accountId = accountEntry.getKey(); |
| 1183 | final Account account = accountEntry.getValue(); |
| 1184 | if (obsoleteAuthType.contains(account.type)) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1185 | Slog.w(TAG, "deleting account " + account.toSafeString() |
| 1186 | + " because type " + account.type |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1187 | + "'s registered authenticator no longer exist."); |
| 1188 | Map<String, Integer> packagesToVisibility = |
| 1189 | getRequestingPackages(account, accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1190 | List<String> accountRemovedReceivers = |
| 1191 | getAccountRemovedReceivers(account, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1192 | accountsDb.beginTransaction(); |
| 1193 | try { |
| 1194 | accountsDb.deleteDeAccount(accountId); |
| 1195 | // Also delete from CE table if user is unlocked; if user is |
| 1196 | // currently locked the account will be removed later by |
| 1197 | // syncDeCeAccountsLocked |
| 1198 | if (userUnlocked) { |
| 1199 | accountsDb.deleteCeAccount(accountId); |
| 1200 | } |
| 1201 | accountsDb.setTransactionSuccessful(); |
| 1202 | } finally { |
| 1203 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1204 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1205 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1206 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1207 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1208 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1209 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1210 | accounts.userDataCache.remove(account); |
| 1211 | accounts.authTokenCache.remove(account); |
| 1212 | accounts.accountTokenCaches.remove(account); |
| 1213 | accounts.visibilityCache.remove(account); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1214 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1215 | for (Entry<String, Integer> packageToVisibility : |
| 1216 | packagesToVisibility.entrySet()) { |
sunjian | 066aa5e | 2017-06-05 12:16:59 -0700 | [diff] [blame] | 1217 | if (isVisible(packageToVisibility.getValue())) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1218 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 1219 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1220 | } |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1221 | for (String packageName : accountRemovedReceivers) { |
| 1222 | sendAccountRemovedBroadcast(account, packageName, accounts.userId); |
| 1223 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1224 | } else { |
| 1225 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
| 1226 | if (accountNames == null) { |
| 1227 | accountNames = new ArrayList<>(); |
| 1228 | accountNamesByType.put(account.type, accountNames); |
| 1229 | } |
| 1230 | accountNames.add(account.name); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1231 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1232 | } |
| 1233 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
| 1234 | final String accountType = cur.getKey(); |
| 1235 | final ArrayList<String> accountNames = cur.getValue(); |
| 1236 | final Account[] accountsForType = new Account[accountNames.size()]; |
| 1237 | for (int i = 0; i < accountsForType.length; i++) { |
| 1238 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
| 1239 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1240 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1241 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1242 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1243 | accounts.visibilityCache.putAll(accountsDb.findAllVisibilityValues()); |
| 1244 | } finally { |
| 1245 | if (accountDeleted) { |
| 1246 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1247 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1248 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1249 | } |
| 1250 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1253 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1254 | // Get the UIDs of all apps that might have data on the device. We want |
| 1255 | // to preserve user data if the app might otherwise be storing data. |
| 1256 | List<PackageInfo> pkgsWithData = |
| 1257 | mPackageManager.getInstalledPackagesAsUser( |
| 1258 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1259 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1260 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1261 | if (pkgInfo.applicationInfo != null |
| 1262 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1263 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1264 | } |
| 1265 | } |
| 1266 | return knownUids; |
| 1267 | } |
| 1268 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1269 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1270 | Context context, |
| 1271 | int userId) { |
| 1272 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1273 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1274 | } |
| 1275 | |
| 1276 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1277 | IAccountAuthenticatorCache authCache, |
| 1278 | int userId) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 1279 | HashMap<String, Integer> knownAuth = new LinkedHashMap<>(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1280 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1281 | .getAllServices(userId)) { |
| 1282 | knownAuth.put(service.type.type, service.uid); |
| 1283 | } |
| 1284 | return knownAuth; |
| 1285 | } |
| 1286 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1287 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1288 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | protected UserAccounts getUserAccounts(int userId) { |
Felipe Leme | bc215de | 2019-12-05 15:23:16 -0800 | [diff] [blame] | 1292 | try { |
| 1293 | return getUserAccountsNotChecked(userId); |
| 1294 | } catch (RuntimeException e) { |
| 1295 | if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) { |
| 1296 | // Let it go... |
| 1297 | throw e; |
| 1298 | } |
| 1299 | // User accounts database is corrupted, we must wipe out the whole user, otherwise the |
| 1300 | // system will crash indefinitely |
| 1301 | Slog.wtf(TAG, "Removing user " + userId + " due to exception (" + e + ") reading its " |
| 1302 | + "account database"); |
| 1303 | if (userId == ActivityManager.getCurrentUser() && userId != UserHandle.USER_SYSTEM) { |
| 1304 | Slog.i(TAG, "Switching to system user first"); |
| 1305 | try { |
| 1306 | ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM); |
| 1307 | } catch (RemoteException re) { |
| 1308 | Slog.e(TAG, "Could not switch to " + UserHandle.USER_SYSTEM + ": " + re); |
| 1309 | } |
| 1310 | } |
| 1311 | if (!getUserManager().removeUserEvenWhenDisallowed(userId)) { |
| 1312 | Slog.e(TAG, "could not remove user " + userId); |
| 1313 | } |
| 1314 | throw e; |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | private UserAccounts getUserAccountsNotChecked(int userId) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1319 | synchronized (mUsers) { |
| 1320 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1321 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1322 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1323 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1324 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1325 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1326 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1327 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1328 | validateAccounts = true; |
| 1329 | } |
| 1330 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1331 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1332 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1333 | synchronized (accounts.dbLock) { |
| 1334 | synchronized (accounts.cacheLock) { |
| 1335 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
| 1336 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
| 1337 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1338 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1339 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1340 | } |
| 1341 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1342 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1343 | } |
| 1344 | return accounts; |
| 1345 | } |
| 1346 | } |
| 1347 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1348 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1349 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1350 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1351 | if (!accountsToRemove.isEmpty()) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1352 | Slog.i(TAG, accountsToRemove.size() |
| 1353 | + " accounts were previously deleted while user " |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1354 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1355 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1356 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1357 | |
| 1358 | for (Account account : accountsToRemove) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1359 | removeAccountInternal(accounts, account, Process.SYSTEM_UID); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1364 | private void purgeOldGrantsAll() { |
| 1365 | synchronized (mUsers) { |
| 1366 | for (int i = 0; i < mUsers.size(); i++) { |
| 1367 | purgeOldGrants(mUsers.valueAt(i)); |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | private void purgeOldGrants(UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1373 | synchronized (accounts.dbLock) { |
| 1374 | synchronized (accounts.cacheLock) { |
| 1375 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
| 1376 | for (int uid : uids) { |
| 1377 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1378 | if (packageExists) { |
| 1379 | continue; |
| 1380 | } |
| 1381 | Log.d(TAG, "deleting grants for UID " + uid |
| 1382 | + " because its package is no longer installed"); |
| 1383 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1384 | } |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1389 | private void removeVisibilityValuesForPackage(String packageName) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1390 | if (isSpecialPackageKey(packageName)) { |
| 1391 | return; |
| 1392 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1393 | synchronized (mUsers) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1394 | int numberOfUsers = mUsers.size(); |
| 1395 | for (int i = 0; i < numberOfUsers; i++) { |
| 1396 | UserAccounts accounts = mUsers.valueAt(i); |
| 1397 | try { |
| 1398 | mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 1399 | } catch (NameNotFoundException e) { |
| 1400 | // package does not exist - remove visibility values |
| 1401 | accounts.accountsDb.deleteAccountVisibilityForPackage(packageName); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1402 | synchronized (accounts.dbLock) { |
| 1403 | synchronized (accounts.cacheLock) { |
| 1404 | for (Account account : accounts.visibilityCache.keySet()) { |
| 1405 | Map<String, Integer> accountVisibility = |
| 1406 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 1407 | accountVisibility.remove(packageName); |
| 1408 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1409 | } |
| 1410 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | |
Fyodor Kupolov | 945c97e | 2017-06-21 17:45:19 -0700 | [diff] [blame] | 1416 | private void purgeUserData(int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1417 | UserAccounts accounts; |
| 1418 | synchronized (mUsers) { |
| 1419 | accounts = mUsers.get(userId); |
| 1420 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1421 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1422 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1423 | if (accounts != null) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1424 | synchronized (accounts.dbLock) { |
| 1425 | synchronized (accounts.cacheLock) { |
Dmitry Dementyev | 4744319 | 2018-10-24 13:31:59 -0700 | [diff] [blame] | 1426 | accounts.accountsDb.closeDebugStatement(); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1427 | accounts.accountsDb.close(); |
| 1428 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1429 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1430 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1433 | @VisibleForTesting |
| 1434 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1435 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1436 | } |
| 1437 | |
| 1438 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1439 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1440 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1441 | } |
| 1442 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1443 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1444 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1445 | if (userId < 1) return; |
Fyodor Kupolov | 2bc895d | 2017-12-19 11:53:47 -0800 | [diff] [blame] | 1446 | mHandler.post(() -> syncSharedAccounts(userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1447 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1448 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1449 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1450 | // Check if there's a shared account that needs to be created as an account |
| 1451 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1452 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1453 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1454 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1455 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1456 | : UserHandle.USER_SYSTEM; |
| 1457 | if (parentUserId < 0) { |
| 1458 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1459 | return; |
| 1460 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1461 | for (Account sa : sharedAccounts) { |
| 1462 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1463 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1464 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1468 | @Override |
| 1469 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Dmitry Dementyev | c3e913b | 2019-07-11 11:32:16 -0700 | [diff] [blame] | 1470 | UserInfo user = getUserManager().getUserInfo(userId); |
| 1471 | if (user == null) { |
| 1472 | Log.w(TAG, "onServiceChanged: ignore removed user " + userId); |
| 1473 | return; |
| 1474 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1475 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1478 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1479 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1480 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1481 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1482 | Log.v(TAG, "getPassword: " + account |
| 1483 | + ", caller's uid " + Binder.getCallingUid() |
| 1484 | + ", pid " + Binder.getCallingPid()); |
| 1485 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1486 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1487 | int userId = UserHandle.getCallingUserId(); |
| 1488 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1489 | String msg = String.format( |
| 1490 | "uid %s cannot get secrets for accounts of type: %s", |
| 1491 | callingUid, |
| 1492 | account.type); |
| 1493 | throw new SecurityException(msg); |
| 1494 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1495 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1496 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1497 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1498 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1499 | } finally { |
| 1500 | restoreCallingIdentity(identityToken); |
| 1501 | } |
| 1502 | } |
| 1503 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1504 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1505 | if (account == null) { |
| 1506 | return null; |
| 1507 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1508 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1509 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1510 | return null; |
| 1511 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1512 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1513 | synchronized (accounts.dbLock) { |
| 1514 | synchronized (accounts.cacheLock) { |
| 1515 | return accounts.accountsDb |
| 1516 | .findAccountPasswordByNameAndType(account.name, account.type); |
| 1517 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1521 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1522 | public String getPreviousName(Account account) { |
| 1523 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1524 | Log.v(TAG, "getPreviousName: " + account |
| 1525 | + ", caller's uid " + Binder.getCallingUid() |
| 1526 | + ", pid " + Binder.getCallingPid()); |
| 1527 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1528 | Preconditions.checkNotNull(account, "account cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1529 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1530 | long identityToken = clearCallingIdentity(); |
| 1531 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1532 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1533 | return readPreviousNameInternal(accounts, account); |
| 1534 | } finally { |
| 1535 | restoreCallingIdentity(identityToken); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1540 | if (account == null) { |
| 1541 | return null; |
| 1542 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1543 | synchronized (accounts.dbLock) { |
| 1544 | synchronized (accounts.cacheLock) { |
| 1545 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1546 | if (previousNameRef == null) { |
| 1547 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
| 1548 | previousNameRef = new AtomicReference<>(previousName); |
| 1549 | accounts.previousNameCache.put(account, previousNameRef); |
| 1550 | return previousName; |
| 1551 | } else { |
| 1552 | return previousNameRef.get(); |
| 1553 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1559 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1560 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1561 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1562 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1563 | account, key, callingUid, Binder.getCallingPid()); |
| 1564 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1565 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1566 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 1567 | Preconditions.checkNotNull(key, "key cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1568 | int userId = UserHandle.getCallingUserId(); |
| 1569 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1570 | String msg = String.format( |
| 1571 | "uid %s cannot get user data for accounts of type: %s", |
| 1572 | callingUid, |
| 1573 | account.type); |
| 1574 | throw new SecurityException(msg); |
| 1575 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1576 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1577 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1578 | return null; |
| 1579 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1580 | long identityToken = clearCallingIdentity(); |
| 1581 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1582 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1583 | if (!accountExistsCache(accounts, account)) { |
| 1584 | return null; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1585 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1586 | return readUserDataInternal(accounts, account, key); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1587 | } finally { |
| 1588 | restoreCallingIdentity(identityToken); |
| 1589 | } |
| 1590 | } |
| 1591 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1592 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1593 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1594 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1595 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1596 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1597 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1598 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1599 | + ", pid " + Binder.getCallingPid()); |
| 1600 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1601 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1602 | if (isCrossUser(callingUid, userId)) { |
| 1603 | throw new SecurityException( |
| 1604 | String.format( |
| 1605 | "User %s tying to get authenticator types for %s" , |
| 1606 | UserHandle.getCallingUserId(), |
| 1607 | userId)); |
| 1608 | } |
| 1609 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1610 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1611 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1612 | return getAuthenticatorTypesInternal(userId); |
| 1613 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1614 | } finally { |
| 1615 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1616 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1619 | /** |
| 1620 | * Should only be called inside of a clearCallingIdentity block. |
| 1621 | */ |
| 1622 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1623 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1624 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1625 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1626 | AuthenticatorDescription[] types = |
| 1627 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1628 | int i = 0; |
| 1629 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1630 | : authenticatorCollection) { |
| 1631 | types[i] = authenticator.type; |
| 1632 | i++; |
| 1633 | } |
| 1634 | return types; |
| 1635 | } |
| 1636 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1637 | private boolean isCrossUser(int callingUid, int userId) { |
| 1638 | return (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1639 | && callingUid != Process.SYSTEM_UID |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1640 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1641 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1642 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1643 | } |
| 1644 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1645 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1646 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1647 | return addAccountExplicitlyWithVisibility(account, password, extras, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1650 | @Override |
| 1651 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1652 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1653 | int callingUid = Binder.getCallingUid(); |
| 1654 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1655 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1656 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1657 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1658 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1659 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1660 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1661 | if (response != null) { |
| 1662 | Bundle result = new Bundle(); |
| 1663 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1664 | try { |
| 1665 | response.onResult(result); |
| 1666 | } catch (RemoteException e) { |
| 1667 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1668 | } |
| 1669 | } |
| 1670 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1671 | } |
| 1672 | |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1673 | Slog.d(TAG, "Copying account " + account.toSafeString() |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1674 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1675 | long identityToken = clearCallingIdentity(); |
| 1676 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1677 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1678 | false /* stripAuthTokenFromResult */, account.name, |
| 1679 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1680 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1681 | protected String toDebugString(long now) { |
| 1682 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1683 | + ", " + account.type; |
| 1684 | } |
| 1685 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1686 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1687 | public void run() throws RemoteException { |
| 1688 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1689 | } |
| 1690 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1691 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1692 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1693 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1694 | if (result != null |
| 1695 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1696 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1697 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1698 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1699 | super.onResult(result); |
| 1700 | } |
| 1701 | } |
| 1702 | }.bind(); |
| 1703 | } finally { |
| 1704 | restoreCallingIdentity(identityToken); |
| 1705 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1706 | } |
| 1707 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1708 | @Override |
| 1709 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1710 | final int callingUid = Binder.getCallingUid(); |
| 1711 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1712 | String msg = String.format( |
| 1713 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1714 | account, |
| 1715 | callingUid); |
| 1716 | Log.v(TAG, msg); |
| 1717 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1718 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1719 | int userId = UserHandle.getCallingUserId(); |
| 1720 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1721 | String msg = String.format( |
| 1722 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1723 | callingUid, |
| 1724 | account.type); |
| 1725 | throw new SecurityException(msg); |
| 1726 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1727 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1728 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1729 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1730 | return false; |
| 1731 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1732 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1733 | long identityToken = clearCallingIdentity(); |
| 1734 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1735 | UserAccounts accounts = getUserAccounts(userId); |
| 1736 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1737 | } finally { |
| 1738 | restoreCallingIdentity(identityToken); |
| 1739 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1743 | final UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1744 | synchronized (accounts.dbLock) { |
| 1745 | synchronized (accounts.cacheLock) { |
| 1746 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
| 1747 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1748 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1749 | } |
| 1750 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1751 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1752 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1753 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1754 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1755 | long id = clearCallingIdentity(); |
| 1756 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1757 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1758 | false /* stripAuthTokenFromResult */, account.name, |
| 1759 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1760 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1761 | protected String toDebugString(long now) { |
| 1762 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1763 | + ", " + account.type; |
| 1764 | } |
| 1765 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1766 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1767 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1768 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 16bedd4 | 2017-03-30 10:00:49 -0700 | [diff] [blame] | 1769 | for (Account acc : getAccounts(parentUserId, mContext.getOpPackageName())) { |
| 1770 | if (acc.equals(account)) { |
| 1771 | mAuthenticator.addAccountFromCredentials( |
| 1772 | this, account, accountCredentials); |
| 1773 | break; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1774 | } |
| 1775 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1776 | } |
| 1777 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1778 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1779 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1780 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1781 | // TODO: Anything to do if if succedded? |
| 1782 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1783 | // account to avoid retries? |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1784 | // TODO: what we do with the visibility? |
| 1785 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1786 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1787 | } |
| 1788 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1789 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1790 | public void onError(int errorCode, String errorMessage) { |
| 1791 | super.onError(errorCode, errorMessage); |
| 1792 | // TODO: Show error notification to user |
| 1793 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1794 | } |
| 1795 | |
| 1796 | }.bind(); |
| 1797 | } finally { |
| 1798 | restoreCallingIdentity(id); |
| 1799 | } |
| 1800 | } |
| 1801 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1802 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1803 | Bundle extras, int callingUid, Map<String, Integer> packageToVisibility) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1804 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1805 | if (account == null) { |
| 1806 | return false; |
| 1807 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1808 | if (!isLocalUnlockedUser(accounts.userId)) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1809 | Log.w(TAG, "Account " + account.toSafeString() + " cannot be added - user " |
| 1810 | + accounts.userId + " is locked. callingUid=" + callingUid); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1811 | return false; |
| 1812 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1813 | synchronized (accounts.dbLock) { |
| 1814 | synchronized (accounts.cacheLock) { |
| 1815 | accounts.accountsDb.beginTransaction(); |
| 1816 | try { |
| 1817 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1818 | Log.w(TAG, "insertAccountIntoDatabase: " + account.toSafeString() |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1819 | + ", skipping since the account already exists"); |
| 1820 | return false; |
| 1821 | } |
| 1822 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
| 1823 | if (accountId < 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1824 | Log.w(TAG, "insertAccountIntoDatabase: " + account.toSafeString() |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1825 | + ", skipping the DB insert failed"); |
| 1826 | return false; |
| 1827 | } |
| 1828 | // Insert into DE table |
| 1829 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1830 | Log.w(TAG, "insertAccountIntoDatabase: " + account.toSafeString() |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1831 | + ", skipping the DB insert failed"); |
| 1832 | return false; |
| 1833 | } |
| 1834 | if (extras != null) { |
| 1835 | for (String key : extras.keySet()) { |
| 1836 | final String value = extras.getString(key); |
| 1837 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1838 | Log.w(TAG, "insertAccountIntoDatabase: " |
| 1839 | + account.toSafeString() |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1840 | + ", skipping since insertExtra failed for key " + key); |
| 1841 | return false; |
| 1842 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1843 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1844 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1845 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1846 | if (packageToVisibility != null) { |
| 1847 | for (Entry<String, Integer> entry : packageToVisibility.entrySet()) { |
| 1848 | setAccountVisibility(account, entry.getKey() /* package */, |
| 1849 | entry.getValue() /* visibility */, false /* notify */, |
| 1850 | accounts); |
| 1851 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1852 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1853 | accounts.accountsDb.setTransactionSuccessful(); |
| 1854 | |
| 1855 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 1856 | accountId, |
| 1857 | accounts, callingUid); |
| 1858 | |
| 1859 | insertAccountIntoCacheLocked(accounts, account); |
| 1860 | } finally { |
| 1861 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1862 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1863 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1864 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1865 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1866 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1867 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1868 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1869 | sendNotificationAccountUpdated(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1870 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1871 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1872 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1873 | return true; |
| 1874 | } |
| 1875 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1876 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1877 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1878 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1879 | } |
| 1880 | } |
| 1881 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1882 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1883 | * 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] | 1884 | * running, then clone the account too. |
| 1885 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1886 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1887 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1888 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1889 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1890 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1891 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1892 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1893 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1894 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1895 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1896 | } |
| 1897 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1901 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1902 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1903 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1904 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1905 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1906 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1907 | Log.v(TAG, "hasFeatures: " + account |
| 1908 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1909 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1910 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1911 | + ", pid " + Binder.getCallingPid()); |
| 1912 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1913 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1914 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1915 | Preconditions.checkArgument(features != null, "features cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1916 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1917 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1918 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1919 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1920 | long identityToken = clearCallingIdentity(); |
| 1921 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1922 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1923 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1924 | } finally { |
| 1925 | restoreCallingIdentity(identityToken); |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | private class TestFeaturesSession extends Session { |
| 1930 | private final String[] mFeatures; |
| 1931 | private final Account mAccount; |
| 1932 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1933 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1934 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1935 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1936 | true /* stripAuthTokenFromResult */, account.name, |
| 1937 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1938 | mFeatures = features; |
| 1939 | mAccount = account; |
| 1940 | } |
| 1941 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1942 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1943 | public void run() throws RemoteException { |
| 1944 | try { |
| 1945 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1946 | } catch (RemoteException e) { |
| 1947 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1948 | } |
| 1949 | } |
| 1950 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1951 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1952 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1953 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1954 | IAccountManagerResponse response = getResponseAndClose(); |
| 1955 | if (response != null) { |
| 1956 | try { |
| 1957 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1958 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1959 | return; |
| 1960 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1961 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1962 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1963 | + response); |
| 1964 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1965 | final Bundle newResult = new Bundle(); |
| 1966 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1967 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1968 | response.onResult(newResult); |
| 1969 | } catch (RemoteException e) { |
| 1970 | // if the caller is dead then there is no one to care about remote exceptions |
| 1971 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1972 | Log.v(TAG, "failure while notifying response", e); |
| 1973 | } |
| 1974 | } |
| 1975 | } |
| 1976 | } |
| 1977 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1978 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1979 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1980 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1981 | + ", " + mAccount |
| 1982 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1983 | } |
| 1984 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1985 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1986 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1987 | public void renameAccount( |
| 1988 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1989 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1990 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1991 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1992 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1993 | + ", pid " + Binder.getCallingPid()); |
| 1994 | } |
| 1995 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1996 | int userId = UserHandle.getCallingUserId(); |
| 1997 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1998 | String msg = String.format( |
| 1999 | "uid %s cannot rename accounts of type: %s", |
| 2000 | callingUid, |
| 2001 | accountToRename.type); |
| 2002 | throw new SecurityException(msg); |
| 2003 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2004 | long identityToken = clearCallingIdentity(); |
| 2005 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2006 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2007 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2008 | Bundle result = new Bundle(); |
| 2009 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 2010 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 2011 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 2012 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2013 | try { |
| 2014 | response.onResult(result); |
| 2015 | } catch (RemoteException e) { |
| 2016 | Log.w(TAG, e.getMessage()); |
| 2017 | } |
| 2018 | } finally { |
| 2019 | restoreCallingIdentity(identityToken); |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2024 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2025 | Account resultAccount = null; |
| 2026 | /* |
| 2027 | * Cancel existing notifications. Let authenticators |
| 2028 | * re-post notifications as required. But we don't know if |
| 2029 | * the authenticators have bound their notifications to |
| 2030 | * now stale account name data. |
| 2031 | * |
| 2032 | * With a rename api, we might not need to do this anymore but it |
| 2033 | * shouldn't hurt. |
| 2034 | */ |
| 2035 | cancelNotification( |
| 2036 | getSigninRequiredNotificationId(accounts, accountToRename), |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2037 | new UserHandle(accounts.userId)); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2038 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 2039 | for (Pair<Pair<Account, String>, Integer> pair: |
| 2040 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2041 | if (accountToRename.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2042 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2043 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 2044 | } |
| 2045 | } |
| 2046 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2047 | synchronized (accounts.dbLock) { |
| 2048 | synchronized (accounts.cacheLock) { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2049 | List<String> accountRemovedReceivers = |
| 2050 | getAccountRemovedReceivers(accountToRename, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2051 | accounts.accountsDb.beginTransaction(); |
| 2052 | Account renamedAccount = new Account(newName, accountToRename.type); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2053 | try { |
chengang | f2d081c | 2017-12-27 17:17:32 +0800 | [diff] [blame] | 2054 | if ((accounts.accountsDb.findCeAccountId(renamedAccount) >= 0)) { |
| 2055 | Log.e(TAG, "renameAccount failed - account with new name already exists"); |
| 2056 | return null; |
| 2057 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2058 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
| 2059 | if (accountId >= 0) { |
| 2060 | accounts.accountsDb.renameCeAccount(accountId, newName); |
| 2061 | if (accounts.accountsDb.renameDeAccount( |
| 2062 | accountId, newName, accountToRename.name)) { |
| 2063 | accounts.accountsDb.setTransactionSuccessful(); |
| 2064 | } else { |
| 2065 | Log.e(TAG, "renameAccount failed"); |
| 2066 | return null; |
| 2067 | } |
| 2068 | } else { |
| 2069 | Log.e(TAG, "renameAccount failed - old account does not exist"); |
| 2070 | return null; |
| 2071 | } |
| 2072 | } finally { |
| 2073 | accounts.accountsDb.endTransaction(); |
| 2074 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2075 | /* |
| 2076 | * Database transaction was successful. Clean up cached |
| 2077 | * data associated with the account in the user profile. |
| 2078 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2079 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2080 | /* |
| 2081 | * Extract the data and token caches before removing the |
| 2082 | * old account to preserve the user data associated with |
| 2083 | * the account. |
| 2084 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2085 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 2086 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
| 2087 | Map<String, Integer> tmpVisibility = accounts.visibilityCache.get(accountToRename); |
| 2088 | removeAccountFromCacheLocked(accounts, accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2089 | /* |
| 2090 | * Update the cached data associated with the renamed |
| 2091 | * account. |
| 2092 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2093 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 2094 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 2095 | accounts.visibilityCache.put(renamedAccount, tmpVisibility); |
| 2096 | accounts.previousNameCache.put( |
| 2097 | renamedAccount, |
| 2098 | new AtomicReference<>(accountToRename.name)); |
| 2099 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2100 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2101 | int parentUserId = accounts.userId; |
| 2102 | if (canHaveProfile(parentUserId)) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2103 | /* |
| 2104 | * Owner or system user account was renamed, rename the account for |
| 2105 | * those users with which the account was shared. |
| 2106 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2107 | List<UserInfo> users = getUserManager().getUsers(true); |
| 2108 | for (UserInfo user : users) { |
| 2109 | if (user.isRestricted() |
| 2110 | && (user.restrictedProfileParentId == parentUserId)) { |
| 2111 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
| 2112 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2113 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2114 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2115 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2116 | sendNotificationAccountUpdated(resultAccount, accounts); |
| 2117 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2118 | for (String packageName : accountRemovedReceivers) { |
| 2119 | sendAccountRemovedBroadcast(accountToRename, packageName, accounts.userId); |
| 2120 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2121 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2122 | } |
| 2123 | return resultAccount; |
| 2124 | } |
| 2125 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2126 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2127 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2128 | return userInfo != null && userInfo.canHaveProfile(); |
| 2129 | } |
| 2130 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2131 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2132 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 2133 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2134 | removeAccountAsUser( |
| 2135 | response, |
| 2136 | account, |
| 2137 | expectActivityLaunch, |
| 2138 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | @Override |
| 2142 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2143 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2144 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2145 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2146 | Log.v(TAG, "removeAccount: " + account |
| 2147 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2148 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2149 | + ", pid " + Binder.getCallingPid() |
| 2150 | + ", for user id " + userId); |
| 2151 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2152 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 2153 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 2154 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2155 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2156 | if (isCrossUser(callingUid, userId)) { |
| 2157 | throw new SecurityException( |
| 2158 | String.format( |
| 2159 | "User %s tying remove account for %s" , |
| 2160 | UserHandle.getCallingUserId(), |
| 2161 | userId)); |
| 2162 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2163 | /* |
Alex Chau | f788f9c | 2017-12-08 15:16:46 +0000 | [diff] [blame] | 2164 | * Only the system, authenticator or profile owner should be allowed to remove accounts for |
| 2165 | * that authenticator. This will let users remove accounts (via Settings in the system) but |
| 2166 | * not arbitrary applications (like competing authenticators). |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2167 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2168 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 2169 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
Alex Chau | f788f9c | 2017-12-08 15:16:46 +0000 | [diff] [blame] | 2170 | && !isSystemUid(callingUid) |
| 2171 | && !isProfileOwner(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2172 | String msg = String.format( |
| 2173 | "uid %s cannot remove accounts of type: %s", |
| 2174 | callingUid, |
| 2175 | account.type); |
| 2176 | throw new SecurityException(msg); |
| 2177 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2178 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2179 | try { |
| 2180 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2181 | "User cannot modify accounts"); |
| 2182 | } catch (RemoteException re) { |
| 2183 | } |
| 2184 | return; |
| 2185 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2186 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2187 | try { |
| 2188 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2189 | "User cannot modify accounts of this type (policy)."); |
| 2190 | } catch (RemoteException re) { |
| 2191 | } |
| 2192 | return; |
| 2193 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2194 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2195 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2196 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2197 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2198 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2199 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2200 | if (account.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2201 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2202 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2206 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2207 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2208 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2209 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2210 | accountId, |
| 2211 | accounts, |
| 2212 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2213 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2214 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2215 | } finally { |
| 2216 | restoreCallingIdentity(identityToken); |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | @Override |
| 2221 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2222 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2223 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2224 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2225 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2226 | + ", pid " + Binder.getCallingPid()); |
| 2227 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2228 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2229 | if (account == null) { |
| 2230 | /* |
| 2231 | * Null accounts should result in returning false, as per |
| 2232 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2233 | */ |
| 2234 | Log.e(TAG, "account is null"); |
| 2235 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2236 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2237 | String msg = String.format( |
Brandon Weeks | 9e4e96d | 2017-08-24 15:24:16 -0700 | [diff] [blame] | 2238 | "uid %s cannot explicitly remove accounts of type: %s", |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2239 | callingUid, |
| 2240 | account.type); |
| 2241 | throw new SecurityException(msg); |
| 2242 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2243 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2244 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2245 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2246 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2247 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2248 | accountId, |
| 2249 | accounts, |
| 2250 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2251 | long identityToken = clearCallingIdentity(); |
| 2252 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2253 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2254 | } finally { |
| 2255 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2256 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2259 | private class RemoveAccountSession extends Session { |
| 2260 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2261 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2262 | Account account, boolean expectActivityLaunch) { |
| 2263 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2264 | true /* stripAuthTokenFromResult */, account.name, |
| 2265 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2266 | mAccount = account; |
| 2267 | } |
| 2268 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2269 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2270 | protected String toDebugString(long now) { |
| 2271 | return super.toDebugString(now) + ", removeAccount" |
| 2272 | + ", account " + mAccount; |
| 2273 | } |
| 2274 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2275 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2276 | public void run() throws RemoteException { |
| 2277 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2278 | } |
| 2279 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2280 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2281 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2282 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2283 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2284 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2285 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2286 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2287 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2288 | } |
| 2289 | IAccountManagerResponse response = getResponseAndClose(); |
| 2290 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2291 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2292 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2293 | + response); |
| 2294 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2295 | try { |
tiansiming | 5330b5a | 2017-10-13 10:57:25 +0800 | [diff] [blame] | 2296 | response.onResult(result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2297 | } catch (RemoteException e) { |
tiansiming | 5330b5a | 2017-10-13 10:57:25 +0800 | [diff] [blame] | 2298 | Slog.e(TAG, "Error calling onResult()", e); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2299 | } |
| 2300 | } |
| 2301 | } |
| 2302 | super.onResult(result); |
| 2303 | } |
| 2304 | } |
| 2305 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2306 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2307 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2308 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2309 | } |
| 2310 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2311 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2312 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2313 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2314 | if (!userUnlocked) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 2315 | Slog.i(TAG, "Removing account " + account.toSafeString() |
| 2316 | + " while user " + accounts.userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2317 | + " is still locked. CE data will be removed later"); |
| 2318 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2319 | synchronized (accounts.dbLock) { |
| 2320 | synchronized (accounts.cacheLock) { |
| 2321 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, |
| 2322 | accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2323 | List<String> accountRemovedReceivers = |
| 2324 | getAccountRemovedReceivers(account, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2325 | accounts.accountsDb.beginTransaction(); |
| 2326 | // Set to a dummy value, this will only be used if the database |
| 2327 | // transaction succeeds. |
| 2328 | long accountId = -1; |
| 2329 | try { |
| 2330 | accountId = accounts.accountsDb.findDeAccountId(account); |
| 2331 | if (accountId >= 0) { |
| 2332 | isChanged = accounts.accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2333 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2334 | // always delete from CE table if CE storage is available |
| 2335 | // DE account could be removed while CE was locked |
| 2336 | if (userUnlocked) { |
| 2337 | long ceAccountId = accounts.accountsDb.findCeAccountId(account); |
| 2338 | if (ceAccountId >= 0) { |
| 2339 | accounts.accountsDb.deleteCeAccount(ceAccountId); |
| 2340 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2341 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2342 | accounts.accountsDb.setTransactionSuccessful(); |
| 2343 | } finally { |
| 2344 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2345 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2346 | if (isChanged) { |
| 2347 | removeAccountFromCacheLocked(accounts, account); |
| 2348 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 2349 | .entrySet()) { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2350 | if ((packageToVisibility.getValue() == AccountManager.VISIBILITY_VISIBLE) |
| 2351 | || (packageToVisibility.getValue() |
| 2352 | == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE)) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2353 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 2354 | } |
| 2355 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2356 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2357 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occurred. |
| 2358 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2359 | for (String packageName : accountRemovedReceivers) { |
| 2360 | sendAccountRemovedBroadcast(account, packageName, accounts.userId); |
| 2361 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2362 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2363 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2364 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
| 2365 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2366 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2367 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2368 | long id = Binder.clearCallingIdentity(); |
| 2369 | try { |
| 2370 | int parentUserId = accounts.userId; |
| 2371 | if (canHaveProfile(parentUserId)) { |
| 2372 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2373 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2374 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2375 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2376 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2377 | } |
| 2378 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2379 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2380 | } finally { |
| 2381 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2382 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2383 | |
| 2384 | if (isChanged) { |
| 2385 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2386 | for (Pair<Pair<Account, String>, Integer> key |
| 2387 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2388 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2389 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2390 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2391 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2392 | account, uid, false)); |
| 2393 | } |
| 2394 | } |
| 2395 | } |
| 2396 | } |
| 2397 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2398 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2399 | } |
| 2400 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2401 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2402 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2403 | int callerUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2404 | Preconditions.checkNotNull(accountType, "accountType cannot be null"); |
| 2405 | Preconditions.checkNotNull(authToken, "authToken cannot be null"); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2406 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2407 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2408 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2409 | + ", pid " + Binder.getCallingPid()); |
| 2410 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2411 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2412 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2413 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2414 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2415 | List<Pair<Account, String>> deletedTokens; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2416 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2417 | accounts.accountsDb.beginTransaction(); |
| 2418 | try { |
| 2419 | deletedTokens = invalidateAuthTokenLocked(accounts, accountType, authToken); |
| 2420 | accounts.accountsDb.setTransactionSuccessful(); |
| 2421 | } finally { |
| 2422 | accounts.accountsDb.endTransaction(); |
| 2423 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2424 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2425 | for (Pair<Account, String> tokenInfo : deletedTokens) { |
| 2426 | Account act = tokenInfo.first; |
| 2427 | String tokenType = tokenInfo.second; |
| 2428 | writeAuthTokenIntoCacheLocked(accounts, act, tokenType, null); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2429 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2430 | // wipe out cached token in memory. |
| 2431 | accounts.accountTokenCaches.remove(accountType, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2432 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2433 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2434 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2435 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2439 | private List<Pair<Account, String>> invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2440 | String authToken) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2441 | // TODO Move to AccountsDB |
| 2442 | List<Pair<Account, String>> results = new ArrayList<>(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2443 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2444 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2445 | try { |
| 2446 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2447 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2448 | String accountName = cursor.getString(1); |
| 2449 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2450 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2451 | results.add(Pair.create(new Account(accountName, accountType), authTokenType)); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2452 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2453 | } finally { |
| 2454 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2455 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2456 | return results; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2459 | private void saveCachedToken( |
| 2460 | UserAccounts accounts, |
| 2461 | Account account, |
| 2462 | String callerPkg, |
| 2463 | byte[] callerSigDigest, |
| 2464 | String tokenType, |
| 2465 | String token, |
| 2466 | long expiryMillis) { |
| 2467 | |
| 2468 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2469 | return; |
| 2470 | } |
| 2471 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2472 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2473 | synchronized (accounts.cacheLock) { |
| 2474 | accounts.accountTokenCaches.put( |
| 2475 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2479 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2480 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2481 | if (account == null || type == null) { |
| 2482 | return false; |
| 2483 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2484 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2485 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2486 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2487 | accounts.accountsDb.beginTransaction(); |
| 2488 | boolean updateCache = false; |
| 2489 | try { |
| 2490 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2491 | if (accountId < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2492 | return false; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2493 | } |
| 2494 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2495 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2496 | accounts.accountsDb.setTransactionSuccessful(); |
| 2497 | updateCache = true; |
| 2498 | return true; |
| 2499 | } |
| 2500 | return false; |
| 2501 | } finally { |
| 2502 | accounts.accountsDb.endTransaction(); |
| 2503 | if (updateCache) { |
| 2504 | synchronized (accounts.cacheLock) { |
| 2505 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
| 2506 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2507 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2508 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2509 | } |
| 2510 | } |
| 2511 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2512 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2513 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2514 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2515 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2516 | Log.v(TAG, "peekAuthToken: " + account |
| 2517 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2518 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2519 | + ", pid " + Binder.getCallingPid()); |
| 2520 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2521 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2522 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2523 | int userId = UserHandle.getCallingUserId(); |
| 2524 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2525 | String msg = String.format( |
| 2526 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2527 | callingUid, |
| 2528 | account.type); |
| 2529 | throw new SecurityException(msg); |
| 2530 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2531 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2532 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2533 | + callingUid); |
| 2534 | return null; |
| 2535 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2536 | long identityToken = clearCallingIdentity(); |
| 2537 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2538 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2539 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2540 | } finally { |
| 2541 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2542 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2543 | } |
| 2544 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2545 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2546 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2547 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2548 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2549 | Log.v(TAG, "setAuthToken: " + account |
| 2550 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2551 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2552 | + ", pid " + Binder.getCallingPid()); |
| 2553 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2554 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2555 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2556 | int userId = UserHandle.getCallingUserId(); |
| 2557 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2558 | String msg = String.format( |
| 2559 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2560 | callingUid, |
| 2561 | account.type); |
| 2562 | throw new SecurityException(msg); |
| 2563 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2564 | long identityToken = clearCallingIdentity(); |
| 2565 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2566 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2567 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2568 | } finally { |
| 2569 | restoreCallingIdentity(identityToken); |
| 2570 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2571 | } |
| 2572 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2573 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2574 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2575 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2576 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2577 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2578 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2579 | + ", pid " + Binder.getCallingPid()); |
| 2580 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2581 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2582 | int userId = UserHandle.getCallingUserId(); |
| 2583 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2584 | String msg = String.format( |
| 2585 | "uid %s cannot set secrets for accounts of type: %s", |
| 2586 | callingUid, |
| 2587 | account.type); |
| 2588 | throw new SecurityException(msg); |
| 2589 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2590 | long identityToken = clearCallingIdentity(); |
| 2591 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2592 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2593 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2594 | } finally { |
| 2595 | restoreCallingIdentity(identityToken); |
| 2596 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2597 | } |
| 2598 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2599 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2600 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2601 | if (account == null) { |
| 2602 | return; |
| 2603 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2604 | boolean isChanged = false; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2605 | synchronized (accounts.dbLock) { |
| 2606 | synchronized (accounts.cacheLock) { |
| 2607 | accounts.accountsDb.beginTransaction(); |
| 2608 | try { |
| 2609 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2610 | if (accountId >= 0) { |
| 2611 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2612 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
| 2613 | accounts.authTokenCache.remove(account); |
| 2614 | accounts.accountTokenCaches.remove(account); |
| 2615 | accounts.accountsDb.setTransactionSuccessful(); |
| 2616 | // If there is an account whose password will be updated and the database |
| 2617 | // transactions succeed, then we say that a change has occured. Even if the |
| 2618 | // new password is the same as the old and there were no authtokens to |
| 2619 | // delete. |
| 2620 | isChanged = true; |
| 2621 | String action = (password == null || password.length() == 0) ? |
| 2622 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2623 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2624 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, |
| 2625 | callingUid); |
| 2626 | } |
| 2627 | } finally { |
| 2628 | accounts.accountsDb.endTransaction(); |
| 2629 | if (isChanged) { |
| 2630 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
| 2631 | sendNotificationAccountUpdated(account, accounts); |
| 2632 | sendAccountsChangedBroadcast(accounts.userId); |
| 2633 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2634 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2635 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2636 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2637 | } |
| 2638 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2639 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2640 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2641 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2642 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2643 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2644 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2645 | + ", pid " + Binder.getCallingPid()); |
| 2646 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2647 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2648 | int userId = UserHandle.getCallingUserId(); |
| 2649 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2650 | String msg = String.format( |
| 2651 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2652 | callingUid, |
| 2653 | account.type); |
| 2654 | throw new SecurityException(msg); |
| 2655 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2656 | long identityToken = clearCallingIdentity(); |
| 2657 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2658 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2659 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2660 | } finally { |
| 2661 | restoreCallingIdentity(identityToken); |
| 2662 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2663 | } |
| 2664 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2665 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2666 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2667 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2668 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2669 | Log.v(TAG, "setUserData: " + account |
| 2670 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2671 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2672 | + ", pid " + Binder.getCallingPid()); |
| 2673 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2674 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2675 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2676 | int userId = UserHandle.getCallingUserId(); |
| 2677 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2678 | String msg = String.format( |
| 2679 | "uid %s cannot set user data for accounts of type: %s", |
| 2680 | callingUid, |
| 2681 | account.type); |
| 2682 | throw new SecurityException(msg); |
| 2683 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2684 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2685 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2686 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2687 | if (!accountExistsCache(accounts, account)) { |
| 2688 | return; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2689 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2690 | setUserdataInternal(accounts, account, key, value); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2691 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2692 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2696 | private boolean accountExistsCache(UserAccounts accounts, Account account) { |
| 2697 | synchronized (accounts.cacheLock) { |
| 2698 | if (accounts.accountCache.containsKey(account.type)) { |
| 2699 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2700 | if (acc.name.equals(account.name)) { |
| 2701 | return true; |
| 2702 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2703 | } |
| 2704 | } |
| 2705 | } |
| 2706 | return false; |
| 2707 | } |
| 2708 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2709 | private void setUserdataInternal(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2710 | String value) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2711 | synchronized (accounts.dbLock) { |
| 2712 | accounts.accountsDb.beginTransaction(); |
| 2713 | try { |
| 2714 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2715 | if (accountId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2716 | return; |
| 2717 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2718 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
| 2719 | if (extrasId < 0) { |
| 2720 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
| 2721 | if (extrasId < 0) { |
| 2722 | return; |
| 2723 | } |
| 2724 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
| 2725 | return; |
| 2726 | } |
| 2727 | accounts.accountsDb.setTransactionSuccessful(); |
| 2728 | } finally { |
| 2729 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2730 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2731 | synchronized (accounts.cacheLock) { |
| 2732 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2733 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2734 | } |
| 2735 | } |
| 2736 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2737 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2738 | if (result == null) { |
| 2739 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2740 | } |
| 2741 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2742 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2743 | + response); |
| 2744 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2745 | try { |
| 2746 | response.onResult(result); |
| 2747 | } catch (RemoteException e) { |
| 2748 | // if the caller is dead then there is no one to care about remote |
| 2749 | // exceptions |
| 2750 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2751 | Log.v(TAG, "failure while notifying response", e); |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2756 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2757 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2758 | final String authTokenType) |
| 2759 | throws RemoteException { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2760 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
| 2761 | Preconditions.checkArgument(authTokenType != null, "authTokenType cannot be null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2762 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2763 | final int callingUid = getCallingUid(); |
| 2764 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2765 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2766 | throw new SecurityException("can only call from system"); |
| 2767 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2768 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2769 | long identityToken = clearCallingIdentity(); |
| 2770 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2771 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2772 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2773 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2774 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2775 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2776 | protected String toDebugString(long now) { |
| 2777 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2778 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2779 | + ", authTokenType " + authTokenType; |
| 2780 | } |
| 2781 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2782 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2783 | public void run() throws RemoteException { |
| 2784 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2785 | } |
| 2786 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2787 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2788 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2789 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2790 | if (result != null) { |
| 2791 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2792 | Bundle bundle = new Bundle(); |
| 2793 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2794 | super.onResult(bundle); |
| 2795 | return; |
| 2796 | } else { |
| 2797 | super.onResult(result); |
| 2798 | } |
| 2799 | } |
| 2800 | }.bind(); |
| 2801 | } finally { |
| 2802 | restoreCallingIdentity(identityToken); |
| 2803 | } |
| 2804 | } |
| 2805 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2806 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2807 | public void getAuthToken( |
| 2808 | IAccountManagerResponse response, |
| 2809 | final Account account, |
| 2810 | final String authTokenType, |
| 2811 | final boolean notifyOnAuthFailure, |
| 2812 | final boolean expectActivityLaunch, |
| 2813 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2814 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2815 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2816 | Log.v(TAG, "getAuthToken: " + account |
| 2817 | + ", response " + response |
| 2818 | + ", authTokenType " + authTokenType |
| 2819 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2820 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2821 | + ", caller's uid " + Binder.getCallingUid() |
| 2822 | + ", pid " + Binder.getCallingPid()); |
| 2823 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2824 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2825 | try { |
| 2826 | if (account == null) { |
| 2827 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2828 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2829 | return; |
| 2830 | } |
| 2831 | if (authTokenType == null) { |
| 2832 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2833 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2834 | return; |
| 2835 | } |
| 2836 | } catch (RemoteException e) { |
| 2837 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2838 | return; |
| 2839 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2840 | int userId = UserHandle.getCallingUserId(); |
| 2841 | long ident = Binder.clearCallingIdentity(); |
| 2842 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2843 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2844 | try { |
| 2845 | accounts = getUserAccounts(userId); |
| 2846 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2847 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2848 | } finally { |
| 2849 | Binder.restoreCallingIdentity(ident); |
| 2850 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2851 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2852 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2853 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2854 | |
| 2855 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2856 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2857 | final boolean permissionGranted = |
| 2858 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2859 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2860 | // Get the calling package. We will use it for the purpose of caching. |
| 2861 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2862 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2863 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2864 | try { |
| 2865 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2866 | } finally { |
| 2867 | Binder.restoreCallingIdentity(ident); |
| 2868 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2869 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2870 | String msg = String.format( |
| 2871 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2872 | callerUid, |
| 2873 | callerPkg); |
| 2874 | throw new SecurityException(msg); |
| 2875 | } |
| 2876 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2877 | // let authenticator know the identity of the caller |
| 2878 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2879 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2880 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2881 | if (notifyOnAuthFailure) { |
| 2882 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2883 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2884 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2885 | long identityToken = clearCallingIdentity(); |
| 2886 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2887 | // Distill the caller's package signatures into a single digest. |
| 2888 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2889 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2890 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2891 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2892 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2893 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2894 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2895 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2896 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2897 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2898 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2899 | onResult(response, result); |
| 2900 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2901 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2902 | } |
| 2903 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2904 | if (customTokens) { |
| 2905 | /* |
| 2906 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2907 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2908 | * ANDORID_PACKAGE_NAME. |
| 2909 | */ |
| 2910 | String token = readCachedTokenInternal( |
| 2911 | accounts, |
| 2912 | account, |
| 2913 | authTokenType, |
| 2914 | callerPkg, |
| 2915 | callerPkgSigDigest); |
| 2916 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2917 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2918 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2919 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2920 | Bundle result = new Bundle(); |
| 2921 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2922 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2923 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2924 | onResult(response, result); |
| 2925 | return; |
| 2926 | } |
| 2927 | } |
| 2928 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2929 | new Session( |
| 2930 | accounts, |
| 2931 | response, |
| 2932 | account.type, |
| 2933 | expectActivityLaunch, |
| 2934 | false /* stripAuthTokenFromResult */, |
| 2935 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2936 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2937 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2938 | protected String toDebugString(long now) { |
| 2939 | if (loginOptions != null) loginOptions.keySet(); |
| 2940 | return super.toDebugString(now) + ", getAuthToken" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 2941 | + ", " + account.toSafeString() |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2942 | + ", authTokenType " + authTokenType |
| 2943 | + ", loginOptions " + loginOptions |
| 2944 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2945 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2946 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2947 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2948 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2949 | // If the caller doesn't have permission then create and return the |
| 2950 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2951 | if (!permissionGranted) { |
| 2952 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2953 | } else { |
| 2954 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2955 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2958 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2959 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2960 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2961 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2962 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2963 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2964 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2965 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2966 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2967 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2968 | authTokenType, |
| 2969 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2970 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2971 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2972 | onResult(bundle); |
| 2973 | return; |
| 2974 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2975 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2976 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2977 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2978 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2979 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2980 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2981 | "the type and name should not be empty"); |
| 2982 | return; |
| 2983 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2984 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2985 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2986 | saveAuthTokenToDatabase( |
| 2987 | mAccounts, |
| 2988 | resultAccount, |
| 2989 | authTokenType, |
| 2990 | authToken); |
| 2991 | } |
| 2992 | long expiryMillis = result.getLong( |
| 2993 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2994 | if (customTokens |
| 2995 | && expiryMillis > System.currentTimeMillis()) { |
| 2996 | saveCachedToken( |
| 2997 | mAccounts, |
| 2998 | account, |
| 2999 | callerPkg, |
| 3000 | callerPkgSigDigest, |
| 3001 | authTokenType, |
| 3002 | authToken, |
| 3003 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 3004 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3005 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3006 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3007 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 3008 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 3009 | /* |
| 3010 | * Make sure that the supplied intent is owned by the authenticator |
| 3011 | * giving it to the system. Otherwise a malicious authenticator could |
| 3012 | * have users launching arbitrary activities by tricking users to |
| 3013 | * interact with malicious notifications. |
| 3014 | */ |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 3015 | if (!checkKeyIntent( |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 3016 | Binder.getCallingUid(), |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 3017 | intent)) { |
| 3018 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3019 | "invalid intent in bundle returned"); |
| 3020 | return; |
| 3021 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3022 | doNotification( |
| 3023 | mAccounts, |
| 3024 | account, |
| 3025 | result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3026 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3027 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3028 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3029 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3030 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3031 | }.bind(); |
| 3032 | } finally { |
| 3033 | restoreCallingIdentity(identityToken); |
| 3034 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 3037 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 3038 | MessageDigest digester; |
| 3039 | try { |
| 3040 | digester = MessageDigest.getInstance("SHA-256"); |
| 3041 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 3042 | callerPkg, PackageManager.GET_SIGNATURES); |
| 3043 | for (Signature sig : pkgInfo.signatures) { |
| 3044 | digester.update(sig.toByteArray()); |
| 3045 | } |
| 3046 | } catch (NoSuchAlgorithmException x) { |
| 3047 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 3048 | digester = null; |
| 3049 | } catch (NameNotFoundException e) { |
| 3050 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 3051 | digester = null; |
| 3052 | } |
| 3053 | return (digester == null) ? null : digester.digest(); |
| 3054 | } |
| 3055 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3056 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3057 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3058 | int uid = intent.getIntExtra( |
| 3059 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 3060 | String authTokenType = intent.getStringExtra( |
| 3061 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 3062 | final String titleAndSubtitle = |
| 3063 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 3064 | account.name); |
| 3065 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 3066 | String title = titleAndSubtitle; |
| 3067 | String subtitle = ""; |
| 3068 | if (index > 0) { |
| 3069 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 3070 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 3071 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 3072 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 3073 | Context contextForUser = getContextForUser(user); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3074 | Notification n = |
| 3075 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
| 3076 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 3077 | .setWhen(0) |
| 3078 | .setColor(contextForUser.getColor( |
| 3079 | com.android.internal.R.color.system_notification_accent_color)) |
| 3080 | .setContentTitle(title) |
| 3081 | .setContentText(subtitle) |
| 3082 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 3083 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 3084 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3085 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3086 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3089 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 3090 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 3091 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3092 | |
| 3093 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 3094 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3095 | if (startInNewTask) { |
| 3096 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 3097 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 3098 | // the intent from a non-Activity context. This is the default behavior. |
| 3099 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3100 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3101 | intent.addCategory(getCredentialPermissionNotificationId(account, |
| 3102 | authTokenType, uid).mTag + (packageName != null ? packageName : "")); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3103 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3104 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 3105 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3106 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 3107 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3108 | return intent; |
| 3109 | } |
| 3110 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3111 | private NotificationId getCredentialPermissionNotificationId(Account account, |
| 3112 | String authTokenType, int uid) { |
| 3113 | NotificationId nId; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3114 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3115 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3116 | final Pair<Pair<Account, String>, Integer> key = |
| 3117 | new Pair<Pair<Account, String>, Integer>( |
| 3118 | new Pair<Account, String>(account, authTokenType), uid); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3119 | nId = accounts.credentialsPermissionNotificationIds.get(key); |
| 3120 | if (nId == null) { |
| 3121 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION |
| 3122 | + ":" + account.hashCode() + ":" + authTokenType.hashCode(); |
| 3123 | int id = SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION; |
| 3124 | nId = new NotificationId(tag, id); |
| 3125 | accounts.credentialsPermissionNotificationIds.put(key, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3126 | } |
| 3127 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3128 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3129 | } |
| 3130 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3131 | private NotificationId getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
| 3132 | NotificationId nId; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3133 | synchronized (accounts.signinRequiredNotificationIds) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3134 | nId = accounts.signinRequiredNotificationIds.get(account); |
| 3135 | if (nId == null) { |
| 3136 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN |
| 3137 | + ":" + account.hashCode(); |
| 3138 | int id = SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN; |
| 3139 | nId = new NotificationId(tag, id); |
| 3140 | accounts.signinRequiredNotificationIds.put(account, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3141 | } |
| 3142 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3143 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3144 | } |
| 3145 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3146 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3147 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3148 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3149 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3150 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3151 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3152 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3153 | + ", response " + response |
| 3154 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3155 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3156 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3157 | + ", caller's uid " + Binder.getCallingUid() |
| 3158 | + ", pid " + Binder.getCallingPid()); |
| 3159 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3160 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3161 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3162 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3163 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3164 | final int uid = Binder.getCallingUid(); |
| 3165 | final int userId = UserHandle.getUserId(uid); |
| 3166 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3167 | try { |
| 3168 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3169 | "User is not allowed to add an account!"); |
| 3170 | } catch (RemoteException re) { |
| 3171 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3172 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3173 | return; |
| 3174 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3175 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 3176 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3177 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3178 | "User cannot modify accounts of this type (policy)."); |
| 3179 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 3180 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3181 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3182 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3183 | return; |
| 3184 | } |
| 3185 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3186 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3187 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3188 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3189 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3190 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3191 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3192 | long identityToken = clearCallingIdentity(); |
| 3193 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3194 | UserAccounts accounts = getUserAccounts(usrId); |
| 3195 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3196 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3197 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3198 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3199 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3200 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3201 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3202 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 3203 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3204 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3205 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3206 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3207 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3208 | protected String toDebugString(long now) { |
| 3209 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3210 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3211 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3212 | } |
| 3213 | }.bind(); |
| 3214 | } finally { |
| 3215 | restoreCallingIdentity(identityToken); |
| 3216 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3217 | } |
| 3218 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3219 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3220 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 3221 | final String authTokenType, final String[] requiredFeatures, |
| 3222 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3223 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3224 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3225 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3226 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3227 | + ", response " + response |
| 3228 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3229 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3230 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3231 | + ", caller's uid " + Binder.getCallingUid() |
| 3232 | + ", pid " + Binder.getCallingPid() |
| 3233 | + ", for user id " + userId); |
| 3234 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3235 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3236 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3237 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3238 | if (isCrossUser(callingUid, userId)) { |
| 3239 | throw new SecurityException( |
| 3240 | String.format( |
| 3241 | "User %s trying to add account for %s" , |
| 3242 | UserHandle.getCallingUserId(), |
| 3243 | userId)); |
| 3244 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3245 | |
| 3246 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3247 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3248 | try { |
| 3249 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3250 | "User is not allowed to add an account!"); |
| 3251 | } catch (RemoteException re) { |
| 3252 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3253 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3254 | return; |
| 3255 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3256 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3257 | try { |
| 3258 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3259 | "User cannot modify accounts of this type (policy)."); |
| 3260 | } catch (RemoteException re) { |
| 3261 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3262 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3263 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3264 | return; |
| 3265 | } |
| 3266 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3267 | final int pid = Binder.getCallingPid(); |
| 3268 | final int uid = Binder.getCallingUid(); |
| 3269 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3270 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3271 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3272 | |
| 3273 | long identityToken = clearCallingIdentity(); |
| 3274 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3275 | UserAccounts accounts = getUserAccounts(userId); |
| 3276 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3277 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
Valentin Iftime | 0e7f5bb | 2019-10-30 19:29:33 +0100 | [diff] [blame] | 3278 | uid); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3279 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3280 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3281 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3282 | @Override |
| 3283 | public void run() throws RemoteException { |
| 3284 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3285 | options); |
| 3286 | } |
| 3287 | |
| 3288 | @Override |
| 3289 | protected String toDebugString(long now) { |
| 3290 | return super.toDebugString(now) + ", addAccount" |
| 3291 | + ", accountType " + accountType |
| 3292 | + ", requiredFeatures " |
| 3293 | + (requiredFeatures != null |
| 3294 | ? TextUtils.join(",", requiredFeatures) |
| 3295 | : null); |
| 3296 | } |
| 3297 | }.bind(); |
| 3298 | } finally { |
| 3299 | restoreCallingIdentity(identityToken); |
| 3300 | } |
| 3301 | } |
| 3302 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3303 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3304 | public void startAddAccountSession( |
| 3305 | final IAccountManagerResponse response, |
| 3306 | final String accountType, |
| 3307 | final String authTokenType, |
| 3308 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3309 | final boolean expectActivityLaunch, |
| 3310 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3311 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3312 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3313 | Log.v(TAG, |
| 3314 | "startAddAccountSession: accountType " + accountType |
| 3315 | + ", response " + response |
| 3316 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3317 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3318 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3319 | + ", caller's uid " + Binder.getCallingUid() |
| 3320 | + ", pid " + Binder.getCallingPid()); |
| 3321 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3322 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3323 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3324 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3325 | final int uid = Binder.getCallingUid(); |
| 3326 | final int userId = UserHandle.getUserId(uid); |
| 3327 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3328 | try { |
| 3329 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3330 | "User is not allowed to add an account!"); |
| 3331 | } catch (RemoteException re) { |
| 3332 | } |
| 3333 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3334 | return; |
| 3335 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3336 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3337 | try { |
| 3338 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3339 | "User cannot modify accounts of this type (policy)."); |
| 3340 | } catch (RemoteException re) { |
| 3341 | } |
| 3342 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3343 | userId); |
| 3344 | return; |
| 3345 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3346 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3347 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3348 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3349 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3350 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3351 | // Check to see if the Password should be included to the caller. |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 3352 | String callerPkg = options.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3353 | boolean isPasswordForwardingAllowed = checkPermissionAndNote( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3354 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3355 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3356 | long identityToken = clearCallingIdentity(); |
| 3357 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3358 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3359 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3360 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3361 | new StartAccountSession( |
| 3362 | accounts, |
| 3363 | response, |
| 3364 | accountType, |
| 3365 | expectActivityLaunch, |
| 3366 | null /* accountName */, |
| 3367 | false /* authDetailsRequired */, |
| 3368 | true /* updateLastAuthenticationTime */, |
| 3369 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3370 | @Override |
| 3371 | public void run() throws RemoteException { |
| 3372 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3373 | requiredFeatures, options); |
| 3374 | } |
| 3375 | |
| 3376 | @Override |
| 3377 | protected String toDebugString(long now) { |
| 3378 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3379 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3380 | + accountType + ", requiredFeatures " |
| 3381 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3382 | } |
| 3383 | }.bind(); |
| 3384 | } finally { |
| 3385 | restoreCallingIdentity(identityToken); |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3390 | private abstract class StartAccountSession extends Session { |
| 3391 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3392 | private final boolean mIsPasswordForwardingAllowed; |
| 3393 | |
| 3394 | public StartAccountSession( |
| 3395 | UserAccounts accounts, |
| 3396 | IAccountManagerResponse response, |
| 3397 | String accountType, |
| 3398 | boolean expectActivityLaunch, |
| 3399 | String accountName, |
| 3400 | boolean authDetailsRequired, |
| 3401 | boolean updateLastAuthenticationTime, |
| 3402 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3403 | super(accounts, response, accountType, expectActivityLaunch, |
| 3404 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3405 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3406 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | @Override |
| 3410 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3411 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3412 | mNumResults++; |
| 3413 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3414 | if (result != null |
| 3415 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 3416 | if (!checkKeyIntent( |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3417 | Binder.getCallingUid(), |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 3418 | intent)) { |
| 3419 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3420 | "invalid intent in bundle returned"); |
| 3421 | return; |
| 3422 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3423 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3424 | IAccountManagerResponse response; |
| 3425 | if (mExpectActivityLaunch && result != null |
| 3426 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3427 | response = mResponse; |
| 3428 | } else { |
| 3429 | response = getResponseAndClose(); |
| 3430 | } |
| 3431 | if (response == null) { |
| 3432 | return; |
| 3433 | } |
| 3434 | if (result == null) { |
| 3435 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3436 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3437 | + response); |
| 3438 | } |
| 3439 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3440 | "null bundle returned"); |
| 3441 | return; |
| 3442 | } |
| 3443 | |
| 3444 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3445 | // All AccountManager error codes are greater |
| 3446 | // than 0 |
| 3447 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3448 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3449 | return; |
| 3450 | } |
| 3451 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3452 | // Omit passwords if the caller isn't permitted to see them. |
| 3453 | if (!mIsPasswordForwardingAllowed) { |
| 3454 | result.remove(AccountManager.KEY_PASSWORD); |
| 3455 | } |
| 3456 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3457 | // Strip auth token from result. |
| 3458 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3459 | |
| 3460 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3461 | Log.v(TAG, |
| 3462 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3463 | } |
| 3464 | |
| 3465 | // Get the session bundle created by authenticator. The |
| 3466 | // bundle contains data necessary for finishing the session |
| 3467 | // later. The session bundle will be encrypted here and |
| 3468 | // decrypted later when trying to finish the session. |
| 3469 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3470 | if (sessionBundle != null) { |
| 3471 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3472 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3473 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3474 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3475 | } |
| 3476 | // Add accountType info to session bundle. This will |
| 3477 | // override any value set by authenticator. |
| 3478 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3479 | |
| 3480 | // Encrypt session bundle before returning to caller. |
| 3481 | try { |
| 3482 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3483 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3484 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3485 | } catch (GeneralSecurityException e) { |
| 3486 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3487 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3488 | } |
| 3489 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3490 | "failed to encrypt session bundle"); |
| 3491 | return; |
| 3492 | } |
| 3493 | } |
| 3494 | |
| 3495 | sendResponse(response, result); |
| 3496 | } |
| 3497 | } |
| 3498 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3499 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3500 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3501 | @NonNull Bundle sessionBundle, |
| 3502 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3503 | Bundle appInfo, |
| 3504 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3505 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3506 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3507 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3508 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3509 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3510 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3511 | + ", caller's uid " + callingUid |
| 3512 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3513 | + ", pid " + Binder.getCallingPid() |
| 3514 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3515 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3516 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3517 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3518 | // Account type is added to it before encryption. |
| 3519 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3520 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3521 | } |
| 3522 | |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 3523 | // Only allow the system process to finish session for other users. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3524 | if (isCrossUser(callingUid, userId)) { |
| 3525 | throw new SecurityException( |
| 3526 | String.format( |
| 3527 | "User %s trying to finish session for %s without cross user permission", |
| 3528 | UserHandle.getCallingUserId(), |
| 3529 | userId)); |
| 3530 | } |
| 3531 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3532 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3533 | sendErrorResponse(response, |
| 3534 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3535 | "User is not allowed to add an account!"); |
| 3536 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3537 | return; |
| 3538 | } |
| 3539 | |
| 3540 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3541 | final Bundle decryptedBundle; |
| 3542 | final String accountType; |
| 3543 | // First decrypt session bundle to get account type for checking permission. |
| 3544 | try { |
| 3545 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3546 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3547 | if (decryptedBundle == null) { |
| 3548 | sendErrorResponse( |
| 3549 | response, |
| 3550 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3551 | "failed to decrypt session bundle"); |
| 3552 | return; |
| 3553 | } |
| 3554 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3555 | // Account type cannot be null. This should not happen if session bundle was created |
| 3556 | // properly by #StartAccountSession. |
| 3557 | if (TextUtils.isEmpty(accountType)) { |
| 3558 | sendErrorResponse( |
| 3559 | response, |
| 3560 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3561 | "accountType is empty"); |
| 3562 | return; |
| 3563 | } |
| 3564 | |
| 3565 | // If by any chances, decryptedBundle contains colliding keys with |
| 3566 | // system info |
| 3567 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3568 | // update credentials flow, we should replace with the new values of the current call. |
| 3569 | if (appInfo != null) { |
| 3570 | decryptedBundle.putAll(appInfo); |
| 3571 | } |
| 3572 | |
| 3573 | // 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] | 3574 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3575 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3576 | } catch (GeneralSecurityException e) { |
| 3577 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3578 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3579 | } |
| 3580 | sendErrorResponse( |
| 3581 | response, |
| 3582 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3583 | "failed to decrypt session bundle"); |
| 3584 | return; |
| 3585 | } |
| 3586 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3587 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3588 | sendErrorResponse( |
| 3589 | response, |
| 3590 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3591 | "User cannot modify accounts of this type (policy)."); |
| 3592 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3593 | userId); |
| 3594 | return; |
| 3595 | } |
| 3596 | |
| 3597 | long identityToken = clearCallingIdentity(); |
| 3598 | try { |
| 3599 | UserAccounts accounts = getUserAccounts(userId); |
| 3600 | logRecordWithUid( |
| 3601 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3602 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3603 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3604 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3605 | new Session( |
| 3606 | accounts, |
| 3607 | response, |
| 3608 | accountType, |
| 3609 | expectActivityLaunch, |
| 3610 | true /* stripAuthTokenFromResult */, |
| 3611 | null /* accountName */, |
| 3612 | false /* authDetailsRequired */, |
| 3613 | true /* updateLastAuthenticationTime */) { |
| 3614 | @Override |
| 3615 | public void run() throws RemoteException { |
| 3616 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3617 | } |
| 3618 | |
| 3619 | @Override |
| 3620 | protected String toDebugString(long now) { |
| 3621 | return super.toDebugString(now) |
| 3622 | + ", finishSession" |
| 3623 | + ", accountType " + accountType; |
| 3624 | } |
| 3625 | }.bind(); |
| 3626 | } finally { |
| 3627 | restoreCallingIdentity(identityToken); |
| 3628 | } |
| 3629 | } |
| 3630 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3631 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3632 | final DevicePolicyManagerInternal dpmi = |
| 3633 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3634 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3635 | if (dpmi == null) { |
| 3636 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3637 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3638 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3639 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3640 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3641 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3642 | } |
| 3643 | if (intent == null) { |
| 3644 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3645 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3646 | long identityToken = clearCallingIdentity(); |
| 3647 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3648 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3649 | } finally { |
| 3650 | restoreCallingIdentity(identityToken); |
| 3651 | } |
| 3652 | } |
| 3653 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3654 | /** |
| 3655 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3656 | */ |
| 3657 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3658 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3659 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3660 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3661 | return cantAddAccount; |
| 3662 | } |
| 3663 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3664 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3665 | public void confirmCredentialsAsUser( |
| 3666 | IAccountManagerResponse response, |
| 3667 | final Account account, |
| 3668 | final Bundle options, |
| 3669 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3670 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3671 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3672 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3673 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3674 | Log.v(TAG, "confirmCredentials: " + account |
| 3675 | + ", response " + response |
| 3676 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3677 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3678 | + ", pid " + Binder.getCallingPid()); |
| 3679 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3680 | // Only allow the system process to read accounts of other users |
| 3681 | if (isCrossUser(callingUid, userId)) { |
| 3682 | throw new SecurityException( |
| 3683 | String.format( |
| 3684 | "User %s trying to confirm account credentials for %s" , |
| 3685 | UserHandle.getCallingUserId(), |
| 3686 | userId)); |
| 3687 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3688 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3689 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3690 | long identityToken = clearCallingIdentity(); |
| 3691 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3692 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3693 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3694 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3695 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3696 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3697 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3698 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3699 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3700 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3701 | protected String toDebugString(long now) { |
| 3702 | return super.toDebugString(now) + ", confirmCredentials" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 3703 | + ", " + account.toSafeString(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3704 | } |
| 3705 | }.bind(); |
| 3706 | } finally { |
| 3707 | restoreCallingIdentity(identityToken); |
| 3708 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3709 | } |
| 3710 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3711 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3712 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3713 | final String authTokenType, final boolean expectActivityLaunch, |
| 3714 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3715 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3716 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3717 | Log.v(TAG, "updateCredentials: " + account |
| 3718 | + ", response " + response |
| 3719 | + ", authTokenType " + authTokenType |
| 3720 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3721 | + ", caller's uid " + Binder.getCallingUid() |
| 3722 | + ", pid " + Binder.getCallingPid()); |
| 3723 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3724 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3725 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3726 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3727 | long identityToken = clearCallingIdentity(); |
| 3728 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3729 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3730 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3731 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3732 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
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 | public void run() throws RemoteException { |
| 3735 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3736 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3737 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3738 | protected String toDebugString(long now) { |
| 3739 | if (loginOptions != null) loginOptions.keySet(); |
| 3740 | return super.toDebugString(now) + ", updateCredentials" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 3741 | + ", " + account.toSafeString() |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3742 | + ", authTokenType " + authTokenType |
| 3743 | + ", loginOptions " + loginOptions; |
| 3744 | } |
| 3745 | }.bind(); |
| 3746 | } finally { |
| 3747 | restoreCallingIdentity(identityToken); |
| 3748 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3751 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3752 | public void startUpdateCredentialsSession( |
| 3753 | IAccountManagerResponse response, |
| 3754 | final Account account, |
| 3755 | final String authTokenType, |
| 3756 | final boolean expectActivityLaunch, |
| 3757 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3758 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3759 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3760 | Log.v(TAG, |
| 3761 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3762 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3763 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3764 | + ", pid " + Binder.getCallingPid()); |
| 3765 | } |
| 3766 | if (response == null) { |
| 3767 | throw new IllegalArgumentException("response is null"); |
| 3768 | } |
| 3769 | if (account == null) { |
| 3770 | throw new IllegalArgumentException("account is null"); |
| 3771 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3772 | |
| 3773 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3774 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3775 | |
| 3776 | // Check to see if the Password should be included to the caller. |
| 3777 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 3778 | boolean isPasswordForwardingAllowed = checkPermissionAndNote( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3779 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3780 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3781 | long identityToken = clearCallingIdentity(); |
| 3782 | try { |
| 3783 | UserAccounts accounts = getUserAccounts(userId); |
| 3784 | new StartAccountSession( |
| 3785 | accounts, |
| 3786 | response, |
| 3787 | account.type, |
| 3788 | expectActivityLaunch, |
| 3789 | account.name, |
| 3790 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3791 | true /* updateLastCredentialTime */, |
| 3792 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3793 | @Override |
| 3794 | public void run() throws RemoteException { |
| 3795 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3796 | loginOptions); |
| 3797 | } |
| 3798 | |
| 3799 | @Override |
| 3800 | protected String toDebugString(long now) { |
| 3801 | if (loginOptions != null) |
| 3802 | loginOptions.keySet(); |
| 3803 | return super.toDebugString(now) |
| 3804 | + ", startUpdateCredentialsSession" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 3805 | + ", " + account.toSafeString() |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3806 | + ", authTokenType " + authTokenType |
| 3807 | + ", loginOptions " + loginOptions; |
| 3808 | } |
| 3809 | }.bind(); |
| 3810 | } finally { |
| 3811 | restoreCallingIdentity(identityToken); |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3816 | public void isCredentialsUpdateSuggested( |
| 3817 | IAccountManagerResponse response, |
| 3818 | final Account account, |
| 3819 | final String statusToken) { |
| 3820 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3821 | Log.v(TAG, |
| 3822 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3823 | + ", caller's uid " + Binder.getCallingUid() |
| 3824 | + ", pid " + Binder.getCallingPid()); |
| 3825 | } |
| 3826 | if (response == null) { |
| 3827 | throw new IllegalArgumentException("response is null"); |
| 3828 | } |
| 3829 | if (account == null) { |
| 3830 | throw new IllegalArgumentException("account is null"); |
| 3831 | } |
| 3832 | if (TextUtils.isEmpty(statusToken)) { |
| 3833 | throw new IllegalArgumentException("status token is empty"); |
| 3834 | } |
| 3835 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3836 | int usrId = UserHandle.getCallingUserId(); |
| 3837 | long identityToken = clearCallingIdentity(); |
| 3838 | try { |
| 3839 | UserAccounts accounts = getUserAccounts(usrId); |
| 3840 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3841 | false /* stripAuthTokenFromResult */, account.name, |
| 3842 | false /* authDetailsRequired */) { |
| 3843 | @Override |
| 3844 | protected String toDebugString(long now) { |
| 3845 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 3846 | + ", " + account.toSafeString(); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | @Override |
| 3850 | public void run() throws RemoteException { |
| 3851 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3852 | } |
| 3853 | |
| 3854 | @Override |
| 3855 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3856 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3857 | IAccountManagerResponse response = getResponseAndClose(); |
| 3858 | if (response == null) { |
| 3859 | return; |
| 3860 | } |
| 3861 | |
| 3862 | if (result == null) { |
| 3863 | sendErrorResponse( |
| 3864 | response, |
| 3865 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3866 | "null bundle"); |
| 3867 | return; |
| 3868 | } |
| 3869 | |
| 3870 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3871 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3872 | + response); |
| 3873 | } |
| 3874 | // Check to see if an error occurred. We know if an error occurred because all |
| 3875 | // error codes are greater than 0. |
| 3876 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3877 | sendErrorResponse(response, |
| 3878 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3879 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3880 | return; |
| 3881 | } |
| 3882 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3883 | sendErrorResponse( |
| 3884 | response, |
| 3885 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3886 | "no result in response"); |
| 3887 | return; |
| 3888 | } |
| 3889 | final Bundle newResult = new Bundle(); |
| 3890 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3891 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3892 | sendResponse(response, newResult); |
| 3893 | } |
| 3894 | }.bind(); |
| 3895 | } finally { |
| 3896 | restoreCallingIdentity(identityToken); |
| 3897 | } |
| 3898 | } |
| 3899 | |
| 3900 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3901 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3902 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3903 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3904 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3905 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3906 | + ", response " + response |
| 3907 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3908 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3909 | + ", pid " + Binder.getCallingPid()); |
| 3910 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3911 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3912 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3913 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3914 | if (!isAccountManagedByCaller(accountType, callingUid, userId) |
| 3915 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3916 | String msg = String.format( |
| 3917 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3918 | callingUid, |
| 3919 | accountType); |
| 3920 | throw new SecurityException(msg); |
| 3921 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3922 | long identityToken = clearCallingIdentity(); |
| 3923 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3924 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3925 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3926 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3927 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3928 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3929 | public void run() throws RemoteException { |
| 3930 | mAuthenticator.editProperties(this, mAccountType); |
| 3931 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3932 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3933 | protected String toDebugString(long now) { |
| 3934 | return super.toDebugString(now) + ", editProperties" |
| 3935 | + ", accountType " + accountType; |
| 3936 | } |
| 3937 | }.bind(); |
| 3938 | } finally { |
| 3939 | restoreCallingIdentity(identityToken); |
| 3940 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3941 | } |
| 3942 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3943 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3944 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3945 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3946 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3947 | throw new SecurityException("Can be called only by system UID"); |
| 3948 | } |
| 3949 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3950 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3951 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3952 | |
| 3953 | final int userId = userHandle.getIdentifier(); |
| 3954 | |
| 3955 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3956 | |
| 3957 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3958 | int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3959 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3960 | } catch (NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3961 | Log.d(TAG, "Package not found " + e.getMessage()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3962 | return false; |
| 3963 | } |
| 3964 | } |
| 3965 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3966 | // Returns package with oldest target SDK for given UID. |
| 3967 | private String getPackageNameForUid(int uid) { |
| 3968 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3969 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3970 | return null; |
| 3971 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3972 | String packageName = packageNames[0]; |
Fyodor Kupolov | 892fc8d | 2017-03-22 12:57:04 -0700 | [diff] [blame] | 3973 | if (packageNames.length == 1) { |
| 3974 | return packageName; |
| 3975 | } |
| 3976 | // Due to visibility changes we want to use package with oldest target SDK |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3977 | int oldestVersion = Integer.MAX_VALUE; |
| 3978 | for (String name : packageNames) { |
| 3979 | try { |
| 3980 | ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(name, 0); |
| 3981 | if (applicationInfo != null) { |
| 3982 | int version = applicationInfo.targetSdkVersion; |
| 3983 | if (version < oldestVersion) { |
| 3984 | oldestVersion = version; |
| 3985 | packageName = name; |
| 3986 | } |
| 3987 | } |
| 3988 | } catch (NameNotFoundException e) { |
| 3989 | // skip |
| 3990 | } |
| 3991 | } |
| 3992 | return packageName; |
| 3993 | } |
| 3994 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3995 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3996 | int uid) { |
| 3997 | if (packageName == null) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3998 | packageName = getPackageNameForUid(uid); |
| 3999 | if (packageName == null) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 4000 | return false; |
| 4001 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | // Use null token which means any token. Having a token means the package |
| 4005 | // is trusted by the authenticator, hence it is fine to access the account. |
| 4006 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 4007 | return true; |
| 4008 | } |
| 4009 | // In addition to the permissions required to get an auth token we also allow |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4010 | // the account to be accessed by apps for which user or authenticator granted visibility. |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 4011 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4012 | int visibility = resolveAccountVisibility(account, packageName, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 4013 | getUserAccounts(UserHandle.getUserId(uid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4014 | return (visibility == AccountManager.VISIBILITY_VISIBLE |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 4015 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
| 4018 | @Override |
| 4019 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 4020 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 4021 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4022 | throw new SecurityException("Can be called only by system UID"); |
| 4023 | } |
| 4024 | |
| 4025 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 4026 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 4027 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 4028 | |
| 4029 | final int userId = userHandle.getIdentifier(); |
| 4030 | |
| 4031 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 4032 | |
| 4033 | final int uid; |
| 4034 | try { |
| 4035 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4036 | } catch (NameNotFoundException e) { |
| 4037 | Slog.e(TAG, "Unknown package " + packageName); |
| 4038 | return null; |
| 4039 | } |
| 4040 | |
| 4041 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 4042 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 4043 | final long identity = Binder.clearCallingIdentity(); |
| 4044 | try { |
| 4045 | return PendingIntent.getActivityAsUser( |
| 4046 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 4047 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 4048 | null, new UserHandle(userId)).getIntentSender(); |
| 4049 | } finally { |
| 4050 | Binder.restoreCallingIdentity(identity); |
| 4051 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 4055 | int uid, RemoteCallback callback) { |
| 4056 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 4057 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 4058 | @Override |
| 4059 | public void onResult(Bundle value) throws RemoteException { |
| 4060 | handleAuthenticatorResponse(true); |
| 4061 | } |
| 4062 | |
| 4063 | @Override |
| 4064 | public void onRequestContinued() { |
| 4065 | /* ignore */ |
| 4066 | } |
| 4067 | |
| 4068 | @Override |
| 4069 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 4070 | handleAuthenticatorResponse(false); |
| 4071 | } |
| 4072 | |
| 4073 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 4074 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 4075 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4076 | UserHandle.getUserHandleForUid(uid)); |
| 4077 | if (callback != null) { |
| 4078 | Bundle result = new Bundle(); |
| 4079 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 4080 | callback.sendResult(result); |
| 4081 | } |
| 4082 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 4083 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 4087 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 4088 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 4089 | throw new SecurityException("Only system can check for accounts across users"); |
| 4090 | } |
| 4091 | final long token = Binder.clearCallingIdentity(); |
| 4092 | try { |
| 4093 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 4094 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 4095 | if (allAccounts[i].account.equals(account)) { |
| 4096 | return true; |
| 4097 | } |
| 4098 | } |
| 4099 | return false; |
| 4100 | } finally { |
| 4101 | Binder.restoreCallingIdentity(token); |
| 4102 | } |
| 4103 | } |
| 4104 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4105 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 4106 | private final String[] mFeatures; |
| 4107 | private volatile Account[] mAccountsOfType = null; |
| 4108 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 4109 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4110 | private final int mCallingUid; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4111 | private final String mPackageName; |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4112 | private final boolean mIncludeManagedNotVisible; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4113 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4114 | public GetAccountsByTypeAndFeatureSession( |
| 4115 | UserAccounts accounts, |
| 4116 | IAccountManagerResponse response, |
| 4117 | String type, |
| 4118 | String[] features, |
| 4119 | int callingUid, |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4120 | String packageName, |
| 4121 | boolean includeManagedNotVisible) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4122 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4123 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 4124 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4125 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4126 | mFeatures = features; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4127 | mPackageName = packageName; |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4128 | mIncludeManagedNotVisible = includeManagedNotVisible; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4129 | } |
| 4130 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4131 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4132 | public void run() throws RemoteException { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4133 | mAccountsOfType = getAccountsFromCache(mAccounts, mAccountType, |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4134 | mCallingUid, mPackageName, mIncludeManagedNotVisible); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4135 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 4136 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4137 | mCurrentAccount = 0; |
| 4138 | |
| 4139 | checkAccount(); |
| 4140 | } |
| 4141 | |
| 4142 | public void checkAccount() { |
| 4143 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 4144 | sendResult(); |
| 4145 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4146 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4147 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 4148 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 4149 | if (accountAuthenticator == null) { |
| 4150 | // It is possible that the authenticator has died, which is indicated by |
| 4151 | // mAuthenticator being set to null. If this happens then just abort. |
| 4152 | // There is no need to send back a result or error in this case since |
| 4153 | // that already happened when mAuthenticator was cleared. |
| 4154 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4155 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 4156 | + " connected to the authenticator, " + toDebugString()); |
| 4157 | } |
| 4158 | return; |
| 4159 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4160 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 4161 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4162 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4163 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4164 | } |
| 4165 | } |
| 4166 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4167 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4168 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4169 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4170 | mNumResults++; |
| 4171 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4172 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4173 | return; |
| 4174 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4175 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4176 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 4177 | } |
| 4178 | mCurrentAccount++; |
| 4179 | checkAccount(); |
| 4180 | } |
| 4181 | |
| 4182 | public void sendResult() { |
| 4183 | IAccountManagerResponse response = getResponseAndClose(); |
| 4184 | if (response != null) { |
| 4185 | try { |
| 4186 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 4187 | for (int i = 0; i < accounts.length; i++) { |
| 4188 | accounts[i] = mAccountsWithFeatures.get(i); |
| 4189 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4190 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4191 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 4192 | + response); |
| 4193 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4194 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4195 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4196 | response.onResult(result); |
| 4197 | } catch (RemoteException e) { |
| 4198 | // if the caller is dead then there is no one to care about remote exceptions |
| 4199 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4200 | Log.v(TAG, "failure while notifying response", e); |
| 4201 | } |
| 4202 | } |
| 4203 | } |
| 4204 | } |
| 4205 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4206 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4207 | protected String toDebugString(long now) { |
| 4208 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 4209 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 4210 | } |
| 4211 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4212 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4213 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4214 | * 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] | 4215 | * @hide |
| 4216 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4217 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4218 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4219 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4220 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4221 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4222 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4223 | if (visibleAccountTypes.isEmpty()) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4224 | return EMPTY_ACCOUNT_ARRAY; |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4225 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4226 | long identityToken = clearCallingIdentity(); |
| 4227 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4228 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4229 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4230 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4231 | callingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4232 | opPackageName, |
| 4233 | visibleAccountTypes, |
| 4234 | false /* includeUserManagedNotVisible */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4235 | } finally { |
| 4236 | restoreCallingIdentity(identityToken); |
| 4237 | } |
| 4238 | } |
| 4239 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4240 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4241 | * Returns accounts for all running users, ignores visibility values. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4242 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4243 | * @hide |
| 4244 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4245 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4246 | public AccountAndUser[] getRunningAccounts() { |
| 4247 | final int[] runningUserIds; |
| 4248 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4249 | runningUserIds = ActivityManager.getService().getRunningUserIds(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4250 | } catch (RemoteException e) { |
| 4251 | // Running in system_server; should never happen |
| 4252 | throw new RuntimeException(e); |
| 4253 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4254 | return getAccounts(runningUserIds); |
| 4255 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4256 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4257 | /** |
| 4258 | * Returns accounts for all users, ignores visibility values. |
| 4259 | * |
| 4260 | * @hide |
| 4261 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4262 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4263 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 4264 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4265 | final int[] userIds = new int[users.size()]; |
| 4266 | for (int i = 0; i < userIds.length; i++) { |
| 4267 | userIds[i] = users.get(i).id; |
| 4268 | } |
| 4269 | return getAccounts(userIds); |
| 4270 | } |
| 4271 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4272 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4273 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4274 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4275 | for (int userId : userIds) { |
| 4276 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4277 | if (userAccounts == null) continue; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4278 | Account[] accounts = getAccountsFromCache( |
| 4279 | userAccounts, |
| 4280 | null /* type */, |
| 4281 | Binder.getCallingUid(), |
| 4282 | null /* packageName */, |
| 4283 | false /* include managed not visible*/); |
| 4284 | for (Account account : accounts) { |
| 4285 | runningAccounts.add(new AccountAndUser(account, userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4286 | } |
| 4287 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4288 | |
| 4289 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4290 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4291 | } |
| 4292 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4293 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4294 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4295 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4296 | int callingUid = Binder.getCallingUid(); |
| 4297 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4298 | return getAccountsAsUserForPackage(type, userId, opPackageName /* callingPackage */, -1, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4299 | opPackageName, false /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4300 | } |
| 4301 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4302 | @NonNull |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4303 | private Account[] getAccountsAsUserForPackage( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4304 | String type, |
| 4305 | int userId, |
| 4306 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4307 | int packageUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4308 | String opPackageName, |
| 4309 | boolean includeUserManagedNotVisible) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4310 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4311 | // Only allow the system process to read accounts of other users |
| 4312 | if (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4313 | && callingUid != Process.SYSTEM_UID |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4314 | && mContext.checkCallingOrSelfPermission( |
| 4315 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4316 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4317 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4318 | + " trying to get account for " + userId); |
| 4319 | } |
| 4320 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4321 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4322 | Log.v(TAG, "getAccounts: accountType " + type |
| 4323 | + ", caller's uid " + Binder.getCallingUid() |
| 4324 | + ", pid " + Binder.getCallingPid()); |
| 4325 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4326 | |
| 4327 | // If the original calling app was using account choosing activity |
| 4328 | // provided by the framework or authenticator we'll passing in |
| 4329 | // the original caller's uid here, which is what should be used for filtering. |
| 4330 | List<String> managedTypes = |
| 4331 | getTypesManagedByCaller(callingUid, UserHandle.getUserId(callingUid)); |
| 4332 | if (packageUid != -1 && |
| 4333 | ((UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 4334 | || (type != null && managedTypes.contains(type))))) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4335 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4336 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4337 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4338 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4339 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4340 | if (visibleAccountTypes.isEmpty() |
| 4341 | || (type != null && !visibleAccountTypes.contains(type))) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4342 | return EMPTY_ACCOUNT_ARRAY; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4343 | } else if (visibleAccountTypes.contains(type)) { |
| 4344 | // Prune the list down to just the requested type. |
| 4345 | visibleAccountTypes = new ArrayList<>(); |
| 4346 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4347 | } // else aggregate all the visible accounts (it won't matter if the |
| 4348 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4349 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4350 | long identityToken = clearCallingIdentity(); |
| 4351 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4352 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 4353 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4354 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4355 | callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4356 | opPackageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4357 | visibleAccountTypes, |
| 4358 | includeUserManagedNotVisible); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4359 | } finally { |
| 4360 | restoreCallingIdentity(identityToken); |
| 4361 | } |
| 4362 | } |
| 4363 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4364 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4365 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4366 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4367 | int callingUid, |
| 4368 | String callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4369 | List<String> visibleAccountTypes, |
| 4370 | boolean includeUserManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4371 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4372 | for (String visibleType : visibleAccountTypes) { |
| 4373 | Account[] accountsForType = getAccountsFromCache( |
| 4374 | userAccounts, visibleType, callingUid, callingPackage, |
| 4375 | includeUserManagedNotVisible); |
| 4376 | if (accountsForType != null) { |
| 4377 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4378 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4379 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4380 | Account[] result = new Account[visibleAccounts.size()]; |
| 4381 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4382 | result[i] = visibleAccounts.get(i); |
| 4383 | } |
| 4384 | return result; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4387 | @Override |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4388 | public void addSharedAccountsFromParentUser(int parentUserId, int userId, |
| 4389 | String opPackageName) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4390 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4391 | Account[] accounts = getAccountsAsUser(null, parentUserId, opPackageName); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4392 | for (Account account : accounts) { |
| 4393 | addSharedAccountAsUser(account, userId); |
| 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4398 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4399 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4400 | accounts.accountsDb.deleteSharedAccount(account); |
| 4401 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4402 | if (accountId < 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 4403 | Log.w(TAG, "insertAccountIntoDatabase: " + account.toSafeString() |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4404 | + ", skipping the DB insert failed"); |
| 4405 | return false; |
| 4406 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4407 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4408 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4409 | return true; |
| 4410 | } |
| 4411 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4412 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4413 | userId = handleIncomingUser(userId); |
| 4414 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4415 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4416 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4417 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4418 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4419 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4420 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4421 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4422 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4423 | } |
| 4424 | return r > 0; |
| 4425 | } |
| 4426 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4427 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4428 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4429 | } |
| 4430 | |
| 4431 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4432 | userId = handleIncomingUser(userId); |
| 4433 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4434 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4435 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4436 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4437 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4438 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4439 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4440 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4441 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4442 | } |
| 4443 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4444 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4445 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4446 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4447 | synchronized (accounts.dbLock) { |
| 4448 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
| 4449 | Account[] accountArray = new Account[accountList.size()]; |
| 4450 | accountList.toArray(accountArray); |
| 4451 | return accountArray; |
| 4452 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4456 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4457 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4458 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4459 | } |
| 4460 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4461 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4462 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4463 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4464 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4465 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4466 | // Don't do opPackageName check - caller is system. |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4467 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4468 | + callingUid + " with uid=" + uid); |
| 4469 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4470 | return getAccountsAsUserForPackage(null, UserHandle.getCallingUserId(), packageName, uid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4471 | opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4472 | } |
| 4473 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4474 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4475 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4476 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4477 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4478 | int callingUid = Binder.getCallingUid(); |
| 4479 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4480 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4481 | int packageUid = -1; |
| 4482 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4483 | packageUid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4484 | } catch (NameNotFoundException re) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4485 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4486 | return EMPTY_ACCOUNT_ARRAY; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4487 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4488 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4489 | && (type != null && !isAccountManagedByCaller(type, callingUid, userId))) { |
| 4490 | return EMPTY_ACCOUNT_ARRAY; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4491 | } |
sunjian | d62dc39 | 2017-06-01 12:05:59 -0700 | [diff] [blame] | 4492 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) && type == null) { |
| 4493 | return getAccountsAsUserForPackage(type, userId, |
| 4494 | packageName, packageUid, opPackageName, false /* includeUserManagedNotVisible */); |
| 4495 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4496 | return getAccountsAsUserForPackage(type, userId, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4497 | packageName, packageUid, opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4498 | } |
| 4499 | |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4500 | private boolean needToStartChooseAccountActivity(Account[] accounts, String callingPackage) { |
| 4501 | if (accounts.length < 1) return false; |
| 4502 | if (accounts.length > 1) return true; |
| 4503 | Account account = accounts[0]; |
| 4504 | UserAccounts userAccounts = getUserAccounts(UserHandle.getCallingUserId()); |
| 4505 | int visibility = resolveAccountVisibility(account, callingPackage, userAccounts); |
| 4506 | if (visibility == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE) return true; |
| 4507 | return false; |
| 4508 | } |
| 4509 | |
| 4510 | private void startChooseAccountActivityWithAccounts( |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 4511 | IAccountManagerResponse response, Account[] accounts, String callingPackage) { |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4512 | Intent intent = new Intent(mContext, ChooseAccountActivity.class); |
| 4513 | intent.putExtra(AccountManager.KEY_ACCOUNTS, accounts); |
| 4514 | intent.putExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE, |
| 4515 | new AccountManagerResponse(response)); |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 4516 | intent.putExtra(AccountManager.KEY_ANDROID_PACKAGE_NAME, callingPackage); |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4517 | |
| 4518 | mContext.startActivityAsUser(intent, UserHandle.of(UserHandle.getCallingUserId())); |
| 4519 | } |
| 4520 | |
| 4521 | private void handleGetAccountsResult( |
| 4522 | IAccountManagerResponse response, |
| 4523 | Account[] accounts, |
| 4524 | String callingPackage) { |
| 4525 | |
| 4526 | if (needToStartChooseAccountActivity(accounts, callingPackage)) { |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 4527 | startChooseAccountActivityWithAccounts(response, accounts, callingPackage); |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4528 | return; |
| 4529 | } |
| 4530 | if (accounts.length == 1) { |
| 4531 | Bundle bundle = new Bundle(); |
| 4532 | bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accounts[0].name); |
| 4533 | bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accounts[0].type); |
| 4534 | onResult(response, bundle); |
| 4535 | return; |
| 4536 | } |
| 4537 | // No qualified account exists, return an empty Bundle. |
| 4538 | onResult(response, new Bundle()); |
| 4539 | } |
| 4540 | |
| 4541 | @Override |
| 4542 | public void getAccountByTypeAndFeatures( |
| 4543 | IAccountManagerResponse response, |
| 4544 | String accountType, |
| 4545 | String[] features, |
| 4546 | String opPackageName) { |
| 4547 | |
| 4548 | int callingUid = Binder.getCallingUid(); |
| 4549 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 4550 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4551 | Log.v(TAG, "getAccount: accountType " + accountType |
| 4552 | + ", response " + response |
| 4553 | + ", features " + Arrays.toString(features) |
| 4554 | + ", caller's uid " + callingUid |
| 4555 | + ", pid " + Binder.getCallingPid()); |
| 4556 | } |
| 4557 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4558 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 4559 | |
| 4560 | int userId = UserHandle.getCallingUserId(); |
| 4561 | |
| 4562 | long identityToken = clearCallingIdentity(); |
| 4563 | try { |
| 4564 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4565 | if (ArrayUtils.isEmpty(features)) { |
| 4566 | Account[] accountsWithManagedNotVisible = getAccountsFromCache( |
| 4567 | userAccounts, accountType, callingUid, opPackageName, |
| 4568 | true /* include managed not visible */); |
| 4569 | handleGetAccountsResult( |
| 4570 | response, accountsWithManagedNotVisible, opPackageName); |
| 4571 | return; |
| 4572 | } |
| 4573 | |
| 4574 | IAccountManagerResponse retrieveAccountsResponse = |
| 4575 | new IAccountManagerResponse.Stub() { |
| 4576 | @Override |
| 4577 | public void onResult(Bundle value) throws RemoteException { |
| 4578 | Parcelable[] parcelables = value.getParcelableArray( |
| 4579 | AccountManager.KEY_ACCOUNTS); |
| 4580 | Account[] accounts = new Account[parcelables.length]; |
| 4581 | for (int i = 0; i < parcelables.length; i++) { |
| 4582 | accounts[i] = (Account) parcelables[i]; |
| 4583 | } |
| 4584 | handleGetAccountsResult( |
| 4585 | response, accounts, opPackageName); |
| 4586 | } |
| 4587 | |
| 4588 | @Override |
| 4589 | public void onError(int errorCode, String errorMessage) |
| 4590 | throws RemoteException { |
| 4591 | // Will not be called in this case. |
| 4592 | } |
| 4593 | }; |
| 4594 | new GetAccountsByTypeAndFeatureSession( |
| 4595 | userAccounts, |
| 4596 | retrieveAccountsResponse, |
| 4597 | accountType, |
| 4598 | features, |
| 4599 | callingUid, |
| 4600 | opPackageName, |
| 4601 | true /* include managed not visible */).bind(); |
| 4602 | } finally { |
| 4603 | restoreCallingIdentity(identityToken); |
| 4604 | } |
| 4605 | } |
| 4606 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4607 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4608 | public void getAccountsByFeatures( |
| 4609 | IAccountManagerResponse response, |
| 4610 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4611 | String[] features, |
| 4612 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4613 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4614 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4615 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4616 | Log.v(TAG, "getAccounts: accountType " + type |
| 4617 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4618 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4619 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4620 | + ", pid " + Binder.getCallingPid()); |
| 4621 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4622 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4623 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4624 | int userId = UserHandle.getCallingUserId(); |
| 4625 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4626 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4627 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4628 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4629 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4630 | // Need to return just the accounts that are from matching signatures. |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4631 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, EMPTY_ACCOUNT_ARRAY); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4632 | try { |
| 4633 | response.onResult(result); |
| 4634 | } catch (RemoteException e) { |
| 4635 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4636 | } |
| 4637 | return; |
| 4638 | } |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4639 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4640 | long identityToken = clearCallingIdentity(); |
| 4641 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4642 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4643 | if (features == null || features.length == 0) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4644 | Account[] accounts = getAccountsFromCache(userAccounts, type, callingUid, |
| 4645 | opPackageName, false); |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4646 | Bundle result = new Bundle(); |
| 4647 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4648 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4649 | return; |
| 4650 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4651 | new GetAccountsByTypeAndFeatureSession( |
| 4652 | userAccounts, |
| 4653 | response, |
| 4654 | type, |
| 4655 | features, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4656 | callingUid, |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4657 | opPackageName, |
| 4658 | false /* include managed not visible */).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4659 | } finally { |
| 4660 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4661 | } |
| 4662 | } |
| 4663 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4664 | @Override |
| 4665 | public void onAccountAccessed(String token) throws RemoteException { |
| 4666 | final int uid = Binder.getCallingUid(); |
| 4667 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4668 | return; |
| 4669 | } |
| 4670 | final int userId = UserHandle.getCallingUserId(); |
| 4671 | final long identity = Binder.clearCallingIdentity(); |
| 4672 | try { |
| 4673 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4674 | if (Objects.equals(account.getAccessId(), token)) { |
| 4675 | // An app just accessed the account. At this point it knows about |
| 4676 | // it and there is not need to hide this account from the app. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4677 | // Do we need to update account visibility here? |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4678 | if (!hasAccountAccess(account, null, uid)) { |
| 4679 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4680 | uid, true); |
| 4681 | } |
| 4682 | } |
| 4683 | } |
| 4684 | } finally { |
| 4685 | Binder.restoreCallingIdentity(identity); |
| 4686 | } |
| 4687 | } |
| 4688 | |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 4689 | @Override |
| 4690 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 4691 | FileDescriptor err, String[] args, ShellCallback callback, |
| 4692 | ResultReceiver resultReceiver) { |
| 4693 | new AccountManagerServiceShellCommand(this).exec(this, in, out, err, args, |
| 4694 | callback, resultReceiver); |
| 4695 | } |
| 4696 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4697 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4698 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4699 | IAccountManagerResponse mResponse; |
| 4700 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4701 | final boolean mExpectActivityLaunch; |
| 4702 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4703 | final String mAccountName; |
| 4704 | // Indicates if we need to add auth details(like last credential time) |
| 4705 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4706 | // If set, we need to update the last authenticated time. This is |
| 4707 | // currently |
| 4708 | // used on |
| 4709 | // successful confirming credentials. |
| 4710 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4711 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4712 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4713 | private int mNumRequestContinued = 0; |
| 4714 | private int mNumErrors = 0; |
| 4715 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4716 | IAccountAuthenticator mAuthenticator = null; |
| 4717 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4718 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4719 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4720 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4721 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4722 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4723 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4724 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4725 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4726 | } |
| 4727 | |
| 4728 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4729 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4730 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4731 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4732 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4733 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4734 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4735 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4736 | mResponse = response; |
| 4737 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4738 | mExpectActivityLaunch = expectActivityLaunch; |
| 4739 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4740 | mAccountName = accountName; |
| 4741 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4742 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4743 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4744 | synchronized (mSessions) { |
| 4745 | mSessions.put(toString(), this); |
| 4746 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4747 | if (response != null) { |
| 4748 | try { |
| 4749 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4750 | } catch (RemoteException e) { |
| 4751 | mResponse = null; |
| 4752 | binderDied(); |
| 4753 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4754 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4755 | } |
| 4756 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4757 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4758 | if (mResponse == null) { |
| 4759 | // this session has already been closed |
| 4760 | return null; |
| 4761 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4762 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4763 | 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] | 4764 | return response; |
| 4765 | } |
| 4766 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4767 | /** |
| 4768 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4769 | * security policy. |
| 4770 | * |
| 4771 | * In particular we want to make sure that the Authenticator doesn't try to trick users |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4772 | * into launching arbitrary intents on the device via by tricking to click authenticator |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4773 | * supplied entries in the system Settings app. |
| 4774 | */ |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4775 | protected boolean checkKeyIntent(int authUid, Intent intent) { |
Jeff Sharkey | d722e78 | 2017-06-12 17:33:07 -0600 | [diff] [blame] | 4776 | intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4777 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 4778 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 4779 | | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION)); |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4780 | long bid = Binder.clearCallingIdentity(); |
| 4781 | try { |
| 4782 | PackageManager pm = mContext.getPackageManager(); |
| 4783 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4784 | if (resolveInfo == null) { |
| 4785 | return false; |
| 4786 | } |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4787 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4788 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 4789 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4790 | if (!isExportedSystemActivity(targetActivityInfo) |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 4791 | && !pmi.hasSignatureCapability( |
| 4792 | targetUid, authUid, |
| 4793 | PackageParser.SigningDetails.CertCapabilities.AUTH)) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4794 | String pkgName = targetActivityInfo.packageName; |
| 4795 | String activityName = targetActivityInfo.name; |
| 4796 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4797 | + "does not share a signature with the supplying authenticator (%s)."; |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4798 | Log.e(TAG, String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4799 | return false; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4800 | } |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4801 | return true; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4802 | } finally { |
| 4803 | Binder.restoreCallingIdentity(bid); |
| 4804 | } |
| 4805 | } |
| 4806 | |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4807 | private boolean isExportedSystemActivity(ActivityInfo activityInfo) { |
| 4808 | String className = activityInfo.name; |
| 4809 | return "android".equals(activityInfo.packageName) && |
| 4810 | (GrantCredentialsPermissionActivity.class.getName().equals(className) |
| 4811 | || CantAddAccountActivity.class.getName().equals(className)); |
| 4812 | } |
| 4813 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4814 | private void close() { |
| 4815 | synchronized (mSessions) { |
| 4816 | if (mSessions.remove(toString()) == null) { |
| 4817 | // the session was already closed, so bail out now |
| 4818 | return; |
| 4819 | } |
| 4820 | } |
| 4821 | if (mResponse != null) { |
| 4822 | // stop listening for response deaths |
| 4823 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4824 | |
| 4825 | // clear this so that we don't accidentally send any further results |
| 4826 | mResponse = null; |
| 4827 | } |
| 4828 | cancelTimeout(); |
| 4829 | unbind(); |
| 4830 | } |
| 4831 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4832 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4833 | public void binderDied() { |
| 4834 | mResponse = null; |
| 4835 | close(); |
| 4836 | } |
| 4837 | |
| 4838 | protected String toDebugString() { |
| 4839 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4840 | } |
| 4841 | |
| 4842 | protected String toDebugString(long now) { |
| 4843 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4844 | + ", connected " + (mAuthenticator != null) |
| 4845 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4846 | + "/" + mNumErrors + ")" |
| 4847 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4848 | } |
| 4849 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4850 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4851 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4852 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4853 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4854 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4855 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4856 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | private void unbind() { |
| 4861 | if (mAuthenticator != null) { |
| 4862 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4863 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4864 | } |
| 4865 | } |
| 4866 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4867 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4868 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4869 | } |
| 4870 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4871 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4872 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4873 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4874 | try { |
| 4875 | run(); |
| 4876 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4877 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4878 | "remote exception"); |
| 4879 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4880 | } |
| 4881 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4882 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4883 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4884 | mAuthenticator = null; |
| 4885 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4886 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4887 | try { |
| 4888 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4889 | "disconnected"); |
| 4890 | } catch (RemoteException e) { |
| 4891 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4892 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4893 | + "caught RemoteException while responding", e); |
| 4894 | } |
| 4895 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4896 | } |
| 4897 | } |
| 4898 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4899 | public abstract void run() throws RemoteException; |
| 4900 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4901 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4902 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4903 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4904 | try { |
| 4905 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4906 | "timeout"); |
| 4907 | } catch (RemoteException e) { |
| 4908 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4909 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4910 | e); |
| 4911 | } |
| 4912 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4913 | } |
| 4914 | } |
| 4915 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4916 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4917 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4918 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4919 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4920 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4921 | if (result != null) { |
| 4922 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4923 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4924 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4925 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4926 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4927 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4928 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4929 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4930 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4931 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4932 | if (needUpdate || mAuthDetailsRequired) { |
| 4933 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4934 | if (needUpdate && accountPresent) { |
| 4935 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4936 | } |
| 4937 | if (mAuthDetailsRequired) { |
| 4938 | long lastAuthenticatedTime = -1; |
| 4939 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4940 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4941 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4942 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4943 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4944 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4945 | lastAuthenticatedTime); |
| 4946 | } |
| 4947 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4948 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4949 | if (result != null |
| 4950 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4951 | if (!checkKeyIntent( |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4952 | Binder.getCallingUid(), |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4953 | intent)) { |
| 4954 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 4955 | "invalid intent in bundle returned"); |
| 4956 | return; |
| 4957 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4958 | } |
| 4959 | if (result != null |
| 4960 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4961 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4962 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4963 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4964 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4965 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4966 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4967 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4968 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4969 | IAccountManagerResponse response; |
| 4970 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4971 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4972 | response = mResponse; |
| 4973 | } else { |
| 4974 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4975 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4976 | if (response != null) { |
| 4977 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4978 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4979 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4980 | Log.v(TAG, getClass().getSimpleName() |
| 4981 | + " calling onError() on response " + response); |
| 4982 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4983 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4984 | "null bundle returned"); |
| 4985 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4986 | if (mStripAuthTokenFromResult) { |
| 4987 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4988 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4989 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4990 | Log.v(TAG, getClass().getSimpleName() |
| 4991 | + " calling onResult() on response " + response); |
| 4992 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4993 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4994 | (intent == null)) { |
| 4995 | // All AccountManager error codes are greater than 0 |
| 4996 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4997 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4998 | } else { |
| 4999 | response.onResult(result); |
| 5000 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5001 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5002 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5003 | // if the caller is dead then there is no one to care about remote exceptions |
| 5004 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5005 | Log.v(TAG, "failure while notifying response", e); |
| 5006 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5007 | } |
| 5008 | } |
| 5009 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5010 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5011 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5012 | public void onRequestContinued() { |
| 5013 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5014 | } |
| 5015 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5016 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5017 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5018 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5019 | IAccountManagerResponse response = getResponseAndClose(); |
| 5020 | if (response != null) { |
| 5021 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5022 | Log.v(TAG, getClass().getSimpleName() |
| 5023 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5024 | } |
| 5025 | try { |
| 5026 | response.onError(errorCode, errorMessage); |
| 5027 | } catch (RemoteException e) { |
| 5028 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5029 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 5030 | } |
| 5031 | } |
| 5032 | } else { |
| 5033 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5034 | Log.v(TAG, "Session.onError: already closed"); |
| 5035 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5036 | } |
| 5037 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5038 | |
| 5039 | /** |
| 5040 | * find the component name for the authenticator and initiate a bind |
| 5041 | * if no authenticator or the bind fails then return false, otherwise return true |
| 5042 | */ |
| 5043 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5044 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 5045 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 5046 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5047 | if (authenticatorInfo == null) { |
| 5048 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5049 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 5050 | + ", bailing out"); |
| 5051 | } |
| 5052 | return false; |
| 5053 | } |
| 5054 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 5055 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 5056 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 5057 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 5058 | + " which isn't encryption aware"); |
| 5059 | return false; |
| 5060 | } |
| 5061 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5062 | Intent intent = new Intent(); |
| 5063 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 5064 | intent.setComponent(authenticatorInfo.componentName); |
| 5065 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5066 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 5067 | } |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 5068 | int flags = Context.BIND_AUTO_CREATE; |
| 5069 | if (mAuthenticatorCache.getBindInstantServiceAllowed(mAccounts.userId)) { |
| 5070 | flags |= Context.BIND_ALLOW_INSTANT; |
| 5071 | } |
| 5072 | if (!mContext.bindServiceAsUser(intent, this, flags, UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5073 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5074 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 5075 | } |
| 5076 | return false; |
| 5077 | } |
| 5078 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5079 | return true; |
| 5080 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5081 | } |
| 5082 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5083 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5084 | MessageHandler(Looper looper) { |
| 5085 | super(looper); |
| 5086 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 5087 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 5088 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5089 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5090 | switch (msg.what) { |
| 5091 | case MESSAGE_TIMED_OUT: |
| 5092 | Session session = (Session)msg.obj; |
| 5093 | session.onTimedOut(); |
| 5094 | break; |
| 5095 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 5096 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 5097 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 5098 | break; |
| 5099 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5100 | default: |
| 5101 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 5102 | } |
| 5103 | } |
| 5104 | } |
| 5105 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5106 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5107 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5108 | } |
| 5109 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 5110 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5111 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 5112 | } |
| 5113 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5114 | /* |
| 5115 | * This function receives an opened writable database. |
| 5116 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5117 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5118 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5119 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5123 | * 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] | 5124 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5125 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5126 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5127 | |
| 5128 | class LogRecordTask implements Runnable { |
| 5129 | private final String action; |
| 5130 | private final String tableName; |
| 5131 | private final long accountId; |
| 5132 | private final UserAccounts userAccount; |
| 5133 | private final int callingUid; |
| 5134 | private final long userDebugDbInsertionPoint; |
| 5135 | |
| 5136 | LogRecordTask(final String action, |
| 5137 | final String tableName, |
| 5138 | final long accountId, |
| 5139 | final UserAccounts userAccount, |
| 5140 | final int callingUid, |
| 5141 | final long userDebugDbInsertionPoint) { |
| 5142 | this.action = action; |
| 5143 | this.tableName = tableName; |
| 5144 | this.accountId = accountId; |
| 5145 | this.userAccount = userAccount; |
| 5146 | this.callingUid = callingUid; |
| 5147 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 5148 | } |
| 5149 | |
Andrew Scull | c7770d6 | 2017-05-22 17:49:58 +0100 | [diff] [blame] | 5150 | @Override |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5151 | public void run() { |
Dmitry Dementyev | 4744319 | 2018-10-24 13:31:59 -0700 | [diff] [blame] | 5152 | synchronized (userAccount.accountsDb.mDebugStatementLock) { |
| 5153 | SQLiteStatement logStatement = userAccount.accountsDb.getStatementForLogging(); |
| 5154 | if (logStatement == null) { |
| 5155 | return; // Can't log. |
| 5156 | } |
| 5157 | logStatement.bindLong(1, accountId); |
| 5158 | logStatement.bindString(2, action); |
| 5159 | logStatement.bindString(3, mDateFormat.format(new Date())); |
| 5160 | logStatement.bindLong(4, callingUid); |
| 5161 | logStatement.bindString(5, tableName); |
| 5162 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 5163 | try { |
| 5164 | logStatement.execute(); |
| 5165 | } catch (IllegalStateException e) { |
| 5166 | // Guard against crash, DB can already be closed |
| 5167 | // since this statement is executed on a handler thread |
| 5168 | Slog.w(TAG, "Failed to insert a log record. accountId=" + accountId |
| 5169 | + " action=" + action + " tableName=" + tableName + " Error: " + e); |
| 5170 | } finally { |
| 5171 | logStatement.clearBindings(); |
| 5172 | } |
Tetsutoki Shiozawa | be2d96a | 2017-10-24 18:44:00 +0900 | [diff] [blame] | 5173 | } |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5174 | } |
| 5175 | } |
Dmitry Dementyev | 4744319 | 2018-10-24 13:31:59 -0700 | [diff] [blame] | 5176 | long insertionPoint = userAccount.accountsDb.reserveDebugDbInsertionPoint(); |
| 5177 | if (insertionPoint != -1) { |
| 5178 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 5179 | callingUid, insertionPoint); |
| 5180 | mHandler.post(logTask); |
| 5181 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5182 | } |
| 5183 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5184 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5185 | return asBinder(); |
| 5186 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5187 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5188 | /** |
| 5189 | * Searches array of arguments for the specified string |
| 5190 | * @param args array of argument strings |
| 5191 | * @param value value to search for |
| 5192 | * @return true if the value is contained in the array |
| 5193 | */ |
| 5194 | private static boolean scanArgs(String[] args, String value) { |
| 5195 | if (args != null) { |
| 5196 | for (String arg : args) { |
| 5197 | if (value.equals(arg)) { |
| 5198 | return true; |
| 5199 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5200 | } |
| 5201 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5202 | return false; |
| 5203 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5204 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5205 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5206 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 5207 | if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5208 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5209 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 5210 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5211 | final List<UserInfo> users = getUserManager().getUsers(); |
| 5212 | for (UserInfo user : users) { |
| 5213 | ipw.println("User " + user + ":"); |
| 5214 | ipw.increaseIndent(); |
| 5215 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 5216 | ipw.println(); |
| 5217 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5218 | } |
| 5219 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5220 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5221 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 5222 | String[] args, boolean isCheckinRequest) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5223 | if (isCheckinRequest) { |
| 5224 | // This is a checkin request. *Only* upload the account types and the count of |
| 5225 | // each. |
| 5226 | synchronized (userAccounts.dbLock) { |
| 5227 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
| 5228 | } |
| 5229 | } else { |
| 5230 | Account[] accounts = getAccountsFromCache(userAccounts, null /* type */, |
| 5231 | Process.SYSTEM_UID, null /* packageName */, false); |
| 5232 | fout.println("Accounts: " + accounts.length); |
| 5233 | for (Account account : accounts) { |
Makoto Onuki | b942522 | 2019-01-23 11:35:43 -0800 | [diff] [blame] | 5234 | fout.println(" " + account.toString()); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5235 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5236 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5237 | // Add debug information. |
| 5238 | fout.println(); |
| 5239 | synchronized (userAccounts.dbLock) { |
| 5240 | userAccounts.accountsDb.dumpDebugTable(fout); |
| 5241 | } |
| 5242 | fout.println(); |
| 5243 | synchronized (mSessions) { |
| 5244 | final long now = SystemClock.elapsedRealtime(); |
| 5245 | fout.println("Active Sessions: " + mSessions.size()); |
| 5246 | for (Session session : mSessions.values()) { |
| 5247 | fout.println(" " + session.toDebugString(now)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5248 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5249 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5250 | |
| 5251 | fout.println(); |
| 5252 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Tony Mak | 6d14d77 | 2017-07-13 17:49:46 +0800 | [diff] [blame] | 5253 | |
| 5254 | boolean isUserUnlocked; |
| 5255 | synchronized (mUsers) { |
| 5256 | isUserUnlocked = isLocalUnlockedUser(userAccounts.userId); |
| 5257 | } |
| 5258 | // Following logs are printed only when user is unlocked. |
| 5259 | if (!isUserUnlocked) { |
| 5260 | return; |
| 5261 | } |
| 5262 | fout.println(); |
| 5263 | synchronized (userAccounts.dbLock) { |
| 5264 | Map<Account, Map<String, Integer>> allVisibilityValues = |
| 5265 | userAccounts.accountsDb.findAllVisibilityValues(); |
| 5266 | fout.println("Account visibility:"); |
| 5267 | for (Account account : allVisibilityValues.keySet()) { |
| 5268 | fout.println(" " + account.name); |
| 5269 | Map<String, Integer> visibilities = allVisibilityValues.get(account); |
| 5270 | for (Entry<String, Integer> entry : visibilities.entrySet()) { |
| 5271 | fout.println(" " + entry.getKey() + ", " + entry.getValue()); |
| 5272 | } |
| 5273 | } |
| 5274 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5275 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5276 | } |
| 5277 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5278 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5279 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5280 | long identityToken = clearCallingIdentity(); |
| 5281 | try { |
| 5282 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5283 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 5284 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5285 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5286 | if (intent.getComponent() != null && |
| 5287 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 5288 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5289 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5290 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5291 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5292 | final NotificationId id = getSigninRequiredNotificationId(accounts, account); |
| 5293 | intent.addCategory(id.mTag); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5294 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 5295 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5296 | contextForUser.getText(R.string.notification_title).toString(); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 5297 | Notification n = |
| 5298 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 5299 | .setWhen(0) |
| 5300 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 5301 | .setColor(contextForUser.getColor( |
| 5302 | com.android.internal.R.color.system_notification_accent_color)) |
| 5303 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 5304 | .setContentText(message) |
| 5305 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 5306 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5307 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 5308 | .build(); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5309 | installNotification(id, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5310 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5311 | } finally { |
| 5312 | restoreCallingIdentity(identityToken); |
| 5313 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5314 | } |
| 5315 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5316 | private void installNotification(NotificationId id, final Notification notification, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5317 | String packageName, int userId) { |
| 5318 | final long token = clearCallingIdentity(); |
| 5319 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5320 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5321 | try { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 5322 | // The calling uid must match either the package or op package, so use an op |
| 5323 | // package that matches the cleared calling identity. |
| 5324 | notificationManager.enqueueNotificationWithTag(packageName, "android", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 5325 | id.mTag, id.mId, notification, userId); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5326 | } catch (RemoteException e) { |
| 5327 | /* ignore - local call */ |
| 5328 | } |
| 5329 | } finally { |
| 5330 | Binder.restoreCallingIdentity(token); |
| 5331 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5332 | } |
| 5333 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5334 | private void cancelNotification(NotificationId id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5335 | cancelNotification(id, mContext.getPackageName(), user); |
| 5336 | } |
| 5337 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5338 | private void cancelNotification(NotificationId id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5339 | long identityToken = clearCallingIdentity(); |
| 5340 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5341 | INotificationManager service = mInjector.getNotificationManager(); |
Julia Reynolds | e4a47dd | 2019-06-07 13:40:59 -0400 | [diff] [blame] | 5342 | service.cancelNotificationWithTag( |
| 5343 | packageName, "android", id.mTag, id.mId, user.getIdentifier()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5344 | } catch (RemoteException e) { |
| 5345 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5346 | } finally { |
| 5347 | restoreCallingIdentity(identityToken); |
| 5348 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5349 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5350 | |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 5351 | private boolean isPermittedForPackage(String packageName, int userId, String... permissions) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5352 | final long identity = Binder.clearCallingIdentity(); |
| 5353 | try { |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 5354 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5355 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 5356 | for (String perm : permissions) { |
| 5357 | if (pm.checkPermission(perm, packageName, userId) |
| 5358 | == PackageManager.PERMISSION_GRANTED) { |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5359 | // Checks runtime permission revocation. |
| 5360 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 5361 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.checkOpNoThrow( |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5362 | opCode, uid, packageName) == AppOpsManager.MODE_ALLOWED) { |
| 5363 | return true; |
| 5364 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5365 | } |
| 5366 | } |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 5367 | } catch (NameNotFoundException | RemoteException e) { |
| 5368 | // Assume permission is not granted if an error accrued. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5369 | } finally { |
| 5370 | Binder.restoreCallingIdentity(identity); |
| 5371 | } |
| 5372 | return false; |
| 5373 | } |
| 5374 | |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 5375 | /** |
| 5376 | * Checks that package has at least one of given permissions and makes note of app |
| 5377 | * performing the action. |
| 5378 | */ |
| 5379 | private boolean checkPermissionAndNote(String opPackageName, int callingUid, |
| 5380 | String... permissions) { |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5381 | for (String perm : permissions) { |
| 5382 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 5383 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5384 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 5385 | } |
| 5386 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
Tony Mak | 58f2815 | 2017-09-20 21:23:48 +0100 | [diff] [blame] | 5387 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOpNoThrow( |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5388 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 5389 | return true; |
| 5390 | } |
| 5391 | } |
| 5392 | } |
| 5393 | return false; |
| 5394 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5395 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5396 | private int handleIncomingUser(int userId) { |
| 5397 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5398 | return ActivityManager.getService().handleIncomingUser( |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5399 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 5400 | } catch (RemoteException re) { |
| 5401 | // Shouldn't happen, local. |
| 5402 | } |
| 5403 | return userId; |
| 5404 | } |
| 5405 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 5406 | private boolean isPrivileged(int callingUid) { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5407 | String[] packages; |
| 5408 | long identityToken = Binder.clearCallingIdentity(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5409 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5410 | packages = mPackageManager.getPackagesForUid(callingUid); |
sunjian | 9ae597b6 | 2017-08-14 15:45:04 -0700 | [diff] [blame] | 5411 | if (packages == null) { |
| 5412 | Log.d(TAG, "No packages for callingUid " + callingUid); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5413 | return false; |
| 5414 | } |
sunjian | 9ae597b6 | 2017-08-14 15:45:04 -0700 | [diff] [blame] | 5415 | for (String name : packages) { |
| 5416 | try { |
| 5417 | PackageInfo packageInfo = |
| 5418 | mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5419 | if (packageInfo != null |
| 5420 | && (packageInfo.applicationInfo.privateFlags |
| 5421 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 5422 | return true; |
| 5423 | } |
| 5424 | } catch (PackageManager.NameNotFoundException e) { |
| 5425 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 5426 | } |
| 5427 | } |
| 5428 | } finally { |
| 5429 | Binder.restoreCallingIdentity(identityToken); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5430 | } |
| 5431 | return false; |
| 5432 | } |
| 5433 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5434 | private boolean permissionIsGranted( |
| 5435 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5436 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 5437 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5438 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 5439 | } |
| 5440 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5441 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5442 | |
| 5443 | if (isPrivileged(callerUid)) { |
| 5444 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5445 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5446 | + callerUid + " privileged"); |
| 5447 | } |
| 5448 | return true; |
| 5449 | } |
| 5450 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 5451 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5452 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5453 | + callerUid + " manages the account"); |
| 5454 | } |
| 5455 | return true; |
| 5456 | } |
| 5457 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5458 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5459 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5460 | + callerUid + " user granted access"); |
| 5461 | } |
| 5462 | return true; |
| 5463 | } |
| 5464 | |
| 5465 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5466 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5467 | } |
| 5468 | |
| 5469 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5470 | } |
| 5471 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5472 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5473 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5474 | if (accountType == null) { |
| 5475 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5476 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5477 | return getTypesVisibleToCaller(callingUid, userId, |
| 5478 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5479 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5480 | } |
| 5481 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5482 | // Method checks visibility for applications targeing API level below {@link |
| 5483 | // android.os.Build.VERSION_CODES#O}, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5484 | // returns true if the the app has GET_ACCOUNTS or GET_ACCOUNTS_PRIVILEGED permission. |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 5485 | private boolean checkGetAccountsPermission(String packageName, int userId) { |
| 5486 | return isPermittedForPackage(packageName, userId, Manifest.permission.GET_ACCOUNTS, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5487 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 5488 | } |
| 5489 | |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 5490 | private boolean checkReadContactsPermission(String packageName, int userId) { |
| 5491 | return isPermittedForPackage(packageName, userId, Manifest.permission.READ_CONTACTS); |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | // Heuristic to check that account type may be associated with some contacts data and |
| 5495 | // therefore READ_CONTACTS permission grants the access to account by default. |
| 5496 | private boolean accountTypeManagesContacts(String accountType, int userId) { |
| 5497 | if (accountType == null) { |
| 5498 | return false; |
| 5499 | } |
| 5500 | long identityToken = Binder.clearCallingIdentity(); |
| 5501 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5502 | try { |
| 5503 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5504 | } finally { |
| 5505 | Binder.restoreCallingIdentity(identityToken); |
| 5506 | } |
| 5507 | // Check contacts related permissions for authenticator. |
| 5508 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5509 | : serviceInfos) { |
| 5510 | if (accountType.equals(serviceInfo.type.type)) { |
Dmitry Dementyev | 6fb038c | 2019-04-15 11:44:00 -0700 | [diff] [blame] | 5511 | return isPermittedForPackage(serviceInfo.type.packageName, userId, |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5512 | Manifest.permission.WRITE_CONTACTS); |
| 5513 | } |
| 5514 | } |
| 5515 | return false; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5516 | } |
| 5517 | |
| 5518 | /** |
| 5519 | * Method checks package uid and signature with Authenticator which manages accountType. |
| 5520 | * |
| 5521 | * @return SIGNATURE_CHECK_UID_MATCH for uid match, SIGNATURE_CHECK_MATCH for signature match, |
| 5522 | * SIGNATURE_CHECK_MISMATCH otherwise. |
| 5523 | */ |
| 5524 | private int checkPackageSignature(String accountType, int callingUid, int userId) { |
| 5525 | if (accountType == null) { |
| 5526 | return SIGNATURE_CHECK_MISMATCH; |
| 5527 | } |
| 5528 | |
| 5529 | long identityToken = Binder.clearCallingIdentity(); |
| 5530 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5531 | try { |
| 5532 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5533 | } finally { |
| 5534 | Binder.restoreCallingIdentity(identityToken); |
| 5535 | } |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 5536 | // Check for signature match with Authenticator.LocalServices.getService(PackageManagerInternal.class); |
| 5537 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5538 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5539 | : serviceInfos) { |
| 5540 | if (accountType.equals(serviceInfo.type.type)) { |
| 5541 | if (serviceInfo.uid == callingUid) { |
| 5542 | return SIGNATURE_CHECK_UID_MATCH; |
| 5543 | } |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 5544 | if (pmi.hasSignatureCapability( |
| 5545 | serviceInfo.uid, callingUid, |
| 5546 | PackageParser.SigningDetails.CertCapabilities.AUTH)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5547 | return SIGNATURE_CHECK_MATCH; |
| 5548 | } |
| 5549 | } |
| 5550 | } |
| 5551 | return SIGNATURE_CHECK_MISMATCH; |
| 5552 | } |
| 5553 | |
| 5554 | // returns true for applications with the same signature as authenticator. |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5555 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5556 | if (accountType == null) { |
| 5557 | return false; |
| 5558 | } else { |
| 5559 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5560 | } |
| 5561 | } |
| 5562 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5563 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5564 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5565 | return getTypesForCaller(callingUid, userId, true /* isOtherwisePermitted*/); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
Dmitry Dementyev | 2e22cfb | 2017-01-09 18:42:14 +0000 | [diff] [blame] | 5569 | return getTypesForCaller(callingUid, userId, false); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | private List<String> getTypesForCaller( |
| 5573 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5574 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5575 | long identityToken = Binder.clearCallingIdentity(); |
| 5576 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5577 | try { |
| 5578 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5579 | } finally { |
| 5580 | Binder.restoreCallingIdentity(identityToken); |
| 5581 | } |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 5582 | |
| 5583 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5584 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5585 | serviceInfos) { |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 5586 | if (isOtherwisePermitted || pmi.hasSignatureCapability( |
| 5587 | serviceInfo.uid, callingUid, |
| 5588 | PackageParser.SigningDetails.CertCapabilities.AUTH)) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5589 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5590 | } |
| 5591 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5592 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5593 | } |
| 5594 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5595 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5596 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5597 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5598 | if (account.name.equals(accountName)) { |
| 5599 | return true; |
| 5600 | } |
| 5601 | } |
| 5602 | } |
| 5603 | return false; |
| 5604 | } |
| 5605 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5606 | private static void checkManageUsersPermission(String message) { |
| 5607 | if (ActivityManager.checkComponentPermission( |
| 5608 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5609 | != PackageManager.PERMISSION_GRANTED) { |
| 5610 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5611 | } |
| 5612 | } |
| 5613 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5614 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5615 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5616 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5617 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5618 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5619 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5620 | + message); |
| 5621 | } |
| 5622 | } |
| 5623 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5624 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5625 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5626 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5627 | return true; |
| 5628 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5629 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5630 | synchronized (accounts.dbLock) { |
| 5631 | synchronized (accounts.cacheLock) { |
| 5632 | long grantsCount; |
| 5633 | if (authTokenType != null) { |
| 5634 | grantsCount = accounts.accountsDb |
| 5635 | .findMatchingGrantsCount(callerUid, authTokenType, account); |
| 5636 | } else { |
| 5637 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
| 5638 | account); |
| 5639 | } |
| 5640 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5641 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5642 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5643 | // TODO: Skip this check when running automated tests. Replace this |
| 5644 | // with a more general solution. |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 5645 | Log.d(TAG, "no credentials permission for usage of " |
| 5646 | + account.toSafeString() + ", " |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5647 | + authTokenType + " by uid " + callerUid |
| 5648 | + " but ignoring since device is in test harness."); |
| 5649 | return true; |
| 5650 | } |
| 5651 | return permissionGranted; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5652 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5653 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5654 | } |
| 5655 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5656 | private boolean isSystemUid(int callingUid) { |
| 5657 | String[] packages = null; |
| 5658 | long ident = Binder.clearCallingIdentity(); |
| 5659 | try { |
| 5660 | packages = mPackageManager.getPackagesForUid(callingUid); |
Alex Chau | 81a47cd | 2018-01-02 16:49:14 +0000 | [diff] [blame] | 5661 | if (packages != null) { |
| 5662 | for (String name : packages) { |
| 5663 | try { |
| 5664 | PackageInfo packageInfo = |
| 5665 | mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5666 | if (packageInfo != null |
| 5667 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5668 | != 0) { |
| 5669 | return true; |
| 5670 | } |
| 5671 | } catch (NameNotFoundException e) { |
| 5672 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5673 | } |
| 5674 | } |
| 5675 | } else { |
| 5676 | Log.w(TAG, "No known packages with uid " + callingUid); |
| 5677 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5678 | } finally { |
| 5679 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5680 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5681 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5684 | /** Succeeds if any of the specified permissions are granted. */ |
| 5685 | private void checkReadAccountsPermitted( |
| 5686 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5687 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5688 | int userId, |
| 5689 | String opPackageName) { |
| 5690 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5691 | String msg = String.format( |
| 5692 | "caller uid %s cannot access %s accounts", |
| 5693 | callingUid, |
| 5694 | accountType); |
| 5695 | Log.w(TAG, " " + msg); |
| 5696 | throw new SecurityException(msg); |
| 5697 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5698 | } |
| 5699 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5700 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5701 | // the managing app can always modify accounts |
| 5702 | if (isProfileOwner(callingUid)) { |
| 5703 | return true; |
| 5704 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5705 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5706 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5707 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5708 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5709 | return true; |
| 5710 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5711 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5712 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5713 | // the managing app can always modify accounts |
| 5714 | if (isProfileOwner(callingUid)) { |
| 5715 | return true; |
| 5716 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5717 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5718 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5719 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5720 | if (typesArray == null) { |
| 5721 | return true; |
| 5722 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5723 | for (String forbiddenType : typesArray) { |
| 5724 | if (forbiddenType.equals(accountType)) { |
| 5725 | return false; |
| 5726 | } |
| 5727 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5728 | return true; |
| 5729 | } |
| 5730 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5731 | private boolean isProfileOwner(int uid) { |
| 5732 | final DevicePolicyManagerInternal dpmi = |
| 5733 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5734 | return (dpmi != null) |
| 5735 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5736 | } |
| 5737 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5738 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5739 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5740 | throws RemoteException { |
| 5741 | final int callingUid = getCallingUid(); |
| 5742 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5743 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5744 | throw new SecurityException(); |
| 5745 | } |
| 5746 | |
| 5747 | if (value) { |
| 5748 | grantAppPermission(account, authTokenType, uid); |
| 5749 | } else { |
| 5750 | revokeAppPermission(account, authTokenType, uid); |
| 5751 | } |
| 5752 | } |
| 5753 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5754 | /** |
| 5755 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5756 | * <p> |
| 5757 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5758 | * which is in the system. This means we don't need to protect it with permissions. |
| 5759 | * @hide |
| 5760 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5761 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5762 | if (account == null || authTokenType == null) { |
| 5763 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5764 | return; |
| 5765 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5766 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5767 | synchronized (accounts.dbLock) { |
| 5768 | synchronized (accounts.cacheLock) { |
| 5769 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5770 | if (accountId >= 0) { |
| 5771 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
| 5772 | } |
| 5773 | cancelNotification( |
| 5774 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5775 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5776 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5777 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
| 5778 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5779 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5780 | |
| 5781 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5782 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5783 | : mAppPermissionChangeListeners) { |
| 5784 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5785 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5786 | } |
| 5787 | |
| 5788 | /** |
| 5789 | * Don't allow callers with the given uid permission to get credentials for |
| 5790 | * account/authTokenType. |
| 5791 | * <p> |
| 5792 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5793 | * which is in the system. This means we don't need to protect it with permissions. |
| 5794 | * @hide |
| 5795 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5796 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5797 | if (account == null || authTokenType == null) { |
| 5798 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5799 | return; |
| 5800 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5801 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5802 | synchronized (accounts.dbLock) { |
| 5803 | synchronized (accounts.cacheLock) { |
| 5804 | accounts.accountsDb.beginTransaction(); |
| 5805 | try { |
| 5806 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5807 | if (accountId >= 0) { |
| 5808 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5809 | accountId, authTokenType, uid); |
| 5810 | accounts.accountsDb.setTransactionSuccessful(); |
| 5811 | } |
| 5812 | } finally { |
| 5813 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5814 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5815 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5816 | cancelNotification( |
| 5817 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5818 | UserHandle.of(accounts.userId)); |
| 5819 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5820 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5821 | |
| 5822 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5823 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5824 | : mAppPermissionChangeListeners) { |
| 5825 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5826 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5827 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5828 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5829 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5830 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5831 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5832 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5833 | for (Account curAccount : oldAccountsForType) { |
| 5834 | if (!curAccount.equals(account)) { |
| 5835 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5836 | } |
| 5837 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5838 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5839 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5840 | } else { |
| 5841 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5842 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5843 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5844 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5845 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5846 | accounts.userDataCache.remove(account); |
| 5847 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5848 | accounts.previousNameCache.remove(account); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 5849 | accounts.visibilityCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | /** |
| 5853 | * 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] | 5854 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5855 | * processes and if you will return this account to apps you should return the result. |
| 5856 | * @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] | 5857 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5858 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5859 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5860 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5861 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5862 | if (accountsForType != null) { |
| 5863 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5864 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5865 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5866 | : UUID.randomUUID().toString(); |
| 5867 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5868 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5869 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5870 | } |
| 5871 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5872 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5873 | private Account[] filterAccounts(UserAccounts accounts, Account[] unfiltered, int callingUid, |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5874 | @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5875 | String visibilityFilterPackage = callingPackage; |
| 5876 | if (visibilityFilterPackage == null) { |
| 5877 | visibilityFilterPackage = getPackageNameForUid(callingUid); |
| 5878 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5879 | Map<Account, Integer> firstPass = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5880 | for (Account account : unfiltered) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5881 | int visibility = resolveAccountVisibility(account, visibilityFilterPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5882 | if ((visibility == AccountManager.VISIBILITY_VISIBLE |
| 5883 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) |
| 5884 | || (includeManagedNotVisible |
| 5885 | && (visibility |
| 5886 | == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE))) { |
| 5887 | firstPass.put(account, visibility); |
| 5888 | } |
| 5889 | } |
| 5890 | Map<Account, Integer> secondPass = |
| 5891 | filterSharedAccounts(accounts, firstPass, callingUid, callingPackage); |
| 5892 | |
| 5893 | Account[] filtered = new Account[secondPass.size()]; |
| 5894 | filtered = secondPass.keySet().toArray(filtered); |
| 5895 | return filtered; |
| 5896 | } |
| 5897 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5898 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5899 | private Map<Account, Integer> filterSharedAccounts(UserAccounts userAccounts, |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5900 | @NonNull Map<Account, Integer> unfiltered, int callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5901 | @Nullable String callingPackage) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5902 | // first part is to filter shared accounts. |
| 5903 | // unfiltered type check is not necessary. |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5904 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5905 | || callingUid == Process.SYSTEM_UID) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5906 | return unfiltered; |
| 5907 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5908 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5909 | if (user != null && user.isRestricted()) { |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5910 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5911 | if (packages == null) { |
| 5912 | packages = new String[] {}; |
| 5913 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5914 | // 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] | 5915 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5916 | // This might be a temporary way to specify a visible list |
| 5917 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5918 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5919 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5920 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5921 | return unfiltered; |
| 5922 | } |
| 5923 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5924 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5925 | if (ArrayUtils.isEmpty(sharedAccounts)) { |
| 5926 | return unfiltered; |
| 5927 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5928 | String requiredAccountType = ""; |
| 5929 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5930 | // If there's an explicit callingPackage specified, check if that package |
| 5931 | // opted in to see restricted accounts. |
| 5932 | if (callingPackage != null) { |
| 5933 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5934 | if (pi != null && pi.restrictedAccountType != null) { |
| 5935 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5936 | } |
| 5937 | } else { |
| 5938 | // Otherwise check if the callingUid has a package that has opted in |
| 5939 | for (String packageName : packages) { |
| 5940 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5941 | if (pi != null && pi.restrictedAccountType != null) { |
| 5942 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5943 | break; |
| 5944 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5945 | } |
| 5946 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5947 | } catch (NameNotFoundException e) { |
| 5948 | Log.d(TAG, "Package not found " + e.getMessage()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5949 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5950 | Map<Account, Integer> filtered = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5951 | for (Map.Entry<Account, Integer> entry : unfiltered.entrySet()) { |
| 5952 | Account account = entry.getKey(); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5953 | if (account.type.equals(requiredAccountType)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5954 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5955 | } else { |
| 5956 | boolean found = false; |
| 5957 | for (Account shared : sharedAccounts) { |
| 5958 | if (shared.equals(account)) { |
| 5959 | found = true; |
| 5960 | break; |
| 5961 | } |
| 5962 | } |
| 5963 | if (!found) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5964 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5965 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5966 | } |
| 5967 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5968 | return filtered; |
| 5969 | } else { |
| 5970 | return unfiltered; |
| 5971 | } |
| 5972 | } |
| 5973 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5974 | /* |
| 5975 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5976 | * that the package is not allowed to access. |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5977 | * |
| 5978 | * <p>The method shouldn't be called with UserAccounts#cacheLock held, otherwise it will cause a |
| 5979 | * deadlock |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5980 | */ |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5981 | @NonNull |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5982 | protected Account[] getAccountsFromCache(UserAccounts userAccounts, String accountType, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5983 | int callingUid, @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5984 | Preconditions.checkState(!Thread.holdsLock(userAccounts.cacheLock), |
| 5985 | "Method should not be called with cacheLock"); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5986 | if (accountType != null) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5987 | Account[] accounts; |
| 5988 | synchronized (userAccounts.cacheLock) { |
| 5989 | accounts = userAccounts.accountCache.get(accountType); |
| 5990 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5991 | if (accounts == null) { |
| 5992 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5993 | } else { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5994 | return filterAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
| 5995 | callingUid, callingPackage, includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5996 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5997 | } else { |
| 5998 | int totalLength = 0; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5999 | Account[] accountsArray; |
| 6000 | synchronized (userAccounts.cacheLock) { |
| 6001 | for (Account[] accounts : userAccounts.accountCache.values()) { |
| 6002 | totalLength += accounts.length; |
| 6003 | } |
| 6004 | if (totalLength == 0) { |
| 6005 | return EMPTY_ACCOUNT_ARRAY; |
| 6006 | } |
| 6007 | accountsArray = new Account[totalLength]; |
| 6008 | totalLength = 0; |
| 6009 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
| 6010 | System.arraycopy(accountsOfType, 0, accountsArray, totalLength, |
| 6011 | accountsOfType.length); |
| 6012 | totalLength += accountsOfType.length; |
| 6013 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 6014 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 6015 | return filterAccounts(userAccounts, accountsArray, callingUid, callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 6016 | includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6017 | } |
| 6018 | } |
| 6019 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 6020 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 6021 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6022 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 6023 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 6024 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 6025 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6026 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 6027 | } |
| 6028 | if (value == null) { |
| 6029 | userDataForAccount.remove(key); |
| 6030 | } else { |
| 6031 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6032 | } |
| 6033 | } |
| 6034 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 6035 | protected String readCachedTokenInternal( |
| 6036 | UserAccounts accounts, |
| 6037 | Account account, |
| 6038 | String tokenType, |
| 6039 | String callingPackage, |
| 6040 | byte[] pkgSigDigest) { |
Dmitry Dementyev | 18f0ca9 | 2017-06-12 17:56:47 -0700 | [diff] [blame] | 6041 | synchronized (accounts.cacheLock) { |
| 6042 | return accounts.accountTokenCaches.get( |
| 6043 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 6044 | } |
| 6045 | } |
| 6046 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 6047 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 6048 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6049 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 6050 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 6051 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 6052 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6053 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 6054 | } |
| 6055 | if (value == null) { |
| 6056 | authTokensForAccount.remove(key); |
| 6057 | } else { |
| 6058 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6059 | } |
| 6060 | } |
| 6061 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6062 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 6063 | String authTokenType) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 6064 | // Fast path - check if account is already cached |
| 6065 | synchronized (accounts.cacheLock) { |
| 6066 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 6067 | if (authTokensForAccount != null) { |
| 6068 | return authTokensForAccount.get(authTokenType); |
| 6069 | } |
| 6070 | } |
| 6071 | // If not cached yet - do slow path and sync with db if necessary |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 6072 | synchronized (accounts.dbLock) { |
| 6073 | synchronized (accounts.cacheLock) { |
| 6074 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 6075 | if (authTokensForAccount == null) { |
| 6076 | // need to populate the cache for this account |
| 6077 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
| 6078 | accounts.authTokenCache.put(account, authTokensForAccount); |
| 6079 | } |
| 6080 | return authTokensForAccount.get(authTokenType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6081 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6082 | } |
| 6083 | } |
| 6084 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 6085 | private String readUserDataInternal(UserAccounts accounts, Account account, String key) { |
| 6086 | Map<String, String> userDataForAccount; |
| 6087 | // Fast path - check if data is already cached |
| 6088 | synchronized (accounts.cacheLock) { |
| 6089 | userDataForAccount = accounts.userDataCache.get(account); |
| 6090 | } |
| 6091 | // If not cached yet - do slow path and sync with db if necessary |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 6092 | if (userDataForAccount == null) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 6093 | synchronized (accounts.dbLock) { |
| 6094 | synchronized (accounts.cacheLock) { |
| 6095 | userDataForAccount = accounts.userDataCache.get(account); |
| 6096 | if (userDataForAccount == null) { |
| 6097 | // need to populate the cache for this account |
| 6098 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
| 6099 | accounts.userDataCache.put(account, userDataForAccount); |
| 6100 | } |
| 6101 | } |
| 6102 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6103 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 6104 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6105 | } |
| 6106 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 6107 | private Context getContextForUser(UserHandle user) { |
| 6108 | try { |
| 6109 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 6110 | } catch (NameNotFoundException e) { |
| 6111 | // Default to mContext, not finding the package system is running as is unlikely. |
| 6112 | return mContext; |
| 6113 | } |
| 6114 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 6115 | |
| 6116 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 6117 | try { |
| 6118 | response.onResult(result); |
| 6119 | } catch (RemoteException e) { |
| 6120 | // if the caller is dead then there is no one to care about remote |
| 6121 | // exceptions |
| 6122 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 6123 | Log.v(TAG, "failure while notifying response", e); |
| 6124 | } |
| 6125 | } |
| 6126 | } |
| 6127 | |
| 6128 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 6129 | String errorMessage) { |
| 6130 | try { |
| 6131 | response.onError(errorCode, errorMessage); |
| 6132 | } catch (RemoteException e) { |
| 6133 | // if the caller is dead then there is no one to care about remote |
| 6134 | // exceptions |
| 6135 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 6136 | Log.v(TAG, "failure while notifying response", e); |
| 6137 | } |
| 6138 | } |
| 6139 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 6140 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6141 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 6142 | private final Object mLock = new Object(); |
| 6143 | |
| 6144 | @GuardedBy("mLock") |
| 6145 | private AccountManagerBackupHelper mBackupHelper; |
| 6146 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6147 | @Override |
| 6148 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 6149 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 6150 | if (account == null) { |
| 6151 | Slog.w(TAG, "account cannot be null"); |
| 6152 | return; |
| 6153 | } |
| 6154 | if (packageName == null) { |
| 6155 | Slog.w(TAG, "packageName cannot be null"); |
| 6156 | return; |
| 6157 | } |
| 6158 | if (userId < UserHandle.USER_SYSTEM) { |
| 6159 | Slog.w(TAG, "user id must be concrete"); |
| 6160 | return; |
| 6161 | } |
| 6162 | if (callback == null) { |
| 6163 | Slog.w(TAG, "callback cannot be null"); |
| 6164 | return; |
| 6165 | } |
| 6166 | |
Dmitry Dementyev | 7b3ea13 | 2017-05-10 12:45:02 -0700 | [diff] [blame] | 6167 | int visibility = |
| 6168 | resolveAccountVisibility(account, packageName, getUserAccounts(userId)); |
| 6169 | if (visibility == AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 6170 | Slog.w(TAG, "requestAccountAccess: account is hidden"); |
| 6171 | return; |
| 6172 | } |
| 6173 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 6174 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 6175 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6176 | Bundle result = new Bundle(); |
| 6177 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 6178 | callback.sendResult(result); |
| 6179 | return; |
| 6180 | } |
| 6181 | |
| 6182 | final int uid; |
| 6183 | try { |
Dmitry Dementyev | 940b760 | 2018-10-09 16:40:06 -0700 | [diff] [blame] | 6184 | long identityToken = clearCallingIdentity(); |
| 6185 | try { |
| 6186 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 6187 | } finally { |
| 6188 | restoreCallingIdentity(identityToken); |
| 6189 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6190 | } catch (NameNotFoundException e) { |
| 6191 | Slog.e(TAG, "Unknown package " + packageName); |
| 6192 | return; |
| 6193 | } |
| 6194 | |
| 6195 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 6196 | final UserAccounts userAccounts; |
| 6197 | synchronized (mUsers) { |
| 6198 | userAccounts = mUsers.get(userId); |
| 6199 | } |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 6200 | SystemNotificationChannels.createAccountChannelForPackage(packageName, uid, mContext); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 6201 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 6202 | } |
| 6203 | |
| 6204 | @Override |
| 6205 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 6206 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 6207 | mAppPermissionChangeListeners.add(listener); |
| 6208 | } |
| 6209 | |
| 6210 | @Override |
| 6211 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 6212 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6213 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 6214 | |
| 6215 | @Override |
| 6216 | public byte[] backupAccountAccessPermissions(int userId) { |
| 6217 | synchronized (mLock) { |
| 6218 | if (mBackupHelper == null) { |
| 6219 | mBackupHelper = new AccountManagerBackupHelper( |
| 6220 | AccountManagerService.this, this); |
| 6221 | } |
| 6222 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 6223 | } |
| 6224 | } |
| 6225 | |
| 6226 | @Override |
| 6227 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 6228 | synchronized (mLock) { |
| 6229 | if (mBackupHelper == null) { |
| 6230 | mBackupHelper = new AccountManagerBackupHelper( |
| 6231 | AccountManagerService.this, this); |
| 6232 | } |
| 6233 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 6234 | } |
| 6235 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 6236 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6237 | |
| 6238 | @VisibleForTesting |
| 6239 | static class Injector { |
| 6240 | private final Context mContext; |
| 6241 | |
| 6242 | public Injector(Context context) { |
| 6243 | mContext = context; |
| 6244 | } |
| 6245 | |
| 6246 | Looper getMessageHandlerLooper() { |
| 6247 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 6248 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 6249 | serviceThread.start(); |
| 6250 | return serviceThread.getLooper(); |
| 6251 | } |
| 6252 | |
| 6253 | Context getContext() { |
| 6254 | return mContext; |
| 6255 | } |
| 6256 | |
| 6257 | void addLocalService(AccountManagerInternal service) { |
| 6258 | LocalServices.addService(AccountManagerInternal.class, service); |
| 6259 | } |
| 6260 | |
| 6261 | String getDeDatabaseName(int userId) { |
| 6262 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 6263 | AccountsDb.DE_DATABASE_NAME); |
| 6264 | return databaseFile.getPath(); |
| 6265 | } |
| 6266 | |
| 6267 | String getCeDatabaseName(int userId) { |
| 6268 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 6269 | AccountsDb.CE_DATABASE_NAME); |
| 6270 | return databaseFile.getPath(); |
| 6271 | } |
| 6272 | |
| 6273 | String getPreNDatabaseName(int userId) { |
| 6274 | File systemDir = Environment.getDataSystemDirectory(); |
| 6275 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 6276 | PRE_N_DATABASE_NAME); |
| 6277 | if (userId == 0) { |
| 6278 | // Migrate old file, if it exists, to the new location. |
| 6279 | // Make sure the new file doesn't already exist. A dummy file could have been |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 6280 | // accidentally created in the old location, |
| 6281 | // causing the new one to become corrupted as well. |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6282 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 6283 | if (oldFile.exists() && !databaseFile.exists()) { |
| 6284 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 6285 | File userDir = Environment.getUserSystemDirectory(userId); |
| 6286 | if (!userDir.exists()) { |
| 6287 | if (!userDir.mkdirs()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 6288 | throw new IllegalStateException( |
| 6289 | "User dir cannot be created: " + userDir); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6290 | } |
| 6291 | } |
| 6292 | if (!oldFile.renameTo(databaseFile)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 6293 | throw new IllegalStateException( |
| 6294 | "User dir cannot be migrated: " + databaseFile); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6295 | } |
| 6296 | } |
| 6297 | } |
| 6298 | return databaseFile.getPath(); |
| 6299 | } |
| 6300 | |
| 6301 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 6302 | return new AccountAuthenticatorCache(mContext); |
| 6303 | } |
| 6304 | |
| 6305 | INotificationManager getNotificationManager() { |
| 6306 | return NotificationManager.getService(); |
| 6307 | } |
| 6308 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 6309 | |
Andrew Scull | c7770d6 | 2017-05-22 17:49:58 +0100 | [diff] [blame] | 6310 | private static class NotificationId { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 6311 | final String mTag; |
| 6312 | private final int mId; |
| 6313 | |
| 6314 | NotificationId(String tag, int type) { |
| 6315 | mTag = tag; |
| 6316 | mId = type; |
| 6317 | } |
| 6318 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 6319 | } |