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 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 244 | private int debugDbInsertionPoint = -1; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 245 | private SQLiteStatement statementForLogging; // TODO Move to AccountsDb |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 246 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 247 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 248 | this.userId = userId; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 249 | synchronized (dbLock) { |
| 250 | synchronized (cacheLock) { |
| 251 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
| 252 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 257 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 258 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 259 | // Not thread-safe. Only use in synchronized context |
| 260 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 261 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 262 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 263 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 264 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 265 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 266 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 267 | /** |
| 268 | * This should only be called by system code. One should only call this after the service |
| 269 | * has started. |
| 270 | * @return a reference to the AccountManagerService instance |
| 271 | * @hide |
| 272 | */ |
| 273 | public static AccountManagerService getSingleton() { |
| 274 | return sThis.get(); |
| 275 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 276 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 277 | public AccountManagerService(Injector injector) { |
| 278 | mInjector = injector; |
| 279 | mContext = injector.getContext(); |
| 280 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 281 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 282 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 283 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 284 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 285 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 286 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 287 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 288 | IntentFilter intentFilter = new IntentFilter(); |
| 289 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 290 | intentFilter.addDataScheme("package"); |
| 291 | mContext.registerReceiver(new BroadcastReceiver() { |
| 292 | @Override |
| 293 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 294 | // Don't delete accounts when updating a authenticator's |
| 295 | // package. |
| 296 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 297 | /* Purging data requires file io, don't block the main thread. This is probably |
| 298 | * less than ideal because we are introducing a race condition where old grants |
| 299 | * could be exercised until they are purged. But that race condition existed |
| 300 | * anyway with the broadcast receiver. |
| 301 | * |
| 302 | * Ideally, we would completely clear the cache, purge data from the database, |
| 303 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 304 | * large at this point. |
| 305 | */ |
Dmitry Dementyev | 0b67642 | 2017-03-09 11:51:26 -0800 | [diff] [blame] | 306 | final String removedPackageName = intent.getData().getSchemeSpecificPart(); |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 307 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 308 | @Override |
| 309 | public void run() { |
| 310 | purgeOldGrantsAll(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 311 | // Notify authenticator about removed app? |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 312 | removeVisibilityValuesForPackage(removedPackageName); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 313 | } |
| 314 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 315 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 316 | } |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 317 | } |
| 318 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 319 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 320 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 321 | |
Fyodor Kupolov | 945c97e | 2017-06-21 17:45:19 -0700 | [diff] [blame] | 322 | IntentFilter userFilter = new IntentFilter(); |
| 323 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 324 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 325 | @Override |
| 326 | public void onReceive(Context context, Intent intent) { |
| 327 | String action = intent.getAction(); |
| 328 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 329 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 330 | if (userId < 1) return; |
| 331 | Slog.i(TAG, "User " + userId + " removed"); |
| 332 | purgeUserData(userId); |
| 333 | } |
| 334 | } |
| 335 | }, UserHandle.ALL, userFilter, null, null); |
| 336 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 337 | // Need to cancel account request notifications if the update/install can access the account |
| 338 | new PackageMonitor() { |
| 339 | @Override |
| 340 | public void onPackageAdded(String packageName, int uid) { |
| 341 | // Called on a handler, and running as the system |
| 342 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 347 | // Called on a handler, and running as the system |
| 348 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 349 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 350 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 351 | |
| 352 | // Cancel account request notification if an app op was preventing the account access |
| 353 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 354 | new AppOpsManager.OnOpChangedInternalListener() { |
| 355 | @Override |
| 356 | public void onOpChanged(int op, String packageName) { |
| 357 | try { |
| 358 | final int userId = ActivityManager.getCurrentUser(); |
| 359 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 360 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 361 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 362 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 363 | final long identity = Binder.clearCallingIdentity(); |
| 364 | try { |
| 365 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 366 | } finally { |
| 367 | Binder.restoreCallingIdentity(identity); |
| 368 | } |
| 369 | } |
| 370 | } catch (NameNotFoundException e) { |
| 371 | /* ignore */ |
| 372 | } |
| 373 | } |
| 374 | }); |
| 375 | |
| 376 | // Cancel account request notification if a permission was preventing the account access |
| 377 | mPackageManager.addOnPermissionsChangeListener( |
| 378 | (int uid) -> { |
| 379 | Account[] accounts = null; |
| 380 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 381 | if (packageNames != null) { |
| 382 | final int userId = UserHandle.getUserId(uid); |
| 383 | final long identity = Binder.clearCallingIdentity(); |
| 384 | try { |
| 385 | for (String packageName : packageNames) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 386 | // if app asked for permission we need to cancel notification even |
| 387 | // for O+ applications. |
| 388 | if (mPackageManager.checkPermission( |
| 389 | Manifest.permission.GET_ACCOUNTS, |
| 390 | packageName) != PackageManager.PERMISSION_GRANTED) { |
| 391 | continue; |
| 392 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 393 | |
| 394 | if (accounts == null) { |
| 395 | accounts = getAccountsAsUser(null, userId, "android"); |
| 396 | if (ArrayUtils.isEmpty(accounts)) { |
| 397 | return; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | for (Account account : accounts) { |
| 402 | cancelAccountAccessRequestNotificationIfNeeded( |
| 403 | account, uid, packageName, true); |
| 404 | } |
| 405 | } |
| 406 | } finally { |
| 407 | Binder.restoreCallingIdentity(identity); |
| 408 | } |
| 409 | } |
| 410 | }); |
| 411 | } |
| 412 | |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 413 | |
| 414 | boolean getBindInstantServiceAllowed(int userId) { |
| 415 | return mAuthenticatorCache.getBindInstantServiceAllowed(userId); |
| 416 | } |
| 417 | |
| 418 | void setBindInstantServiceAllowed(int userId, boolean allowed) { |
| 419 | mAuthenticatorCache.setBindInstantServiceAllowed(userId, allowed); |
| 420 | } |
| 421 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 422 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 423 | boolean checkAccess) { |
| 424 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 425 | for (Account account : accounts) { |
| 426 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 431 | boolean checkAccess) { |
| 432 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 433 | for (Account account : accounts) { |
| 434 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 439 | boolean checkAccess) { |
| 440 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 441 | if (packageNames != null) { |
| 442 | for (String packageName : packageNames) { |
| 443 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 444 | packageName, checkAccess); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 450 | int uid, String packageName, boolean checkAccess) { |
| 451 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 452 | UserHandle.getUserHandleForUid(uid))) { |
| 453 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 454 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 455 | UserHandle.getUserHandleForUid(uid)); |
| 456 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 459 | @Override |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 460 | public boolean addAccountExplicitlyWithVisibility(Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 461 | Bundle extras, Map packageToVisibility) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 462 | Bundle.setDefusable(extras, true); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 463 | int callingUid = Binder.getCallingUid(); |
| 464 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 465 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 466 | Log.v(TAG, "addAccountExplicitly: " + account + ", caller's uid " + callingUid |
| 467 | + ", pid " + Binder.getCallingPid()); |
| 468 | } |
| 469 | Preconditions.checkNotNull(account, "account cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 470 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
| 471 | String msg = String.format("uid %s cannot explicitly add accounts of type: %s", |
| 472 | callingUid, account.type); |
| 473 | throw new SecurityException(msg); |
| 474 | } |
| 475 | /* |
| 476 | * Child users are not allowed to add accounts. Only the accounts that are shared by the |
| 477 | * parent profile can be added to child profile. |
| 478 | * |
| 479 | * TODO: Only allow accounts that were shared to be added by a limited user. |
| 480 | */ |
| 481 | // fails if the account already exists |
| 482 | long identityToken = clearCallingIdentity(); |
| 483 | try { |
| 484 | UserAccounts accounts = getUserAccounts(userId); |
| 485 | return addAccountInternal(accounts, account, password, extras, callingUid, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 486 | (Map<String, Integer>) packageToVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 487 | } finally { |
| 488 | restoreCallingIdentity(identityToken); |
| 489 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | @Override |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 493 | public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 494 | String accountType) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 495 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 496 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 497 | boolean isSystemUid = UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 498 | List<String> managedTypes = getTypesForCaller(callingUid, userId, isSystemUid); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 499 | |
| 500 | if ((accountType != null && !managedTypes.contains(accountType)) |
| 501 | || (accountType == null && !isSystemUid)) { |
| 502 | throw new SecurityException( |
| 503 | "getAccountsAndVisibilityForPackage() called from unauthorized uid " |
| 504 | + callingUid + " with packageName=" + packageName); |
| 505 | } |
| 506 | if (accountType != null) { |
| 507 | managedTypes = new ArrayList<String>(); |
| 508 | managedTypes.add(accountType); |
| 509 | } |
| 510 | |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 511 | long identityToken = clearCallingIdentity(); |
| 512 | try { |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 513 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 514 | return getAccountsAndVisibilityForPackage(packageName, managedTypes, callingUid, |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 515 | accounts); |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 516 | } finally { |
| 517 | restoreCallingIdentity(identityToken); |
| 518 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* |
| 522 | * accountTypes may not be null |
| 523 | */ |
| 524 | private Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 525 | List<String> accountTypes, Integer callingUid, UserAccounts accounts) { |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 526 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 527 | Log.d(TAG, "Package not found " + packageName); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 528 | return new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 531 | Map<Account, Integer> result = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 532 | for (String accountType : accountTypes) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 533 | synchronized (accounts.dbLock) { |
| 534 | synchronized (accounts.cacheLock) { |
| 535 | final Account[] accountsOfType = accounts.accountCache.get(accountType); |
| 536 | if (accountsOfType != null) { |
| 537 | for (Account account : accountsOfType) { |
| 538 | result.put(account, |
| 539 | resolveAccountVisibility(account, packageName, accounts)); |
| 540 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | return filterSharedAccounts(accounts, result, callingUid, packageName); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | @Override |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 549 | public Map<String, Integer> getPackagesAndVisibilityForAccount(Account account) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 550 | Preconditions.checkNotNull(account, "account cannot be null"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 551 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 552 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 553 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
| 554 | && !isSystemUid(callingUid)) { |
| 555 | String msg = |
| 556 | String.format("uid %s cannot get secrets for account %s", callingUid, account); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 557 | throw new SecurityException(msg); |
| 558 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 559 | |
| 560 | long identityToken = clearCallingIdentity(); |
| 561 | try { |
| 562 | UserAccounts accounts = getUserAccounts(userId); |
| 563 | synchronized (accounts.dbLock) { |
| 564 | synchronized (accounts.cacheLock) { |
| 565 | return getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 566 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 567 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 568 | } finally { |
| 569 | restoreCallingIdentity(identityToken); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 570 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 571 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 575 | * Returns Map with all package names and visibility values for given account. |
| 576 | * The method and returned map must be guarded by accounts.cacheLock |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 577 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 578 | * @param account Account to get visibility values. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 579 | * @param accounts UserAccount that currently hosts the account and application |
| 580 | * |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 581 | * @return Map with cache for package names to visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 582 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 583 | private @NonNull Map<String, Integer> getPackagesAndVisibilityForAccountLocked(Account account, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 584 | UserAccounts accounts) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 585 | Map<String, Integer> accountVisibility = accounts.visibilityCache.get(account); |
| 586 | if (accountVisibility == null) { |
| 587 | Log.d(TAG, "Visibility was not initialized"); |
| 588 | accountVisibility = new HashMap<>(); |
| 589 | accounts.visibilityCache.put(account, accountVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 590 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 591 | return accountVisibility; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 595 | public int getAccountVisibility(Account account, String packageName) { |
| 596 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 597 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 598 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 599 | int userId = UserHandle.getCallingUserId(); |
| 600 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 601 | && !isSystemUid(callingUid)) { |
| 602 | String msg = String.format( |
| 603 | "uid %s cannot get secrets for accounts of type: %s", |
| 604 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 605 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 606 | throw new SecurityException(msg); |
| 607 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 608 | long identityToken = clearCallingIdentity(); |
| 609 | try { |
| 610 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | cbe1bd1 | 2017-04-25 17:02:47 -0700 | [diff] [blame] | 611 | if (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName)) { |
| 612 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
| 613 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 614 | return visibility; |
| 615 | } else { |
| 616 | return AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 617 | } |
| 618 | } |
| 619 | if (AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)) { |
| 620 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
| 621 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 622 | return visibility; |
| 623 | } else { |
| 624 | return AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 625 | } |
| 626 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 627 | return resolveAccountVisibility(account, packageName, accounts); |
| 628 | } finally { |
| 629 | restoreCallingIdentity(identityToken); |
| 630 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 634 | * Method returns visibility for given account and package name. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 635 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 636 | * @param account The account to check visibility. |
| 637 | * @param packageName Package name to check visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 638 | * @param accounts UserAccount that currently hosts the account and application |
| 639 | * |
| 640 | * @return Visibility value, AccountManager.VISIBILITY_UNDEFINED if no value was stored. |
| 641 | * |
| 642 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 643 | private int getAccountVisibilityFromCache(Account account, String packageName, |
| 644 | UserAccounts accounts) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 645 | synchronized (accounts.cacheLock) { |
| 646 | Map<String, Integer> accountVisibility = |
| 647 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 648 | Integer visibility = accountVisibility.get(packageName); |
| 649 | return visibility != null ? visibility : AccountManager.VISIBILITY_UNDEFINED; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Method which handles default values for Account visibility. |
| 655 | * |
| 656 | * @param account The account to check visibility. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 657 | * @param packageName Package name to check visibility |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 658 | * @param accounts UserAccount that currently hosts the account and application |
| 659 | * |
| 660 | * @return Visibility value, the method never returns AccountManager.VISIBILITY_UNDEFINED |
| 661 | * |
| 662 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 663 | private Integer resolveAccountVisibility(Account account, @NonNull String packageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 664 | UserAccounts accounts) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 665 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 666 | int uid = -1; |
| 667 | try { |
| 668 | long identityToken = clearCallingIdentity(); |
| 669 | try { |
| 670 | uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 671 | } finally { |
| 672 | restoreCallingIdentity(identityToken); |
| 673 | } |
| 674 | } catch (NameNotFoundException e) { |
| 675 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 676 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 677 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 678 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 679 | // System visibility can not be restricted. |
| 680 | if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) { |
| 681 | return AccountManager.VISIBILITY_VISIBLE; |
| 682 | } |
| 683 | |
| 684 | int signatureCheckResult = |
| 685 | checkPackageSignature(account.type, uid, accounts.userId); |
| 686 | |
| 687 | // Authenticator can not restrict visibility to itself. |
| 688 | if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) { |
| 689 | return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account |
| 690 | } |
| 691 | |
| 692 | // Return stored value if it was set. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 693 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 694 | |
| 695 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 696 | return visibility; |
| 697 | } |
| 698 | |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 699 | boolean isPrivileged = isPermittedForPackage(packageName, uid, accounts.userId, |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 700 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 701 | |
| 702 | // Device/Profile owner gets visibility by default. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 703 | if (isProfileOwner(uid)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 704 | return AccountManager.VISIBILITY_VISIBLE; |
| 705 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 706 | |
| 707 | boolean preO = isPreOApplication(packageName); |
| 708 | if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH) |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 709 | || (preO && checkGetAccountsPermission(packageName, uid, accounts.userId)) |
| 710 | || (checkReadContactsPermission(packageName, uid, accounts.userId) |
| 711 | && accountTypeManagesContacts(account.type, accounts.userId)) |
| 712 | || isPrivileged) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 713 | // Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature |
| 714 | // match. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 715 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 716 | AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 717 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 718 | visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 719 | } |
| 720 | } else { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 721 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 722 | AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 723 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 724 | visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 725 | } |
| 726 | } |
| 727 | return visibility; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Checks targetSdk for a package; |
| 732 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 733 | * @param packageName Package name |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 734 | * |
| 735 | * @return True if package's target SDK is below {@link android.os.Build.VERSION_CODES#O}, or |
| 736 | * undefined |
| 737 | */ |
| 738 | private boolean isPreOApplication(String packageName) { |
| 739 | try { |
| 740 | long identityToken = clearCallingIdentity(); |
| 741 | ApplicationInfo applicationInfo; |
| 742 | try { |
| 743 | applicationInfo = mPackageManager.getApplicationInfo(packageName, 0); |
| 744 | } finally { |
| 745 | restoreCallingIdentity(identityToken); |
| 746 | } |
| 747 | |
| 748 | if (applicationInfo != null) { |
| 749 | int version = applicationInfo.targetSdkVersion; |
| 750 | return version < android.os.Build.VERSION_CODES.O; |
| 751 | } |
| 752 | return true; |
| 753 | } catch (NameNotFoundException e) { |
| 754 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 755 | return true; |
| 756 | } |
Dmitry Dementyev | 58fa8362 | 2016-12-20 18:08:51 -0800 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 760 | public boolean setAccountVisibility(Account account, String packageName, int newVisibility) { |
| 761 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 762 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 763 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 764 | int userId = UserHandle.getCallingUserId(); |
| 765 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 766 | && !isSystemUid(callingUid)) { |
| 767 | String msg = String.format( |
| 768 | "uid %s cannot get secrets for accounts of type: %s", |
| 769 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 770 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 771 | throw new SecurityException(msg); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 772 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 773 | long identityToken = clearCallingIdentity(); |
| 774 | try { |
| 775 | UserAccounts accounts = getUserAccounts(userId); |
| 776 | return setAccountVisibility(account, packageName, newVisibility, true /* notify */, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 777 | accounts); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 778 | } finally { |
| 779 | restoreCallingIdentity(identityToken); |
| 780 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 781 | } |
| 782 | |
sunjian | 066aa5e | 2017-06-05 12:16:59 -0700 | [diff] [blame] | 783 | private boolean isVisible(int visibility) { |
| 784 | return visibility == AccountManager.VISIBILITY_VISIBLE || |
| 785 | visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 786 | } |
| 787 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 788 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 789 | * Updates visibility for given account name and package. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 790 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 791 | * @param account Account to update visibility. |
| 792 | * @param packageName Package name for which visibility is updated. |
| 793 | * @param newVisibility New visibility calue |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 794 | * @param notify if the flag is set applications will get notification about visibility change |
| 795 | * @param accounts UserAccount that currently hosts the account and application |
| 796 | * |
| 797 | * @return True if account visibility was changed. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 798 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 799 | private boolean setAccountVisibility(Account account, String packageName, int newVisibility, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 800 | boolean notify, UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 801 | synchronized (accounts.dbLock) { |
| 802 | synchronized (accounts.cacheLock) { |
| 803 | Map<String, Integer> packagesToVisibility; |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 804 | List<String> accountRemovedReceivers; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 805 | if (notify) { |
| 806 | if (isSpecialPackageKey(packageName)) { |
| 807 | packagesToVisibility = |
| 808 | getRequestingPackages(account, accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 809 | accountRemovedReceivers = getAccountRemovedReceivers(account, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 810 | } else { |
| 811 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 812 | return false; // package is not installed. |
| 813 | } |
| 814 | packagesToVisibility = new HashMap<>(); |
| 815 | packagesToVisibility.put(packageName, |
| 816 | resolveAccountVisibility(account, packageName, accounts)); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 817 | accountRemovedReceivers = new ArrayList<>(); |
| 818 | if (shouldNotifyPackageOnAccountRemoval(account, packageName, accounts)) { |
| 819 | accountRemovedReceivers.add(packageName); |
| 820 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 821 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 822 | } else { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 823 | // Notifications will not be send - only used during add account. |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 824 | if (!isSpecialPackageKey(packageName) && |
| 825 | !packageExistsForUser(packageName, accounts.userId)) { |
| 826 | // package is not installed and not meta value. |
| 827 | return false; |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 828 | } |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 829 | packagesToVisibility = Collections.emptyMap(); |
| 830 | accountRemovedReceivers = Collections.emptyList(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 831 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 832 | |
| 833 | if (!updateAccountVisibilityLocked(account, packageName, newVisibility, accounts)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 834 | return false; |
| 835 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 836 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 837 | if (notify) { |
| 838 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 839 | .entrySet()) { |
sunjian | 066aa5e | 2017-06-05 12:16:59 -0700 | [diff] [blame] | 840 | int oldVisibility = packageToVisibility.getValue(); |
| 841 | int currentVisibility = |
| 842 | resolveAccountVisibility(account, packageName, accounts); |
| 843 | if (isVisible(oldVisibility) != isVisible(currentVisibility)) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 844 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 845 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 846 | } |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 847 | for (String packageNameToNotify : accountRemovedReceivers) { |
| 848 | sendAccountRemovedBroadcast(account, packageNameToNotify, accounts.userId); |
| 849 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 850 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 851 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 852 | return true; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 853 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 854 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 857 | // Update account visibility in cache and database. |
| 858 | private boolean updateAccountVisibilityLocked(Account account, String packageName, |
| 859 | int newVisibility, UserAccounts accounts) { |
| 860 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 861 | if (accountId < 0) { |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
| 866 | try { |
| 867 | if (!accounts.accountsDb.setAccountVisibility(accountId, packageName, |
| 868 | newVisibility)) { |
| 869 | return false; |
| 870 | } |
| 871 | } finally { |
| 872 | StrictMode.setThreadPolicy(oldPolicy); |
| 873 | } |
| 874 | Map<String, Integer> accountVisibility = |
| 875 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 876 | accountVisibility.put(packageName, newVisibility); |
| 877 | return true; |
| 878 | } |
| 879 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 880 | @Override |
| 881 | public void registerAccountListener(String[] accountTypes, String opPackageName) { |
| 882 | int callingUid = Binder.getCallingUid(); |
| 883 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 884 | |
| 885 | int userId = UserHandle.getCallingUserId(); |
| 886 | long identityToken = clearCallingIdentity(); |
| 887 | try { |
| 888 | UserAccounts accounts = getUserAccounts(userId); |
| 889 | registerAccountListener(accountTypes, opPackageName, accounts); |
| 890 | } finally { |
| 891 | restoreCallingIdentity(identityToken); |
| 892 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 893 | } |
| 894 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 895 | private void registerAccountListener(String[] accountTypes, String opPackageName, |
| 896 | UserAccounts accounts) { |
| 897 | synchronized (accounts.mReceiversForType) { |
| 898 | if (accountTypes == null) { |
| 899 | // null for any type |
| 900 | accountTypes = new String[] {null}; |
| 901 | } |
| 902 | for (String type : accountTypes) { |
| 903 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 904 | if (receivers == null) { |
| 905 | receivers = new HashMap<>(); |
| 906 | accounts.mReceiversForType.put(type, receivers); |
| 907 | } |
| 908 | Integer cnt = receivers.get(opPackageName); |
| 909 | receivers.put(opPackageName, cnt != null ? cnt + 1 : 1); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 914 | @Override |
| 915 | public void unregisterAccountListener(String[] accountTypes, String opPackageName) { |
| 916 | int callingUid = Binder.getCallingUid(); |
| 917 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 918 | int userId = UserHandle.getCallingUserId(); |
| 919 | long identityToken = clearCallingIdentity(); |
| 920 | try { |
| 921 | UserAccounts accounts = getUserAccounts(userId); |
| 922 | unregisterAccountListener(accountTypes, opPackageName, accounts); |
| 923 | } finally { |
| 924 | restoreCallingIdentity(identityToken); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | private void unregisterAccountListener(String[] accountTypes, String opPackageName, |
| 929 | UserAccounts accounts) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 930 | synchronized (accounts.mReceiversForType) { |
| 931 | if (accountTypes == null) { |
| 932 | // null for any type |
| 933 | accountTypes = new String[] {null}; |
| 934 | } |
| 935 | for (String type : accountTypes) { |
| 936 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 937 | if (receivers == null || receivers.get(opPackageName) == null) { |
| 938 | throw new IllegalArgumentException("attempt to unregister wrong receiver"); |
| 939 | } |
| 940 | Integer cnt = receivers.get(opPackageName); |
| 941 | if (cnt == 1) { |
| 942 | receivers.remove(opPackageName); |
| 943 | } else { |
| 944 | receivers.put(opPackageName, cnt - 1); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | // Send notification to all packages which can potentially see the account |
| 951 | private void sendNotificationAccountUpdated(Account account, UserAccounts accounts) { |
| 952 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 953 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 954 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 955 | if ((packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) |
| 956 | && (packageToVisibility.getValue() |
| 957 | != AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE)) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 958 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Sends a direct intent to a package, notifying it of account visibility change. |
| 965 | * |
| 966 | * @param packageName to send Account to |
| 967 | * @param accounts UserAccount that currently hosts the account |
| 968 | */ |
| 969 | private void notifyPackage(String packageName, UserAccounts accounts) { |
| 970 | Intent intent = new Intent(AccountManager.ACTION_VISIBLE_ACCOUNTS_CHANGED); |
| 971 | intent.setPackage(packageName); |
| 972 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 973 | mContext.sendBroadcastAsUser(intent, new UserHandle(accounts.userId)); |
| 974 | } |
| 975 | |
| 976 | // Returns a map from package name to visibility, for packages subscribed |
| 977 | // to notifications about any account type, or type of provided account |
| 978 | // account type or all types. |
| 979 | private Map<String, Integer> getRequestingPackages(Account account, UserAccounts accounts) { |
| 980 | Set<String> packages = new HashSet<>(); |
| 981 | synchronized (accounts.mReceiversForType) { |
| 982 | for (String type : new String[] {account.type, null}) { |
| 983 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 984 | if (receivers != null) { |
| 985 | packages.addAll(receivers.keySet()); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | Map<String, Integer> result = new HashMap<>(); |
| 990 | for (String packageName : packages) { |
| 991 | result.put(packageName, resolveAccountVisibility(account, packageName, accounts)); |
| 992 | } |
| 993 | return result; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 994 | } |
| 995 | |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 996 | // Returns a list of packages listening to ACTION_ACCOUNT_REMOVED able to see the account. |
| 997 | private List<String> getAccountRemovedReceivers(Account account, UserAccounts accounts) { |
| 998 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 999 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 1000 | List<ResolveInfo> receivers = |
| 1001 | mPackageManager.queryBroadcastReceiversAsUser(intent, 0, accounts.userId); |
| 1002 | List<String> result = new ArrayList<>(); |
| 1003 | if (receivers == null) { |
| 1004 | return result; |
| 1005 | } |
| 1006 | for (ResolveInfo resolveInfo: receivers) { |
| 1007 | String packageName = resolveInfo.activityInfo.applicationInfo.packageName; |
| 1008 | int visibility = resolveAccountVisibility(account, packageName, accounts); |
| 1009 | if (visibility == AccountManager.VISIBILITY_VISIBLE |
| 1010 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) { |
| 1011 | result.add(packageName); |
| 1012 | } |
| 1013 | } |
| 1014 | return result; |
| 1015 | } |
| 1016 | |
| 1017 | // Returns true if given package is listening to ACTION_ACCOUNT_REMOVED and can see the account. |
| 1018 | private boolean shouldNotifyPackageOnAccountRemoval(Account account, |
| 1019 | String packageName, UserAccounts accounts) { |
| 1020 | int visibility = resolveAccountVisibility(account, packageName, accounts); |
| 1021 | if (visibility != AccountManager.VISIBILITY_VISIBLE |
| 1022 | && visibility != AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) { |
| 1023 | return false; |
| 1024 | } |
| 1025 | |
| 1026 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 1027 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 1028 | intent.setPackage(packageName); |
| 1029 | List<ResolveInfo> receivers = |
| 1030 | mPackageManager.queryBroadcastReceiversAsUser(intent, 0, accounts.userId); |
| 1031 | return (receivers != null && receivers.size() > 0); |
| 1032 | } |
| 1033 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1034 | private boolean packageExistsForUser(String packageName, int userId) { |
| 1035 | try { |
| 1036 | long identityToken = clearCallingIdentity(); |
| 1037 | try { |
| 1038 | mPackageManager.getPackageUidAsUser(packageName, userId); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 1039 | return true; |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1040 | } finally { |
| 1041 | restoreCallingIdentity(identityToken); |
| 1042 | } |
| 1043 | } catch (NameNotFoundException e) { |
| 1044 | return false; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * Returns true if packageName is one of special values. |
| 1050 | */ |
| 1051 | private boolean isSpecialPackageKey(String packageName) { |
| 1052 | return (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName) |
| 1053 | || AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)); |
| 1054 | } |
| 1055 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1056 | private void sendAccountsChangedBroadcast(int userId) { |
| 1057 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 1058 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
| 1059 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1062 | private void sendAccountRemovedBroadcast(Account account, String packageName, int userId) { |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame] | 1063 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 1064 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1065 | intent.setPackage(packageName); |
| 1066 | intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 1067 | intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame] | 1068 | mContext.sendBroadcastAsUser(intent, new UserHandle(userId)); |
| 1069 | } |
| 1070 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1071 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 1072 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1073 | throws RemoteException { |
| 1074 | try { |
| 1075 | return super.onTransact(code, data, reply, flags); |
| 1076 | } catch (RuntimeException e) { |
| 1077 | // The account manager only throws security exceptions, so let's |
| 1078 | // log all others. |
| 1079 | if (!(e instanceof SecurityException)) { |
| 1080 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 1081 | } |
| 1082 | throw e; |
| 1083 | } |
| 1084 | } |
| 1085 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1086 | private UserManager getUserManager() { |
| 1087 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1088 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1089 | } |
| 1090 | return mUserManager; |
| 1091 | } |
| 1092 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1093 | /** |
| 1094 | * Validate internal set of accounts against installed authenticators for |
| 1095 | * given user. Clears cached authenticators before validating. |
| 1096 | */ |
| 1097 | public void validateAccounts(int userId) { |
| 1098 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1099 | // Invalidate user-specific cache to make sure we catch any |
| 1100 | // removed authenticators. |
| 1101 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * Validate internal set of accounts against installed authenticators for |
| 1106 | * given user. Clear cached authenticators before validating when requested. |
| 1107 | */ |
| 1108 | private void validateAccountsInternal( |
| 1109 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1110 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1111 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1112 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1113 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1114 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1115 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1116 | if (invalidateAuthenticatorCache) { |
| 1117 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 1118 | } |
| 1119 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1120 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 1121 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1122 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1123 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1124 | synchronized (accounts.dbLock) { |
| 1125 | synchronized (accounts.cacheLock) { |
| 1126 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1127 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1128 | // Get a map of stored authenticator types to UID |
| 1129 | final AccountsDb accountsDb = accounts.accountsDb; |
| 1130 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
| 1131 | // Create a list of authenticator type whose previous uid no longer exists |
| 1132 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
| 1133 | SparseBooleanArray knownUids = null; |
| 1134 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 1135 | String type = authToUidEntry.getKey(); |
| 1136 | int uid = authToUidEntry.getValue(); |
| 1137 | Integer knownUid = knownAuth.get(type); |
| 1138 | if (knownUid != null && uid == knownUid) { |
| 1139 | // Remove it from the knownAuth list if it's unchanged. |
| 1140 | knownAuth.remove(type); |
| 1141 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1142 | /* |
| 1143 | * The authenticator is presently not cached and should only be triggered |
| 1144 | * when we think an authenticator has been removed (or is being updated). |
| 1145 | * But we still want to check if any data with the associated uid is |
| 1146 | * around. This is an (imperfect) signal that the package may be updating. |
| 1147 | * |
| 1148 | * A side effect of this is that an authenticator sharing a uid with |
| 1149 | * multiple apps won't get its credentials wiped as long as some app with |
| 1150 | * that uid is still on the device. But I suspect that this is a rare case. |
| 1151 | * And it isn't clear to me how an attacker could really exploit that |
| 1152 | * feature. |
| 1153 | * |
| 1154 | * The upshot is that we don't have to worry about accounts getting |
| 1155 | * uninstalled while the authenticator's package is being updated. |
| 1156 | * |
| 1157 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1158 | if (knownUids == null) { |
| 1159 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
| 1160 | } |
| 1161 | if (!knownUids.get(uid)) { |
| 1162 | // The authenticator is not presently available to the cache. And the |
| 1163 | // package no longer has a data directory (so we surmise it isn't |
| 1164 | // updating). So purge its data from the account databases. |
| 1165 | obsoleteAuthType.add(type); |
| 1166 | // And delete it from the TABLE_META |
| 1167 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
| 1168 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1169 | } |
| 1170 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1171 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1172 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
| 1173 | // been re-enabled (after being updated for example), then we just overwrite the old |
| 1174 | // values. |
| 1175 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
| 1176 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
| 1177 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1178 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1179 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
| 1180 | try { |
| 1181 | accounts.accountCache.clear(); |
| 1182 | final HashMap<String, ArrayList<String>> accountNamesByType |
| 1183 | = new LinkedHashMap<>(); |
| 1184 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1185 | final long accountId = accountEntry.getKey(); |
| 1186 | final Account account = accountEntry.getValue(); |
| 1187 | if (obsoleteAuthType.contains(account.type)) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1188 | Slog.w(TAG, "deleting account " + account.toSafeString() |
| 1189 | + " because type " + account.type |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1190 | + "'s registered authenticator no longer exist."); |
| 1191 | Map<String, Integer> packagesToVisibility = |
| 1192 | getRequestingPackages(account, accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1193 | List<String> accountRemovedReceivers = |
| 1194 | getAccountRemovedReceivers(account, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1195 | accountsDb.beginTransaction(); |
| 1196 | try { |
| 1197 | accountsDb.deleteDeAccount(accountId); |
| 1198 | // Also delete from CE table if user is unlocked; if user is |
| 1199 | // currently locked the account will be removed later by |
| 1200 | // syncDeCeAccountsLocked |
| 1201 | if (userUnlocked) { |
| 1202 | accountsDb.deleteCeAccount(accountId); |
| 1203 | } |
| 1204 | accountsDb.setTransactionSuccessful(); |
| 1205 | } finally { |
| 1206 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1207 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1208 | accountDeleted = true; |
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 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1211 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1212 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1213 | accounts.userDataCache.remove(account); |
| 1214 | accounts.authTokenCache.remove(account); |
| 1215 | accounts.accountTokenCaches.remove(account); |
| 1216 | accounts.visibilityCache.remove(account); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1217 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1218 | for (Entry<String, Integer> packageToVisibility : |
| 1219 | packagesToVisibility.entrySet()) { |
sunjian | 066aa5e | 2017-06-05 12:16:59 -0700 | [diff] [blame] | 1220 | if (isVisible(packageToVisibility.getValue())) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1221 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 1222 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1223 | } |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1224 | for (String packageName : accountRemovedReceivers) { |
| 1225 | sendAccountRemovedBroadcast(account, packageName, accounts.userId); |
| 1226 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1227 | } else { |
| 1228 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
| 1229 | if (accountNames == null) { |
| 1230 | accountNames = new ArrayList<>(); |
| 1231 | accountNamesByType.put(account.type, accountNames); |
| 1232 | } |
| 1233 | accountNames.add(account.name); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1234 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1235 | } |
| 1236 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
| 1237 | final String accountType = cur.getKey(); |
| 1238 | final ArrayList<String> accountNames = cur.getValue(); |
| 1239 | final Account[] accountsForType = new Account[accountNames.size()]; |
| 1240 | for (int i = 0; i < accountsForType.length; i++) { |
| 1241 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
| 1242 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1243 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1244 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1245 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1246 | accounts.visibilityCache.putAll(accountsDb.findAllVisibilityValues()); |
| 1247 | } finally { |
| 1248 | if (accountDeleted) { |
| 1249 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1250 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1251 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1252 | } |
| 1253 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1256 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1257 | // Get the UIDs of all apps that might have data on the device. We want |
| 1258 | // to preserve user data if the app might otherwise be storing data. |
| 1259 | List<PackageInfo> pkgsWithData = |
| 1260 | mPackageManager.getInstalledPackagesAsUser( |
| 1261 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1262 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1263 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1264 | if (pkgInfo.applicationInfo != null |
| 1265 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1266 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1267 | } |
| 1268 | } |
| 1269 | return knownUids; |
| 1270 | } |
| 1271 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1272 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1273 | Context context, |
| 1274 | int userId) { |
| 1275 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1276 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1277 | } |
| 1278 | |
| 1279 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1280 | IAccountAuthenticatorCache authCache, |
| 1281 | int userId) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 1282 | HashMap<String, Integer> knownAuth = new LinkedHashMap<>(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1283 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1284 | .getAllServices(userId)) { |
| 1285 | knownAuth.put(service.type.type, service.uid); |
| 1286 | } |
| 1287 | return knownAuth; |
| 1288 | } |
| 1289 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1290 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1291 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | protected UserAccounts getUserAccounts(int userId) { |
| 1295 | synchronized (mUsers) { |
| 1296 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1297 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1298 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1299 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1300 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1301 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1302 | initializeDebugDbSizeAndCompileSqlStatementForLogging(accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1303 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1304 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1305 | validateAccounts = true; |
| 1306 | } |
| 1307 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1308 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1309 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1310 | synchronized (accounts.dbLock) { |
| 1311 | synchronized (accounts.cacheLock) { |
| 1312 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
| 1313 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
| 1314 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1315 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1316 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1317 | } |
| 1318 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1319 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1320 | } |
| 1321 | return accounts; |
| 1322 | } |
| 1323 | } |
| 1324 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1325 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1326 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1327 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1328 | if (!accountsToRemove.isEmpty()) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1329 | Slog.i(TAG, accountsToRemove.size() |
| 1330 | + " accounts were previously deleted while user " |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1331 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1332 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1333 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1334 | |
| 1335 | for (Account account : accountsToRemove) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1336 | removeAccountInternal(accounts, account, Process.SYSTEM_UID); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1341 | private void purgeOldGrantsAll() { |
| 1342 | synchronized (mUsers) { |
| 1343 | for (int i = 0; i < mUsers.size(); i++) { |
| 1344 | purgeOldGrants(mUsers.valueAt(i)); |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | private void purgeOldGrants(UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1350 | synchronized (accounts.dbLock) { |
| 1351 | synchronized (accounts.cacheLock) { |
| 1352 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
| 1353 | for (int uid : uids) { |
| 1354 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1355 | if (packageExists) { |
| 1356 | continue; |
| 1357 | } |
| 1358 | Log.d(TAG, "deleting grants for UID " + uid |
| 1359 | + " because its package is no longer installed"); |
| 1360 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1361 | } |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1366 | private void removeVisibilityValuesForPackage(String packageName) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1367 | if (isSpecialPackageKey(packageName)) { |
| 1368 | return; |
| 1369 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1370 | synchronized (mUsers) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1371 | int numberOfUsers = mUsers.size(); |
| 1372 | for (int i = 0; i < numberOfUsers; i++) { |
| 1373 | UserAccounts accounts = mUsers.valueAt(i); |
| 1374 | try { |
| 1375 | mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 1376 | } catch (NameNotFoundException e) { |
| 1377 | // package does not exist - remove visibility values |
| 1378 | accounts.accountsDb.deleteAccountVisibilityForPackage(packageName); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1379 | synchronized (accounts.dbLock) { |
| 1380 | synchronized (accounts.cacheLock) { |
| 1381 | for (Account account : accounts.visibilityCache.keySet()) { |
| 1382 | Map<String, Integer> accountVisibility = |
| 1383 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 1384 | accountVisibility.remove(packageName); |
| 1385 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1386 | } |
| 1387 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
Fyodor Kupolov | 945c97e | 2017-06-21 17:45:19 -0700 | [diff] [blame] | 1393 | private void purgeUserData(int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1394 | UserAccounts accounts; |
| 1395 | synchronized (mUsers) { |
| 1396 | accounts = mUsers.get(userId); |
| 1397 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1398 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1399 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1400 | if (accounts != null) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1401 | synchronized (accounts.dbLock) { |
| 1402 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 56e158f | 2017-05-23 16:41:51 -0700 | [diff] [blame] | 1403 | accounts.statementForLogging.close(); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1404 | accounts.accountsDb.close(); |
| 1405 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1406 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1407 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1410 | @VisibleForTesting |
| 1411 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1412 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1413 | } |
| 1414 | |
| 1415 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1416 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1417 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1418 | } |
| 1419 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1420 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1421 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1422 | if (userId < 1) return; |
Fyodor Kupolov | 2bc895d | 2017-12-19 11:53:47 -0800 | [diff] [blame] | 1423 | mHandler.post(() -> syncSharedAccounts(userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1424 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1425 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1426 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1427 | // Check if there's a shared account that needs to be created as an account |
| 1428 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1429 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1430 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1431 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1432 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1433 | : UserHandle.USER_SYSTEM; |
| 1434 | if (parentUserId < 0) { |
| 1435 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1436 | return; |
| 1437 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1438 | for (Account sa : sharedAccounts) { |
| 1439 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1440 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1441 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1445 | @Override |
| 1446 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1447 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1450 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1451 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1452 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1453 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1454 | Log.v(TAG, "getPassword: " + account |
| 1455 | + ", caller's uid " + Binder.getCallingUid() |
| 1456 | + ", pid " + Binder.getCallingPid()); |
| 1457 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1458 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1459 | int userId = UserHandle.getCallingUserId(); |
| 1460 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1461 | String msg = String.format( |
| 1462 | "uid %s cannot get secrets for accounts of type: %s", |
| 1463 | callingUid, |
| 1464 | account.type); |
| 1465 | throw new SecurityException(msg); |
| 1466 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1467 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1468 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1469 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1470 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1471 | } finally { |
| 1472 | restoreCallingIdentity(identityToken); |
| 1473 | } |
| 1474 | } |
| 1475 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1476 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1477 | if (account == null) { |
| 1478 | return null; |
| 1479 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1480 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1481 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1482 | return null; |
| 1483 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1484 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1485 | synchronized (accounts.dbLock) { |
| 1486 | synchronized (accounts.cacheLock) { |
| 1487 | return accounts.accountsDb |
| 1488 | .findAccountPasswordByNameAndType(account.name, account.type); |
| 1489 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1493 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1494 | public String getPreviousName(Account account) { |
| 1495 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1496 | Log.v(TAG, "getPreviousName: " + account |
| 1497 | + ", caller's uid " + Binder.getCallingUid() |
| 1498 | + ", pid " + Binder.getCallingPid()); |
| 1499 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1500 | Preconditions.checkNotNull(account, "account cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1501 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1502 | long identityToken = clearCallingIdentity(); |
| 1503 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1504 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1505 | return readPreviousNameInternal(accounts, account); |
| 1506 | } finally { |
| 1507 | restoreCallingIdentity(identityToken); |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1512 | if (account == null) { |
| 1513 | return null; |
| 1514 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1515 | synchronized (accounts.dbLock) { |
| 1516 | synchronized (accounts.cacheLock) { |
| 1517 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1518 | if (previousNameRef == null) { |
| 1519 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
| 1520 | previousNameRef = new AtomicReference<>(previousName); |
| 1521 | accounts.previousNameCache.put(account, previousNameRef); |
| 1522 | return previousName; |
| 1523 | } else { |
| 1524 | return previousNameRef.get(); |
| 1525 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1531 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1532 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1533 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1534 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1535 | account, key, callingUid, Binder.getCallingPid()); |
| 1536 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1537 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1538 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 1539 | Preconditions.checkNotNull(key, "key cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1540 | int userId = UserHandle.getCallingUserId(); |
| 1541 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1542 | String msg = String.format( |
| 1543 | "uid %s cannot get user data for accounts of type: %s", |
| 1544 | callingUid, |
| 1545 | account.type); |
| 1546 | throw new SecurityException(msg); |
| 1547 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1548 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1549 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1550 | return null; |
| 1551 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1552 | long identityToken = clearCallingIdentity(); |
| 1553 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1554 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1555 | if (!accountExistsCache(accounts, account)) { |
| 1556 | return null; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1557 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1558 | return readUserDataInternal(accounts, account, key); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1559 | } finally { |
| 1560 | restoreCallingIdentity(identityToken); |
| 1561 | } |
| 1562 | } |
| 1563 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1564 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1565 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1566 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1567 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1568 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1569 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1570 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1571 | + ", pid " + Binder.getCallingPid()); |
| 1572 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1573 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1574 | if (isCrossUser(callingUid, userId)) { |
| 1575 | throw new SecurityException( |
| 1576 | String.format( |
| 1577 | "User %s tying to get authenticator types for %s" , |
| 1578 | UserHandle.getCallingUserId(), |
| 1579 | userId)); |
| 1580 | } |
| 1581 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1582 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1583 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1584 | return getAuthenticatorTypesInternal(userId); |
| 1585 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1586 | } finally { |
| 1587 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1588 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1591 | /** |
| 1592 | * Should only be called inside of a clearCallingIdentity block. |
| 1593 | */ |
| 1594 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1595 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1596 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1597 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1598 | AuthenticatorDescription[] types = |
| 1599 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1600 | int i = 0; |
| 1601 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1602 | : authenticatorCollection) { |
| 1603 | types[i] = authenticator.type; |
| 1604 | i++; |
| 1605 | } |
| 1606 | return types; |
| 1607 | } |
| 1608 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1609 | private boolean isCrossUser(int callingUid, int userId) { |
| 1610 | return (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1611 | && callingUid != Process.SYSTEM_UID |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1612 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1613 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1614 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1615 | } |
| 1616 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1617 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1618 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1619 | return addAccountExplicitlyWithVisibility(account, password, extras, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1622 | @Override |
| 1623 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1624 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1625 | int callingUid = Binder.getCallingUid(); |
| 1626 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1627 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1628 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1629 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1630 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1631 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1632 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1633 | if (response != null) { |
| 1634 | Bundle result = new Bundle(); |
| 1635 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1636 | try { |
| 1637 | response.onResult(result); |
| 1638 | } catch (RemoteException e) { |
| 1639 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1640 | } |
| 1641 | } |
| 1642 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1643 | } |
| 1644 | |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1645 | Slog.d(TAG, "Copying account " + account.toSafeString() |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1646 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1647 | long identityToken = clearCallingIdentity(); |
| 1648 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1649 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1650 | false /* stripAuthTokenFromResult */, account.name, |
| 1651 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1652 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1653 | protected String toDebugString(long now) { |
| 1654 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1655 | + ", " + account.type; |
| 1656 | } |
| 1657 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1658 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1659 | public void run() throws RemoteException { |
| 1660 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1661 | } |
| 1662 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1663 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1664 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1665 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1666 | if (result != null |
| 1667 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1668 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1669 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1670 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1671 | super.onResult(result); |
| 1672 | } |
| 1673 | } |
| 1674 | }.bind(); |
| 1675 | } finally { |
| 1676 | restoreCallingIdentity(identityToken); |
| 1677 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1680 | @Override |
| 1681 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1682 | final int callingUid = Binder.getCallingUid(); |
| 1683 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1684 | String msg = String.format( |
| 1685 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1686 | account, |
| 1687 | callingUid); |
| 1688 | Log.v(TAG, msg); |
| 1689 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1690 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1691 | int userId = UserHandle.getCallingUserId(); |
| 1692 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1693 | String msg = String.format( |
| 1694 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1695 | callingUid, |
| 1696 | account.type); |
| 1697 | throw new SecurityException(msg); |
| 1698 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1699 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1700 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1701 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1702 | return false; |
| 1703 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1704 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1705 | long identityToken = clearCallingIdentity(); |
| 1706 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1707 | UserAccounts accounts = getUserAccounts(userId); |
| 1708 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1709 | } finally { |
| 1710 | restoreCallingIdentity(identityToken); |
| 1711 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1715 | final UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1716 | synchronized (accounts.dbLock) { |
| 1717 | synchronized (accounts.cacheLock) { |
| 1718 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
| 1719 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1720 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1721 | } |
| 1722 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1723 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1724 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1725 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1726 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1727 | long id = clearCallingIdentity(); |
| 1728 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1729 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1730 | false /* stripAuthTokenFromResult */, account.name, |
| 1731 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1732 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1733 | protected String toDebugString(long now) { |
| 1734 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1735 | + ", " + account.type; |
| 1736 | } |
| 1737 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1738 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1739 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1740 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 16bedd4 | 2017-03-30 10:00:49 -0700 | [diff] [blame] | 1741 | for (Account acc : getAccounts(parentUserId, mContext.getOpPackageName())) { |
| 1742 | if (acc.equals(account)) { |
| 1743 | mAuthenticator.addAccountFromCredentials( |
| 1744 | this, account, accountCredentials); |
| 1745 | break; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1746 | } |
| 1747 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1748 | } |
| 1749 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1750 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1751 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1752 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1753 | // TODO: Anything to do if if succedded? |
| 1754 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1755 | // account to avoid retries? |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1756 | // TODO: what we do with the visibility? |
| 1757 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1758 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1759 | } |
| 1760 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1761 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1762 | public void onError(int errorCode, String errorMessage) { |
| 1763 | super.onError(errorCode, errorMessage); |
| 1764 | // TODO: Show error notification to user |
| 1765 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1766 | } |
| 1767 | |
| 1768 | }.bind(); |
| 1769 | } finally { |
| 1770 | restoreCallingIdentity(id); |
| 1771 | } |
| 1772 | } |
| 1773 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1774 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1775 | Bundle extras, int callingUid, Map<String, Integer> packageToVisibility) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1776 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1777 | if (account == null) { |
| 1778 | return false; |
| 1779 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1780 | if (!isLocalUnlockedUser(accounts.userId)) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1781 | Log.w(TAG, "Account " + account.toSafeString() + " cannot be added - user " |
| 1782 | + accounts.userId + " is locked. callingUid=" + callingUid); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1783 | return false; |
| 1784 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1785 | synchronized (accounts.dbLock) { |
| 1786 | synchronized (accounts.cacheLock) { |
| 1787 | accounts.accountsDb.beginTransaction(); |
| 1788 | try { |
| 1789 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1790 | Log.w(TAG, "insertAccountIntoDatabase: " + account.toSafeString() |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1791 | + ", skipping since the account already exists"); |
| 1792 | return false; |
| 1793 | } |
| 1794 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
| 1795 | if (accountId < 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1796 | Log.w(TAG, "insertAccountIntoDatabase: " + account.toSafeString() |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1797 | + ", skipping the DB insert failed"); |
| 1798 | return false; |
| 1799 | } |
| 1800 | // Insert into DE table |
| 1801 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1802 | Log.w(TAG, "insertAccountIntoDatabase: " + account.toSafeString() |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1803 | + ", skipping the DB insert failed"); |
| 1804 | return false; |
| 1805 | } |
| 1806 | if (extras != null) { |
| 1807 | for (String key : extras.keySet()) { |
| 1808 | final String value = extras.getString(key); |
| 1809 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 1810 | Log.w(TAG, "insertAccountIntoDatabase: " |
| 1811 | + account.toSafeString() |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1812 | + ", skipping since insertExtra failed for key " + key); |
| 1813 | return false; |
| 1814 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1815 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1816 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1817 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1818 | if (packageToVisibility != null) { |
| 1819 | for (Entry<String, Integer> entry : packageToVisibility.entrySet()) { |
| 1820 | setAccountVisibility(account, entry.getKey() /* package */, |
| 1821 | entry.getValue() /* visibility */, false /* notify */, |
| 1822 | accounts); |
| 1823 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1824 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1825 | accounts.accountsDb.setTransactionSuccessful(); |
| 1826 | |
| 1827 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 1828 | accountId, |
| 1829 | accounts, callingUid); |
| 1830 | |
| 1831 | insertAccountIntoCacheLocked(accounts, account); |
| 1832 | } finally { |
| 1833 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1834 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1835 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1836 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1837 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1838 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1839 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1840 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1841 | sendNotificationAccountUpdated(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1842 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1843 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1844 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1845 | return true; |
| 1846 | } |
| 1847 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1848 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1849 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1850 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1851 | } |
| 1852 | } |
| 1853 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1854 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1855 | * 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] | 1856 | * running, then clone the account too. |
| 1857 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1858 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1859 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1860 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1861 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1862 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1863 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1864 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1865 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1866 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1867 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1868 | } |
| 1869 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1873 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1874 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1875 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1876 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1877 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1878 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1879 | Log.v(TAG, "hasFeatures: " + account |
| 1880 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1881 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1882 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1883 | + ", pid " + Binder.getCallingPid()); |
| 1884 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1885 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1886 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1887 | Preconditions.checkArgument(features != null, "features cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1888 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1889 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1890 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1891 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1892 | long identityToken = clearCallingIdentity(); |
| 1893 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1894 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1895 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1896 | } finally { |
| 1897 | restoreCallingIdentity(identityToken); |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | private class TestFeaturesSession extends Session { |
| 1902 | private final String[] mFeatures; |
| 1903 | private final Account mAccount; |
| 1904 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1905 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1906 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1907 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1908 | true /* stripAuthTokenFromResult */, account.name, |
| 1909 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1910 | mFeatures = features; |
| 1911 | mAccount = account; |
| 1912 | } |
| 1913 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1914 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1915 | public void run() throws RemoteException { |
| 1916 | try { |
| 1917 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1918 | } catch (RemoteException e) { |
| 1919 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1920 | } |
| 1921 | } |
| 1922 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1923 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1924 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1925 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1926 | IAccountManagerResponse response = getResponseAndClose(); |
| 1927 | if (response != null) { |
| 1928 | try { |
| 1929 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1930 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1931 | return; |
| 1932 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1933 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1934 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1935 | + response); |
| 1936 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1937 | final Bundle newResult = new Bundle(); |
| 1938 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1939 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1940 | response.onResult(newResult); |
| 1941 | } catch (RemoteException e) { |
| 1942 | // if the caller is dead then there is no one to care about remote exceptions |
| 1943 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1944 | Log.v(TAG, "failure while notifying response", e); |
| 1945 | } |
| 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1950 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1951 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1952 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1953 | + ", " + mAccount |
| 1954 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1955 | } |
| 1956 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1957 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1958 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1959 | public void renameAccount( |
| 1960 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1961 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1962 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1963 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1964 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1965 | + ", pid " + Binder.getCallingPid()); |
| 1966 | } |
| 1967 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1968 | int userId = UserHandle.getCallingUserId(); |
| 1969 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1970 | String msg = String.format( |
| 1971 | "uid %s cannot rename accounts of type: %s", |
| 1972 | callingUid, |
| 1973 | accountToRename.type); |
| 1974 | throw new SecurityException(msg); |
| 1975 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1976 | long identityToken = clearCallingIdentity(); |
| 1977 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1978 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1979 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1980 | Bundle result = new Bundle(); |
| 1981 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1982 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1983 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1984 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1985 | try { |
| 1986 | response.onResult(result); |
| 1987 | } catch (RemoteException e) { |
| 1988 | Log.w(TAG, e.getMessage()); |
| 1989 | } |
| 1990 | } finally { |
| 1991 | restoreCallingIdentity(identityToken); |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1996 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1997 | Account resultAccount = null; |
| 1998 | /* |
| 1999 | * Cancel existing notifications. Let authenticators |
| 2000 | * re-post notifications as required. But we don't know if |
| 2001 | * the authenticators have bound their notifications to |
| 2002 | * now stale account name data. |
| 2003 | * |
| 2004 | * With a rename api, we might not need to do this anymore but it |
| 2005 | * shouldn't hurt. |
| 2006 | */ |
| 2007 | cancelNotification( |
| 2008 | getSigninRequiredNotificationId(accounts, accountToRename), |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2009 | new UserHandle(accounts.userId)); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2010 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 2011 | for (Pair<Pair<Account, String>, Integer> pair: |
| 2012 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2013 | if (accountToRename.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2014 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2015 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 2016 | } |
| 2017 | } |
| 2018 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2019 | synchronized (accounts.dbLock) { |
| 2020 | synchronized (accounts.cacheLock) { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2021 | List<String> accountRemovedReceivers = |
| 2022 | getAccountRemovedReceivers(accountToRename, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2023 | accounts.accountsDb.beginTransaction(); |
| 2024 | Account renamedAccount = new Account(newName, accountToRename.type); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2025 | try { |
chengang | f2d081c | 2017-12-27 17:17:32 +0800 | [diff] [blame] | 2026 | if ((accounts.accountsDb.findCeAccountId(renamedAccount) >= 0)) { |
| 2027 | Log.e(TAG, "renameAccount failed - account with new name already exists"); |
| 2028 | return null; |
| 2029 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2030 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
| 2031 | if (accountId >= 0) { |
| 2032 | accounts.accountsDb.renameCeAccount(accountId, newName); |
| 2033 | if (accounts.accountsDb.renameDeAccount( |
| 2034 | accountId, newName, accountToRename.name)) { |
| 2035 | accounts.accountsDb.setTransactionSuccessful(); |
| 2036 | } else { |
| 2037 | Log.e(TAG, "renameAccount failed"); |
| 2038 | return null; |
| 2039 | } |
| 2040 | } else { |
| 2041 | Log.e(TAG, "renameAccount failed - old account does not exist"); |
| 2042 | return null; |
| 2043 | } |
| 2044 | } finally { |
| 2045 | accounts.accountsDb.endTransaction(); |
| 2046 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2047 | /* |
| 2048 | * Database transaction was successful. Clean up cached |
| 2049 | * data associated with the account in the user profile. |
| 2050 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2051 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2052 | /* |
| 2053 | * Extract the data and token caches before removing the |
| 2054 | * old account to preserve the user data associated with |
| 2055 | * the account. |
| 2056 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2057 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 2058 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
| 2059 | Map<String, Integer> tmpVisibility = accounts.visibilityCache.get(accountToRename); |
| 2060 | removeAccountFromCacheLocked(accounts, accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2061 | /* |
| 2062 | * Update the cached data associated with the renamed |
| 2063 | * account. |
| 2064 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2065 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 2066 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 2067 | accounts.visibilityCache.put(renamedAccount, tmpVisibility); |
| 2068 | accounts.previousNameCache.put( |
| 2069 | renamedAccount, |
| 2070 | new AtomicReference<>(accountToRename.name)); |
| 2071 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2072 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2073 | int parentUserId = accounts.userId; |
| 2074 | if (canHaveProfile(parentUserId)) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2075 | /* |
| 2076 | * Owner or system user account was renamed, rename the account for |
| 2077 | * those users with which the account was shared. |
| 2078 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2079 | List<UserInfo> users = getUserManager().getUsers(true); |
| 2080 | for (UserInfo user : users) { |
| 2081 | if (user.isRestricted() |
| 2082 | && (user.restrictedProfileParentId == parentUserId)) { |
| 2083 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
| 2084 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2085 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2086 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2087 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2088 | sendNotificationAccountUpdated(resultAccount, accounts); |
| 2089 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2090 | for (String packageName : accountRemovedReceivers) { |
| 2091 | sendAccountRemovedBroadcast(accountToRename, packageName, accounts.userId); |
| 2092 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2093 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2094 | } |
| 2095 | return resultAccount; |
| 2096 | } |
| 2097 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2098 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2099 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2100 | return userInfo != null && userInfo.canHaveProfile(); |
| 2101 | } |
| 2102 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2103 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2104 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 2105 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2106 | removeAccountAsUser( |
| 2107 | response, |
| 2108 | account, |
| 2109 | expectActivityLaunch, |
| 2110 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | @Override |
| 2114 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2115 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2116 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2117 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2118 | Log.v(TAG, "removeAccount: " + account |
| 2119 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2120 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2121 | + ", pid " + Binder.getCallingPid() |
| 2122 | + ", for user id " + userId); |
| 2123 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2124 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 2125 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 2126 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2127 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2128 | if (isCrossUser(callingUid, userId)) { |
| 2129 | throw new SecurityException( |
| 2130 | String.format( |
| 2131 | "User %s tying remove account for %s" , |
| 2132 | UserHandle.getCallingUserId(), |
| 2133 | userId)); |
| 2134 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2135 | /* |
Alex Chau | f788f9c | 2017-12-08 15:16:46 +0000 | [diff] [blame] | 2136 | * Only the system, authenticator or profile owner should be allowed to remove accounts for |
| 2137 | * that authenticator. This will let users remove accounts (via Settings in the system) but |
| 2138 | * not arbitrary applications (like competing authenticators). |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2139 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2140 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 2141 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
Alex Chau | f788f9c | 2017-12-08 15:16:46 +0000 | [diff] [blame] | 2142 | && !isSystemUid(callingUid) |
| 2143 | && !isProfileOwner(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2144 | String msg = String.format( |
| 2145 | "uid %s cannot remove accounts of type: %s", |
| 2146 | callingUid, |
| 2147 | account.type); |
| 2148 | throw new SecurityException(msg); |
| 2149 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2150 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2151 | try { |
| 2152 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2153 | "User cannot modify accounts"); |
| 2154 | } catch (RemoteException re) { |
| 2155 | } |
| 2156 | return; |
| 2157 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2158 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2159 | try { |
| 2160 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2161 | "User cannot modify accounts of this type (policy)."); |
| 2162 | } catch (RemoteException re) { |
| 2163 | } |
| 2164 | return; |
| 2165 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2166 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2167 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2168 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2169 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2170 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2171 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2172 | if (account.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2173 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2174 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2175 | } |
| 2176 | } |
| 2177 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2178 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2179 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2180 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2181 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2182 | accountId, |
| 2183 | accounts, |
| 2184 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2185 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2186 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2187 | } finally { |
| 2188 | restoreCallingIdentity(identityToken); |
| 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | @Override |
| 2193 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2194 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2195 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2196 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2197 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2198 | + ", pid " + Binder.getCallingPid()); |
| 2199 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2200 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2201 | if (account == null) { |
| 2202 | /* |
| 2203 | * Null accounts should result in returning false, as per |
| 2204 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2205 | */ |
| 2206 | Log.e(TAG, "account is null"); |
| 2207 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2208 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2209 | String msg = String.format( |
Brandon Weeks | 9e4e96d | 2017-08-24 15:24:16 -0700 | [diff] [blame] | 2210 | "uid %s cannot explicitly remove accounts of type: %s", |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2211 | callingUid, |
| 2212 | account.type); |
| 2213 | throw new SecurityException(msg); |
| 2214 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2215 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2216 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2217 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2218 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2219 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2220 | accountId, |
| 2221 | accounts, |
| 2222 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2223 | long identityToken = clearCallingIdentity(); |
| 2224 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2225 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2226 | } finally { |
| 2227 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2228 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2231 | private class RemoveAccountSession extends Session { |
| 2232 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2233 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2234 | Account account, boolean expectActivityLaunch) { |
| 2235 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2236 | true /* stripAuthTokenFromResult */, account.name, |
| 2237 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2238 | mAccount = account; |
| 2239 | } |
| 2240 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2241 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2242 | protected String toDebugString(long now) { |
| 2243 | return super.toDebugString(now) + ", removeAccount" |
| 2244 | + ", account " + mAccount; |
| 2245 | } |
| 2246 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2247 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2248 | public void run() throws RemoteException { |
| 2249 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2250 | } |
| 2251 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2252 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2253 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2254 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2255 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2256 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2257 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2258 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2259 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2260 | } |
| 2261 | IAccountManagerResponse response = getResponseAndClose(); |
| 2262 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2263 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2264 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2265 | + response); |
| 2266 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2267 | try { |
tiansiming | 5330b5a | 2017-10-13 10:57:25 +0800 | [diff] [blame] | 2268 | response.onResult(result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2269 | } catch (RemoteException e) { |
tiansiming | 5330b5a | 2017-10-13 10:57:25 +0800 | [diff] [blame] | 2270 | Slog.e(TAG, "Error calling onResult()", e); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | } |
| 2274 | super.onResult(result); |
| 2275 | } |
| 2276 | } |
| 2277 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2278 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2279 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2280 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2283 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2284 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2285 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2286 | if (!userUnlocked) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 2287 | Slog.i(TAG, "Removing account " + account.toSafeString() |
| 2288 | + " while user " + accounts.userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2289 | + " is still locked. CE data will be removed later"); |
| 2290 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2291 | synchronized (accounts.dbLock) { |
| 2292 | synchronized (accounts.cacheLock) { |
| 2293 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, |
| 2294 | accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2295 | List<String> accountRemovedReceivers = |
| 2296 | getAccountRemovedReceivers(account, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2297 | accounts.accountsDb.beginTransaction(); |
| 2298 | // Set to a dummy value, this will only be used if the database |
| 2299 | // transaction succeeds. |
| 2300 | long accountId = -1; |
| 2301 | try { |
| 2302 | accountId = accounts.accountsDb.findDeAccountId(account); |
| 2303 | if (accountId >= 0) { |
| 2304 | isChanged = accounts.accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2305 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2306 | // always delete from CE table if CE storage is available |
| 2307 | // DE account could be removed while CE was locked |
| 2308 | if (userUnlocked) { |
| 2309 | long ceAccountId = accounts.accountsDb.findCeAccountId(account); |
| 2310 | if (ceAccountId >= 0) { |
| 2311 | accounts.accountsDb.deleteCeAccount(ceAccountId); |
| 2312 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2313 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2314 | accounts.accountsDb.setTransactionSuccessful(); |
| 2315 | } finally { |
| 2316 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2317 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2318 | if (isChanged) { |
| 2319 | removeAccountFromCacheLocked(accounts, account); |
| 2320 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 2321 | .entrySet()) { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2322 | if ((packageToVisibility.getValue() == AccountManager.VISIBILITY_VISIBLE) |
| 2323 | || (packageToVisibility.getValue() |
| 2324 | == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE)) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2325 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 2326 | } |
| 2327 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2328 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2329 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occurred. |
| 2330 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2331 | for (String packageName : accountRemovedReceivers) { |
| 2332 | sendAccountRemovedBroadcast(account, packageName, accounts.userId); |
| 2333 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2334 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2335 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2336 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
| 2337 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2338 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2339 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2340 | long id = Binder.clearCallingIdentity(); |
| 2341 | try { |
| 2342 | int parentUserId = accounts.userId; |
| 2343 | if (canHaveProfile(parentUserId)) { |
| 2344 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2345 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2346 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2347 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2348 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2349 | } |
| 2350 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2351 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2352 | } finally { |
| 2353 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2354 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2355 | |
| 2356 | if (isChanged) { |
| 2357 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2358 | for (Pair<Pair<Account, String>, Integer> key |
| 2359 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2360 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2361 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2362 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2363 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2364 | account, uid, false)); |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | } |
| 2369 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2370 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2373 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2374 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2375 | int callerUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2376 | Preconditions.checkNotNull(accountType, "accountType cannot be null"); |
| 2377 | Preconditions.checkNotNull(authToken, "authToken cannot be null"); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2378 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2379 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2380 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2381 | + ", pid " + Binder.getCallingPid()); |
| 2382 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2383 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2384 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2385 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2386 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2387 | List<Pair<Account, String>> deletedTokens; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2388 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2389 | accounts.accountsDb.beginTransaction(); |
| 2390 | try { |
| 2391 | deletedTokens = invalidateAuthTokenLocked(accounts, accountType, authToken); |
| 2392 | accounts.accountsDb.setTransactionSuccessful(); |
| 2393 | } finally { |
| 2394 | accounts.accountsDb.endTransaction(); |
| 2395 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2396 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2397 | for (Pair<Account, String> tokenInfo : deletedTokens) { |
| 2398 | Account act = tokenInfo.first; |
| 2399 | String tokenType = tokenInfo.second; |
| 2400 | writeAuthTokenIntoCacheLocked(accounts, act, tokenType, null); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2401 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2402 | // wipe out cached token in memory. |
| 2403 | accounts.accountTokenCaches.remove(accountType, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2404 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2405 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2406 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2407 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2408 | } |
| 2409 | } |
| 2410 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2411 | private List<Pair<Account, String>> invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2412 | String authToken) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2413 | // TODO Move to AccountsDB |
| 2414 | List<Pair<Account, String>> results = new ArrayList<>(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2415 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2416 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2417 | try { |
| 2418 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2419 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2420 | String accountName = cursor.getString(1); |
| 2421 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2422 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2423 | results.add(Pair.create(new Account(accountName, accountType), authTokenType)); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2424 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2425 | } finally { |
| 2426 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2427 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2428 | return results; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2431 | private void saveCachedToken( |
| 2432 | UserAccounts accounts, |
| 2433 | Account account, |
| 2434 | String callerPkg, |
| 2435 | byte[] callerSigDigest, |
| 2436 | String tokenType, |
| 2437 | String token, |
| 2438 | long expiryMillis) { |
| 2439 | |
| 2440 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2441 | return; |
| 2442 | } |
| 2443 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2444 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2445 | synchronized (accounts.cacheLock) { |
| 2446 | accounts.accountTokenCaches.put( |
| 2447 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2448 | } |
| 2449 | } |
| 2450 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2451 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2452 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2453 | if (account == null || type == null) { |
| 2454 | return false; |
| 2455 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2456 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2457 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2458 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2459 | accounts.accountsDb.beginTransaction(); |
| 2460 | boolean updateCache = false; |
| 2461 | try { |
| 2462 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2463 | if (accountId < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2464 | return false; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2465 | } |
| 2466 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2467 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2468 | accounts.accountsDb.setTransactionSuccessful(); |
| 2469 | updateCache = true; |
| 2470 | return true; |
| 2471 | } |
| 2472 | return false; |
| 2473 | } finally { |
| 2474 | accounts.accountsDb.endTransaction(); |
| 2475 | if (updateCache) { |
| 2476 | synchronized (accounts.cacheLock) { |
| 2477 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
| 2478 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2479 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2480 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2484 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2485 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2486 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2487 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2488 | Log.v(TAG, "peekAuthToken: " + account |
| 2489 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2490 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2491 | + ", pid " + Binder.getCallingPid()); |
| 2492 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2493 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2494 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2495 | int userId = UserHandle.getCallingUserId(); |
| 2496 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2497 | String msg = String.format( |
| 2498 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2499 | callingUid, |
| 2500 | account.type); |
| 2501 | throw new SecurityException(msg); |
| 2502 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2503 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2504 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2505 | + callingUid); |
| 2506 | return null; |
| 2507 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2508 | long identityToken = clearCallingIdentity(); |
| 2509 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2510 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2511 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2512 | } finally { |
| 2513 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2514 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2517 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2518 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2519 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2520 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2521 | Log.v(TAG, "setAuthToken: " + account |
| 2522 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2523 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2524 | + ", pid " + Binder.getCallingPid()); |
| 2525 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2526 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2527 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2528 | int userId = UserHandle.getCallingUserId(); |
| 2529 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2530 | String msg = String.format( |
| 2531 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2532 | callingUid, |
| 2533 | account.type); |
| 2534 | throw new SecurityException(msg); |
| 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 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2540 | } finally { |
| 2541 | restoreCallingIdentity(identityToken); |
| 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 setPassword(Account account, String password) { |
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 |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2550 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2551 | + ", pid " + Binder.getCallingPid()); |
| 2552 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2553 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2554 | int userId = UserHandle.getCallingUserId(); |
| 2555 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2556 | String msg = String.format( |
| 2557 | "uid %s cannot set secrets for accounts of type: %s", |
| 2558 | callingUid, |
| 2559 | account.type); |
| 2560 | throw new SecurityException(msg); |
| 2561 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2562 | long identityToken = clearCallingIdentity(); |
| 2563 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2564 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2565 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2566 | } finally { |
| 2567 | restoreCallingIdentity(identityToken); |
| 2568 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2569 | } |
| 2570 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2571 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2572 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2573 | if (account == null) { |
| 2574 | return; |
| 2575 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2576 | boolean isChanged = false; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2577 | synchronized (accounts.dbLock) { |
| 2578 | synchronized (accounts.cacheLock) { |
| 2579 | accounts.accountsDb.beginTransaction(); |
| 2580 | try { |
| 2581 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2582 | if (accountId >= 0) { |
| 2583 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2584 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
| 2585 | accounts.authTokenCache.remove(account); |
| 2586 | accounts.accountTokenCaches.remove(account); |
| 2587 | accounts.accountsDb.setTransactionSuccessful(); |
| 2588 | // If there is an account whose password will be updated and the database |
| 2589 | // transactions succeed, then we say that a change has occured. Even if the |
| 2590 | // new password is the same as the old and there were no authtokens to |
| 2591 | // delete. |
| 2592 | isChanged = true; |
| 2593 | String action = (password == null || password.length() == 0) ? |
| 2594 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2595 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2596 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, |
| 2597 | callingUid); |
| 2598 | } |
| 2599 | } finally { |
| 2600 | accounts.accountsDb.endTransaction(); |
| 2601 | if (isChanged) { |
| 2602 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
| 2603 | sendNotificationAccountUpdated(account, accounts); |
| 2604 | sendAccountsChangedBroadcast(accounts.userId); |
| 2605 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2606 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2607 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2608 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2611 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2612 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2613 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2614 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2615 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2616 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2617 | + ", pid " + Binder.getCallingPid()); |
| 2618 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2619 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2620 | int userId = UserHandle.getCallingUserId(); |
| 2621 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2622 | String msg = String.format( |
| 2623 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2624 | callingUid, |
| 2625 | account.type); |
| 2626 | throw new SecurityException(msg); |
| 2627 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2628 | long identityToken = clearCallingIdentity(); |
| 2629 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2630 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2631 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2632 | } finally { |
| 2633 | restoreCallingIdentity(identityToken); |
| 2634 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2637 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2638 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2639 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2640 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2641 | Log.v(TAG, "setUserData: " + account |
| 2642 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2643 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2644 | + ", pid " + Binder.getCallingPid()); |
| 2645 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2646 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2647 | if (account == null) throw new IllegalArgumentException("account is 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 set user data 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(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2657 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2658 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2659 | if (!accountExistsCache(accounts, account)) { |
| 2660 | return; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2661 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2662 | setUserdataInternal(accounts, account, key, value); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2663 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2664 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2668 | private boolean accountExistsCache(UserAccounts accounts, Account account) { |
| 2669 | synchronized (accounts.cacheLock) { |
| 2670 | if (accounts.accountCache.containsKey(account.type)) { |
| 2671 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2672 | if (acc.name.equals(account.name)) { |
| 2673 | return true; |
| 2674 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2675 | } |
| 2676 | } |
| 2677 | } |
| 2678 | return false; |
| 2679 | } |
| 2680 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2681 | private void setUserdataInternal(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2682 | String value) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2683 | synchronized (accounts.dbLock) { |
| 2684 | accounts.accountsDb.beginTransaction(); |
| 2685 | try { |
| 2686 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2687 | if (accountId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2688 | return; |
| 2689 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2690 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
| 2691 | if (extrasId < 0) { |
| 2692 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
| 2693 | if (extrasId < 0) { |
| 2694 | return; |
| 2695 | } |
| 2696 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
| 2697 | return; |
| 2698 | } |
| 2699 | accounts.accountsDb.setTransactionSuccessful(); |
| 2700 | } finally { |
| 2701 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2702 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2703 | synchronized (accounts.cacheLock) { |
| 2704 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2705 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2706 | } |
| 2707 | } |
| 2708 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2709 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2710 | if (result == null) { |
| 2711 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2712 | } |
| 2713 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2714 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2715 | + response); |
| 2716 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2717 | try { |
| 2718 | response.onResult(result); |
| 2719 | } catch (RemoteException e) { |
| 2720 | // if the caller is dead then there is no one to care about remote |
| 2721 | // exceptions |
| 2722 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2723 | Log.v(TAG, "failure while notifying response", e); |
| 2724 | } |
| 2725 | } |
| 2726 | } |
| 2727 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2728 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2729 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2730 | final String authTokenType) |
| 2731 | throws RemoteException { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2732 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
| 2733 | Preconditions.checkArgument(authTokenType != null, "authTokenType cannot be null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2734 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2735 | final int callingUid = getCallingUid(); |
| 2736 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2737 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2738 | throw new SecurityException("can only call from system"); |
| 2739 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2740 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2741 | long identityToken = clearCallingIdentity(); |
| 2742 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2743 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2744 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2745 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2746 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2747 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2748 | protected String toDebugString(long now) { |
| 2749 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2750 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2751 | + ", authTokenType " + authTokenType; |
| 2752 | } |
| 2753 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2754 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2755 | public void run() throws RemoteException { |
| 2756 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2757 | } |
| 2758 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2759 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2760 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2761 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2762 | if (result != null) { |
| 2763 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2764 | Bundle bundle = new Bundle(); |
| 2765 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2766 | super.onResult(bundle); |
| 2767 | return; |
| 2768 | } else { |
| 2769 | super.onResult(result); |
| 2770 | } |
| 2771 | } |
| 2772 | }.bind(); |
| 2773 | } finally { |
| 2774 | restoreCallingIdentity(identityToken); |
| 2775 | } |
| 2776 | } |
| 2777 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2778 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2779 | public void getAuthToken( |
| 2780 | IAccountManagerResponse response, |
| 2781 | final Account account, |
| 2782 | final String authTokenType, |
| 2783 | final boolean notifyOnAuthFailure, |
| 2784 | final boolean expectActivityLaunch, |
| 2785 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2786 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2787 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2788 | Log.v(TAG, "getAuthToken: " + account |
| 2789 | + ", response " + response |
| 2790 | + ", authTokenType " + authTokenType |
| 2791 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2792 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2793 | + ", caller's uid " + Binder.getCallingUid() |
| 2794 | + ", pid " + Binder.getCallingPid()); |
| 2795 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2796 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2797 | try { |
| 2798 | if (account == null) { |
| 2799 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2800 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2801 | return; |
| 2802 | } |
| 2803 | if (authTokenType == null) { |
| 2804 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2805 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2806 | return; |
| 2807 | } |
| 2808 | } catch (RemoteException e) { |
| 2809 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2810 | return; |
| 2811 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2812 | int userId = UserHandle.getCallingUserId(); |
| 2813 | long ident = Binder.clearCallingIdentity(); |
| 2814 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2815 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2816 | try { |
| 2817 | accounts = getUserAccounts(userId); |
| 2818 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2819 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2820 | } finally { |
| 2821 | Binder.restoreCallingIdentity(ident); |
| 2822 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2823 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2824 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2825 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2826 | |
| 2827 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2828 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2829 | final boolean permissionGranted = |
| 2830 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2831 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2832 | // Get the calling package. We will use it for the purpose of caching. |
| 2833 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2834 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2835 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2836 | try { |
| 2837 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2838 | } finally { |
| 2839 | Binder.restoreCallingIdentity(ident); |
| 2840 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2841 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2842 | String msg = String.format( |
| 2843 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2844 | callerUid, |
| 2845 | callerPkg); |
| 2846 | throw new SecurityException(msg); |
| 2847 | } |
| 2848 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2849 | // let authenticator know the identity of the caller |
| 2850 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2851 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2852 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2853 | if (notifyOnAuthFailure) { |
| 2854 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2855 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2856 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2857 | long identityToken = clearCallingIdentity(); |
| 2858 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2859 | // Distill the caller's package signatures into a single digest. |
| 2860 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2861 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2862 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2863 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2864 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2865 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2866 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2867 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2868 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2869 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2870 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2871 | onResult(response, result); |
| 2872 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2873 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2874 | } |
| 2875 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2876 | if (customTokens) { |
| 2877 | /* |
| 2878 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2879 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2880 | * ANDORID_PACKAGE_NAME. |
| 2881 | */ |
| 2882 | String token = readCachedTokenInternal( |
| 2883 | accounts, |
| 2884 | account, |
| 2885 | authTokenType, |
| 2886 | callerPkg, |
| 2887 | callerPkgSigDigest); |
| 2888 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2889 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2890 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2891 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2892 | Bundle result = new Bundle(); |
| 2893 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2894 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2895 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2896 | onResult(response, result); |
| 2897 | return; |
| 2898 | } |
| 2899 | } |
| 2900 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2901 | new Session( |
| 2902 | accounts, |
| 2903 | response, |
| 2904 | account.type, |
| 2905 | expectActivityLaunch, |
| 2906 | false /* stripAuthTokenFromResult */, |
| 2907 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2908 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2909 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2910 | protected String toDebugString(long now) { |
| 2911 | if (loginOptions != null) loginOptions.keySet(); |
| 2912 | return super.toDebugString(now) + ", getAuthToken" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 2913 | + ", " + account.toSafeString() |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2914 | + ", authTokenType " + authTokenType |
| 2915 | + ", loginOptions " + loginOptions |
| 2916 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2917 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2918 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2919 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2920 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2921 | // If the caller doesn't have permission then create and return the |
| 2922 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2923 | if (!permissionGranted) { |
| 2924 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2925 | } else { |
| 2926 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2927 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2928 | } |
| 2929 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2930 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2931 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2932 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2933 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2934 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2935 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2936 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2937 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2938 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2939 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2940 | authTokenType, |
| 2941 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2942 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2943 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2944 | onResult(bundle); |
| 2945 | return; |
| 2946 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2947 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2948 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2949 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2950 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2951 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2952 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2953 | "the type and name should not be empty"); |
| 2954 | return; |
| 2955 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2956 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2957 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2958 | saveAuthTokenToDatabase( |
| 2959 | mAccounts, |
| 2960 | resultAccount, |
| 2961 | authTokenType, |
| 2962 | authToken); |
| 2963 | } |
| 2964 | long expiryMillis = result.getLong( |
| 2965 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2966 | if (customTokens |
| 2967 | && expiryMillis > System.currentTimeMillis()) { |
| 2968 | saveCachedToken( |
| 2969 | mAccounts, |
| 2970 | account, |
| 2971 | callerPkg, |
| 2972 | callerPkgSigDigest, |
| 2973 | authTokenType, |
| 2974 | authToken, |
| 2975 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2976 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2977 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2978 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2979 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2980 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2981 | /* |
| 2982 | * Make sure that the supplied intent is owned by the authenticator |
| 2983 | * giving it to the system. Otherwise a malicious authenticator could |
| 2984 | * have users launching arbitrary activities by tricking users to |
| 2985 | * interact with malicious notifications. |
| 2986 | */ |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 2987 | if (!checkKeyIntent( |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2988 | Binder.getCallingUid(), |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 2989 | intent)) { |
| 2990 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 2991 | "invalid intent in bundle returned"); |
| 2992 | return; |
| 2993 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2994 | doNotification( |
| 2995 | mAccounts, |
| 2996 | account, |
| 2997 | result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2998 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2999 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3000 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3001 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3002 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3003 | }.bind(); |
| 3004 | } finally { |
| 3005 | restoreCallingIdentity(identityToken); |
| 3006 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3007 | } |
| 3008 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 3009 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 3010 | MessageDigest digester; |
| 3011 | try { |
| 3012 | digester = MessageDigest.getInstance("SHA-256"); |
| 3013 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 3014 | callerPkg, PackageManager.GET_SIGNATURES); |
| 3015 | for (Signature sig : pkgInfo.signatures) { |
| 3016 | digester.update(sig.toByteArray()); |
| 3017 | } |
| 3018 | } catch (NoSuchAlgorithmException x) { |
| 3019 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 3020 | digester = null; |
| 3021 | } catch (NameNotFoundException e) { |
| 3022 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 3023 | digester = null; |
| 3024 | } |
| 3025 | return (digester == null) ? null : digester.digest(); |
| 3026 | } |
| 3027 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3028 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3029 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3030 | int uid = intent.getIntExtra( |
| 3031 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 3032 | String authTokenType = intent.getStringExtra( |
| 3033 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 3034 | final String titleAndSubtitle = |
| 3035 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 3036 | account.name); |
| 3037 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 3038 | String title = titleAndSubtitle; |
| 3039 | String subtitle = ""; |
| 3040 | if (index > 0) { |
| 3041 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 3042 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 3043 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 3044 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 3045 | Context contextForUser = getContextForUser(user); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3046 | Notification n = |
| 3047 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
| 3048 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 3049 | .setWhen(0) |
| 3050 | .setColor(contextForUser.getColor( |
| 3051 | com.android.internal.R.color.system_notification_accent_color)) |
| 3052 | .setContentTitle(title) |
| 3053 | .setContentText(subtitle) |
| 3054 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 3055 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 3056 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3057 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3058 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3059 | } |
| 3060 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3061 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 3062 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 3063 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3064 | |
| 3065 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 3066 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3067 | if (startInNewTask) { |
| 3068 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 3069 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 3070 | // the intent from a non-Activity context. This is the default behavior. |
| 3071 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3072 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3073 | intent.addCategory(getCredentialPermissionNotificationId(account, |
| 3074 | authTokenType, uid).mTag + (packageName != null ? packageName : "")); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3075 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3076 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 3077 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3078 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 3079 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3080 | return intent; |
| 3081 | } |
| 3082 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3083 | private NotificationId getCredentialPermissionNotificationId(Account account, |
| 3084 | String authTokenType, int uid) { |
| 3085 | NotificationId nId; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3086 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3087 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3088 | final Pair<Pair<Account, String>, Integer> key = |
| 3089 | new Pair<Pair<Account, String>, Integer>( |
| 3090 | new Pair<Account, String>(account, authTokenType), uid); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3091 | nId = accounts.credentialsPermissionNotificationIds.get(key); |
| 3092 | if (nId == null) { |
| 3093 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION |
| 3094 | + ":" + account.hashCode() + ":" + authTokenType.hashCode(); |
| 3095 | int id = SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION; |
| 3096 | nId = new NotificationId(tag, id); |
| 3097 | accounts.credentialsPermissionNotificationIds.put(key, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3098 | } |
| 3099 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3100 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3101 | } |
| 3102 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3103 | private NotificationId getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
| 3104 | NotificationId nId; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3105 | synchronized (accounts.signinRequiredNotificationIds) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3106 | nId = accounts.signinRequiredNotificationIds.get(account); |
| 3107 | if (nId == null) { |
| 3108 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN |
| 3109 | + ":" + account.hashCode(); |
| 3110 | int id = SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN; |
| 3111 | nId = new NotificationId(tag, id); |
| 3112 | accounts.signinRequiredNotificationIds.put(account, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3113 | } |
| 3114 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3115 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3116 | } |
| 3117 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3118 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3119 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3120 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3121 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3122 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3123 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3124 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3125 | + ", response " + response |
| 3126 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3127 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3128 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3129 | + ", caller's uid " + Binder.getCallingUid() |
| 3130 | + ", pid " + Binder.getCallingPid()); |
| 3131 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3132 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3133 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3134 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3135 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3136 | final int uid = Binder.getCallingUid(); |
| 3137 | final int userId = UserHandle.getUserId(uid); |
| 3138 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3139 | try { |
| 3140 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3141 | "User is not allowed to add an account!"); |
| 3142 | } catch (RemoteException re) { |
| 3143 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3144 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3145 | return; |
| 3146 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3147 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 3148 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3149 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3150 | "User cannot modify accounts of this type (policy)."); |
| 3151 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 3152 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3153 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3154 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3155 | return; |
| 3156 | } |
| 3157 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3158 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3159 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3160 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3161 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3162 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3163 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3164 | long identityToken = clearCallingIdentity(); |
| 3165 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3166 | UserAccounts accounts = getUserAccounts(usrId); |
| 3167 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3168 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3169 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3170 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3171 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3172 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3173 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3174 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 3175 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3176 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3177 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3178 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3179 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3180 | protected String toDebugString(long now) { |
| 3181 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3182 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3183 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3184 | } |
| 3185 | }.bind(); |
| 3186 | } finally { |
| 3187 | restoreCallingIdentity(identityToken); |
| 3188 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3189 | } |
| 3190 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3191 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3192 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 3193 | final String authTokenType, final String[] requiredFeatures, |
| 3194 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3195 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3196 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3197 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3198 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3199 | + ", response " + response |
| 3200 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3201 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3202 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3203 | + ", caller's uid " + Binder.getCallingUid() |
| 3204 | + ", pid " + Binder.getCallingPid() |
| 3205 | + ", for user id " + userId); |
| 3206 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3207 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3208 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3209 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3210 | if (isCrossUser(callingUid, userId)) { |
| 3211 | throw new SecurityException( |
| 3212 | String.format( |
| 3213 | "User %s trying to add account for %s" , |
| 3214 | UserHandle.getCallingUserId(), |
| 3215 | userId)); |
| 3216 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3217 | |
| 3218 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3219 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3220 | try { |
| 3221 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3222 | "User is not allowed to add an account!"); |
| 3223 | } catch (RemoteException re) { |
| 3224 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3225 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3226 | return; |
| 3227 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3228 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3229 | try { |
| 3230 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3231 | "User cannot modify accounts of this type (policy)."); |
| 3232 | } catch (RemoteException re) { |
| 3233 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3234 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3235 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3236 | return; |
| 3237 | } |
| 3238 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3239 | final int pid = Binder.getCallingPid(); |
| 3240 | final int uid = Binder.getCallingUid(); |
| 3241 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3242 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3243 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3244 | |
| 3245 | long identityToken = clearCallingIdentity(); |
| 3246 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3247 | UserAccounts accounts = getUserAccounts(userId); |
| 3248 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3249 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3250 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3251 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3252 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3253 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3254 | @Override |
| 3255 | public void run() throws RemoteException { |
| 3256 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3257 | options); |
| 3258 | } |
| 3259 | |
| 3260 | @Override |
| 3261 | protected String toDebugString(long now) { |
| 3262 | return super.toDebugString(now) + ", addAccount" |
| 3263 | + ", accountType " + accountType |
| 3264 | + ", requiredFeatures " |
| 3265 | + (requiredFeatures != null |
| 3266 | ? TextUtils.join(",", requiredFeatures) |
| 3267 | : null); |
| 3268 | } |
| 3269 | }.bind(); |
| 3270 | } finally { |
| 3271 | restoreCallingIdentity(identityToken); |
| 3272 | } |
| 3273 | } |
| 3274 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3275 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3276 | public void startAddAccountSession( |
| 3277 | final IAccountManagerResponse response, |
| 3278 | final String accountType, |
| 3279 | final String authTokenType, |
| 3280 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3281 | final boolean expectActivityLaunch, |
| 3282 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3283 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3284 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3285 | Log.v(TAG, |
| 3286 | "startAddAccountSession: accountType " + accountType |
| 3287 | + ", response " + response |
| 3288 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3289 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3290 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3291 | + ", caller's uid " + Binder.getCallingUid() |
| 3292 | + ", pid " + Binder.getCallingPid()); |
| 3293 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3294 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3295 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3296 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3297 | final int uid = Binder.getCallingUid(); |
| 3298 | final int userId = UserHandle.getUserId(uid); |
| 3299 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3300 | try { |
| 3301 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3302 | "User is not allowed to add an account!"); |
| 3303 | } catch (RemoteException re) { |
| 3304 | } |
| 3305 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3306 | return; |
| 3307 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3308 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3309 | try { |
| 3310 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3311 | "User cannot modify accounts of this type (policy)."); |
| 3312 | } catch (RemoteException re) { |
| 3313 | } |
| 3314 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3315 | userId); |
| 3316 | return; |
| 3317 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3318 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3319 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3320 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3321 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3322 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3323 | // Check to see if the Password should be included to the caller. |
| 3324 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3325 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3326 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3327 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3328 | long identityToken = clearCallingIdentity(); |
| 3329 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3330 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3331 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3332 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3333 | new StartAccountSession( |
| 3334 | accounts, |
| 3335 | response, |
| 3336 | accountType, |
| 3337 | expectActivityLaunch, |
| 3338 | null /* accountName */, |
| 3339 | false /* authDetailsRequired */, |
| 3340 | true /* updateLastAuthenticationTime */, |
| 3341 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3342 | @Override |
| 3343 | public void run() throws RemoteException { |
| 3344 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3345 | requiredFeatures, options); |
| 3346 | } |
| 3347 | |
| 3348 | @Override |
| 3349 | protected String toDebugString(long now) { |
| 3350 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3351 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3352 | + accountType + ", requiredFeatures " |
| 3353 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3354 | } |
| 3355 | }.bind(); |
| 3356 | } finally { |
| 3357 | restoreCallingIdentity(identityToken); |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3362 | private abstract class StartAccountSession extends Session { |
| 3363 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3364 | private final boolean mIsPasswordForwardingAllowed; |
| 3365 | |
| 3366 | public StartAccountSession( |
| 3367 | UserAccounts accounts, |
| 3368 | IAccountManagerResponse response, |
| 3369 | String accountType, |
| 3370 | boolean expectActivityLaunch, |
| 3371 | String accountName, |
| 3372 | boolean authDetailsRequired, |
| 3373 | boolean updateLastAuthenticationTime, |
| 3374 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3375 | super(accounts, response, accountType, expectActivityLaunch, |
| 3376 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3377 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3378 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | @Override |
| 3382 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3383 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3384 | mNumResults++; |
| 3385 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3386 | if (result != null |
| 3387 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 3388 | if (!checkKeyIntent( |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3389 | Binder.getCallingUid(), |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 3390 | intent)) { |
| 3391 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3392 | "invalid intent in bundle returned"); |
| 3393 | return; |
| 3394 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3395 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3396 | IAccountManagerResponse response; |
| 3397 | if (mExpectActivityLaunch && result != null |
| 3398 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3399 | response = mResponse; |
| 3400 | } else { |
| 3401 | response = getResponseAndClose(); |
| 3402 | } |
| 3403 | if (response == null) { |
| 3404 | return; |
| 3405 | } |
| 3406 | if (result == null) { |
| 3407 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3408 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3409 | + response); |
| 3410 | } |
| 3411 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3412 | "null bundle returned"); |
| 3413 | return; |
| 3414 | } |
| 3415 | |
| 3416 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3417 | // All AccountManager error codes are greater |
| 3418 | // than 0 |
| 3419 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3420 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3421 | return; |
| 3422 | } |
| 3423 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3424 | // Omit passwords if the caller isn't permitted to see them. |
| 3425 | if (!mIsPasswordForwardingAllowed) { |
| 3426 | result.remove(AccountManager.KEY_PASSWORD); |
| 3427 | } |
| 3428 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3429 | // Strip auth token from result. |
| 3430 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3431 | |
| 3432 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3433 | Log.v(TAG, |
| 3434 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3435 | } |
| 3436 | |
| 3437 | // Get the session bundle created by authenticator. The |
| 3438 | // bundle contains data necessary for finishing the session |
| 3439 | // later. The session bundle will be encrypted here and |
| 3440 | // decrypted later when trying to finish the session. |
| 3441 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3442 | if (sessionBundle != null) { |
| 3443 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3444 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3445 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3446 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3447 | } |
| 3448 | // Add accountType info to session bundle. This will |
| 3449 | // override any value set by authenticator. |
| 3450 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3451 | |
| 3452 | // Encrypt session bundle before returning to caller. |
| 3453 | try { |
| 3454 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3455 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3456 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3457 | } catch (GeneralSecurityException e) { |
| 3458 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3459 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3460 | } |
| 3461 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3462 | "failed to encrypt session bundle"); |
| 3463 | return; |
| 3464 | } |
| 3465 | } |
| 3466 | |
| 3467 | sendResponse(response, result); |
| 3468 | } |
| 3469 | } |
| 3470 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3471 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3472 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3473 | @NonNull Bundle sessionBundle, |
| 3474 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3475 | Bundle appInfo, |
| 3476 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3477 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3478 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3479 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3480 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3481 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3482 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3483 | + ", caller's uid " + callingUid |
| 3484 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3485 | + ", pid " + Binder.getCallingPid() |
| 3486 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3487 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3488 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3489 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3490 | // Account type is added to it before encryption. |
| 3491 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3492 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3493 | } |
| 3494 | |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 3495 | // Only allow the system process to finish session for other users. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3496 | if (isCrossUser(callingUid, userId)) { |
| 3497 | throw new SecurityException( |
| 3498 | String.format( |
| 3499 | "User %s trying to finish session for %s without cross user permission", |
| 3500 | UserHandle.getCallingUserId(), |
| 3501 | userId)); |
| 3502 | } |
| 3503 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3504 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3505 | sendErrorResponse(response, |
| 3506 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3507 | "User is not allowed to add an account!"); |
| 3508 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3509 | return; |
| 3510 | } |
| 3511 | |
| 3512 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3513 | final Bundle decryptedBundle; |
| 3514 | final String accountType; |
| 3515 | // First decrypt session bundle to get account type for checking permission. |
| 3516 | try { |
| 3517 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3518 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3519 | if (decryptedBundle == null) { |
| 3520 | sendErrorResponse( |
| 3521 | response, |
| 3522 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3523 | "failed to decrypt session bundle"); |
| 3524 | return; |
| 3525 | } |
| 3526 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3527 | // Account type cannot be null. This should not happen if session bundle was created |
| 3528 | // properly by #StartAccountSession. |
| 3529 | if (TextUtils.isEmpty(accountType)) { |
| 3530 | sendErrorResponse( |
| 3531 | response, |
| 3532 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3533 | "accountType is empty"); |
| 3534 | return; |
| 3535 | } |
| 3536 | |
| 3537 | // If by any chances, decryptedBundle contains colliding keys with |
| 3538 | // system info |
| 3539 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3540 | // update credentials flow, we should replace with the new values of the current call. |
| 3541 | if (appInfo != null) { |
| 3542 | decryptedBundle.putAll(appInfo); |
| 3543 | } |
| 3544 | |
| 3545 | // 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] | 3546 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3547 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3548 | } catch (GeneralSecurityException e) { |
| 3549 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3550 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3551 | } |
| 3552 | sendErrorResponse( |
| 3553 | response, |
| 3554 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3555 | "failed to decrypt session bundle"); |
| 3556 | return; |
| 3557 | } |
| 3558 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3559 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3560 | sendErrorResponse( |
| 3561 | response, |
| 3562 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3563 | "User cannot modify accounts of this type (policy)."); |
| 3564 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3565 | userId); |
| 3566 | return; |
| 3567 | } |
| 3568 | |
| 3569 | long identityToken = clearCallingIdentity(); |
| 3570 | try { |
| 3571 | UserAccounts accounts = getUserAccounts(userId); |
| 3572 | logRecordWithUid( |
| 3573 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3574 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3575 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3576 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3577 | new Session( |
| 3578 | accounts, |
| 3579 | response, |
| 3580 | accountType, |
| 3581 | expectActivityLaunch, |
| 3582 | true /* stripAuthTokenFromResult */, |
| 3583 | null /* accountName */, |
| 3584 | false /* authDetailsRequired */, |
| 3585 | true /* updateLastAuthenticationTime */) { |
| 3586 | @Override |
| 3587 | public void run() throws RemoteException { |
| 3588 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3589 | } |
| 3590 | |
| 3591 | @Override |
| 3592 | protected String toDebugString(long now) { |
| 3593 | return super.toDebugString(now) |
| 3594 | + ", finishSession" |
| 3595 | + ", accountType " + accountType; |
| 3596 | } |
| 3597 | }.bind(); |
| 3598 | } finally { |
| 3599 | restoreCallingIdentity(identityToken); |
| 3600 | } |
| 3601 | } |
| 3602 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3603 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3604 | final DevicePolicyManagerInternal dpmi = |
| 3605 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3606 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3607 | if (dpmi == null) { |
| 3608 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3609 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3610 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3611 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3612 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3613 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3614 | } |
| 3615 | if (intent == null) { |
| 3616 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3617 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3618 | long identityToken = clearCallingIdentity(); |
| 3619 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3620 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3621 | } finally { |
| 3622 | restoreCallingIdentity(identityToken); |
| 3623 | } |
| 3624 | } |
| 3625 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3626 | /** |
| 3627 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3628 | */ |
| 3629 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3630 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3631 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3632 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3633 | return cantAddAccount; |
| 3634 | } |
| 3635 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3636 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3637 | public void confirmCredentialsAsUser( |
| 3638 | IAccountManagerResponse response, |
| 3639 | final Account account, |
| 3640 | final Bundle options, |
| 3641 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3642 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3643 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3644 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3645 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3646 | Log.v(TAG, "confirmCredentials: " + account |
| 3647 | + ", response " + response |
| 3648 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3649 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3650 | + ", pid " + Binder.getCallingPid()); |
| 3651 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3652 | // Only allow the system process to read accounts of other users |
| 3653 | if (isCrossUser(callingUid, userId)) { |
| 3654 | throw new SecurityException( |
| 3655 | String.format( |
| 3656 | "User %s trying to confirm account credentials for %s" , |
| 3657 | UserHandle.getCallingUserId(), |
| 3658 | userId)); |
| 3659 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3660 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3661 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3662 | long identityToken = clearCallingIdentity(); |
| 3663 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3664 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3665 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3666 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3667 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3668 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3669 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3670 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3671 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3672 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3673 | protected String toDebugString(long now) { |
| 3674 | return super.toDebugString(now) + ", confirmCredentials" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 3675 | + ", " + account.toSafeString(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3676 | } |
| 3677 | }.bind(); |
| 3678 | } finally { |
| 3679 | restoreCallingIdentity(identityToken); |
| 3680 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3681 | } |
| 3682 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3683 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3684 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3685 | final String authTokenType, final boolean expectActivityLaunch, |
| 3686 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3687 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3688 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3689 | Log.v(TAG, "updateCredentials: " + account |
| 3690 | + ", response " + response |
| 3691 | + ", authTokenType " + authTokenType |
| 3692 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3693 | + ", caller's uid " + Binder.getCallingUid() |
| 3694 | + ", pid " + Binder.getCallingPid()); |
| 3695 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3696 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3697 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3698 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3699 | long identityToken = clearCallingIdentity(); |
| 3700 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3701 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3702 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3703 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3704 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3705 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3706 | public void run() throws RemoteException { |
| 3707 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3708 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3709 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3710 | protected String toDebugString(long now) { |
| 3711 | if (loginOptions != null) loginOptions.keySet(); |
| 3712 | return super.toDebugString(now) + ", updateCredentials" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 3713 | + ", " + account.toSafeString() |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3714 | + ", authTokenType " + authTokenType |
| 3715 | + ", loginOptions " + loginOptions; |
| 3716 | } |
| 3717 | }.bind(); |
| 3718 | } finally { |
| 3719 | restoreCallingIdentity(identityToken); |
| 3720 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3721 | } |
| 3722 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3723 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3724 | public void startUpdateCredentialsSession( |
| 3725 | IAccountManagerResponse response, |
| 3726 | final Account account, |
| 3727 | final String authTokenType, |
| 3728 | final boolean expectActivityLaunch, |
| 3729 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3730 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3731 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3732 | Log.v(TAG, |
| 3733 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3734 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3735 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3736 | + ", pid " + Binder.getCallingPid()); |
| 3737 | } |
| 3738 | if (response == null) { |
| 3739 | throw new IllegalArgumentException("response is null"); |
| 3740 | } |
| 3741 | if (account == null) { |
| 3742 | throw new IllegalArgumentException("account is null"); |
| 3743 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3744 | |
| 3745 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3746 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3747 | |
| 3748 | // Check to see if the Password should be included to the caller. |
| 3749 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3750 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3751 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3752 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3753 | long identityToken = clearCallingIdentity(); |
| 3754 | try { |
| 3755 | UserAccounts accounts = getUserAccounts(userId); |
| 3756 | new StartAccountSession( |
| 3757 | accounts, |
| 3758 | response, |
| 3759 | account.type, |
| 3760 | expectActivityLaunch, |
| 3761 | account.name, |
| 3762 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3763 | true /* updateLastCredentialTime */, |
| 3764 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3765 | @Override |
| 3766 | public void run() throws RemoteException { |
| 3767 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3768 | loginOptions); |
| 3769 | } |
| 3770 | |
| 3771 | @Override |
| 3772 | protected String toDebugString(long now) { |
| 3773 | if (loginOptions != null) |
| 3774 | loginOptions.keySet(); |
| 3775 | return super.toDebugString(now) |
| 3776 | + ", startUpdateCredentialsSession" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 3777 | + ", " + account.toSafeString() |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3778 | + ", authTokenType " + authTokenType |
| 3779 | + ", loginOptions " + loginOptions; |
| 3780 | } |
| 3781 | }.bind(); |
| 3782 | } finally { |
| 3783 | restoreCallingIdentity(identityToken); |
| 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3788 | public void isCredentialsUpdateSuggested( |
| 3789 | IAccountManagerResponse response, |
| 3790 | final Account account, |
| 3791 | final String statusToken) { |
| 3792 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3793 | Log.v(TAG, |
| 3794 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3795 | + ", caller's uid " + Binder.getCallingUid() |
| 3796 | + ", pid " + Binder.getCallingPid()); |
| 3797 | } |
| 3798 | if (response == null) { |
| 3799 | throw new IllegalArgumentException("response is null"); |
| 3800 | } |
| 3801 | if (account == null) { |
| 3802 | throw new IllegalArgumentException("account is null"); |
| 3803 | } |
| 3804 | if (TextUtils.isEmpty(statusToken)) { |
| 3805 | throw new IllegalArgumentException("status token is empty"); |
| 3806 | } |
| 3807 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3808 | int usrId = UserHandle.getCallingUserId(); |
| 3809 | long identityToken = clearCallingIdentity(); |
| 3810 | try { |
| 3811 | UserAccounts accounts = getUserAccounts(usrId); |
| 3812 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3813 | false /* stripAuthTokenFromResult */, account.name, |
| 3814 | false /* authDetailsRequired */) { |
| 3815 | @Override |
| 3816 | protected String toDebugString(long now) { |
| 3817 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 3818 | + ", " + account.toSafeString(); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | @Override |
| 3822 | public void run() throws RemoteException { |
| 3823 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3824 | } |
| 3825 | |
| 3826 | @Override |
| 3827 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3828 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3829 | IAccountManagerResponse response = getResponseAndClose(); |
| 3830 | if (response == null) { |
| 3831 | return; |
| 3832 | } |
| 3833 | |
| 3834 | if (result == null) { |
| 3835 | sendErrorResponse( |
| 3836 | response, |
| 3837 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3838 | "null bundle"); |
| 3839 | return; |
| 3840 | } |
| 3841 | |
| 3842 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3843 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3844 | + response); |
| 3845 | } |
| 3846 | // Check to see if an error occurred. We know if an error occurred because all |
| 3847 | // error codes are greater than 0. |
| 3848 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3849 | sendErrorResponse(response, |
| 3850 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3851 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3852 | return; |
| 3853 | } |
| 3854 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3855 | sendErrorResponse( |
| 3856 | response, |
| 3857 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3858 | "no result in response"); |
| 3859 | return; |
| 3860 | } |
| 3861 | final Bundle newResult = new Bundle(); |
| 3862 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3863 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3864 | sendResponse(response, newResult); |
| 3865 | } |
| 3866 | }.bind(); |
| 3867 | } finally { |
| 3868 | restoreCallingIdentity(identityToken); |
| 3869 | } |
| 3870 | } |
| 3871 | |
| 3872 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3873 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3874 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3875 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3876 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3877 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3878 | + ", response " + response |
| 3879 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3880 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3881 | + ", pid " + Binder.getCallingPid()); |
| 3882 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3883 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3884 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3885 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3886 | if (!isAccountManagedByCaller(accountType, callingUid, userId) |
| 3887 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3888 | String msg = String.format( |
| 3889 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3890 | callingUid, |
| 3891 | accountType); |
| 3892 | throw new SecurityException(msg); |
| 3893 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3894 | long identityToken = clearCallingIdentity(); |
| 3895 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3896 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3897 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3898 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3899 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3900 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3901 | public void run() throws RemoteException { |
| 3902 | mAuthenticator.editProperties(this, mAccountType); |
| 3903 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3904 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3905 | protected String toDebugString(long now) { |
| 3906 | return super.toDebugString(now) + ", editProperties" |
| 3907 | + ", accountType " + accountType; |
| 3908 | } |
| 3909 | }.bind(); |
| 3910 | } finally { |
| 3911 | restoreCallingIdentity(identityToken); |
| 3912 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3913 | } |
| 3914 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3915 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3916 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3917 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3918 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3919 | throw new SecurityException("Can be called only by system UID"); |
| 3920 | } |
| 3921 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3922 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3923 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3924 | |
| 3925 | final int userId = userHandle.getIdentifier(); |
| 3926 | |
| 3927 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3928 | |
| 3929 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3930 | int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3931 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3932 | } catch (NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3933 | Log.d(TAG, "Package not found " + e.getMessage()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3934 | return false; |
| 3935 | } |
| 3936 | } |
| 3937 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3938 | // Returns package with oldest target SDK for given UID. |
| 3939 | private String getPackageNameForUid(int uid) { |
| 3940 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3941 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3942 | return null; |
| 3943 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3944 | String packageName = packageNames[0]; |
Fyodor Kupolov | 892fc8d | 2017-03-22 12:57:04 -0700 | [diff] [blame] | 3945 | if (packageNames.length == 1) { |
| 3946 | return packageName; |
| 3947 | } |
| 3948 | // 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] | 3949 | int oldestVersion = Integer.MAX_VALUE; |
| 3950 | for (String name : packageNames) { |
| 3951 | try { |
| 3952 | ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(name, 0); |
| 3953 | if (applicationInfo != null) { |
| 3954 | int version = applicationInfo.targetSdkVersion; |
| 3955 | if (version < oldestVersion) { |
| 3956 | oldestVersion = version; |
| 3957 | packageName = name; |
| 3958 | } |
| 3959 | } |
| 3960 | } catch (NameNotFoundException e) { |
| 3961 | // skip |
| 3962 | } |
| 3963 | } |
| 3964 | return packageName; |
| 3965 | } |
| 3966 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3967 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3968 | int uid) { |
| 3969 | if (packageName == null) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3970 | packageName = getPackageNameForUid(uid); |
| 3971 | if (packageName == null) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3972 | return false; |
| 3973 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | // Use null token which means any token. Having a token means the package |
| 3977 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3978 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3979 | return true; |
| 3980 | } |
| 3981 | // 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] | 3982 | // 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] | 3983 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3984 | int visibility = resolveAccountVisibility(account, packageName, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3985 | getUserAccounts(UserHandle.getUserId(uid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3986 | return (visibility == AccountManager.VISIBILITY_VISIBLE |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3987 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | @Override |
| 3991 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3992 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3993 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3994 | throw new SecurityException("Can be called only by system UID"); |
| 3995 | } |
| 3996 | |
| 3997 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3998 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3999 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 4000 | |
| 4001 | final int userId = userHandle.getIdentifier(); |
| 4002 | |
| 4003 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 4004 | |
| 4005 | final int uid; |
| 4006 | try { |
| 4007 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4008 | } catch (NameNotFoundException e) { |
| 4009 | Slog.e(TAG, "Unknown package " + packageName); |
| 4010 | return null; |
| 4011 | } |
| 4012 | |
| 4013 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 4014 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 4015 | final long identity = Binder.clearCallingIdentity(); |
| 4016 | try { |
| 4017 | return PendingIntent.getActivityAsUser( |
| 4018 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 4019 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 4020 | null, new UserHandle(userId)).getIntentSender(); |
| 4021 | } finally { |
| 4022 | Binder.restoreCallingIdentity(identity); |
| 4023 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4024 | } |
| 4025 | |
| 4026 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 4027 | int uid, RemoteCallback callback) { |
| 4028 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 4029 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 4030 | @Override |
| 4031 | public void onResult(Bundle value) throws RemoteException { |
| 4032 | handleAuthenticatorResponse(true); |
| 4033 | } |
| 4034 | |
| 4035 | @Override |
| 4036 | public void onRequestContinued() { |
| 4037 | /* ignore */ |
| 4038 | } |
| 4039 | |
| 4040 | @Override |
| 4041 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 4042 | handleAuthenticatorResponse(false); |
| 4043 | } |
| 4044 | |
| 4045 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 4046 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 4047 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4048 | UserHandle.getUserHandleForUid(uid)); |
| 4049 | if (callback != null) { |
| 4050 | Bundle result = new Bundle(); |
| 4051 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 4052 | callback.sendResult(result); |
| 4053 | } |
| 4054 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 4055 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 4059 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 4060 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 4061 | throw new SecurityException("Only system can check for accounts across users"); |
| 4062 | } |
| 4063 | final long token = Binder.clearCallingIdentity(); |
| 4064 | try { |
| 4065 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 4066 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 4067 | if (allAccounts[i].account.equals(account)) { |
| 4068 | return true; |
| 4069 | } |
| 4070 | } |
| 4071 | return false; |
| 4072 | } finally { |
| 4073 | Binder.restoreCallingIdentity(token); |
| 4074 | } |
| 4075 | } |
| 4076 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4077 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 4078 | private final String[] mFeatures; |
| 4079 | private volatile Account[] mAccountsOfType = null; |
| 4080 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 4081 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4082 | private final int mCallingUid; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4083 | private final String mPackageName; |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4084 | private final boolean mIncludeManagedNotVisible; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4085 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4086 | public GetAccountsByTypeAndFeatureSession( |
| 4087 | UserAccounts accounts, |
| 4088 | IAccountManagerResponse response, |
| 4089 | String type, |
| 4090 | String[] features, |
| 4091 | int callingUid, |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4092 | String packageName, |
| 4093 | boolean includeManagedNotVisible) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4094 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4095 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 4096 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4097 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4098 | mFeatures = features; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4099 | mPackageName = packageName; |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4100 | mIncludeManagedNotVisible = includeManagedNotVisible; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4101 | } |
| 4102 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4103 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4104 | public void run() throws RemoteException { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4105 | mAccountsOfType = getAccountsFromCache(mAccounts, mAccountType, |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4106 | mCallingUid, mPackageName, mIncludeManagedNotVisible); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4107 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 4108 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4109 | mCurrentAccount = 0; |
| 4110 | |
| 4111 | checkAccount(); |
| 4112 | } |
| 4113 | |
| 4114 | public void checkAccount() { |
| 4115 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 4116 | sendResult(); |
| 4117 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4118 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4119 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 4120 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 4121 | if (accountAuthenticator == null) { |
| 4122 | // It is possible that the authenticator has died, which is indicated by |
| 4123 | // mAuthenticator being set to null. If this happens then just abort. |
| 4124 | // There is no need to send back a result or error in this case since |
| 4125 | // that already happened when mAuthenticator was cleared. |
| 4126 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4127 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 4128 | + " connected to the authenticator, " + toDebugString()); |
| 4129 | } |
| 4130 | return; |
| 4131 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4132 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 4133 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4134 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4135 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4136 | } |
| 4137 | } |
| 4138 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4139 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4140 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4141 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4142 | mNumResults++; |
| 4143 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4144 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4145 | return; |
| 4146 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4147 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4148 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 4149 | } |
| 4150 | mCurrentAccount++; |
| 4151 | checkAccount(); |
| 4152 | } |
| 4153 | |
| 4154 | public void sendResult() { |
| 4155 | IAccountManagerResponse response = getResponseAndClose(); |
| 4156 | if (response != null) { |
| 4157 | try { |
| 4158 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 4159 | for (int i = 0; i < accounts.length; i++) { |
| 4160 | accounts[i] = mAccountsWithFeatures.get(i); |
| 4161 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4162 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4163 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 4164 | + response); |
| 4165 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4166 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4167 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4168 | response.onResult(result); |
| 4169 | } catch (RemoteException e) { |
| 4170 | // if the caller is dead then there is no one to care about remote exceptions |
| 4171 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4172 | Log.v(TAG, "failure while notifying response", e); |
| 4173 | } |
| 4174 | } |
| 4175 | } |
| 4176 | } |
| 4177 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4178 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4179 | protected String toDebugString(long now) { |
| 4180 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 4181 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 4182 | } |
| 4183 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4184 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4185 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4186 | * 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] | 4187 | * @hide |
| 4188 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4189 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4190 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4191 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4192 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4193 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4194 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4195 | if (visibleAccountTypes.isEmpty()) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4196 | return EMPTY_ACCOUNT_ARRAY; |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4197 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4198 | long identityToken = clearCallingIdentity(); |
| 4199 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4200 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4201 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4202 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4203 | callingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4204 | opPackageName, |
| 4205 | visibleAccountTypes, |
| 4206 | false /* includeUserManagedNotVisible */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4207 | } finally { |
| 4208 | restoreCallingIdentity(identityToken); |
| 4209 | } |
| 4210 | } |
| 4211 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4212 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4213 | * Returns accounts for all running users, ignores visibility values. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4214 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4215 | * @hide |
| 4216 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4217 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4218 | public AccountAndUser[] getRunningAccounts() { |
| 4219 | final int[] runningUserIds; |
| 4220 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4221 | runningUserIds = ActivityManager.getService().getRunningUserIds(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4222 | } catch (RemoteException e) { |
| 4223 | // Running in system_server; should never happen |
| 4224 | throw new RuntimeException(e); |
| 4225 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4226 | return getAccounts(runningUserIds); |
| 4227 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4228 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4229 | /** |
| 4230 | * Returns accounts for all users, ignores visibility values. |
| 4231 | * |
| 4232 | * @hide |
| 4233 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4234 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4235 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 4236 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4237 | final int[] userIds = new int[users.size()]; |
| 4238 | for (int i = 0; i < userIds.length; i++) { |
| 4239 | userIds[i] = users.get(i).id; |
| 4240 | } |
| 4241 | return getAccounts(userIds); |
| 4242 | } |
| 4243 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4244 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4245 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4246 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4247 | for (int userId : userIds) { |
| 4248 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4249 | if (userAccounts == null) continue; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4250 | Account[] accounts = getAccountsFromCache( |
| 4251 | userAccounts, |
| 4252 | null /* type */, |
| 4253 | Binder.getCallingUid(), |
| 4254 | null /* packageName */, |
| 4255 | false /* include managed not visible*/); |
| 4256 | for (Account account : accounts) { |
| 4257 | runningAccounts.add(new AccountAndUser(account, userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4258 | } |
| 4259 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4260 | |
| 4261 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4262 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4263 | } |
| 4264 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4265 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4266 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4267 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4268 | int callingUid = Binder.getCallingUid(); |
| 4269 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4270 | return getAccountsAsUserForPackage(type, userId, opPackageName /* callingPackage */, -1, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4271 | opPackageName, false /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4272 | } |
| 4273 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4274 | @NonNull |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4275 | private Account[] getAccountsAsUserForPackage( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4276 | String type, |
| 4277 | int userId, |
| 4278 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4279 | int packageUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4280 | String opPackageName, |
| 4281 | boolean includeUserManagedNotVisible) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4282 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4283 | // Only allow the system process to read accounts of other users |
| 4284 | if (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4285 | && callingUid != Process.SYSTEM_UID |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4286 | && mContext.checkCallingOrSelfPermission( |
| 4287 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4288 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4289 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4290 | + " trying to get account for " + userId); |
| 4291 | } |
| 4292 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4293 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4294 | Log.v(TAG, "getAccounts: accountType " + type |
| 4295 | + ", caller's uid " + Binder.getCallingUid() |
| 4296 | + ", pid " + Binder.getCallingPid()); |
| 4297 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4298 | |
| 4299 | // If the original calling app was using account choosing activity |
| 4300 | // provided by the framework or authenticator we'll passing in |
| 4301 | // the original caller's uid here, which is what should be used for filtering. |
| 4302 | List<String> managedTypes = |
| 4303 | getTypesManagedByCaller(callingUid, UserHandle.getUserId(callingUid)); |
| 4304 | if (packageUid != -1 && |
| 4305 | ((UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 4306 | || (type != null && managedTypes.contains(type))))) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4307 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4308 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4309 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4310 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4311 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4312 | if (visibleAccountTypes.isEmpty() |
| 4313 | || (type != null && !visibleAccountTypes.contains(type))) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4314 | return EMPTY_ACCOUNT_ARRAY; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4315 | } else if (visibleAccountTypes.contains(type)) { |
| 4316 | // Prune the list down to just the requested type. |
| 4317 | visibleAccountTypes = new ArrayList<>(); |
| 4318 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4319 | } // else aggregate all the visible accounts (it won't matter if the |
| 4320 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4321 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4322 | long identityToken = clearCallingIdentity(); |
| 4323 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4324 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 4325 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4326 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4327 | callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4328 | opPackageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4329 | visibleAccountTypes, |
| 4330 | includeUserManagedNotVisible); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4331 | } finally { |
| 4332 | restoreCallingIdentity(identityToken); |
| 4333 | } |
| 4334 | } |
| 4335 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4336 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4337 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4338 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4339 | int callingUid, |
| 4340 | String callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4341 | List<String> visibleAccountTypes, |
| 4342 | boolean includeUserManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4343 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4344 | for (String visibleType : visibleAccountTypes) { |
| 4345 | Account[] accountsForType = getAccountsFromCache( |
| 4346 | userAccounts, visibleType, callingUid, callingPackage, |
| 4347 | includeUserManagedNotVisible); |
| 4348 | if (accountsForType != null) { |
| 4349 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4350 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4351 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4352 | Account[] result = new Account[visibleAccounts.size()]; |
| 4353 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4354 | result[i] = visibleAccounts.get(i); |
| 4355 | } |
| 4356 | return result; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4359 | @Override |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4360 | public void addSharedAccountsFromParentUser(int parentUserId, int userId, |
| 4361 | String opPackageName) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4362 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4363 | Account[] accounts = getAccountsAsUser(null, parentUserId, opPackageName); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4364 | for (Account account : accounts) { |
| 4365 | addSharedAccountAsUser(account, userId); |
| 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4370 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4371 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4372 | accounts.accountsDb.deleteSharedAccount(account); |
| 4373 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4374 | if (accountId < 0) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 4375 | Log.w(TAG, "insertAccountIntoDatabase: " + account.toSafeString() |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4376 | + ", skipping the DB insert failed"); |
| 4377 | return false; |
| 4378 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4379 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4380 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4381 | return true; |
| 4382 | } |
| 4383 | |
| 4384 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4385 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4386 | userId = handleIncomingUser(userId); |
| 4387 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4388 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4389 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4390 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4391 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4392 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4393 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4394 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4395 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4396 | } |
| 4397 | return r > 0; |
| 4398 | } |
| 4399 | |
| 4400 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4401 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4402 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4403 | } |
| 4404 | |
| 4405 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4406 | userId = handleIncomingUser(userId); |
| 4407 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4408 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4409 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4410 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4411 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4412 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4413 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4414 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4415 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | @Override |
| 4419 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4420 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4421 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4422 | synchronized (accounts.dbLock) { |
| 4423 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
| 4424 | Account[] accountArray = new Account[accountList.size()]; |
| 4425 | accountList.toArray(accountArray); |
| 4426 | return accountArray; |
| 4427 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4431 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4432 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4433 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4434 | } |
| 4435 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4436 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4437 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4438 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4439 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4440 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4441 | // Don't do opPackageName check - caller is system. |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4442 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4443 | + callingUid + " with uid=" + uid); |
| 4444 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4445 | return getAccountsAsUserForPackage(null, UserHandle.getCallingUserId(), packageName, uid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4446 | opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4447 | } |
| 4448 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4449 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4450 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4451 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4452 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4453 | int callingUid = Binder.getCallingUid(); |
| 4454 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4455 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4456 | int packageUid = -1; |
| 4457 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4458 | packageUid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4459 | } catch (NameNotFoundException re) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4460 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4461 | return EMPTY_ACCOUNT_ARRAY; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4462 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4463 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4464 | && (type != null && !isAccountManagedByCaller(type, callingUid, userId))) { |
| 4465 | return EMPTY_ACCOUNT_ARRAY; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4466 | } |
sunjian | d62dc39 | 2017-06-01 12:05:59 -0700 | [diff] [blame] | 4467 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) && type == null) { |
| 4468 | return getAccountsAsUserForPackage(type, userId, |
| 4469 | packageName, packageUid, opPackageName, false /* includeUserManagedNotVisible */); |
| 4470 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4471 | return getAccountsAsUserForPackage(type, userId, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4472 | packageName, packageUid, opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4473 | } |
| 4474 | |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4475 | private boolean needToStartChooseAccountActivity(Account[] accounts, String callingPackage) { |
| 4476 | if (accounts.length < 1) return false; |
| 4477 | if (accounts.length > 1) return true; |
| 4478 | Account account = accounts[0]; |
| 4479 | UserAccounts userAccounts = getUserAccounts(UserHandle.getCallingUserId()); |
| 4480 | int visibility = resolveAccountVisibility(account, callingPackage, userAccounts); |
| 4481 | if (visibility == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE) return true; |
| 4482 | return false; |
| 4483 | } |
| 4484 | |
| 4485 | private void startChooseAccountActivityWithAccounts( |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 4486 | IAccountManagerResponse response, Account[] accounts, String callingPackage) { |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4487 | Intent intent = new Intent(mContext, ChooseAccountActivity.class); |
| 4488 | intent.putExtra(AccountManager.KEY_ACCOUNTS, accounts); |
| 4489 | intent.putExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE, |
| 4490 | new AccountManagerResponse(response)); |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 4491 | intent.putExtra(AccountManager.KEY_ANDROID_PACKAGE_NAME, callingPackage); |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4492 | |
| 4493 | mContext.startActivityAsUser(intent, UserHandle.of(UserHandle.getCallingUserId())); |
| 4494 | } |
| 4495 | |
| 4496 | private void handleGetAccountsResult( |
| 4497 | IAccountManagerResponse response, |
| 4498 | Account[] accounts, |
| 4499 | String callingPackage) { |
| 4500 | |
| 4501 | if (needToStartChooseAccountActivity(accounts, callingPackage)) { |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 4502 | startChooseAccountActivityWithAccounts(response, accounts, callingPackage); |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4503 | return; |
| 4504 | } |
| 4505 | if (accounts.length == 1) { |
| 4506 | Bundle bundle = new Bundle(); |
| 4507 | bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accounts[0].name); |
| 4508 | bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accounts[0].type); |
| 4509 | onResult(response, bundle); |
| 4510 | return; |
| 4511 | } |
| 4512 | // No qualified account exists, return an empty Bundle. |
| 4513 | onResult(response, new Bundle()); |
| 4514 | } |
| 4515 | |
| 4516 | @Override |
| 4517 | public void getAccountByTypeAndFeatures( |
| 4518 | IAccountManagerResponse response, |
| 4519 | String accountType, |
| 4520 | String[] features, |
| 4521 | String opPackageName) { |
| 4522 | |
| 4523 | int callingUid = Binder.getCallingUid(); |
| 4524 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 4525 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4526 | Log.v(TAG, "getAccount: accountType " + accountType |
| 4527 | + ", response " + response |
| 4528 | + ", features " + Arrays.toString(features) |
| 4529 | + ", caller's uid " + callingUid |
| 4530 | + ", pid " + Binder.getCallingPid()); |
| 4531 | } |
| 4532 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4533 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 4534 | |
| 4535 | int userId = UserHandle.getCallingUserId(); |
| 4536 | |
| 4537 | long identityToken = clearCallingIdentity(); |
| 4538 | try { |
| 4539 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4540 | if (ArrayUtils.isEmpty(features)) { |
| 4541 | Account[] accountsWithManagedNotVisible = getAccountsFromCache( |
| 4542 | userAccounts, accountType, callingUid, opPackageName, |
| 4543 | true /* include managed not visible */); |
| 4544 | handleGetAccountsResult( |
| 4545 | response, accountsWithManagedNotVisible, opPackageName); |
| 4546 | return; |
| 4547 | } |
| 4548 | |
| 4549 | IAccountManagerResponse retrieveAccountsResponse = |
| 4550 | new IAccountManagerResponse.Stub() { |
| 4551 | @Override |
| 4552 | public void onResult(Bundle value) throws RemoteException { |
| 4553 | Parcelable[] parcelables = value.getParcelableArray( |
| 4554 | AccountManager.KEY_ACCOUNTS); |
| 4555 | Account[] accounts = new Account[parcelables.length]; |
| 4556 | for (int i = 0; i < parcelables.length; i++) { |
| 4557 | accounts[i] = (Account) parcelables[i]; |
| 4558 | } |
| 4559 | handleGetAccountsResult( |
| 4560 | response, accounts, opPackageName); |
| 4561 | } |
| 4562 | |
| 4563 | @Override |
| 4564 | public void onError(int errorCode, String errorMessage) |
| 4565 | throws RemoteException { |
| 4566 | // Will not be called in this case. |
| 4567 | } |
| 4568 | }; |
| 4569 | new GetAccountsByTypeAndFeatureSession( |
| 4570 | userAccounts, |
| 4571 | retrieveAccountsResponse, |
| 4572 | accountType, |
| 4573 | features, |
| 4574 | callingUid, |
| 4575 | opPackageName, |
| 4576 | true /* include managed not visible */).bind(); |
| 4577 | } finally { |
| 4578 | restoreCallingIdentity(identityToken); |
| 4579 | } |
| 4580 | } |
| 4581 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4582 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4583 | public void getAccountsByFeatures( |
| 4584 | IAccountManagerResponse response, |
| 4585 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4586 | String[] features, |
| 4587 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4588 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4589 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4590 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4591 | Log.v(TAG, "getAccounts: accountType " + type |
| 4592 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4593 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4594 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4595 | + ", pid " + Binder.getCallingPid()); |
| 4596 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4597 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4598 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4599 | int userId = UserHandle.getCallingUserId(); |
| 4600 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4601 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4602 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4603 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4604 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4605 | // Need to return just the accounts that are from matching signatures. |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4606 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, EMPTY_ACCOUNT_ARRAY); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4607 | try { |
| 4608 | response.onResult(result); |
| 4609 | } catch (RemoteException e) { |
| 4610 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4611 | } |
| 4612 | return; |
| 4613 | } |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4614 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4615 | long identityToken = clearCallingIdentity(); |
| 4616 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4617 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4618 | if (features == null || features.length == 0) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4619 | Account[] accounts = getAccountsFromCache(userAccounts, type, callingUid, |
| 4620 | opPackageName, false); |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4621 | Bundle result = new Bundle(); |
| 4622 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4623 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4624 | return; |
| 4625 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4626 | new GetAccountsByTypeAndFeatureSession( |
| 4627 | userAccounts, |
| 4628 | response, |
| 4629 | type, |
| 4630 | features, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4631 | callingUid, |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4632 | opPackageName, |
| 4633 | false /* include managed not visible */).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4634 | } finally { |
| 4635 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4636 | } |
| 4637 | } |
| 4638 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4639 | @Override |
| 4640 | public void onAccountAccessed(String token) throws RemoteException { |
| 4641 | final int uid = Binder.getCallingUid(); |
| 4642 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4643 | return; |
| 4644 | } |
| 4645 | final int userId = UserHandle.getCallingUserId(); |
| 4646 | final long identity = Binder.clearCallingIdentity(); |
| 4647 | try { |
| 4648 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4649 | if (Objects.equals(account.getAccessId(), token)) { |
| 4650 | // An app just accessed the account. At this point it knows about |
| 4651 | // 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] | 4652 | // Do we need to update account visibility here? |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4653 | if (!hasAccountAccess(account, null, uid)) { |
| 4654 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4655 | uid, true); |
| 4656 | } |
| 4657 | } |
| 4658 | } |
| 4659 | } finally { |
| 4660 | Binder.restoreCallingIdentity(identity); |
| 4661 | } |
| 4662 | } |
| 4663 | |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 4664 | @Override |
| 4665 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 4666 | FileDescriptor err, String[] args, ShellCallback callback, |
| 4667 | ResultReceiver resultReceiver) { |
| 4668 | new AccountManagerServiceShellCommand(this).exec(this, in, out, err, args, |
| 4669 | callback, resultReceiver); |
| 4670 | } |
| 4671 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4672 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4673 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4674 | IAccountManagerResponse mResponse; |
| 4675 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4676 | final boolean mExpectActivityLaunch; |
| 4677 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4678 | final String mAccountName; |
| 4679 | // Indicates if we need to add auth details(like last credential time) |
| 4680 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4681 | // If set, we need to update the last authenticated time. This is |
| 4682 | // currently |
| 4683 | // used on |
| 4684 | // successful confirming credentials. |
| 4685 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4686 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4687 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4688 | private int mNumRequestContinued = 0; |
| 4689 | private int mNumErrors = 0; |
| 4690 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4691 | IAccountAuthenticator mAuthenticator = null; |
| 4692 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4693 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4694 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4695 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4696 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4697 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4698 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4699 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4700 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4701 | } |
| 4702 | |
| 4703 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4704 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4705 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4706 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4707 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4708 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4709 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4710 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4711 | mResponse = response; |
| 4712 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4713 | mExpectActivityLaunch = expectActivityLaunch; |
| 4714 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4715 | mAccountName = accountName; |
| 4716 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4717 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4718 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4719 | synchronized (mSessions) { |
| 4720 | mSessions.put(toString(), this); |
| 4721 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4722 | if (response != null) { |
| 4723 | try { |
| 4724 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4725 | } catch (RemoteException e) { |
| 4726 | mResponse = null; |
| 4727 | binderDied(); |
| 4728 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4729 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4730 | } |
| 4731 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4732 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4733 | if (mResponse == null) { |
| 4734 | // this session has already been closed |
| 4735 | return null; |
| 4736 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4737 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4738 | 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] | 4739 | return response; |
| 4740 | } |
| 4741 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4742 | /** |
| 4743 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4744 | * security policy. |
| 4745 | * |
| 4746 | * 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] | 4747 | * 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] | 4748 | * supplied entries in the system Settings app. |
| 4749 | */ |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4750 | protected boolean checkKeyIntent(int authUid, Intent intent) { |
Jeff Sharkey | d722e78 | 2017-06-12 17:33:07 -0600 | [diff] [blame] | 4751 | intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4752 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 4753 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 4754 | | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION)); |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4755 | long bid = Binder.clearCallingIdentity(); |
| 4756 | try { |
| 4757 | PackageManager pm = mContext.getPackageManager(); |
| 4758 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4759 | if (resolveInfo == null) { |
| 4760 | return false; |
| 4761 | } |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4762 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4763 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 4764 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4765 | if (!isExportedSystemActivity(targetActivityInfo) |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 4766 | && !pmi.hasSignatureCapability( |
| 4767 | targetUid, authUid, |
| 4768 | PackageParser.SigningDetails.CertCapabilities.AUTH)) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4769 | String pkgName = targetActivityInfo.packageName; |
| 4770 | String activityName = targetActivityInfo.name; |
| 4771 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4772 | + "does not share a signature with the supplying authenticator (%s)."; |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4773 | Log.e(TAG, String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4774 | return false; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4775 | } |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4776 | return true; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4777 | } finally { |
| 4778 | Binder.restoreCallingIdentity(bid); |
| 4779 | } |
| 4780 | } |
| 4781 | |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4782 | private boolean isExportedSystemActivity(ActivityInfo activityInfo) { |
| 4783 | String className = activityInfo.name; |
| 4784 | return "android".equals(activityInfo.packageName) && |
| 4785 | (GrantCredentialsPermissionActivity.class.getName().equals(className) |
| 4786 | || CantAddAccountActivity.class.getName().equals(className)); |
| 4787 | } |
| 4788 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4789 | private void close() { |
| 4790 | synchronized (mSessions) { |
| 4791 | if (mSessions.remove(toString()) == null) { |
| 4792 | // the session was already closed, so bail out now |
| 4793 | return; |
| 4794 | } |
| 4795 | } |
| 4796 | if (mResponse != null) { |
| 4797 | // stop listening for response deaths |
| 4798 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4799 | |
| 4800 | // clear this so that we don't accidentally send any further results |
| 4801 | mResponse = null; |
| 4802 | } |
| 4803 | cancelTimeout(); |
| 4804 | unbind(); |
| 4805 | } |
| 4806 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4807 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4808 | public void binderDied() { |
| 4809 | mResponse = null; |
| 4810 | close(); |
| 4811 | } |
| 4812 | |
| 4813 | protected String toDebugString() { |
| 4814 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4815 | } |
| 4816 | |
| 4817 | protected String toDebugString(long now) { |
| 4818 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4819 | + ", connected " + (mAuthenticator != null) |
| 4820 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4821 | + "/" + mNumErrors + ")" |
| 4822 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4823 | } |
| 4824 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4825 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4826 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4827 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4828 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4829 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4830 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4831 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4832 | } |
| 4833 | } |
| 4834 | |
| 4835 | private void unbind() { |
| 4836 | if (mAuthenticator != null) { |
| 4837 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4838 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4839 | } |
| 4840 | } |
| 4841 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4842 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4843 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4844 | } |
| 4845 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4846 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4847 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4848 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4849 | try { |
| 4850 | run(); |
| 4851 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4852 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4853 | "remote exception"); |
| 4854 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4855 | } |
| 4856 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4857 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4858 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4859 | mAuthenticator = null; |
| 4860 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4861 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4862 | try { |
| 4863 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4864 | "disconnected"); |
| 4865 | } catch (RemoteException e) { |
| 4866 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4867 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4868 | + "caught RemoteException while responding", e); |
| 4869 | } |
| 4870 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4871 | } |
| 4872 | } |
| 4873 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4874 | public abstract void run() throws RemoteException; |
| 4875 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4876 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4877 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4878 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4879 | try { |
| 4880 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4881 | "timeout"); |
| 4882 | } catch (RemoteException e) { |
| 4883 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4884 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4885 | e); |
| 4886 | } |
| 4887 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4891 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4892 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4893 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4894 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4895 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4896 | if (result != null) { |
| 4897 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4898 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4899 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4900 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4901 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4902 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4903 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4904 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4905 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4906 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4907 | if (needUpdate || mAuthDetailsRequired) { |
| 4908 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4909 | if (needUpdate && accountPresent) { |
| 4910 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4911 | } |
| 4912 | if (mAuthDetailsRequired) { |
| 4913 | long lastAuthenticatedTime = -1; |
| 4914 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4915 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4916 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4917 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4918 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4919 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4920 | lastAuthenticatedTime); |
| 4921 | } |
| 4922 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4923 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4924 | if (result != null |
| 4925 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4926 | if (!checkKeyIntent( |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4927 | Binder.getCallingUid(), |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4928 | intent)) { |
| 4929 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 4930 | "invalid intent in bundle returned"); |
| 4931 | return; |
| 4932 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4933 | } |
| 4934 | if (result != null |
| 4935 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4936 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4937 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4938 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4939 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4940 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4941 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4942 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4943 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4944 | IAccountManagerResponse response; |
| 4945 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4946 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4947 | response = mResponse; |
| 4948 | } else { |
| 4949 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4950 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4951 | if (response != null) { |
| 4952 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4953 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4954 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4955 | Log.v(TAG, getClass().getSimpleName() |
| 4956 | + " calling onError() on response " + response); |
| 4957 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4958 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4959 | "null bundle returned"); |
| 4960 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4961 | if (mStripAuthTokenFromResult) { |
| 4962 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4963 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4964 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4965 | Log.v(TAG, getClass().getSimpleName() |
| 4966 | + " calling onResult() on response " + response); |
| 4967 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4968 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4969 | (intent == null)) { |
| 4970 | // All AccountManager error codes are greater than 0 |
| 4971 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4972 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4973 | } else { |
| 4974 | response.onResult(result); |
| 4975 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4976 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4977 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4978 | // if the caller is dead then there is no one to care about remote exceptions |
| 4979 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4980 | Log.v(TAG, "failure while notifying response", e); |
| 4981 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4982 | } |
| 4983 | } |
| 4984 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4985 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4986 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4987 | public void onRequestContinued() { |
| 4988 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4989 | } |
| 4990 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4991 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4992 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4993 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4994 | IAccountManagerResponse response = getResponseAndClose(); |
| 4995 | if (response != null) { |
| 4996 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4997 | Log.v(TAG, getClass().getSimpleName() |
| 4998 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4999 | } |
| 5000 | try { |
| 5001 | response.onError(errorCode, errorMessage); |
| 5002 | } catch (RemoteException e) { |
| 5003 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5004 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 5005 | } |
| 5006 | } |
| 5007 | } else { |
| 5008 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5009 | Log.v(TAG, "Session.onError: already closed"); |
| 5010 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5011 | } |
| 5012 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5013 | |
| 5014 | /** |
| 5015 | * find the component name for the authenticator and initiate a bind |
| 5016 | * if no authenticator or the bind fails then return false, otherwise return true |
| 5017 | */ |
| 5018 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5019 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 5020 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 5021 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5022 | if (authenticatorInfo == null) { |
| 5023 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5024 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 5025 | + ", bailing out"); |
| 5026 | } |
| 5027 | return false; |
| 5028 | } |
| 5029 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 5030 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 5031 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 5032 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 5033 | + " which isn't encryption aware"); |
| 5034 | return false; |
| 5035 | } |
| 5036 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5037 | Intent intent = new Intent(); |
| 5038 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 5039 | intent.setComponent(authenticatorInfo.componentName); |
| 5040 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5041 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 5042 | } |
Hongming Jin | 8e2bfc1 | 2018-05-30 11:01:06 -0700 | [diff] [blame] | 5043 | int flags = Context.BIND_AUTO_CREATE; |
| 5044 | if (mAuthenticatorCache.getBindInstantServiceAllowed(mAccounts.userId)) { |
| 5045 | flags |= Context.BIND_ALLOW_INSTANT; |
| 5046 | } |
| 5047 | if (!mContext.bindServiceAsUser(intent, this, flags, UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5048 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5049 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 5050 | } |
| 5051 | return false; |
| 5052 | } |
| 5053 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5054 | return true; |
| 5055 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5056 | } |
| 5057 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5058 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5059 | MessageHandler(Looper looper) { |
| 5060 | super(looper); |
| 5061 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 5062 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 5063 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5064 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5065 | switch (msg.what) { |
| 5066 | case MESSAGE_TIMED_OUT: |
| 5067 | Session session = (Session)msg.obj; |
| 5068 | session.onTimedOut(); |
| 5069 | break; |
| 5070 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 5071 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 5072 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 5073 | break; |
| 5074 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5075 | default: |
| 5076 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 5077 | } |
| 5078 | } |
| 5079 | } |
| 5080 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5081 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5082 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5083 | } |
| 5084 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 5085 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5086 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 5087 | } |
| 5088 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5089 | /* |
| 5090 | * This function receives an opened writable database. |
| 5091 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5092 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5093 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5094 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5098 | * 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] | 5099 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5100 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5101 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5102 | |
| 5103 | class LogRecordTask implements Runnable { |
| 5104 | private final String action; |
| 5105 | private final String tableName; |
| 5106 | private final long accountId; |
| 5107 | private final UserAccounts userAccount; |
| 5108 | private final int callingUid; |
| 5109 | private final long userDebugDbInsertionPoint; |
| 5110 | |
| 5111 | LogRecordTask(final String action, |
| 5112 | final String tableName, |
| 5113 | final long accountId, |
| 5114 | final UserAccounts userAccount, |
| 5115 | final int callingUid, |
| 5116 | final long userDebugDbInsertionPoint) { |
| 5117 | this.action = action; |
| 5118 | this.tableName = tableName; |
| 5119 | this.accountId = accountId; |
| 5120 | this.userAccount = userAccount; |
| 5121 | this.callingUid = callingUid; |
| 5122 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 5123 | } |
| 5124 | |
Andrew Scull | c7770d6 | 2017-05-22 17:49:58 +0100 | [diff] [blame] | 5125 | @Override |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5126 | public void run() { |
| 5127 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 5128 | logStatement.bindLong(1, accountId); |
| 5129 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5130 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5131 | logStatement.bindLong(4, callingUid); |
| 5132 | logStatement.bindString(5, tableName); |
| 5133 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
Tetsutoki Shiozawa | be2d96a | 2017-10-24 18:44:00 +0900 | [diff] [blame] | 5134 | try { |
| 5135 | logStatement.execute(); |
| 5136 | } catch (IllegalStateException e) { |
| 5137 | // Guard against crash, DB can already be closed |
| 5138 | // since this statement is executed on a handler thread |
| 5139 | Slog.w(TAG, "Failed to insert a log record. accountId=" + accountId |
| 5140 | + " action=" + action + " tableName=" + tableName + " Error: " + e); |
| 5141 | } finally { |
| 5142 | logStatement.clearBindings(); |
| 5143 | } |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5144 | } |
| 5145 | } |
| 5146 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 5147 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 5148 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5149 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5150 | % AccountsDb.MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 5151 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5152 | } |
| 5153 | |
| 5154 | /* |
| 5155 | * This should only be called once to compile the sql statement for logging |
| 5156 | * and to find the insertion point. |
| 5157 | */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5158 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(UserAccounts userAccount) { |
| 5159 | userAccount.debugDbInsertionPoint = userAccount.accountsDb |
| 5160 | .calculateDebugTableInsertionPoint(); |
| 5161 | userAccount.statementForLogging = userAccount.accountsDb.compileSqlStatementForLogging(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5162 | } |
| 5163 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5164 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5165 | return asBinder(); |
| 5166 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5167 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5168 | /** |
| 5169 | * Searches array of arguments for the specified string |
| 5170 | * @param args array of argument strings |
| 5171 | * @param value value to search for |
| 5172 | * @return true if the value is contained in the array |
| 5173 | */ |
| 5174 | private static boolean scanArgs(String[] args, String value) { |
| 5175 | if (args != null) { |
| 5176 | for (String arg : args) { |
| 5177 | if (value.equals(arg)) { |
| 5178 | return true; |
| 5179 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5180 | } |
| 5181 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5182 | return false; |
| 5183 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5184 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5185 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5186 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 5187 | if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5188 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5189 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 5190 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5191 | final List<UserInfo> users = getUserManager().getUsers(); |
| 5192 | for (UserInfo user : users) { |
| 5193 | ipw.println("User " + user + ":"); |
| 5194 | ipw.increaseIndent(); |
| 5195 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 5196 | ipw.println(); |
| 5197 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5198 | } |
| 5199 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5200 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5201 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 5202 | String[] args, boolean isCheckinRequest) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5203 | if (isCheckinRequest) { |
| 5204 | // This is a checkin request. *Only* upload the account types and the count of |
| 5205 | // each. |
| 5206 | synchronized (userAccounts.dbLock) { |
| 5207 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
| 5208 | } |
| 5209 | } else { |
| 5210 | Account[] accounts = getAccountsFromCache(userAccounts, null /* type */, |
| 5211 | Process.SYSTEM_UID, null /* packageName */, false); |
| 5212 | fout.println("Accounts: " + accounts.length); |
| 5213 | for (Account account : accounts) { |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 5214 | fout.println(" " + account.toSafeString()); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5215 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5216 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5217 | // Add debug information. |
| 5218 | fout.println(); |
| 5219 | synchronized (userAccounts.dbLock) { |
| 5220 | userAccounts.accountsDb.dumpDebugTable(fout); |
| 5221 | } |
| 5222 | fout.println(); |
| 5223 | synchronized (mSessions) { |
| 5224 | final long now = SystemClock.elapsedRealtime(); |
| 5225 | fout.println("Active Sessions: " + mSessions.size()); |
| 5226 | for (Session session : mSessions.values()) { |
| 5227 | fout.println(" " + session.toDebugString(now)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5228 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5229 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5230 | |
| 5231 | fout.println(); |
| 5232 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Tony Mak | 6d14d77 | 2017-07-13 17:49:46 +0800 | [diff] [blame] | 5233 | |
| 5234 | boolean isUserUnlocked; |
| 5235 | synchronized (mUsers) { |
| 5236 | isUserUnlocked = isLocalUnlockedUser(userAccounts.userId); |
| 5237 | } |
| 5238 | // Following logs are printed only when user is unlocked. |
| 5239 | if (!isUserUnlocked) { |
| 5240 | return; |
| 5241 | } |
| 5242 | fout.println(); |
| 5243 | synchronized (userAccounts.dbLock) { |
| 5244 | Map<Account, Map<String, Integer>> allVisibilityValues = |
| 5245 | userAccounts.accountsDb.findAllVisibilityValues(); |
| 5246 | fout.println("Account visibility:"); |
| 5247 | for (Account account : allVisibilityValues.keySet()) { |
| 5248 | fout.println(" " + account.name); |
| 5249 | Map<String, Integer> visibilities = allVisibilityValues.get(account); |
| 5250 | for (Entry<String, Integer> entry : visibilities.entrySet()) { |
| 5251 | fout.println(" " + entry.getKey() + ", " + entry.getValue()); |
| 5252 | } |
| 5253 | } |
| 5254 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5255 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5256 | } |
| 5257 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5258 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5259 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5260 | long identityToken = clearCallingIdentity(); |
| 5261 | try { |
| 5262 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5263 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 5264 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5265 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5266 | if (intent.getComponent() != null && |
| 5267 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 5268 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5269 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5270 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5271 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5272 | final NotificationId id = getSigninRequiredNotificationId(accounts, account); |
| 5273 | intent.addCategory(id.mTag); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5274 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 5275 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5276 | contextForUser.getText(R.string.notification_title).toString(); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 5277 | Notification n = |
| 5278 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 5279 | .setWhen(0) |
| 5280 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 5281 | .setColor(contextForUser.getColor( |
| 5282 | com.android.internal.R.color.system_notification_accent_color)) |
| 5283 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 5284 | .setContentText(message) |
| 5285 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 5286 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5287 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 5288 | .build(); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5289 | installNotification(id, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5290 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5291 | } finally { |
| 5292 | restoreCallingIdentity(identityToken); |
| 5293 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5294 | } |
| 5295 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5296 | private void installNotification(NotificationId id, final Notification notification, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5297 | String packageName, int userId) { |
| 5298 | final long token = clearCallingIdentity(); |
| 5299 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5300 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5301 | try { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 5302 | // The calling uid must match either the package or op package, so use an op |
| 5303 | // package that matches the cleared calling identity. |
| 5304 | notificationManager.enqueueNotificationWithTag(packageName, "android", |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 5305 | id.mTag, id.mId, notification, userId); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5306 | } catch (RemoteException e) { |
| 5307 | /* ignore - local call */ |
| 5308 | } |
| 5309 | } finally { |
| 5310 | Binder.restoreCallingIdentity(token); |
| 5311 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5312 | } |
| 5313 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5314 | private void cancelNotification(NotificationId id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5315 | cancelNotification(id, mContext.getPackageName(), user); |
| 5316 | } |
| 5317 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5318 | private void cancelNotification(NotificationId id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5319 | long identityToken = clearCallingIdentity(); |
| 5320 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5321 | INotificationManager service = mInjector.getNotificationManager(); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5322 | service.cancelNotificationWithTag(packageName, id.mTag, id.mId, user.getIdentifier()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5323 | } catch (RemoteException e) { |
| 5324 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5325 | } finally { |
| 5326 | restoreCallingIdentity(identityToken); |
| 5327 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5328 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5329 | |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5330 | private boolean isPermittedForPackage(String packageName, int uid, int userId, |
| 5331 | String... permissions) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5332 | final long identity = Binder.clearCallingIdentity(); |
| 5333 | try { |
| 5334 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 5335 | for (String perm : permissions) { |
| 5336 | if (pm.checkPermission(perm, packageName, userId) |
| 5337 | == PackageManager.PERMISSION_GRANTED) { |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5338 | // Checks runtime permission revocation. |
| 5339 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
Tony Mak | 58f2815 | 2017-09-20 21:23:48 +0100 | [diff] [blame] | 5340 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOpNoThrow( |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5341 | opCode, uid, packageName) == AppOpsManager.MODE_ALLOWED) { |
| 5342 | return true; |
| 5343 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5344 | } |
| 5345 | } |
| 5346 | } catch (RemoteException e) { |
| 5347 | /* ignore - local call */ |
| 5348 | } finally { |
| 5349 | Binder.restoreCallingIdentity(identity); |
| 5350 | } |
| 5351 | return false; |
| 5352 | } |
| 5353 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5354 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 5355 | for (String perm : permissions) { |
| 5356 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 5357 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5358 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 5359 | } |
| 5360 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
Tony Mak | 58f2815 | 2017-09-20 21:23:48 +0100 | [diff] [blame] | 5361 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOpNoThrow( |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5362 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 5363 | return true; |
| 5364 | } |
| 5365 | } |
| 5366 | } |
| 5367 | return false; |
| 5368 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5369 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5370 | private int handleIncomingUser(int userId) { |
| 5371 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5372 | return ActivityManager.getService().handleIncomingUser( |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5373 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 5374 | } catch (RemoteException re) { |
| 5375 | // Shouldn't happen, local. |
| 5376 | } |
| 5377 | return userId; |
| 5378 | } |
| 5379 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 5380 | private boolean isPrivileged(int callingUid) { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5381 | String[] packages; |
| 5382 | long identityToken = Binder.clearCallingIdentity(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5383 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5384 | packages = mPackageManager.getPackagesForUid(callingUid); |
sunjian | 9ae597b6 | 2017-08-14 15:45:04 -0700 | [diff] [blame] | 5385 | if (packages == null) { |
| 5386 | Log.d(TAG, "No packages for callingUid " + callingUid); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5387 | return false; |
| 5388 | } |
sunjian | 9ae597b6 | 2017-08-14 15:45:04 -0700 | [diff] [blame] | 5389 | for (String name : packages) { |
| 5390 | try { |
| 5391 | PackageInfo packageInfo = |
| 5392 | mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5393 | if (packageInfo != null |
| 5394 | && (packageInfo.applicationInfo.privateFlags |
| 5395 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 5396 | return true; |
| 5397 | } |
| 5398 | } catch (PackageManager.NameNotFoundException e) { |
| 5399 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 5400 | } |
| 5401 | } |
| 5402 | } finally { |
| 5403 | Binder.restoreCallingIdentity(identityToken); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5404 | } |
| 5405 | return false; |
| 5406 | } |
| 5407 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5408 | private boolean permissionIsGranted( |
| 5409 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5410 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 5411 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5412 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 5413 | } |
| 5414 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5415 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5416 | |
| 5417 | if (isPrivileged(callerUid)) { |
| 5418 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5419 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5420 | + callerUid + " privileged"); |
| 5421 | } |
| 5422 | return true; |
| 5423 | } |
| 5424 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 5425 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5426 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5427 | + callerUid + " manages the account"); |
| 5428 | } |
| 5429 | return true; |
| 5430 | } |
| 5431 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5432 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5433 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5434 | + callerUid + " user granted access"); |
| 5435 | } |
| 5436 | return true; |
| 5437 | } |
| 5438 | |
| 5439 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5440 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5441 | } |
| 5442 | |
| 5443 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5444 | } |
| 5445 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5446 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5447 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5448 | if (accountType == null) { |
| 5449 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5450 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5451 | return getTypesVisibleToCaller(callingUid, userId, |
| 5452 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5453 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5456 | // Method checks visibility for applications targeing API level below {@link |
| 5457 | // android.os.Build.VERSION_CODES#O}, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5458 | // returns true if the the app has GET_ACCOUNTS or GET_ACCOUNTS_PRIVILEGED permission. |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5459 | private boolean checkGetAccountsPermission(String packageName, int uid, int userId) { |
| 5460 | return isPermittedForPackage(packageName, uid, userId, Manifest.permission.GET_ACCOUNTS, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5461 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 5462 | } |
| 5463 | |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5464 | private boolean checkReadContactsPermission(String packageName, int uid, int userId) { |
| 5465 | return isPermittedForPackage(packageName, uid, userId, Manifest.permission.READ_CONTACTS); |
| 5466 | } |
| 5467 | |
| 5468 | // Heuristic to check that account type may be associated with some contacts data and |
| 5469 | // therefore READ_CONTACTS permission grants the access to account by default. |
| 5470 | private boolean accountTypeManagesContacts(String accountType, int userId) { |
| 5471 | if (accountType == null) { |
| 5472 | return false; |
| 5473 | } |
| 5474 | long identityToken = Binder.clearCallingIdentity(); |
| 5475 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5476 | try { |
| 5477 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5478 | } finally { |
| 5479 | Binder.restoreCallingIdentity(identityToken); |
| 5480 | } |
| 5481 | // Check contacts related permissions for authenticator. |
| 5482 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5483 | : serviceInfos) { |
| 5484 | if (accountType.equals(serviceInfo.type.type)) { |
| 5485 | return isPermittedForPackage(serviceInfo.type.packageName, serviceInfo.uid, userId, |
| 5486 | Manifest.permission.WRITE_CONTACTS); |
| 5487 | } |
| 5488 | } |
| 5489 | return false; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | /** |
| 5493 | * Method checks package uid and signature with Authenticator which manages accountType. |
| 5494 | * |
| 5495 | * @return SIGNATURE_CHECK_UID_MATCH for uid match, SIGNATURE_CHECK_MATCH for signature match, |
| 5496 | * SIGNATURE_CHECK_MISMATCH otherwise. |
| 5497 | */ |
| 5498 | private int checkPackageSignature(String accountType, int callingUid, int userId) { |
| 5499 | if (accountType == null) { |
| 5500 | return SIGNATURE_CHECK_MISMATCH; |
| 5501 | } |
| 5502 | |
| 5503 | long identityToken = Binder.clearCallingIdentity(); |
| 5504 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5505 | try { |
| 5506 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5507 | } finally { |
| 5508 | Binder.restoreCallingIdentity(identityToken); |
| 5509 | } |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 5510 | // Check for signature match with Authenticator.LocalServices.getService(PackageManagerInternal.class); |
| 5511 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5512 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5513 | : serviceInfos) { |
| 5514 | if (accountType.equals(serviceInfo.type.type)) { |
| 5515 | if (serviceInfo.uid == callingUid) { |
| 5516 | return SIGNATURE_CHECK_UID_MATCH; |
| 5517 | } |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 5518 | if (pmi.hasSignatureCapability( |
| 5519 | serviceInfo.uid, callingUid, |
| 5520 | PackageParser.SigningDetails.CertCapabilities.AUTH)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5521 | return SIGNATURE_CHECK_MATCH; |
| 5522 | } |
| 5523 | } |
| 5524 | } |
| 5525 | return SIGNATURE_CHECK_MISMATCH; |
| 5526 | } |
| 5527 | |
| 5528 | // returns true for applications with the same signature as authenticator. |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5529 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5530 | if (accountType == null) { |
| 5531 | return false; |
| 5532 | } else { |
| 5533 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5534 | } |
| 5535 | } |
| 5536 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5537 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5538 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5539 | return getTypesForCaller(callingUid, userId, true /* isOtherwisePermitted*/); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
Dmitry Dementyev | 2e22cfb | 2017-01-09 18:42:14 +0000 | [diff] [blame] | 5543 | return getTypesForCaller(callingUid, userId, false); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
| 5546 | private List<String> getTypesForCaller( |
| 5547 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5548 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5549 | long identityToken = Binder.clearCallingIdentity(); |
| 5550 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5551 | try { |
| 5552 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5553 | } finally { |
| 5554 | Binder.restoreCallingIdentity(identityToken); |
| 5555 | } |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 5556 | |
| 5557 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5558 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5559 | serviceInfos) { |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame] | 5560 | if (isOtherwisePermitted || pmi.hasSignatureCapability( |
| 5561 | serviceInfo.uid, callingUid, |
| 5562 | PackageParser.SigningDetails.CertCapabilities.AUTH)) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5563 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5564 | } |
| 5565 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5566 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5567 | } |
| 5568 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5569 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5570 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5571 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5572 | if (account.name.equals(accountName)) { |
| 5573 | return true; |
| 5574 | } |
| 5575 | } |
| 5576 | } |
| 5577 | return false; |
| 5578 | } |
| 5579 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5580 | private static void checkManageUsersPermission(String message) { |
| 5581 | if (ActivityManager.checkComponentPermission( |
| 5582 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5583 | != PackageManager.PERMISSION_GRANTED) { |
| 5584 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5585 | } |
| 5586 | } |
| 5587 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5588 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5589 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5590 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5591 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5592 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5593 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5594 | + message); |
| 5595 | } |
| 5596 | } |
| 5597 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5598 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5599 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5600 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5601 | return true; |
| 5602 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5603 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5604 | synchronized (accounts.dbLock) { |
| 5605 | synchronized (accounts.cacheLock) { |
| 5606 | long grantsCount; |
| 5607 | if (authTokenType != null) { |
| 5608 | grantsCount = accounts.accountsDb |
| 5609 | .findMatchingGrantsCount(callerUid, authTokenType, account); |
| 5610 | } else { |
| 5611 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
| 5612 | account); |
| 5613 | } |
| 5614 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5615 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5616 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5617 | // TODO: Skip this check when running automated tests. Replace this |
| 5618 | // with a more general solution. |
Hui Yu | 139c248 | 2018-08-10 15:37:51 -0700 | [diff] [blame] | 5619 | Log.d(TAG, "no credentials permission for usage of " |
| 5620 | + account.toSafeString() + ", " |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5621 | + authTokenType + " by uid " + callerUid |
| 5622 | + " but ignoring since device is in test harness."); |
| 5623 | return true; |
| 5624 | } |
| 5625 | return permissionGranted; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5626 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5627 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5628 | } |
| 5629 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5630 | private boolean isSystemUid(int callingUid) { |
| 5631 | String[] packages = null; |
| 5632 | long ident = Binder.clearCallingIdentity(); |
| 5633 | try { |
| 5634 | packages = mPackageManager.getPackagesForUid(callingUid); |
Alex Chau | 81a47cd | 2018-01-02 16:49:14 +0000 | [diff] [blame] | 5635 | if (packages != null) { |
| 5636 | for (String name : packages) { |
| 5637 | try { |
| 5638 | PackageInfo packageInfo = |
| 5639 | mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5640 | if (packageInfo != null |
| 5641 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5642 | != 0) { |
| 5643 | return true; |
| 5644 | } |
| 5645 | } catch (NameNotFoundException e) { |
| 5646 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5647 | } |
| 5648 | } |
| 5649 | } else { |
| 5650 | Log.w(TAG, "No known packages with uid " + callingUid); |
| 5651 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5652 | } finally { |
| 5653 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5654 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5655 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5656 | } |
| 5657 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5658 | /** Succeeds if any of the specified permissions are granted. */ |
| 5659 | private void checkReadAccountsPermitted( |
| 5660 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5661 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5662 | int userId, |
| 5663 | String opPackageName) { |
| 5664 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5665 | String msg = String.format( |
| 5666 | "caller uid %s cannot access %s accounts", |
| 5667 | callingUid, |
| 5668 | accountType); |
| 5669 | Log.w(TAG, " " + msg); |
| 5670 | throw new SecurityException(msg); |
| 5671 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5672 | } |
| 5673 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5674 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5675 | // the managing app can always modify accounts |
| 5676 | if (isProfileOwner(callingUid)) { |
| 5677 | return true; |
| 5678 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5679 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5680 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5681 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5682 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5683 | return true; |
| 5684 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5685 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5686 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5687 | // the managing app can always modify accounts |
| 5688 | if (isProfileOwner(callingUid)) { |
| 5689 | return true; |
| 5690 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5691 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5692 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5693 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5694 | if (typesArray == null) { |
| 5695 | return true; |
| 5696 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5697 | for (String forbiddenType : typesArray) { |
| 5698 | if (forbiddenType.equals(accountType)) { |
| 5699 | return false; |
| 5700 | } |
| 5701 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5702 | return true; |
| 5703 | } |
| 5704 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5705 | private boolean isProfileOwner(int uid) { |
| 5706 | final DevicePolicyManagerInternal dpmi = |
| 5707 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5708 | return (dpmi != null) |
| 5709 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5710 | } |
| 5711 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5712 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5713 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5714 | throws RemoteException { |
| 5715 | final int callingUid = getCallingUid(); |
| 5716 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5717 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5718 | throw new SecurityException(); |
| 5719 | } |
| 5720 | |
| 5721 | if (value) { |
| 5722 | grantAppPermission(account, authTokenType, uid); |
| 5723 | } else { |
| 5724 | revokeAppPermission(account, authTokenType, uid); |
| 5725 | } |
| 5726 | } |
| 5727 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5728 | /** |
| 5729 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5730 | * <p> |
| 5731 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5732 | * which is in the system. This means we don't need to protect it with permissions. |
| 5733 | * @hide |
| 5734 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5735 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5736 | if (account == null || authTokenType == null) { |
| 5737 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5738 | return; |
| 5739 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5740 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5741 | synchronized (accounts.dbLock) { |
| 5742 | synchronized (accounts.cacheLock) { |
| 5743 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5744 | if (accountId >= 0) { |
| 5745 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
| 5746 | } |
| 5747 | cancelNotification( |
| 5748 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5749 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5750 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5751 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
| 5752 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5753 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5754 | |
| 5755 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5756 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5757 | : mAppPermissionChangeListeners) { |
| 5758 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5759 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | /** |
| 5763 | * Don't allow callers with the given uid permission to get credentials for |
| 5764 | * account/authTokenType. |
| 5765 | * <p> |
| 5766 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5767 | * which is in the system. This means we don't need to protect it with permissions. |
| 5768 | * @hide |
| 5769 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5770 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5771 | if (account == null || authTokenType == null) { |
| 5772 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5773 | return; |
| 5774 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5775 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5776 | synchronized (accounts.dbLock) { |
| 5777 | synchronized (accounts.cacheLock) { |
| 5778 | accounts.accountsDb.beginTransaction(); |
| 5779 | try { |
| 5780 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5781 | if (accountId >= 0) { |
| 5782 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5783 | accountId, authTokenType, uid); |
| 5784 | accounts.accountsDb.setTransactionSuccessful(); |
| 5785 | } |
| 5786 | } finally { |
| 5787 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5788 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5789 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5790 | cancelNotification( |
| 5791 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5792 | UserHandle.of(accounts.userId)); |
| 5793 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5794 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5795 | |
| 5796 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5797 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5798 | : mAppPermissionChangeListeners) { |
| 5799 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5800 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5801 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5802 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5803 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5804 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5805 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5806 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5807 | for (Account curAccount : oldAccountsForType) { |
| 5808 | if (!curAccount.equals(account)) { |
| 5809 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5810 | } |
| 5811 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5812 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5813 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5814 | } else { |
| 5815 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5816 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5817 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5818 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5819 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5820 | accounts.userDataCache.remove(account); |
| 5821 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5822 | accounts.previousNameCache.remove(account); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 5823 | accounts.visibilityCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | /** |
| 5827 | * 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] | 5828 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5829 | * processes and if you will return this account to apps you should return the result. |
| 5830 | * @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] | 5831 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5832 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5833 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5834 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5835 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5836 | if (accountsForType != null) { |
| 5837 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5838 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5839 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5840 | : UUID.randomUUID().toString(); |
| 5841 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5842 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5843 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5844 | } |
| 5845 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5846 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5847 | private Account[] filterAccounts(UserAccounts accounts, Account[] unfiltered, int callingUid, |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5848 | @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5849 | String visibilityFilterPackage = callingPackage; |
| 5850 | if (visibilityFilterPackage == null) { |
| 5851 | visibilityFilterPackage = getPackageNameForUid(callingUid); |
| 5852 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5853 | Map<Account, Integer> firstPass = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5854 | for (Account account : unfiltered) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5855 | int visibility = resolveAccountVisibility(account, visibilityFilterPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5856 | if ((visibility == AccountManager.VISIBILITY_VISIBLE |
| 5857 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) |
| 5858 | || (includeManagedNotVisible |
| 5859 | && (visibility |
| 5860 | == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE))) { |
| 5861 | firstPass.put(account, visibility); |
| 5862 | } |
| 5863 | } |
| 5864 | Map<Account, Integer> secondPass = |
| 5865 | filterSharedAccounts(accounts, firstPass, callingUid, callingPackage); |
| 5866 | |
| 5867 | Account[] filtered = new Account[secondPass.size()]; |
| 5868 | filtered = secondPass.keySet().toArray(filtered); |
| 5869 | return filtered; |
| 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 Map<Account, Integer> filterSharedAccounts(UserAccounts userAccounts, |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5874 | @NonNull Map<Account, Integer> unfiltered, int callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5875 | @Nullable String callingPackage) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5876 | // first part is to filter shared accounts. |
| 5877 | // unfiltered type check is not necessary. |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5878 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5879 | || callingUid == Process.SYSTEM_UID) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5880 | return unfiltered; |
| 5881 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5882 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5883 | if (user != null && user.isRestricted()) { |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5884 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5885 | if (packages == null) { |
| 5886 | packages = new String[] {}; |
| 5887 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5888 | // 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] | 5889 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5890 | // This might be a temporary way to specify a visible list |
| 5891 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5892 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5893 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5894 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5895 | return unfiltered; |
| 5896 | } |
| 5897 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5898 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5899 | if (ArrayUtils.isEmpty(sharedAccounts)) { |
| 5900 | return unfiltered; |
| 5901 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5902 | String requiredAccountType = ""; |
| 5903 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5904 | // If there's an explicit callingPackage specified, check if that package |
| 5905 | // opted in to see restricted accounts. |
| 5906 | if (callingPackage != null) { |
| 5907 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5908 | if (pi != null && pi.restrictedAccountType != null) { |
| 5909 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5910 | } |
| 5911 | } else { |
| 5912 | // Otherwise check if the callingUid has a package that has opted in |
| 5913 | for (String packageName : packages) { |
| 5914 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5915 | if (pi != null && pi.restrictedAccountType != null) { |
| 5916 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5917 | break; |
| 5918 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5919 | } |
| 5920 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5921 | } catch (NameNotFoundException e) { |
| 5922 | Log.d(TAG, "Package not found " + e.getMessage()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5923 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5924 | Map<Account, Integer> filtered = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5925 | for (Map.Entry<Account, Integer> entry : unfiltered.entrySet()) { |
| 5926 | Account account = entry.getKey(); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5927 | if (account.type.equals(requiredAccountType)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5928 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5929 | } else { |
| 5930 | boolean found = false; |
| 5931 | for (Account shared : sharedAccounts) { |
| 5932 | if (shared.equals(account)) { |
| 5933 | found = true; |
| 5934 | break; |
| 5935 | } |
| 5936 | } |
| 5937 | if (!found) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5938 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5939 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5940 | } |
| 5941 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5942 | return filtered; |
| 5943 | } else { |
| 5944 | return unfiltered; |
| 5945 | } |
| 5946 | } |
| 5947 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5948 | /* |
| 5949 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5950 | * that the package is not allowed to access. |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5951 | * |
| 5952 | * <p>The method shouldn't be called with UserAccounts#cacheLock held, otherwise it will cause a |
| 5953 | * deadlock |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5954 | */ |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5955 | @NonNull |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5956 | protected Account[] getAccountsFromCache(UserAccounts userAccounts, String accountType, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5957 | int callingUid, @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5958 | Preconditions.checkState(!Thread.holdsLock(userAccounts.cacheLock), |
| 5959 | "Method should not be called with cacheLock"); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5960 | if (accountType != null) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5961 | Account[] accounts; |
| 5962 | synchronized (userAccounts.cacheLock) { |
| 5963 | accounts = userAccounts.accountCache.get(accountType); |
| 5964 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5965 | if (accounts == null) { |
| 5966 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5967 | } else { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5968 | return filterAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
| 5969 | callingUid, callingPackage, includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5970 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5971 | } else { |
| 5972 | int totalLength = 0; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5973 | Account[] accountsArray; |
| 5974 | synchronized (userAccounts.cacheLock) { |
| 5975 | for (Account[] accounts : userAccounts.accountCache.values()) { |
| 5976 | totalLength += accounts.length; |
| 5977 | } |
| 5978 | if (totalLength == 0) { |
| 5979 | return EMPTY_ACCOUNT_ARRAY; |
| 5980 | } |
| 5981 | accountsArray = new Account[totalLength]; |
| 5982 | totalLength = 0; |
| 5983 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
| 5984 | System.arraycopy(accountsOfType, 0, accountsArray, totalLength, |
| 5985 | accountsOfType.length); |
| 5986 | totalLength += accountsOfType.length; |
| 5987 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5988 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5989 | return filterAccounts(userAccounts, accountsArray, callingUid, callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5990 | includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5991 | } |
| 5992 | } |
| 5993 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 5994 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5995 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5996 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5997 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5998 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5999 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6000 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 6001 | } |
| 6002 | if (value == null) { |
| 6003 | userDataForAccount.remove(key); |
| 6004 | } else { |
| 6005 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6006 | } |
| 6007 | } |
| 6008 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 6009 | protected String readCachedTokenInternal( |
| 6010 | UserAccounts accounts, |
| 6011 | Account account, |
| 6012 | String tokenType, |
| 6013 | String callingPackage, |
| 6014 | byte[] pkgSigDigest) { |
Dmitry Dementyev | 18f0ca9 | 2017-06-12 17:56:47 -0700 | [diff] [blame] | 6015 | synchronized (accounts.cacheLock) { |
| 6016 | return accounts.accountTokenCaches.get( |
| 6017 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 6018 | } |
| 6019 | } |
| 6020 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 6021 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 6022 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6023 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 6024 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 6025 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 6026 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6027 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 6028 | } |
| 6029 | if (value == null) { |
| 6030 | authTokensForAccount.remove(key); |
| 6031 | } else { |
| 6032 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6033 | } |
| 6034 | } |
| 6035 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6036 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 6037 | String authTokenType) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 6038 | // Fast path - check if account is already cached |
| 6039 | synchronized (accounts.cacheLock) { |
| 6040 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 6041 | if (authTokensForAccount != null) { |
| 6042 | return authTokensForAccount.get(authTokenType); |
| 6043 | } |
| 6044 | } |
| 6045 | // 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] | 6046 | synchronized (accounts.dbLock) { |
| 6047 | synchronized (accounts.cacheLock) { |
| 6048 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 6049 | if (authTokensForAccount == null) { |
| 6050 | // need to populate the cache for this account |
| 6051 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
| 6052 | accounts.authTokenCache.put(account, authTokensForAccount); |
| 6053 | } |
| 6054 | return authTokensForAccount.get(authTokenType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6055 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6056 | } |
| 6057 | } |
| 6058 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 6059 | private String readUserDataInternal(UserAccounts accounts, Account account, String key) { |
| 6060 | Map<String, String> userDataForAccount; |
| 6061 | // Fast path - check if data is already cached |
| 6062 | synchronized (accounts.cacheLock) { |
| 6063 | userDataForAccount = accounts.userDataCache.get(account); |
| 6064 | } |
| 6065 | // 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] | 6066 | if (userDataForAccount == null) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 6067 | synchronized (accounts.dbLock) { |
| 6068 | synchronized (accounts.cacheLock) { |
| 6069 | userDataForAccount = accounts.userDataCache.get(account); |
| 6070 | if (userDataForAccount == null) { |
| 6071 | // need to populate the cache for this account |
| 6072 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
| 6073 | accounts.userDataCache.put(account, userDataForAccount); |
| 6074 | } |
| 6075 | } |
| 6076 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6077 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 6078 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6079 | } |
| 6080 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 6081 | private Context getContextForUser(UserHandle user) { |
| 6082 | try { |
| 6083 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 6084 | } catch (NameNotFoundException e) { |
| 6085 | // Default to mContext, not finding the package system is running as is unlikely. |
| 6086 | return mContext; |
| 6087 | } |
| 6088 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 6089 | |
| 6090 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 6091 | try { |
| 6092 | response.onResult(result); |
| 6093 | } catch (RemoteException e) { |
| 6094 | // if the caller is dead then there is no one to care about remote |
| 6095 | // exceptions |
| 6096 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 6097 | Log.v(TAG, "failure while notifying response", e); |
| 6098 | } |
| 6099 | } |
| 6100 | } |
| 6101 | |
| 6102 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 6103 | String errorMessage) { |
| 6104 | try { |
| 6105 | response.onError(errorCode, errorMessage); |
| 6106 | } catch (RemoteException e) { |
| 6107 | // if the caller is dead then there is no one to care about remote |
| 6108 | // exceptions |
| 6109 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 6110 | Log.v(TAG, "failure while notifying response", e); |
| 6111 | } |
| 6112 | } |
| 6113 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 6114 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6115 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 6116 | private final Object mLock = new Object(); |
| 6117 | |
| 6118 | @GuardedBy("mLock") |
| 6119 | private AccountManagerBackupHelper mBackupHelper; |
| 6120 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6121 | @Override |
| 6122 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 6123 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 6124 | if (account == null) { |
| 6125 | Slog.w(TAG, "account cannot be null"); |
| 6126 | return; |
| 6127 | } |
| 6128 | if (packageName == null) { |
| 6129 | Slog.w(TAG, "packageName cannot be null"); |
| 6130 | return; |
| 6131 | } |
| 6132 | if (userId < UserHandle.USER_SYSTEM) { |
| 6133 | Slog.w(TAG, "user id must be concrete"); |
| 6134 | return; |
| 6135 | } |
| 6136 | if (callback == null) { |
| 6137 | Slog.w(TAG, "callback cannot be null"); |
| 6138 | return; |
| 6139 | } |
| 6140 | |
Dmitry Dementyev | 7b3ea13 | 2017-05-10 12:45:02 -0700 | [diff] [blame] | 6141 | int visibility = |
| 6142 | resolveAccountVisibility(account, packageName, getUserAccounts(userId)); |
| 6143 | if (visibility == AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 6144 | Slog.w(TAG, "requestAccountAccess: account is hidden"); |
| 6145 | return; |
| 6146 | } |
| 6147 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 6148 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 6149 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6150 | Bundle result = new Bundle(); |
| 6151 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 6152 | callback.sendResult(result); |
| 6153 | return; |
| 6154 | } |
| 6155 | |
| 6156 | final int uid; |
| 6157 | try { |
Dmitry Dementyev | 940b760 | 2018-10-09 16:40:06 -0700 | [diff] [blame^] | 6158 | long identityToken = clearCallingIdentity(); |
| 6159 | try { |
| 6160 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 6161 | } finally { |
| 6162 | restoreCallingIdentity(identityToken); |
| 6163 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6164 | } catch (NameNotFoundException e) { |
| 6165 | Slog.e(TAG, "Unknown package " + packageName); |
| 6166 | return; |
| 6167 | } |
| 6168 | |
| 6169 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 6170 | final UserAccounts userAccounts; |
| 6171 | synchronized (mUsers) { |
| 6172 | userAccounts = mUsers.get(userId); |
| 6173 | } |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 6174 | SystemNotificationChannels.createAccountChannelForPackage(packageName, uid, mContext); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 6175 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 6176 | } |
| 6177 | |
| 6178 | @Override |
| 6179 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 6180 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 6181 | mAppPermissionChangeListeners.add(listener); |
| 6182 | } |
| 6183 | |
| 6184 | @Override |
| 6185 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 6186 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6187 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 6188 | |
| 6189 | @Override |
| 6190 | public byte[] backupAccountAccessPermissions(int userId) { |
| 6191 | synchronized (mLock) { |
| 6192 | if (mBackupHelper == null) { |
| 6193 | mBackupHelper = new AccountManagerBackupHelper( |
| 6194 | AccountManagerService.this, this); |
| 6195 | } |
| 6196 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 6197 | } |
| 6198 | } |
| 6199 | |
| 6200 | @Override |
| 6201 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 6202 | synchronized (mLock) { |
| 6203 | if (mBackupHelper == null) { |
| 6204 | mBackupHelper = new AccountManagerBackupHelper( |
| 6205 | AccountManagerService.this, this); |
| 6206 | } |
| 6207 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 6208 | } |
| 6209 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 6210 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6211 | |
| 6212 | @VisibleForTesting |
| 6213 | static class Injector { |
| 6214 | private final Context mContext; |
| 6215 | |
| 6216 | public Injector(Context context) { |
| 6217 | mContext = context; |
| 6218 | } |
| 6219 | |
| 6220 | Looper getMessageHandlerLooper() { |
| 6221 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 6222 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 6223 | serviceThread.start(); |
| 6224 | return serviceThread.getLooper(); |
| 6225 | } |
| 6226 | |
| 6227 | Context getContext() { |
| 6228 | return mContext; |
| 6229 | } |
| 6230 | |
| 6231 | void addLocalService(AccountManagerInternal service) { |
| 6232 | LocalServices.addService(AccountManagerInternal.class, service); |
| 6233 | } |
| 6234 | |
| 6235 | String getDeDatabaseName(int userId) { |
| 6236 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 6237 | AccountsDb.DE_DATABASE_NAME); |
| 6238 | return databaseFile.getPath(); |
| 6239 | } |
| 6240 | |
| 6241 | String getCeDatabaseName(int userId) { |
| 6242 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 6243 | AccountsDb.CE_DATABASE_NAME); |
| 6244 | return databaseFile.getPath(); |
| 6245 | } |
| 6246 | |
| 6247 | String getPreNDatabaseName(int userId) { |
| 6248 | File systemDir = Environment.getDataSystemDirectory(); |
| 6249 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 6250 | PRE_N_DATABASE_NAME); |
| 6251 | if (userId == 0) { |
| 6252 | // Migrate old file, if it exists, to the new location. |
| 6253 | // 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] | 6254 | // accidentally created in the old location, |
| 6255 | // causing the new one to become corrupted as well. |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6256 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 6257 | if (oldFile.exists() && !databaseFile.exists()) { |
| 6258 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 6259 | File userDir = Environment.getUserSystemDirectory(userId); |
| 6260 | if (!userDir.exists()) { |
| 6261 | if (!userDir.mkdirs()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 6262 | throw new IllegalStateException( |
| 6263 | "User dir cannot be created: " + userDir); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6264 | } |
| 6265 | } |
| 6266 | if (!oldFile.renameTo(databaseFile)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 6267 | throw new IllegalStateException( |
| 6268 | "User dir cannot be migrated: " + databaseFile); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6269 | } |
| 6270 | } |
| 6271 | } |
| 6272 | return databaseFile.getPath(); |
| 6273 | } |
| 6274 | |
| 6275 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 6276 | return new AccountAuthenticatorCache(mContext); |
| 6277 | } |
| 6278 | |
| 6279 | INotificationManager getNotificationManager() { |
| 6280 | return NotificationManager.getService(); |
| 6281 | } |
| 6282 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 6283 | |
Andrew Scull | c7770d6 | 2017-05-22 17:49:58 +0100 | [diff] [blame] | 6284 | private static class NotificationId { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 6285 | final String mTag; |
| 6286 | private final int mId; |
| 6287 | |
| 6288 | NotificationId(String tag, int type) { |
| 6289 | mTag = tag; |
| 6290 | mId = type; |
| 6291 | } |
| 6292 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 6293 | } |