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; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 82 | import android.os.StrictMode; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 83 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 84 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 85 | import android.os.UserManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 86 | import android.text.TextUtils; |
| 87 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 88 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 89 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 90 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 91 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 92 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 93 | import com.android.internal.R; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 94 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 95 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 96 | import com.android.internal.content.PackageMonitor; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 97 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 98 | import com.android.internal.notification.SystemNotificationChannels; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 99 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 100 | import com.android.internal.util.DumpUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 101 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 102 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 103 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 104 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 105 | import com.android.server.SystemService; |
| 106 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 107 | import com.google.android.collect.Lists; |
| 108 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 109 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 110 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 111 | import java.io.FileDescriptor; |
| 112 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 113 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 114 | import java.security.MessageDigest; |
| 115 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 116 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 117 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 118 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 119 | import java.util.Collection; |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 120 | import java.util.Collections; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 121 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 122 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 123 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 124 | import java.util.LinkedHashMap; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 125 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 126 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 127 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 128 | import java.util.Objects; |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 129 | import java.util.Set; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 130 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 131 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 132 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 133 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 134 | /** |
| 135 | * A system service that provides account, password, and authtoken management for all |
| 136 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 137 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 138 | * 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] | 139 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 140 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 141 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 142 | public class AccountManagerService |
| 143 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 144 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 145 | private static final String TAG = "AccountManagerService"; |
| 146 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 147 | public static class Lifecycle extends SystemService { |
| 148 | private AccountManagerService mService; |
| 149 | |
| 150 | public Lifecycle(Context context) { |
| 151 | super(context); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void onStart() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 156 | mService = new AccountManagerService(new Injector(getContext())); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 157 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 158 | } |
| 159 | |
| 160 | @Override |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 161 | public void onUnlockUser(int userHandle) { |
| 162 | mService.onUnlockUser(userHandle); |
| 163 | } |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 164 | |
| 165 | @Override |
Fyodor Kupolov | ce25ed2 | 2017-05-04 11:44:31 -0700 | [diff] [blame] | 166 | public void onStopUser(int userHandle) { |
Fyodor Kupolov | 945c97e | 2017-06-21 17:45:19 -0700 | [diff] [blame] | 167 | Slog.i(TAG, "onStopUser " + userHandle); |
| 168 | mService.purgeUserData(userHandle); |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 169 | } |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 170 | } |
| 171 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 172 | final Context mContext; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 173 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 174 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 175 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 176 | private UserManager mUserManager; |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 177 | private final Injector mInjector; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 178 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 179 | final MessageHandler mHandler; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 180 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 181 | // Messages that can be sent on mHandler |
| 182 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 183 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 184 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 185 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 186 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 187 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 188 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 189 | private static final int SIGNATURE_CHECK_MISMATCH = 0; |
| 190 | private static final int SIGNATURE_CHECK_MATCH = 1; |
| 191 | private static final int SIGNATURE_CHECK_UID_MATCH = 2; |
| 192 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 193 | static { |
| 194 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
Christopher Tate | bded68f | 2017-02-21 11:41:55 -0800 | [diff] [blame] | 195 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 196 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 197 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 198 | |
| 199 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 200 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 201 | static class UserAccounts { |
| 202 | private final int userId; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 203 | final AccountsDb accountsDb; |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 204 | private final HashMap<Pair<Pair<Account, String>, Integer>, NotificationId> |
| 205 | credentialsPermissionNotificationIds = new HashMap<>(); |
| 206 | private final HashMap<Account, NotificationId> signinRequiredNotificationIds |
| 207 | = new HashMap<>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 208 | final Object cacheLock = new Object(); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 209 | 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] | 210 | /** protected by the {@link #cacheLock} */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 211 | final HashMap<String, Account[]> accountCache = new LinkedHashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 212 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 213 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
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>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 216 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 217 | private final TokenCache accountTokenCaches = new TokenCache(); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 218 | /** protected by the {@link #cacheLock} */ |
| 219 | private final Map<Account, Map<String, Integer>> visibilityCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 220 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 221 | /** protected by the {@link #mReceiversForType}, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 222 | * type -> (packageName -> number of active receivers) |
| 223 | * type == null is used to get notifications about all account types |
| 224 | */ |
| 225 | private final Map<String, Map<String, Integer>> mReceiversForType = new HashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 226 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 227 | /** |
| 228 | * protected by the {@link #cacheLock} |
| 229 | * |
| 230 | * Caches the previous names associated with an account. Previous names |
| 231 | * should be cached because we expect that when an Account is renamed, |
| 232 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 233 | * want to know if the accounts they care about have been renamed. |
| 234 | * |
| 235 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 236 | * we can distinguish between those accounts with no previous names and |
| 237 | * those whose previous names haven't been cached (yet). |
| 238 | */ |
| 239 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 240 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 241 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 242 | private int debugDbInsertionPoint = -1; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 243 | private SQLiteStatement statementForLogging; // TODO Move to AccountsDb |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 244 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 245 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 246 | this.userId = userId; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 247 | synchronized (dbLock) { |
| 248 | synchronized (cacheLock) { |
| 249 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
| 250 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 255 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 256 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 257 | // Not thread-safe. Only use in synchronized context |
| 258 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 259 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 260 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 261 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 262 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 263 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 264 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 265 | /** |
| 266 | * This should only be called by system code. One should only call this after the service |
| 267 | * has started. |
| 268 | * @return a reference to the AccountManagerService instance |
| 269 | * @hide |
| 270 | */ |
| 271 | public static AccountManagerService getSingleton() { |
| 272 | return sThis.get(); |
| 273 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 274 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 275 | public AccountManagerService(Injector injector) { |
| 276 | mInjector = injector; |
| 277 | mContext = injector.getContext(); |
| 278 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 279 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 280 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 281 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 282 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 283 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 284 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 285 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 286 | IntentFilter intentFilter = new IntentFilter(); |
| 287 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 288 | intentFilter.addDataScheme("package"); |
| 289 | mContext.registerReceiver(new BroadcastReceiver() { |
| 290 | @Override |
| 291 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 292 | // Don't delete accounts when updating a authenticator's |
| 293 | // package. |
| 294 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 295 | /* Purging data requires file io, don't block the main thread. This is probably |
| 296 | * less than ideal because we are introducing a race condition where old grants |
| 297 | * could be exercised until they are purged. But that race condition existed |
| 298 | * anyway with the broadcast receiver. |
| 299 | * |
| 300 | * Ideally, we would completely clear the cache, purge data from the database, |
| 301 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 302 | * large at this point. |
| 303 | */ |
Dmitry Dementyev | 0b67642 | 2017-03-09 11:51:26 -0800 | [diff] [blame] | 304 | final String removedPackageName = intent.getData().getSchemeSpecificPart(); |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 305 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 306 | @Override |
| 307 | public void run() { |
| 308 | purgeOldGrantsAll(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 309 | // Notify authenticator about removed app? |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 310 | removeVisibilityValuesForPackage(removedPackageName); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 311 | } |
| 312 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 313 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 314 | } |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 315 | } |
| 316 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 317 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 318 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 319 | |
Fyodor Kupolov | 945c97e | 2017-06-21 17:45:19 -0700 | [diff] [blame] | 320 | IntentFilter userFilter = new IntentFilter(); |
| 321 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 322 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 323 | @Override |
| 324 | public void onReceive(Context context, Intent intent) { |
| 325 | String action = intent.getAction(); |
| 326 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 327 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 328 | if (userId < 1) return; |
| 329 | Slog.i(TAG, "User " + userId + " removed"); |
| 330 | purgeUserData(userId); |
| 331 | } |
| 332 | } |
| 333 | }, UserHandle.ALL, userFilter, null, null); |
| 334 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 335 | // Need to cancel account request notifications if the update/install can access the account |
| 336 | new PackageMonitor() { |
| 337 | @Override |
| 338 | public void onPackageAdded(String packageName, int uid) { |
| 339 | // Called on a handler, and running as the system |
| 340 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 345 | // Called on a handler, and running as the system |
| 346 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 347 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 348 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 349 | |
| 350 | // Cancel account request notification if an app op was preventing the account access |
| 351 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 352 | new AppOpsManager.OnOpChangedInternalListener() { |
| 353 | @Override |
| 354 | public void onOpChanged(int op, String packageName) { |
| 355 | try { |
| 356 | final int userId = ActivityManager.getCurrentUser(); |
| 357 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 358 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 359 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 360 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 361 | final long identity = Binder.clearCallingIdentity(); |
| 362 | try { |
| 363 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 364 | } finally { |
| 365 | Binder.restoreCallingIdentity(identity); |
| 366 | } |
| 367 | } |
| 368 | } catch (NameNotFoundException e) { |
| 369 | /* ignore */ |
| 370 | } |
| 371 | } |
| 372 | }); |
| 373 | |
| 374 | // Cancel account request notification if a permission was preventing the account access |
| 375 | mPackageManager.addOnPermissionsChangeListener( |
| 376 | (int uid) -> { |
| 377 | Account[] accounts = null; |
| 378 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 379 | if (packageNames != null) { |
| 380 | final int userId = UserHandle.getUserId(uid); |
| 381 | final long identity = Binder.clearCallingIdentity(); |
| 382 | try { |
| 383 | for (String packageName : packageNames) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 384 | // if app asked for permission we need to cancel notification even |
| 385 | // for O+ applications. |
| 386 | if (mPackageManager.checkPermission( |
| 387 | Manifest.permission.GET_ACCOUNTS, |
| 388 | packageName) != PackageManager.PERMISSION_GRANTED) { |
| 389 | continue; |
| 390 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 391 | |
| 392 | if (accounts == null) { |
| 393 | accounts = getAccountsAsUser(null, userId, "android"); |
| 394 | if (ArrayUtils.isEmpty(accounts)) { |
| 395 | return; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | for (Account account : accounts) { |
| 400 | cancelAccountAccessRequestNotificationIfNeeded( |
| 401 | account, uid, packageName, true); |
| 402 | } |
| 403 | } |
| 404 | } finally { |
| 405 | Binder.restoreCallingIdentity(identity); |
| 406 | } |
| 407 | } |
| 408 | }); |
| 409 | } |
| 410 | |
| 411 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 412 | boolean checkAccess) { |
| 413 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 414 | for (Account account : accounts) { |
| 415 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 420 | boolean checkAccess) { |
| 421 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 422 | for (Account account : accounts) { |
| 423 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 428 | boolean checkAccess) { |
| 429 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 430 | if (packageNames != null) { |
| 431 | for (String packageName : packageNames) { |
| 432 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 433 | packageName, checkAccess); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 439 | int uid, String packageName, boolean checkAccess) { |
| 440 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 441 | UserHandle.getUserHandleForUid(uid))) { |
| 442 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 443 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 444 | UserHandle.getUserHandleForUid(uid)); |
| 445 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 448 | @Override |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 449 | public boolean addAccountExplicitlyWithVisibility(Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 450 | Bundle extras, Map packageToVisibility) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 451 | Bundle.setDefusable(extras, true); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 452 | int callingUid = Binder.getCallingUid(); |
| 453 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 454 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 455 | Log.v(TAG, "addAccountExplicitly: " + account + ", caller's uid " + callingUid |
| 456 | + ", pid " + Binder.getCallingPid()); |
| 457 | } |
| 458 | Preconditions.checkNotNull(account, "account cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 459 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
| 460 | String msg = String.format("uid %s cannot explicitly add accounts of type: %s", |
| 461 | callingUid, account.type); |
| 462 | throw new SecurityException(msg); |
| 463 | } |
| 464 | /* |
| 465 | * Child users are not allowed to add accounts. Only the accounts that are shared by the |
| 466 | * parent profile can be added to child profile. |
| 467 | * |
| 468 | * TODO: Only allow accounts that were shared to be added by a limited user. |
| 469 | */ |
| 470 | // fails if the account already exists |
| 471 | long identityToken = clearCallingIdentity(); |
| 472 | try { |
| 473 | UserAccounts accounts = getUserAccounts(userId); |
| 474 | return addAccountInternal(accounts, account, password, extras, callingUid, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 475 | (Map<String, Integer>) packageToVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 476 | } finally { |
| 477 | restoreCallingIdentity(identityToken); |
| 478 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Override |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 482 | public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 483 | String accountType) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 484 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 485 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 486 | boolean isSystemUid = UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 487 | List<String> managedTypes = getTypesForCaller(callingUid, userId, isSystemUid); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 488 | |
| 489 | if ((accountType != null && !managedTypes.contains(accountType)) |
| 490 | || (accountType == null && !isSystemUid)) { |
| 491 | throw new SecurityException( |
| 492 | "getAccountsAndVisibilityForPackage() called from unauthorized uid " |
| 493 | + callingUid + " with packageName=" + packageName); |
| 494 | } |
| 495 | if (accountType != null) { |
| 496 | managedTypes = new ArrayList<String>(); |
| 497 | managedTypes.add(accountType); |
| 498 | } |
| 499 | |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 500 | long identityToken = clearCallingIdentity(); |
| 501 | try { |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 502 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 503 | return getAccountsAndVisibilityForPackage(packageName, managedTypes, callingUid, |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 504 | accounts); |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 505 | } finally { |
| 506 | restoreCallingIdentity(identityToken); |
| 507 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* |
| 511 | * accountTypes may not be null |
| 512 | */ |
| 513 | private Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 514 | List<String> accountTypes, Integer callingUid, UserAccounts accounts) { |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 515 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 516 | Log.d(TAG, "Package not found " + packageName); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 517 | return new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 520 | Map<Account, Integer> result = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 521 | for (String accountType : accountTypes) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 522 | synchronized (accounts.dbLock) { |
| 523 | synchronized (accounts.cacheLock) { |
| 524 | final Account[] accountsOfType = accounts.accountCache.get(accountType); |
| 525 | if (accountsOfType != null) { |
| 526 | for (Account account : accountsOfType) { |
| 527 | result.put(account, |
| 528 | resolveAccountVisibility(account, packageName, accounts)); |
| 529 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | return filterSharedAccounts(accounts, result, callingUid, packageName); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | @Override |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 538 | public Map<String, Integer> getPackagesAndVisibilityForAccount(Account account) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 539 | Preconditions.checkNotNull(account, "account cannot be null"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 540 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 541 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 542 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
| 543 | && !isSystemUid(callingUid)) { |
| 544 | String msg = |
| 545 | String.format("uid %s cannot get secrets for account %s", callingUid, account); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 546 | throw new SecurityException(msg); |
| 547 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 548 | |
| 549 | long identityToken = clearCallingIdentity(); |
| 550 | try { |
| 551 | UserAccounts accounts = getUserAccounts(userId); |
| 552 | synchronized (accounts.dbLock) { |
| 553 | synchronized (accounts.cacheLock) { |
| 554 | return getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 555 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 556 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 557 | } finally { |
| 558 | restoreCallingIdentity(identityToken); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 559 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 560 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /** |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 564 | * Returns Map with all package names and visibility values for given account. |
| 565 | * The method and returned map must be guarded by accounts.cacheLock |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 566 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 567 | * @param account Account to get visibility values. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 568 | * @param accounts UserAccount that currently hosts the account and application |
| 569 | * |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 570 | * @return Map with cache for package names to visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 571 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 572 | private @NonNull Map<String, Integer> getPackagesAndVisibilityForAccountLocked(Account account, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 573 | UserAccounts accounts) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 574 | Map<String, Integer> accountVisibility = accounts.visibilityCache.get(account); |
| 575 | if (accountVisibility == null) { |
| 576 | Log.d(TAG, "Visibility was not initialized"); |
| 577 | accountVisibility = new HashMap<>(); |
| 578 | accounts.visibilityCache.put(account, accountVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 579 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 580 | return accountVisibility; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 584 | public int getAccountVisibility(Account account, String packageName) { |
| 585 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 586 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 587 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 588 | int userId = UserHandle.getCallingUserId(); |
| 589 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 590 | && !isSystemUid(callingUid)) { |
| 591 | String msg = String.format( |
| 592 | "uid %s cannot get secrets for accounts of type: %s", |
| 593 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 594 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 595 | throw new SecurityException(msg); |
| 596 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 597 | long identityToken = clearCallingIdentity(); |
| 598 | try { |
| 599 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | cbe1bd1 | 2017-04-25 17:02:47 -0700 | [diff] [blame] | 600 | if (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName)) { |
| 601 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
| 602 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 603 | return visibility; |
| 604 | } else { |
| 605 | return AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 606 | } |
| 607 | } |
| 608 | if (AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)) { |
| 609 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
| 610 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 611 | return visibility; |
| 612 | } else { |
| 613 | return AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 614 | } |
| 615 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 616 | return resolveAccountVisibility(account, packageName, accounts); |
| 617 | } finally { |
| 618 | restoreCallingIdentity(identityToken); |
| 619 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 623 | * Method returns visibility for given account and package name. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 624 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 625 | * @param account The account to check visibility. |
| 626 | * @param packageName Package name to check visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 627 | * @param accounts UserAccount that currently hosts the account and application |
| 628 | * |
| 629 | * @return Visibility value, AccountManager.VISIBILITY_UNDEFINED if no value was stored. |
| 630 | * |
| 631 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 632 | private int getAccountVisibilityFromCache(Account account, String packageName, |
| 633 | UserAccounts accounts) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 634 | synchronized (accounts.cacheLock) { |
| 635 | Map<String, Integer> accountVisibility = |
| 636 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 637 | Integer visibility = accountVisibility.get(packageName); |
| 638 | return visibility != null ? visibility : AccountManager.VISIBILITY_UNDEFINED; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Method which handles default values for Account visibility. |
| 644 | * |
| 645 | * @param account The account to check visibility. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 646 | * @param packageName Package name to check visibility |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 647 | * @param accounts UserAccount that currently hosts the account and application |
| 648 | * |
| 649 | * @return Visibility value, the method never returns AccountManager.VISIBILITY_UNDEFINED |
| 650 | * |
| 651 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 652 | private Integer resolveAccountVisibility(Account account, @NonNull String packageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 653 | UserAccounts accounts) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 654 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 655 | int uid = -1; |
| 656 | try { |
| 657 | long identityToken = clearCallingIdentity(); |
| 658 | try { |
| 659 | uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 660 | } finally { |
| 661 | restoreCallingIdentity(identityToken); |
| 662 | } |
| 663 | } catch (NameNotFoundException e) { |
| 664 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 665 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 666 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 667 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 668 | // System visibility can not be restricted. |
| 669 | if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) { |
| 670 | return AccountManager.VISIBILITY_VISIBLE; |
| 671 | } |
| 672 | |
| 673 | int signatureCheckResult = |
| 674 | checkPackageSignature(account.type, uid, accounts.userId); |
| 675 | |
| 676 | // Authenticator can not restrict visibility to itself. |
| 677 | if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) { |
| 678 | return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account |
| 679 | } |
| 680 | |
| 681 | // Return stored value if it was set. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 682 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 683 | |
| 684 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 685 | return visibility; |
| 686 | } |
| 687 | |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 688 | boolean isPrivileged = isPermittedForPackage(packageName, uid, accounts.userId, |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 689 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 690 | |
| 691 | // Device/Profile owner gets visibility by default. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 692 | if (isProfileOwner(uid)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 693 | return AccountManager.VISIBILITY_VISIBLE; |
| 694 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 695 | |
| 696 | boolean preO = isPreOApplication(packageName); |
| 697 | if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH) |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 698 | || (preO && checkGetAccountsPermission(packageName, uid, accounts.userId)) |
| 699 | || (checkReadContactsPermission(packageName, uid, accounts.userId) |
| 700 | && accountTypeManagesContacts(account.type, accounts.userId)) |
| 701 | || isPrivileged) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 702 | // Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature |
| 703 | // match. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 704 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 705 | AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 706 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 707 | visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 708 | } |
| 709 | } else { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 710 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 711 | AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 712 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 713 | visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 714 | } |
| 715 | } |
| 716 | return visibility; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Checks targetSdk for a package; |
| 721 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 722 | * @param packageName Package name |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 723 | * |
| 724 | * @return True if package's target SDK is below {@link android.os.Build.VERSION_CODES#O}, or |
| 725 | * undefined |
| 726 | */ |
| 727 | private boolean isPreOApplication(String packageName) { |
| 728 | try { |
| 729 | long identityToken = clearCallingIdentity(); |
| 730 | ApplicationInfo applicationInfo; |
| 731 | try { |
| 732 | applicationInfo = mPackageManager.getApplicationInfo(packageName, 0); |
| 733 | } finally { |
| 734 | restoreCallingIdentity(identityToken); |
| 735 | } |
| 736 | |
| 737 | if (applicationInfo != null) { |
| 738 | int version = applicationInfo.targetSdkVersion; |
| 739 | return version < android.os.Build.VERSION_CODES.O; |
| 740 | } |
| 741 | return true; |
| 742 | } catch (NameNotFoundException e) { |
| 743 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 744 | return true; |
| 745 | } |
Dmitry Dementyev | 58fa8362 | 2016-12-20 18:08:51 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 749 | public boolean setAccountVisibility(Account account, String packageName, int newVisibility) { |
| 750 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 751 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 752 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 753 | int userId = UserHandle.getCallingUserId(); |
| 754 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 755 | && !isSystemUid(callingUid)) { |
| 756 | String msg = String.format( |
| 757 | "uid %s cannot get secrets for accounts of type: %s", |
| 758 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 759 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 760 | throw new SecurityException(msg); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 761 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 762 | long identityToken = clearCallingIdentity(); |
| 763 | try { |
| 764 | UserAccounts accounts = getUserAccounts(userId); |
| 765 | return setAccountVisibility(account, packageName, newVisibility, true /* notify */, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 766 | accounts); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 767 | } finally { |
| 768 | restoreCallingIdentity(identityToken); |
| 769 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 770 | } |
| 771 | |
sunjian | 066aa5e | 2017-06-05 12:16:59 -0700 | [diff] [blame] | 772 | private boolean isVisible(int visibility) { |
| 773 | return visibility == AccountManager.VISIBILITY_VISIBLE || |
| 774 | visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 775 | } |
| 776 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 777 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 778 | * Updates visibility for given account name and package. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 779 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 780 | * @param account Account to update visibility. |
| 781 | * @param packageName Package name for which visibility is updated. |
| 782 | * @param newVisibility New visibility calue |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 783 | * @param notify if the flag is set applications will get notification about visibility change |
| 784 | * @param accounts UserAccount that currently hosts the account and application |
| 785 | * |
| 786 | * @return True if account visibility was changed. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 787 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 788 | private boolean setAccountVisibility(Account account, String packageName, int newVisibility, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 789 | boolean notify, UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 790 | synchronized (accounts.dbLock) { |
| 791 | synchronized (accounts.cacheLock) { |
| 792 | Map<String, Integer> packagesToVisibility; |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 793 | List<String> accountRemovedReceivers; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 794 | if (notify) { |
| 795 | if (isSpecialPackageKey(packageName)) { |
| 796 | packagesToVisibility = |
| 797 | getRequestingPackages(account, accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 798 | accountRemovedReceivers = getAccountRemovedReceivers(account, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 799 | } else { |
| 800 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 801 | return false; // package is not installed. |
| 802 | } |
| 803 | packagesToVisibility = new HashMap<>(); |
| 804 | packagesToVisibility.put(packageName, |
| 805 | resolveAccountVisibility(account, packageName, accounts)); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 806 | accountRemovedReceivers = new ArrayList<>(); |
| 807 | if (shouldNotifyPackageOnAccountRemoval(account, packageName, accounts)) { |
| 808 | accountRemovedReceivers.add(packageName); |
| 809 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 810 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 811 | } else { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 812 | // Notifications will not be send - only used during add account. |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 813 | if (!isSpecialPackageKey(packageName) && |
| 814 | !packageExistsForUser(packageName, accounts.userId)) { |
| 815 | // package is not installed and not meta value. |
| 816 | return false; |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 817 | } |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 818 | packagesToVisibility = Collections.emptyMap(); |
| 819 | accountRemovedReceivers = Collections.emptyList(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 820 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 821 | |
| 822 | if (!updateAccountVisibilityLocked(account, packageName, newVisibility, accounts)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 823 | return false; |
| 824 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 825 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 826 | if (notify) { |
| 827 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 828 | .entrySet()) { |
sunjian | 066aa5e | 2017-06-05 12:16:59 -0700 | [diff] [blame] | 829 | int oldVisibility = packageToVisibility.getValue(); |
| 830 | int currentVisibility = |
| 831 | resolveAccountVisibility(account, packageName, accounts); |
| 832 | if (isVisible(oldVisibility) != isVisible(currentVisibility)) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 833 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 834 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 835 | } |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 836 | for (String packageNameToNotify : accountRemovedReceivers) { |
| 837 | sendAccountRemovedBroadcast(account, packageNameToNotify, accounts.userId); |
| 838 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 839 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 840 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 841 | return true; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 842 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 843 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 846 | // Update account visibility in cache and database. |
| 847 | private boolean updateAccountVisibilityLocked(Account account, String packageName, |
| 848 | int newVisibility, UserAccounts accounts) { |
| 849 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 850 | if (accountId < 0) { |
| 851 | return false; |
| 852 | } |
| 853 | |
| 854 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
| 855 | try { |
| 856 | if (!accounts.accountsDb.setAccountVisibility(accountId, packageName, |
| 857 | newVisibility)) { |
| 858 | return false; |
| 859 | } |
| 860 | } finally { |
| 861 | StrictMode.setThreadPolicy(oldPolicy); |
| 862 | } |
| 863 | Map<String, Integer> accountVisibility = |
| 864 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 865 | accountVisibility.put(packageName, newVisibility); |
| 866 | return true; |
| 867 | } |
| 868 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 869 | @Override |
| 870 | public void registerAccountListener(String[] accountTypes, String opPackageName) { |
| 871 | int callingUid = Binder.getCallingUid(); |
| 872 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 873 | |
| 874 | int userId = UserHandle.getCallingUserId(); |
| 875 | long identityToken = clearCallingIdentity(); |
| 876 | try { |
| 877 | UserAccounts accounts = getUserAccounts(userId); |
| 878 | registerAccountListener(accountTypes, opPackageName, accounts); |
| 879 | } finally { |
| 880 | restoreCallingIdentity(identityToken); |
| 881 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 884 | private void registerAccountListener(String[] accountTypes, String opPackageName, |
| 885 | UserAccounts accounts) { |
| 886 | synchronized (accounts.mReceiversForType) { |
| 887 | if (accountTypes == null) { |
| 888 | // null for any type |
| 889 | accountTypes = new String[] {null}; |
| 890 | } |
| 891 | for (String type : accountTypes) { |
| 892 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 893 | if (receivers == null) { |
| 894 | receivers = new HashMap<>(); |
| 895 | accounts.mReceiversForType.put(type, receivers); |
| 896 | } |
| 897 | Integer cnt = receivers.get(opPackageName); |
| 898 | receivers.put(opPackageName, cnt != null ? cnt + 1 : 1); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 903 | @Override |
| 904 | public void unregisterAccountListener(String[] accountTypes, String opPackageName) { |
| 905 | int callingUid = Binder.getCallingUid(); |
| 906 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 907 | int userId = UserHandle.getCallingUserId(); |
| 908 | long identityToken = clearCallingIdentity(); |
| 909 | try { |
| 910 | UserAccounts accounts = getUserAccounts(userId); |
| 911 | unregisterAccountListener(accountTypes, opPackageName, accounts); |
| 912 | } finally { |
| 913 | restoreCallingIdentity(identityToken); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | private void unregisterAccountListener(String[] accountTypes, String opPackageName, |
| 918 | UserAccounts accounts) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 919 | synchronized (accounts.mReceiversForType) { |
| 920 | if (accountTypes == null) { |
| 921 | // null for any type |
| 922 | accountTypes = new String[] {null}; |
| 923 | } |
| 924 | for (String type : accountTypes) { |
| 925 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 926 | if (receivers == null || receivers.get(opPackageName) == null) { |
| 927 | throw new IllegalArgumentException("attempt to unregister wrong receiver"); |
| 928 | } |
| 929 | Integer cnt = receivers.get(opPackageName); |
| 930 | if (cnt == 1) { |
| 931 | receivers.remove(opPackageName); |
| 932 | } else { |
| 933 | receivers.put(opPackageName, cnt - 1); |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | // Send notification to all packages which can potentially see the account |
| 940 | private void sendNotificationAccountUpdated(Account account, UserAccounts accounts) { |
| 941 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 942 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 943 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 944 | if ((packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) |
| 945 | && (packageToVisibility.getValue() |
| 946 | != AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE)) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 947 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Sends a direct intent to a package, notifying it of account visibility change. |
| 954 | * |
| 955 | * @param packageName to send Account to |
| 956 | * @param accounts UserAccount that currently hosts the account |
| 957 | */ |
| 958 | private void notifyPackage(String packageName, UserAccounts accounts) { |
| 959 | Intent intent = new Intent(AccountManager.ACTION_VISIBLE_ACCOUNTS_CHANGED); |
| 960 | intent.setPackage(packageName); |
| 961 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 962 | mContext.sendBroadcastAsUser(intent, new UserHandle(accounts.userId)); |
| 963 | } |
| 964 | |
| 965 | // Returns a map from package name to visibility, for packages subscribed |
| 966 | // to notifications about any account type, or type of provided account |
| 967 | // account type or all types. |
| 968 | private Map<String, Integer> getRequestingPackages(Account account, UserAccounts accounts) { |
| 969 | Set<String> packages = new HashSet<>(); |
| 970 | synchronized (accounts.mReceiversForType) { |
| 971 | for (String type : new String[] {account.type, null}) { |
| 972 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 973 | if (receivers != null) { |
| 974 | packages.addAll(receivers.keySet()); |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | Map<String, Integer> result = new HashMap<>(); |
| 979 | for (String packageName : packages) { |
| 980 | result.put(packageName, resolveAccountVisibility(account, packageName, accounts)); |
| 981 | } |
| 982 | return result; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 983 | } |
| 984 | |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 985 | // Returns a list of packages listening to ACTION_ACCOUNT_REMOVED able to see the account. |
| 986 | private List<String> getAccountRemovedReceivers(Account account, UserAccounts accounts) { |
| 987 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 988 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 989 | List<ResolveInfo> receivers = |
| 990 | mPackageManager.queryBroadcastReceiversAsUser(intent, 0, accounts.userId); |
| 991 | List<String> result = new ArrayList<>(); |
| 992 | if (receivers == null) { |
| 993 | return result; |
| 994 | } |
| 995 | for (ResolveInfo resolveInfo: receivers) { |
| 996 | String packageName = resolveInfo.activityInfo.applicationInfo.packageName; |
| 997 | int visibility = resolveAccountVisibility(account, packageName, accounts); |
| 998 | if (visibility == AccountManager.VISIBILITY_VISIBLE |
| 999 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) { |
| 1000 | result.add(packageName); |
| 1001 | } |
| 1002 | } |
| 1003 | return result; |
| 1004 | } |
| 1005 | |
| 1006 | // Returns true if given package is listening to ACTION_ACCOUNT_REMOVED and can see the account. |
| 1007 | private boolean shouldNotifyPackageOnAccountRemoval(Account account, |
| 1008 | String packageName, UserAccounts accounts) { |
| 1009 | int visibility = resolveAccountVisibility(account, packageName, accounts); |
| 1010 | if (visibility != AccountManager.VISIBILITY_VISIBLE |
| 1011 | && visibility != AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) { |
| 1012 | return false; |
| 1013 | } |
| 1014 | |
| 1015 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 1016 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 1017 | intent.setPackage(packageName); |
| 1018 | List<ResolveInfo> receivers = |
| 1019 | mPackageManager.queryBroadcastReceiversAsUser(intent, 0, accounts.userId); |
| 1020 | return (receivers != null && receivers.size() > 0); |
| 1021 | } |
| 1022 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1023 | private boolean packageExistsForUser(String packageName, int userId) { |
| 1024 | try { |
| 1025 | long identityToken = clearCallingIdentity(); |
| 1026 | try { |
| 1027 | mPackageManager.getPackageUidAsUser(packageName, userId); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 1028 | return true; |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1029 | } finally { |
| 1030 | restoreCallingIdentity(identityToken); |
| 1031 | } |
| 1032 | } catch (NameNotFoundException e) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * Returns true if packageName is one of special values. |
| 1039 | */ |
| 1040 | private boolean isSpecialPackageKey(String packageName) { |
| 1041 | return (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName) |
| 1042 | || AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)); |
| 1043 | } |
| 1044 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1045 | private void sendAccountsChangedBroadcast(int userId) { |
| 1046 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 1047 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
| 1048 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1051 | private void sendAccountRemovedBroadcast(Account account, String packageName, int userId) { |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame] | 1052 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 1053 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1054 | intent.setPackage(packageName); |
| 1055 | intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 1056 | intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame] | 1057 | mContext.sendBroadcastAsUser(intent, new UserHandle(userId)); |
| 1058 | } |
| 1059 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1060 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 1061 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 1062 | throws RemoteException { |
| 1063 | try { |
| 1064 | return super.onTransact(code, data, reply, flags); |
| 1065 | } catch (RuntimeException e) { |
| 1066 | // The account manager only throws security exceptions, so let's |
| 1067 | // log all others. |
| 1068 | if (!(e instanceof SecurityException)) { |
| 1069 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 1070 | } |
| 1071 | throw e; |
| 1072 | } |
| 1073 | } |
| 1074 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1075 | private UserManager getUserManager() { |
| 1076 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1077 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1078 | } |
| 1079 | return mUserManager; |
| 1080 | } |
| 1081 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1082 | /** |
| 1083 | * Validate internal set of accounts against installed authenticators for |
| 1084 | * given user. Clears cached authenticators before validating. |
| 1085 | */ |
| 1086 | public void validateAccounts(int userId) { |
| 1087 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1088 | // Invalidate user-specific cache to make sure we catch any |
| 1089 | // removed authenticators. |
| 1090 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | * Validate internal set of accounts against installed authenticators for |
| 1095 | * given user. Clear cached authenticators before validating when requested. |
| 1096 | */ |
| 1097 | private void validateAccountsInternal( |
| 1098 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1099 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1100 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1101 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1102 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1103 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1104 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1105 | if (invalidateAuthenticatorCache) { |
| 1106 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 1107 | } |
| 1108 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1109 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 1110 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1111 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1112 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1113 | synchronized (accounts.dbLock) { |
| 1114 | synchronized (accounts.cacheLock) { |
| 1115 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1116 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1117 | // Get a map of stored authenticator types to UID |
| 1118 | final AccountsDb accountsDb = accounts.accountsDb; |
| 1119 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
| 1120 | // Create a list of authenticator type whose previous uid no longer exists |
| 1121 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
| 1122 | SparseBooleanArray knownUids = null; |
| 1123 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 1124 | String type = authToUidEntry.getKey(); |
| 1125 | int uid = authToUidEntry.getValue(); |
| 1126 | Integer knownUid = knownAuth.get(type); |
| 1127 | if (knownUid != null && uid == knownUid) { |
| 1128 | // Remove it from the knownAuth list if it's unchanged. |
| 1129 | knownAuth.remove(type); |
| 1130 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1131 | /* |
| 1132 | * The authenticator is presently not cached and should only be triggered |
| 1133 | * when we think an authenticator has been removed (or is being updated). |
| 1134 | * But we still want to check if any data with the associated uid is |
| 1135 | * around. This is an (imperfect) signal that the package may be updating. |
| 1136 | * |
| 1137 | * A side effect of this is that an authenticator sharing a uid with |
| 1138 | * multiple apps won't get its credentials wiped as long as some app with |
| 1139 | * that uid is still on the device. But I suspect that this is a rare case. |
| 1140 | * And it isn't clear to me how an attacker could really exploit that |
| 1141 | * feature. |
| 1142 | * |
| 1143 | * The upshot is that we don't have to worry about accounts getting |
| 1144 | * uninstalled while the authenticator's package is being updated. |
| 1145 | * |
| 1146 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1147 | if (knownUids == null) { |
| 1148 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
| 1149 | } |
| 1150 | if (!knownUids.get(uid)) { |
| 1151 | // The authenticator is not presently available to the cache. And the |
| 1152 | // package no longer has a data directory (so we surmise it isn't |
| 1153 | // updating). So purge its data from the account databases. |
| 1154 | obsoleteAuthType.add(type); |
| 1155 | // And delete it from the TABLE_META |
| 1156 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
| 1157 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1158 | } |
| 1159 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1160 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1161 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
| 1162 | // been re-enabled (after being updated for example), then we just overwrite the old |
| 1163 | // values. |
| 1164 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
| 1165 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
| 1166 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1167 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1168 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
| 1169 | try { |
| 1170 | accounts.accountCache.clear(); |
| 1171 | final HashMap<String, ArrayList<String>> accountNamesByType |
| 1172 | = new LinkedHashMap<>(); |
| 1173 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1174 | final long accountId = accountEntry.getKey(); |
| 1175 | final Account account = accountEntry.getValue(); |
| 1176 | if (obsoleteAuthType.contains(account.type)) { |
| 1177 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 1178 | + account.type |
| 1179 | + "'s registered authenticator no longer exist."); |
| 1180 | Map<String, Integer> packagesToVisibility = |
| 1181 | getRequestingPackages(account, accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1182 | List<String> accountRemovedReceivers = |
| 1183 | getAccountRemovedReceivers(account, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1184 | accountsDb.beginTransaction(); |
| 1185 | try { |
| 1186 | accountsDb.deleteDeAccount(accountId); |
| 1187 | // Also delete from CE table if user is unlocked; if user is |
| 1188 | // currently locked the account will be removed later by |
| 1189 | // syncDeCeAccountsLocked |
| 1190 | if (userUnlocked) { |
| 1191 | accountsDb.deleteCeAccount(accountId); |
| 1192 | } |
| 1193 | accountsDb.setTransactionSuccessful(); |
| 1194 | } finally { |
| 1195 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1196 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1197 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1198 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1199 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1200 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1201 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1202 | accounts.userDataCache.remove(account); |
| 1203 | accounts.authTokenCache.remove(account); |
| 1204 | accounts.accountTokenCaches.remove(account); |
| 1205 | accounts.visibilityCache.remove(account); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1206 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1207 | for (Entry<String, Integer> packageToVisibility : |
| 1208 | packagesToVisibility.entrySet()) { |
sunjian | 066aa5e | 2017-06-05 12:16:59 -0700 | [diff] [blame] | 1209 | if (isVisible(packageToVisibility.getValue())) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1210 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 1211 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1212 | } |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 1213 | for (String packageName : accountRemovedReceivers) { |
| 1214 | sendAccountRemovedBroadcast(account, packageName, accounts.userId); |
| 1215 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1216 | } else { |
| 1217 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
| 1218 | if (accountNames == null) { |
| 1219 | accountNames = new ArrayList<>(); |
| 1220 | accountNamesByType.put(account.type, accountNames); |
| 1221 | } |
| 1222 | accountNames.add(account.name); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1223 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1224 | } |
| 1225 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
| 1226 | final String accountType = cur.getKey(); |
| 1227 | final ArrayList<String> accountNames = cur.getValue(); |
| 1228 | final Account[] accountsForType = new Account[accountNames.size()]; |
| 1229 | for (int i = 0; i < accountsForType.length; i++) { |
| 1230 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
| 1231 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1232 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1233 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1234 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1235 | accounts.visibilityCache.putAll(accountsDb.findAllVisibilityValues()); |
| 1236 | } finally { |
| 1237 | if (accountDeleted) { |
| 1238 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1239 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1240 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1241 | } |
| 1242 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1245 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1246 | // Get the UIDs of all apps that might have data on the device. We want |
| 1247 | // to preserve user data if the app might otherwise be storing data. |
| 1248 | List<PackageInfo> pkgsWithData = |
| 1249 | mPackageManager.getInstalledPackagesAsUser( |
| 1250 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1251 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1252 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1253 | if (pkgInfo.applicationInfo != null |
| 1254 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1255 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1256 | } |
| 1257 | } |
| 1258 | return knownUids; |
| 1259 | } |
| 1260 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1261 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1262 | Context context, |
| 1263 | int userId) { |
| 1264 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1265 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1266 | } |
| 1267 | |
| 1268 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1269 | IAccountAuthenticatorCache authCache, |
| 1270 | int userId) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 1271 | HashMap<String, Integer> knownAuth = new LinkedHashMap<>(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1272 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1273 | .getAllServices(userId)) { |
| 1274 | knownAuth.put(service.type.type, service.uid); |
| 1275 | } |
| 1276 | return knownAuth; |
| 1277 | } |
| 1278 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1279 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1280 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | protected UserAccounts getUserAccounts(int userId) { |
| 1284 | synchronized (mUsers) { |
| 1285 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1286 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1287 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1288 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1289 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1290 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1291 | initializeDebugDbSizeAndCompileSqlStatementForLogging(accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1292 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1293 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1294 | validateAccounts = true; |
| 1295 | } |
| 1296 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1297 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1298 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1299 | synchronized (accounts.dbLock) { |
| 1300 | synchronized (accounts.cacheLock) { |
| 1301 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
| 1302 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
| 1303 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1304 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1305 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1306 | } |
| 1307 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1308 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1309 | } |
| 1310 | return accounts; |
| 1311 | } |
| 1312 | } |
| 1313 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1314 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1315 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1316 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1317 | if (!accountsToRemove.isEmpty()) { |
| 1318 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1319 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1320 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1321 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1322 | |
| 1323 | for (Account account : accountsToRemove) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1324 | removeAccountInternal(accounts, account, Process.SYSTEM_UID); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | } |
| 1328 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1329 | private void purgeOldGrantsAll() { |
| 1330 | synchronized (mUsers) { |
| 1331 | for (int i = 0; i < mUsers.size(); i++) { |
| 1332 | purgeOldGrants(mUsers.valueAt(i)); |
| 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | private void purgeOldGrants(UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1338 | synchronized (accounts.dbLock) { |
| 1339 | synchronized (accounts.cacheLock) { |
| 1340 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
| 1341 | for (int uid : uids) { |
| 1342 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1343 | if (packageExists) { |
| 1344 | continue; |
| 1345 | } |
| 1346 | Log.d(TAG, "deleting grants for UID " + uid |
| 1347 | + " because its package is no longer installed"); |
| 1348 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1349 | } |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1354 | private void removeVisibilityValuesForPackage(String packageName) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1355 | if (isSpecialPackageKey(packageName)) { |
| 1356 | return; |
| 1357 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1358 | synchronized (mUsers) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1359 | int numberOfUsers = mUsers.size(); |
| 1360 | for (int i = 0; i < numberOfUsers; i++) { |
| 1361 | UserAccounts accounts = mUsers.valueAt(i); |
| 1362 | try { |
| 1363 | mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 1364 | } catch (NameNotFoundException e) { |
| 1365 | // package does not exist - remove visibility values |
| 1366 | accounts.accountsDb.deleteAccountVisibilityForPackage(packageName); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1367 | synchronized (accounts.dbLock) { |
| 1368 | synchronized (accounts.cacheLock) { |
| 1369 | for (Account account : accounts.visibilityCache.keySet()) { |
| 1370 | Map<String, Integer> accountVisibility = |
| 1371 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 1372 | accountVisibility.remove(packageName); |
| 1373 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1374 | } |
| 1375 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | |
Fyodor Kupolov | 945c97e | 2017-06-21 17:45:19 -0700 | [diff] [blame] | 1381 | private void purgeUserData(int userId) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1382 | UserAccounts accounts; |
| 1383 | synchronized (mUsers) { |
| 1384 | accounts = mUsers.get(userId); |
| 1385 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1386 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1387 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1388 | if (accounts != null) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1389 | synchronized (accounts.dbLock) { |
| 1390 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 56e158f | 2017-05-23 16:41:51 -0700 | [diff] [blame] | 1391 | accounts.statementForLogging.close(); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1392 | accounts.accountsDb.close(); |
| 1393 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1394 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1395 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1398 | @VisibleForTesting |
| 1399 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1400 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1401 | } |
| 1402 | |
| 1403 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1404 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1405 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1406 | } |
| 1407 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1408 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1409 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1410 | if (userId < 1) return; |
Fyodor Kupolov | 2bc895d | 2017-12-19 11:53:47 -0800 | [diff] [blame] | 1411 | mHandler.post(() -> syncSharedAccounts(userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1412 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1413 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1414 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1415 | // Check if there's a shared account that needs to be created as an account |
| 1416 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1417 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1418 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1419 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1420 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1421 | : UserHandle.USER_SYSTEM; |
| 1422 | if (parentUserId < 0) { |
| 1423 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1424 | return; |
| 1425 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1426 | for (Account sa : sharedAccounts) { |
| 1427 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1428 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1429 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1433 | @Override |
| 1434 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1435 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1438 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1439 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1440 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1441 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1442 | Log.v(TAG, "getPassword: " + account |
| 1443 | + ", caller's uid " + Binder.getCallingUid() |
| 1444 | + ", pid " + Binder.getCallingPid()); |
| 1445 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1446 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1447 | int userId = UserHandle.getCallingUserId(); |
| 1448 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1449 | String msg = String.format( |
| 1450 | "uid %s cannot get secrets for accounts of type: %s", |
| 1451 | callingUid, |
| 1452 | account.type); |
| 1453 | throw new SecurityException(msg); |
| 1454 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1455 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1456 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1457 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1458 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1459 | } finally { |
| 1460 | restoreCallingIdentity(identityToken); |
| 1461 | } |
| 1462 | } |
| 1463 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1464 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1465 | if (account == null) { |
| 1466 | return null; |
| 1467 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1468 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1469 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1470 | return null; |
| 1471 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1472 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1473 | synchronized (accounts.dbLock) { |
| 1474 | synchronized (accounts.cacheLock) { |
| 1475 | return accounts.accountsDb |
| 1476 | .findAccountPasswordByNameAndType(account.name, account.type); |
| 1477 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1481 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1482 | public String getPreviousName(Account account) { |
| 1483 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1484 | Log.v(TAG, "getPreviousName: " + account |
| 1485 | + ", caller's uid " + Binder.getCallingUid() |
| 1486 | + ", pid " + Binder.getCallingPid()); |
| 1487 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1488 | Preconditions.checkNotNull(account, "account cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1489 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1490 | long identityToken = clearCallingIdentity(); |
| 1491 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1492 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1493 | return readPreviousNameInternal(accounts, account); |
| 1494 | } finally { |
| 1495 | restoreCallingIdentity(identityToken); |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1500 | if (account == null) { |
| 1501 | return null; |
| 1502 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1503 | synchronized (accounts.dbLock) { |
| 1504 | synchronized (accounts.cacheLock) { |
| 1505 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1506 | if (previousNameRef == null) { |
| 1507 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
| 1508 | previousNameRef = new AtomicReference<>(previousName); |
| 1509 | accounts.previousNameCache.put(account, previousNameRef); |
| 1510 | return previousName; |
| 1511 | } else { |
| 1512 | return previousNameRef.get(); |
| 1513 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1519 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1520 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1521 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1522 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1523 | account, key, callingUid, Binder.getCallingPid()); |
| 1524 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1525 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1526 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 1527 | Preconditions.checkNotNull(key, "key cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1528 | int userId = UserHandle.getCallingUserId(); |
| 1529 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1530 | String msg = String.format( |
| 1531 | "uid %s cannot get user data for accounts of type: %s", |
| 1532 | callingUid, |
| 1533 | account.type); |
| 1534 | throw new SecurityException(msg); |
| 1535 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1536 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1537 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1538 | return null; |
| 1539 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1540 | long identityToken = clearCallingIdentity(); |
| 1541 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1542 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1543 | if (!accountExistsCache(accounts, account)) { |
| 1544 | return null; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1545 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1546 | return readUserDataInternal(accounts, account, key); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1547 | } finally { |
| 1548 | restoreCallingIdentity(identityToken); |
| 1549 | } |
| 1550 | } |
| 1551 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1552 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1553 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1554 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1555 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1556 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1557 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1558 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1559 | + ", pid " + Binder.getCallingPid()); |
| 1560 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1561 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1562 | if (isCrossUser(callingUid, userId)) { |
| 1563 | throw new SecurityException( |
| 1564 | String.format( |
| 1565 | "User %s tying to get authenticator types for %s" , |
| 1566 | UserHandle.getCallingUserId(), |
| 1567 | userId)); |
| 1568 | } |
| 1569 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1570 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1571 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1572 | return getAuthenticatorTypesInternal(userId); |
| 1573 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1574 | } finally { |
| 1575 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1576 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1579 | /** |
| 1580 | * Should only be called inside of a clearCallingIdentity block. |
| 1581 | */ |
| 1582 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1583 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1584 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1585 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1586 | AuthenticatorDescription[] types = |
| 1587 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1588 | int i = 0; |
| 1589 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1590 | : authenticatorCollection) { |
| 1591 | types[i] = authenticator.type; |
| 1592 | i++; |
| 1593 | } |
| 1594 | return types; |
| 1595 | } |
| 1596 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1597 | private boolean isCrossUser(int callingUid, int userId) { |
| 1598 | return (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1599 | && callingUid != Process.SYSTEM_UID |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1600 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1601 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1602 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1605 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1606 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1607 | return addAccountExplicitlyWithVisibility(account, password, extras, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1608 | } |
| 1609 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1610 | @Override |
| 1611 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1612 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1613 | int callingUid = Binder.getCallingUid(); |
| 1614 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1615 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1616 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1617 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1618 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1619 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1620 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1621 | if (response != null) { |
| 1622 | Bundle result = new Bundle(); |
| 1623 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1624 | try { |
| 1625 | response.onResult(result); |
| 1626 | } catch (RemoteException e) { |
| 1627 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1628 | } |
| 1629 | } |
| 1630 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1633 | Slog.d(TAG, "Copying account " + account.name |
| 1634 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1635 | long identityToken = clearCallingIdentity(); |
| 1636 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1637 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1638 | false /* stripAuthTokenFromResult */, account.name, |
| 1639 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1640 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1641 | protected String toDebugString(long now) { |
| 1642 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1643 | + ", " + account.type; |
| 1644 | } |
| 1645 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1646 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1647 | public void run() throws RemoteException { |
| 1648 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1649 | } |
| 1650 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1651 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1652 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1653 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1654 | if (result != null |
| 1655 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1656 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1657 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1658 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1659 | super.onResult(result); |
| 1660 | } |
| 1661 | } |
| 1662 | }.bind(); |
| 1663 | } finally { |
| 1664 | restoreCallingIdentity(identityToken); |
| 1665 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1666 | } |
| 1667 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1668 | @Override |
| 1669 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1670 | final int callingUid = Binder.getCallingUid(); |
| 1671 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1672 | String msg = String.format( |
| 1673 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1674 | account, |
| 1675 | callingUid); |
| 1676 | Log.v(TAG, msg); |
| 1677 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1678 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1679 | int userId = UserHandle.getCallingUserId(); |
| 1680 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1681 | String msg = String.format( |
| 1682 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1683 | callingUid, |
| 1684 | account.type); |
| 1685 | throw new SecurityException(msg); |
| 1686 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1687 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1688 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1689 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1690 | return false; |
| 1691 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1692 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1693 | long identityToken = clearCallingIdentity(); |
| 1694 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1695 | UserAccounts accounts = getUserAccounts(userId); |
| 1696 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1697 | } finally { |
| 1698 | restoreCallingIdentity(identityToken); |
| 1699 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1703 | final UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1704 | synchronized (accounts.dbLock) { |
| 1705 | synchronized (accounts.cacheLock) { |
| 1706 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
| 1707 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1708 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1709 | } |
| 1710 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1711 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1712 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1713 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1714 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1715 | long id = clearCallingIdentity(); |
| 1716 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1717 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1718 | false /* stripAuthTokenFromResult */, account.name, |
| 1719 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1720 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1721 | protected String toDebugString(long now) { |
| 1722 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1723 | + ", " + account.type; |
| 1724 | } |
| 1725 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1726 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1727 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1728 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 16bedd4 | 2017-03-30 10:00:49 -0700 | [diff] [blame] | 1729 | for (Account acc : getAccounts(parentUserId, mContext.getOpPackageName())) { |
| 1730 | if (acc.equals(account)) { |
| 1731 | mAuthenticator.addAccountFromCredentials( |
| 1732 | this, account, accountCredentials); |
| 1733 | break; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1734 | } |
| 1735 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 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 onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1740 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1741 | // TODO: Anything to do if if succedded? |
| 1742 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1743 | // account to avoid retries? |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1744 | // TODO: what we do with the visibility? |
| 1745 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1746 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1747 | } |
| 1748 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1749 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1750 | public void onError(int errorCode, String errorMessage) { |
| 1751 | super.onError(errorCode, errorMessage); |
| 1752 | // TODO: Show error notification to user |
| 1753 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1754 | } |
| 1755 | |
| 1756 | }.bind(); |
| 1757 | } finally { |
| 1758 | restoreCallingIdentity(id); |
| 1759 | } |
| 1760 | } |
| 1761 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1762 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1763 | Bundle extras, int callingUid, Map<String, Integer> packageToVisibility) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1764 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1765 | if (account == null) { |
| 1766 | return false; |
| 1767 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1768 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1769 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1770 | + " is locked. callingUid=" + callingUid); |
| 1771 | return false; |
| 1772 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1773 | synchronized (accounts.dbLock) { |
| 1774 | synchronized (accounts.cacheLock) { |
| 1775 | accounts.accountsDb.beginTransaction(); |
| 1776 | try { |
| 1777 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
| 1778 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1779 | + ", skipping since the account already exists"); |
| 1780 | return false; |
| 1781 | } |
| 1782 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
| 1783 | if (accountId < 0) { |
| 1784 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1785 | + ", skipping the DB insert failed"); |
| 1786 | return false; |
| 1787 | } |
| 1788 | // Insert into DE table |
| 1789 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
| 1790 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1791 | + ", skipping the DB insert failed"); |
| 1792 | return false; |
| 1793 | } |
| 1794 | if (extras != null) { |
| 1795 | for (String key : extras.keySet()) { |
| 1796 | final String value = extras.getString(key); |
| 1797 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
| 1798 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1799 | + ", skipping since insertExtra failed for key " + key); |
| 1800 | return false; |
| 1801 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1802 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1803 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1804 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1805 | if (packageToVisibility != null) { |
| 1806 | for (Entry<String, Integer> entry : packageToVisibility.entrySet()) { |
| 1807 | setAccountVisibility(account, entry.getKey() /* package */, |
| 1808 | entry.getValue() /* visibility */, false /* notify */, |
| 1809 | accounts); |
| 1810 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1811 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1812 | accounts.accountsDb.setTransactionSuccessful(); |
| 1813 | |
| 1814 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 1815 | accountId, |
| 1816 | accounts, callingUid); |
| 1817 | |
| 1818 | insertAccountIntoCacheLocked(accounts, account); |
| 1819 | } finally { |
| 1820 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1821 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1822 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1823 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1824 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1825 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1826 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1827 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1828 | sendNotificationAccountUpdated(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1829 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1830 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1831 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1832 | return true; |
| 1833 | } |
| 1834 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1835 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1836 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1837 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1841 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1842 | * 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] | 1843 | * running, then clone the account too. |
| 1844 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1845 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1846 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1847 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1848 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1849 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1850 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1851 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1852 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1853 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1854 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1855 | } |
| 1856 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1860 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1861 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1862 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1863 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1864 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1865 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1866 | Log.v(TAG, "hasFeatures: " + account |
| 1867 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1868 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1869 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1870 | + ", pid " + Binder.getCallingPid()); |
| 1871 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1872 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1873 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1874 | Preconditions.checkArgument(features != null, "features cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1875 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1876 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1877 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1878 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1879 | long identityToken = clearCallingIdentity(); |
| 1880 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1881 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1882 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1883 | } finally { |
| 1884 | restoreCallingIdentity(identityToken); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | private class TestFeaturesSession extends Session { |
| 1889 | private final String[] mFeatures; |
| 1890 | private final Account mAccount; |
| 1891 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1892 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1893 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1894 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1895 | true /* stripAuthTokenFromResult */, account.name, |
| 1896 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1897 | mFeatures = features; |
| 1898 | mAccount = account; |
| 1899 | } |
| 1900 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1901 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1902 | public void run() throws RemoteException { |
| 1903 | try { |
| 1904 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1905 | } catch (RemoteException e) { |
| 1906 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1907 | } |
| 1908 | } |
| 1909 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1910 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1911 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1912 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1913 | IAccountManagerResponse response = getResponseAndClose(); |
| 1914 | if (response != null) { |
| 1915 | try { |
| 1916 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1917 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1918 | return; |
| 1919 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1920 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1921 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1922 | + response); |
| 1923 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1924 | final Bundle newResult = new Bundle(); |
| 1925 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1926 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1927 | response.onResult(newResult); |
| 1928 | } catch (RemoteException e) { |
| 1929 | // if the caller is dead then there is no one to care about remote exceptions |
| 1930 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1931 | Log.v(TAG, "failure while notifying response", e); |
| 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | } |
| 1936 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1937 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1938 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1939 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1940 | + ", " + mAccount |
| 1941 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1942 | } |
| 1943 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1944 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1945 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1946 | public void renameAccount( |
| 1947 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1948 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1949 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1950 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1951 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1952 | + ", pid " + Binder.getCallingPid()); |
| 1953 | } |
| 1954 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1955 | int userId = UserHandle.getCallingUserId(); |
| 1956 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1957 | String msg = String.format( |
| 1958 | "uid %s cannot rename accounts of type: %s", |
| 1959 | callingUid, |
| 1960 | accountToRename.type); |
| 1961 | throw new SecurityException(msg); |
| 1962 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1963 | long identityToken = clearCallingIdentity(); |
| 1964 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1965 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1966 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1967 | Bundle result = new Bundle(); |
| 1968 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1969 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1970 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1971 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1972 | try { |
| 1973 | response.onResult(result); |
| 1974 | } catch (RemoteException e) { |
| 1975 | Log.w(TAG, e.getMessage()); |
| 1976 | } |
| 1977 | } finally { |
| 1978 | restoreCallingIdentity(identityToken); |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1983 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1984 | Account resultAccount = null; |
| 1985 | /* |
| 1986 | * Cancel existing notifications. Let authenticators |
| 1987 | * re-post notifications as required. But we don't know if |
| 1988 | * the authenticators have bound their notifications to |
| 1989 | * now stale account name data. |
| 1990 | * |
| 1991 | * With a rename api, we might not need to do this anymore but it |
| 1992 | * shouldn't hurt. |
| 1993 | */ |
| 1994 | cancelNotification( |
| 1995 | getSigninRequiredNotificationId(accounts, accountToRename), |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 1996 | new UserHandle(accounts.userId)); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1997 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1998 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1999 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2000 | if (accountToRename.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2001 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2002 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 2003 | } |
| 2004 | } |
| 2005 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2006 | synchronized (accounts.dbLock) { |
| 2007 | synchronized (accounts.cacheLock) { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2008 | List<String> accountRemovedReceivers = |
| 2009 | getAccountRemovedReceivers(accountToRename, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2010 | accounts.accountsDb.beginTransaction(); |
| 2011 | Account renamedAccount = new Account(newName, accountToRename.type); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2012 | try { |
chengang | f2d081c | 2017-12-27 17:17:32 +0800 | [diff] [blame] | 2013 | if ((accounts.accountsDb.findCeAccountId(renamedAccount) >= 0)) { |
| 2014 | Log.e(TAG, "renameAccount failed - account with new name already exists"); |
| 2015 | return null; |
| 2016 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2017 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
| 2018 | if (accountId >= 0) { |
| 2019 | accounts.accountsDb.renameCeAccount(accountId, newName); |
| 2020 | if (accounts.accountsDb.renameDeAccount( |
| 2021 | accountId, newName, accountToRename.name)) { |
| 2022 | accounts.accountsDb.setTransactionSuccessful(); |
| 2023 | } else { |
| 2024 | Log.e(TAG, "renameAccount failed"); |
| 2025 | return null; |
| 2026 | } |
| 2027 | } else { |
| 2028 | Log.e(TAG, "renameAccount failed - old account does not exist"); |
| 2029 | return null; |
| 2030 | } |
| 2031 | } finally { |
| 2032 | accounts.accountsDb.endTransaction(); |
| 2033 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2034 | /* |
| 2035 | * Database transaction was successful. Clean up cached |
| 2036 | * data associated with the account in the user profile. |
| 2037 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2038 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2039 | /* |
| 2040 | * Extract the data and token caches before removing the |
| 2041 | * old account to preserve the user data associated with |
| 2042 | * the account. |
| 2043 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2044 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 2045 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
| 2046 | Map<String, Integer> tmpVisibility = accounts.visibilityCache.get(accountToRename); |
| 2047 | removeAccountFromCacheLocked(accounts, accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2048 | /* |
| 2049 | * Update the cached data associated with the renamed |
| 2050 | * account. |
| 2051 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2052 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 2053 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 2054 | accounts.visibilityCache.put(renamedAccount, tmpVisibility); |
| 2055 | accounts.previousNameCache.put( |
| 2056 | renamedAccount, |
| 2057 | new AtomicReference<>(accountToRename.name)); |
| 2058 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2059 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2060 | int parentUserId = accounts.userId; |
| 2061 | if (canHaveProfile(parentUserId)) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2062 | /* |
| 2063 | * Owner or system user account was renamed, rename the account for |
| 2064 | * those users with which the account was shared. |
| 2065 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2066 | List<UserInfo> users = getUserManager().getUsers(true); |
| 2067 | for (UserInfo user : users) { |
| 2068 | if (user.isRestricted() |
| 2069 | && (user.restrictedProfileParentId == parentUserId)) { |
| 2070 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
| 2071 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2072 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2073 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2074 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2075 | sendNotificationAccountUpdated(resultAccount, accounts); |
| 2076 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2077 | for (String packageName : accountRemovedReceivers) { |
| 2078 | sendAccountRemovedBroadcast(accountToRename, packageName, accounts.userId); |
| 2079 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2080 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2081 | } |
| 2082 | return resultAccount; |
| 2083 | } |
| 2084 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2085 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2086 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2087 | return userInfo != null && userInfo.canHaveProfile(); |
| 2088 | } |
| 2089 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 2090 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2091 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 2092 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2093 | removeAccountAsUser( |
| 2094 | response, |
| 2095 | account, |
| 2096 | expectActivityLaunch, |
| 2097 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | @Override |
| 2101 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2102 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2103 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2104 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2105 | Log.v(TAG, "removeAccount: " + account |
| 2106 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2107 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2108 | + ", pid " + Binder.getCallingPid() |
| 2109 | + ", for user id " + userId); |
| 2110 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2111 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 2112 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 2113 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2114 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2115 | if (isCrossUser(callingUid, userId)) { |
| 2116 | throw new SecurityException( |
| 2117 | String.format( |
| 2118 | "User %s tying remove account for %s" , |
| 2119 | UserHandle.getCallingUserId(), |
| 2120 | userId)); |
| 2121 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2122 | /* |
Alex Chau | f788f9c | 2017-12-08 15:16:46 +0000 | [diff] [blame] | 2123 | * Only the system, authenticator or profile owner should be allowed to remove accounts for |
| 2124 | * that authenticator. This will let users remove accounts (via Settings in the system) but |
| 2125 | * not arbitrary applications (like competing authenticators). |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2126 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2127 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 2128 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
Alex Chau | f788f9c | 2017-12-08 15:16:46 +0000 | [diff] [blame] | 2129 | && !isSystemUid(callingUid) |
| 2130 | && !isProfileOwner(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2131 | String msg = String.format( |
| 2132 | "uid %s cannot remove accounts of type: %s", |
| 2133 | callingUid, |
| 2134 | account.type); |
| 2135 | throw new SecurityException(msg); |
| 2136 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2137 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2138 | try { |
| 2139 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2140 | "User cannot modify accounts"); |
| 2141 | } catch (RemoteException re) { |
| 2142 | } |
| 2143 | return; |
| 2144 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2145 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2146 | try { |
| 2147 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2148 | "User cannot modify accounts of this type (policy)."); |
| 2149 | } catch (RemoteException re) { |
| 2150 | } |
| 2151 | return; |
| 2152 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2153 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2154 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2155 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2156 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2157 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2158 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2159 | if (account.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2160 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2161 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2162 | } |
| 2163 | } |
| 2164 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2165 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2166 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2167 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2168 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2169 | accountId, |
| 2170 | accounts, |
| 2171 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2172 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2173 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2174 | } finally { |
| 2175 | restoreCallingIdentity(identityToken); |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | @Override |
| 2180 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2181 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2182 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2183 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2184 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2185 | + ", pid " + Binder.getCallingPid()); |
| 2186 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2187 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2188 | if (account == null) { |
| 2189 | /* |
| 2190 | * Null accounts should result in returning false, as per |
| 2191 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2192 | */ |
| 2193 | Log.e(TAG, "account is null"); |
| 2194 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2195 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2196 | String msg = String.format( |
Brandon Weeks | 9e4e96d | 2017-08-24 15:24:16 -0700 | [diff] [blame] | 2197 | "uid %s cannot explicitly remove accounts of type: %s", |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2198 | callingUid, |
| 2199 | account.type); |
| 2200 | throw new SecurityException(msg); |
| 2201 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2202 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2203 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2204 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2205 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2206 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2207 | accountId, |
| 2208 | accounts, |
| 2209 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2210 | long identityToken = clearCallingIdentity(); |
| 2211 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2212 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2213 | } finally { |
| 2214 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2215 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2218 | private class RemoveAccountSession extends Session { |
| 2219 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2220 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2221 | Account account, boolean expectActivityLaunch) { |
| 2222 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2223 | true /* stripAuthTokenFromResult */, account.name, |
| 2224 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2225 | mAccount = account; |
| 2226 | } |
| 2227 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2228 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2229 | protected String toDebugString(long now) { |
| 2230 | return super.toDebugString(now) + ", removeAccount" |
| 2231 | + ", account " + mAccount; |
| 2232 | } |
| 2233 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2234 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2235 | public void run() throws RemoteException { |
| 2236 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2237 | } |
| 2238 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2239 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2240 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2241 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2242 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2243 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2244 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2245 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2246 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2247 | } |
| 2248 | IAccountManagerResponse response = getResponseAndClose(); |
| 2249 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2250 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2251 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2252 | + response); |
| 2253 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2254 | try { |
tiansiming | 5330b5a | 2017-10-13 10:57:25 +0800 | [diff] [blame] | 2255 | response.onResult(result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2256 | } catch (RemoteException e) { |
tiansiming | 5330b5a | 2017-10-13 10:57:25 +0800 | [diff] [blame] | 2257 | Slog.e(TAG, "Error calling onResult()", e); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | super.onResult(result); |
| 2262 | } |
| 2263 | } |
| 2264 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2265 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2266 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2267 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2268 | } |
| 2269 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2270 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2271 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2272 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2273 | if (!userUnlocked) { |
| 2274 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2275 | + " is still locked. CE data will be removed later"); |
| 2276 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2277 | synchronized (accounts.dbLock) { |
| 2278 | synchronized (accounts.cacheLock) { |
| 2279 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, |
| 2280 | accounts); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2281 | List<String> accountRemovedReceivers = |
| 2282 | getAccountRemovedReceivers(account, accounts); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2283 | accounts.accountsDb.beginTransaction(); |
| 2284 | // Set to a dummy value, this will only be used if the database |
| 2285 | // transaction succeeds. |
| 2286 | long accountId = -1; |
| 2287 | try { |
| 2288 | accountId = accounts.accountsDb.findDeAccountId(account); |
| 2289 | if (accountId >= 0) { |
| 2290 | isChanged = accounts.accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2291 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2292 | // always delete from CE table if CE storage is available |
| 2293 | // DE account could be removed while CE was locked |
| 2294 | if (userUnlocked) { |
| 2295 | long ceAccountId = accounts.accountsDb.findCeAccountId(account); |
| 2296 | if (ceAccountId >= 0) { |
| 2297 | accounts.accountsDb.deleteCeAccount(ceAccountId); |
| 2298 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2299 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2300 | accounts.accountsDb.setTransactionSuccessful(); |
| 2301 | } finally { |
| 2302 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2303 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2304 | if (isChanged) { |
| 2305 | removeAccountFromCacheLocked(accounts, account); |
| 2306 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 2307 | .entrySet()) { |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2308 | if ((packageToVisibility.getValue() == AccountManager.VISIBILITY_VISIBLE) |
| 2309 | || (packageToVisibility.getValue() |
| 2310 | == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE)) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2311 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 2312 | } |
| 2313 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2314 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2315 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occurred. |
| 2316 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | b6a7dc0 | 2017-04-18 13:43:31 -0700 | [diff] [blame] | 2317 | for (String packageName : accountRemovedReceivers) { |
| 2318 | sendAccountRemovedBroadcast(account, packageName, accounts.userId); |
| 2319 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2320 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2321 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2322 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
| 2323 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2324 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2325 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2326 | long id = Binder.clearCallingIdentity(); |
| 2327 | try { |
| 2328 | int parentUserId = accounts.userId; |
| 2329 | if (canHaveProfile(parentUserId)) { |
| 2330 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2331 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2332 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2333 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2334 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2335 | } |
| 2336 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2337 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2338 | } finally { |
| 2339 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2340 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2341 | |
| 2342 | if (isChanged) { |
| 2343 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2344 | for (Pair<Pair<Account, String>, Integer> key |
| 2345 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2346 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2347 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2348 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2349 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2350 | account, uid, false)); |
| 2351 | } |
| 2352 | } |
| 2353 | } |
| 2354 | } |
| 2355 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2356 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2359 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2360 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2361 | int callerUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2362 | Preconditions.checkNotNull(accountType, "accountType cannot be null"); |
| 2363 | Preconditions.checkNotNull(authToken, "authToken cannot be null"); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2364 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2365 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2366 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2367 | + ", pid " + Binder.getCallingPid()); |
| 2368 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2369 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2370 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2371 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2372 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2373 | List<Pair<Account, String>> deletedTokens; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2374 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2375 | accounts.accountsDb.beginTransaction(); |
| 2376 | try { |
| 2377 | deletedTokens = invalidateAuthTokenLocked(accounts, accountType, authToken); |
| 2378 | accounts.accountsDb.setTransactionSuccessful(); |
| 2379 | } finally { |
| 2380 | accounts.accountsDb.endTransaction(); |
| 2381 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2382 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2383 | for (Pair<Account, String> tokenInfo : deletedTokens) { |
| 2384 | Account act = tokenInfo.first; |
| 2385 | String tokenType = tokenInfo.second; |
| 2386 | writeAuthTokenIntoCacheLocked(accounts, act, tokenType, null); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2387 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2388 | // wipe out cached token in memory. |
| 2389 | accounts.accountTokenCaches.remove(accountType, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2390 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2391 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2392 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2393 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2397 | private List<Pair<Account, String>> invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2398 | String authToken) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2399 | // TODO Move to AccountsDB |
| 2400 | List<Pair<Account, String>> results = new ArrayList<>(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2401 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2402 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2403 | try { |
| 2404 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2405 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2406 | String accountName = cursor.getString(1); |
| 2407 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2408 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2409 | results.add(Pair.create(new Account(accountName, accountType), authTokenType)); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2410 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2411 | } finally { |
| 2412 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2413 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2414 | return results; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2415 | } |
| 2416 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2417 | private void saveCachedToken( |
| 2418 | UserAccounts accounts, |
| 2419 | Account account, |
| 2420 | String callerPkg, |
| 2421 | byte[] callerSigDigest, |
| 2422 | String tokenType, |
| 2423 | String token, |
| 2424 | long expiryMillis) { |
| 2425 | |
| 2426 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2427 | return; |
| 2428 | } |
| 2429 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2430 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2431 | synchronized (accounts.cacheLock) { |
| 2432 | accounts.accountTokenCaches.put( |
| 2433 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2437 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2438 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2439 | if (account == null || type == null) { |
| 2440 | return false; |
| 2441 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2442 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2443 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2444 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2445 | accounts.accountsDb.beginTransaction(); |
| 2446 | boolean updateCache = false; |
| 2447 | try { |
| 2448 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2449 | if (accountId < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2450 | return false; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2451 | } |
| 2452 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2453 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2454 | accounts.accountsDb.setTransactionSuccessful(); |
| 2455 | updateCache = true; |
| 2456 | return true; |
| 2457 | } |
| 2458 | return false; |
| 2459 | } finally { |
| 2460 | accounts.accountsDb.endTransaction(); |
| 2461 | if (updateCache) { |
| 2462 | synchronized (accounts.cacheLock) { |
| 2463 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
| 2464 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2465 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2466 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2467 | } |
| 2468 | } |
| 2469 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2470 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2471 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2472 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2473 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2474 | Log.v(TAG, "peekAuthToken: " + account |
| 2475 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2476 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2477 | + ", pid " + Binder.getCallingPid()); |
| 2478 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2479 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2480 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2481 | int userId = UserHandle.getCallingUserId(); |
| 2482 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2483 | String msg = String.format( |
| 2484 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2485 | callingUid, |
| 2486 | account.type); |
| 2487 | throw new SecurityException(msg); |
| 2488 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2489 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2490 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2491 | + callingUid); |
| 2492 | return null; |
| 2493 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2494 | long identityToken = clearCallingIdentity(); |
| 2495 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2496 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2497 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2498 | } finally { |
| 2499 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2500 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2503 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2504 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2505 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2506 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2507 | Log.v(TAG, "setAuthToken: " + account |
| 2508 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2509 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2510 | + ", pid " + Binder.getCallingPid()); |
| 2511 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2512 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2513 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2514 | int userId = UserHandle.getCallingUserId(); |
| 2515 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2516 | String msg = String.format( |
| 2517 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2518 | callingUid, |
| 2519 | account.type); |
| 2520 | throw new SecurityException(msg); |
| 2521 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2522 | long identityToken = clearCallingIdentity(); |
| 2523 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2524 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2525 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2526 | } finally { |
| 2527 | restoreCallingIdentity(identityToken); |
| 2528 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2529 | } |
| 2530 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2531 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2532 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2533 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2534 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2535 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2536 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2537 | + ", pid " + Binder.getCallingPid()); |
| 2538 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2539 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2540 | int userId = UserHandle.getCallingUserId(); |
| 2541 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2542 | String msg = String.format( |
| 2543 | "uid %s cannot set secrets for accounts of type: %s", |
| 2544 | callingUid, |
| 2545 | account.type); |
| 2546 | throw new SecurityException(msg); |
| 2547 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2548 | long identityToken = clearCallingIdentity(); |
| 2549 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2550 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2551 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2552 | } finally { |
| 2553 | restoreCallingIdentity(identityToken); |
| 2554 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2557 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2558 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2559 | if (account == null) { |
| 2560 | return; |
| 2561 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2562 | boolean isChanged = false; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2563 | synchronized (accounts.dbLock) { |
| 2564 | synchronized (accounts.cacheLock) { |
| 2565 | accounts.accountsDb.beginTransaction(); |
| 2566 | try { |
| 2567 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2568 | if (accountId >= 0) { |
| 2569 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2570 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
| 2571 | accounts.authTokenCache.remove(account); |
| 2572 | accounts.accountTokenCaches.remove(account); |
| 2573 | accounts.accountsDb.setTransactionSuccessful(); |
| 2574 | // If there is an account whose password will be updated and the database |
| 2575 | // transactions succeed, then we say that a change has occured. Even if the |
| 2576 | // new password is the same as the old and there were no authtokens to |
| 2577 | // delete. |
| 2578 | isChanged = true; |
| 2579 | String action = (password == null || password.length() == 0) ? |
| 2580 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2581 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2582 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, |
| 2583 | callingUid); |
| 2584 | } |
| 2585 | } finally { |
| 2586 | accounts.accountsDb.endTransaction(); |
| 2587 | if (isChanged) { |
| 2588 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
| 2589 | sendNotificationAccountUpdated(account, accounts); |
| 2590 | sendAccountsChangedBroadcast(accounts.userId); |
| 2591 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2592 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2593 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2594 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2595 | } |
| 2596 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2597 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2598 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2599 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2600 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2601 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2602 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2603 | + ", pid " + Binder.getCallingPid()); |
| 2604 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2605 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2606 | int userId = UserHandle.getCallingUserId(); |
| 2607 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2608 | String msg = String.format( |
| 2609 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2610 | callingUid, |
| 2611 | account.type); |
| 2612 | throw new SecurityException(msg); |
| 2613 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2614 | long identityToken = clearCallingIdentity(); |
| 2615 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2616 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2617 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2618 | } finally { |
| 2619 | restoreCallingIdentity(identityToken); |
| 2620 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2623 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2624 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2625 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2626 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2627 | Log.v(TAG, "setUserData: " + account |
| 2628 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2629 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2630 | + ", pid " + Binder.getCallingPid()); |
| 2631 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2632 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2633 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2634 | int userId = UserHandle.getCallingUserId(); |
| 2635 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2636 | String msg = String.format( |
| 2637 | "uid %s cannot set user data for accounts of type: %s", |
| 2638 | callingUid, |
| 2639 | account.type); |
| 2640 | throw new SecurityException(msg); |
| 2641 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2642 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2643 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2644 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2645 | if (!accountExistsCache(accounts, account)) { |
| 2646 | return; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2647 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2648 | setUserdataInternal(accounts, account, key, value); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2649 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2650 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2654 | private boolean accountExistsCache(UserAccounts accounts, Account account) { |
| 2655 | synchronized (accounts.cacheLock) { |
| 2656 | if (accounts.accountCache.containsKey(account.type)) { |
| 2657 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2658 | if (acc.name.equals(account.name)) { |
| 2659 | return true; |
| 2660 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2661 | } |
| 2662 | } |
| 2663 | } |
| 2664 | return false; |
| 2665 | } |
| 2666 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2667 | private void setUserdataInternal(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2668 | String value) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2669 | synchronized (accounts.dbLock) { |
| 2670 | accounts.accountsDb.beginTransaction(); |
| 2671 | try { |
| 2672 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2673 | if (accountId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2674 | return; |
| 2675 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2676 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
| 2677 | if (extrasId < 0) { |
| 2678 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
| 2679 | if (extrasId < 0) { |
| 2680 | return; |
| 2681 | } |
| 2682 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
| 2683 | return; |
| 2684 | } |
| 2685 | accounts.accountsDb.setTransactionSuccessful(); |
| 2686 | } finally { |
| 2687 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2688 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2689 | synchronized (accounts.cacheLock) { |
| 2690 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2691 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2692 | } |
| 2693 | } |
| 2694 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2695 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2696 | if (result == null) { |
| 2697 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2698 | } |
| 2699 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2700 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2701 | + response); |
| 2702 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2703 | try { |
| 2704 | response.onResult(result); |
| 2705 | } catch (RemoteException e) { |
| 2706 | // if the caller is dead then there is no one to care about remote |
| 2707 | // exceptions |
| 2708 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2709 | Log.v(TAG, "failure while notifying response", e); |
| 2710 | } |
| 2711 | } |
| 2712 | } |
| 2713 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2714 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2715 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2716 | final String authTokenType) |
| 2717 | throws RemoteException { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2718 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
| 2719 | Preconditions.checkArgument(authTokenType != null, "authTokenType cannot be null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2720 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2721 | final int callingUid = getCallingUid(); |
| 2722 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2723 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2724 | throw new SecurityException("can only call from system"); |
| 2725 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2726 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2727 | long identityToken = clearCallingIdentity(); |
| 2728 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2729 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2730 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2731 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2732 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2733 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2734 | protected String toDebugString(long now) { |
| 2735 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2736 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2737 | + ", authTokenType " + authTokenType; |
| 2738 | } |
| 2739 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2740 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2741 | public void run() throws RemoteException { |
| 2742 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2743 | } |
| 2744 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2745 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2746 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2747 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2748 | if (result != null) { |
| 2749 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2750 | Bundle bundle = new Bundle(); |
| 2751 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2752 | super.onResult(bundle); |
| 2753 | return; |
| 2754 | } else { |
| 2755 | super.onResult(result); |
| 2756 | } |
| 2757 | } |
| 2758 | }.bind(); |
| 2759 | } finally { |
| 2760 | restoreCallingIdentity(identityToken); |
| 2761 | } |
| 2762 | } |
| 2763 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2764 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2765 | public void getAuthToken( |
| 2766 | IAccountManagerResponse response, |
| 2767 | final Account account, |
| 2768 | final String authTokenType, |
| 2769 | final boolean notifyOnAuthFailure, |
| 2770 | final boolean expectActivityLaunch, |
| 2771 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2772 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2773 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2774 | Log.v(TAG, "getAuthToken: " + account |
| 2775 | + ", response " + response |
| 2776 | + ", authTokenType " + authTokenType |
| 2777 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2778 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2779 | + ", caller's uid " + Binder.getCallingUid() |
| 2780 | + ", pid " + Binder.getCallingPid()); |
| 2781 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2782 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2783 | try { |
| 2784 | if (account == null) { |
| 2785 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2786 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2787 | return; |
| 2788 | } |
| 2789 | if (authTokenType == null) { |
| 2790 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2791 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2792 | return; |
| 2793 | } |
| 2794 | } catch (RemoteException e) { |
| 2795 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2796 | return; |
| 2797 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2798 | int userId = UserHandle.getCallingUserId(); |
| 2799 | long ident = Binder.clearCallingIdentity(); |
| 2800 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2801 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2802 | try { |
| 2803 | accounts = getUserAccounts(userId); |
| 2804 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2805 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2806 | } finally { |
| 2807 | Binder.restoreCallingIdentity(ident); |
| 2808 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2809 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2810 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2811 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2812 | |
| 2813 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2814 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2815 | final boolean permissionGranted = |
| 2816 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2817 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2818 | // Get the calling package. We will use it for the purpose of caching. |
| 2819 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2820 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2821 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2822 | try { |
| 2823 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2824 | } finally { |
| 2825 | Binder.restoreCallingIdentity(ident); |
| 2826 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2827 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2828 | String msg = String.format( |
| 2829 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2830 | callerUid, |
| 2831 | callerPkg); |
| 2832 | throw new SecurityException(msg); |
| 2833 | } |
| 2834 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2835 | // let authenticator know the identity of the caller |
| 2836 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2837 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2838 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2839 | if (notifyOnAuthFailure) { |
| 2840 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2841 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2842 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2843 | long identityToken = clearCallingIdentity(); |
| 2844 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2845 | // Distill the caller's package signatures into a single digest. |
| 2846 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2847 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2848 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2849 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2850 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2851 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2852 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2853 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2854 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2855 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2856 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2857 | onResult(response, result); |
| 2858 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2859 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2862 | if (customTokens) { |
| 2863 | /* |
| 2864 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2865 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2866 | * ANDORID_PACKAGE_NAME. |
| 2867 | */ |
| 2868 | String token = readCachedTokenInternal( |
| 2869 | accounts, |
| 2870 | account, |
| 2871 | authTokenType, |
| 2872 | callerPkg, |
| 2873 | callerPkgSigDigest); |
| 2874 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2875 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2876 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2877 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2878 | Bundle result = new Bundle(); |
| 2879 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2880 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2881 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2882 | onResult(response, result); |
| 2883 | return; |
| 2884 | } |
| 2885 | } |
| 2886 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2887 | new Session( |
| 2888 | accounts, |
| 2889 | response, |
| 2890 | account.type, |
| 2891 | expectActivityLaunch, |
| 2892 | false /* stripAuthTokenFromResult */, |
| 2893 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2894 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2895 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2896 | protected String toDebugString(long now) { |
| 2897 | if (loginOptions != null) loginOptions.keySet(); |
| 2898 | return super.toDebugString(now) + ", getAuthToken" |
| 2899 | + ", " + account |
| 2900 | + ", authTokenType " + authTokenType |
| 2901 | + ", loginOptions " + loginOptions |
| 2902 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2903 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2904 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2905 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2906 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2907 | // If the caller doesn't have permission then create and return the |
| 2908 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2909 | if (!permissionGranted) { |
| 2910 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2911 | } else { |
| 2912 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2913 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2914 | } |
| 2915 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2916 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2917 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2918 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2919 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2920 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2921 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2922 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2923 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2924 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2925 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2926 | authTokenType, |
| 2927 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2928 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2929 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2930 | onResult(bundle); |
| 2931 | return; |
| 2932 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2933 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2934 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2935 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2936 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2937 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2938 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2939 | "the type and name should not be empty"); |
| 2940 | return; |
| 2941 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2942 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2943 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2944 | saveAuthTokenToDatabase( |
| 2945 | mAccounts, |
| 2946 | resultAccount, |
| 2947 | authTokenType, |
| 2948 | authToken); |
| 2949 | } |
| 2950 | long expiryMillis = result.getLong( |
| 2951 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2952 | if (customTokens |
| 2953 | && expiryMillis > System.currentTimeMillis()) { |
| 2954 | saveCachedToken( |
| 2955 | mAccounts, |
| 2956 | account, |
| 2957 | callerPkg, |
| 2958 | callerPkgSigDigest, |
| 2959 | authTokenType, |
| 2960 | authToken, |
| 2961 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2962 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2963 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2964 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2965 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2966 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2967 | /* |
| 2968 | * Make sure that the supplied intent is owned by the authenticator |
| 2969 | * giving it to the system. Otherwise a malicious authenticator could |
| 2970 | * have users launching arbitrary activities by tricking users to |
| 2971 | * interact with malicious notifications. |
| 2972 | */ |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 2973 | if (!checkKeyIntent( |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2974 | Binder.getCallingUid(), |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 2975 | intent)) { |
| 2976 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 2977 | "invalid intent in bundle returned"); |
| 2978 | return; |
| 2979 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2980 | doNotification( |
| 2981 | mAccounts, |
| 2982 | account, |
| 2983 | result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2984 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2985 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2986 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2987 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2988 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2989 | }.bind(); |
| 2990 | } finally { |
| 2991 | restoreCallingIdentity(identityToken); |
| 2992 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2993 | } |
| 2994 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2995 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2996 | MessageDigest digester; |
| 2997 | try { |
| 2998 | digester = MessageDigest.getInstance("SHA-256"); |
| 2999 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 3000 | callerPkg, PackageManager.GET_SIGNATURES); |
| 3001 | for (Signature sig : pkgInfo.signatures) { |
| 3002 | digester.update(sig.toByteArray()); |
| 3003 | } |
| 3004 | } catch (NoSuchAlgorithmException x) { |
| 3005 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 3006 | digester = null; |
| 3007 | } catch (NameNotFoundException e) { |
| 3008 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 3009 | digester = null; |
| 3010 | } |
| 3011 | return (digester == null) ? null : digester.digest(); |
| 3012 | } |
| 3013 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3014 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3015 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3016 | int uid = intent.getIntExtra( |
| 3017 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 3018 | String authTokenType = intent.getStringExtra( |
| 3019 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 3020 | final String titleAndSubtitle = |
| 3021 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 3022 | account.name); |
| 3023 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 3024 | String title = titleAndSubtitle; |
| 3025 | String subtitle = ""; |
| 3026 | if (index > 0) { |
| 3027 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 3028 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 3029 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 3030 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 3031 | Context contextForUser = getContextForUser(user); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3032 | Notification n = |
| 3033 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
| 3034 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 3035 | .setWhen(0) |
| 3036 | .setColor(contextForUser.getColor( |
| 3037 | com.android.internal.R.color.system_notification_accent_color)) |
| 3038 | .setContentTitle(title) |
| 3039 | .setContentText(subtitle) |
| 3040 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 3041 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 3042 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 3043 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3044 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3045 | } |
| 3046 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3047 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 3048 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 3049 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3050 | |
| 3051 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 3052 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3053 | if (startInNewTask) { |
| 3054 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 3055 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 3056 | // the intent from a non-Activity context. This is the default behavior. |
| 3057 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3058 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3059 | intent.addCategory(getCredentialPermissionNotificationId(account, |
| 3060 | authTokenType, uid).mTag + (packageName != null ? packageName : "")); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3061 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3062 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 3063 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3064 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 3065 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3066 | return intent; |
| 3067 | } |
| 3068 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3069 | private NotificationId getCredentialPermissionNotificationId(Account account, |
| 3070 | String authTokenType, int uid) { |
| 3071 | NotificationId nId; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 3072 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3073 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3074 | final Pair<Pair<Account, String>, Integer> key = |
| 3075 | new Pair<Pair<Account, String>, Integer>( |
| 3076 | new Pair<Account, String>(account, authTokenType), uid); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3077 | nId = accounts.credentialsPermissionNotificationIds.get(key); |
| 3078 | if (nId == null) { |
| 3079 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION |
| 3080 | + ":" + account.hashCode() + ":" + authTokenType.hashCode(); |
| 3081 | int id = SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION; |
| 3082 | nId = new NotificationId(tag, id); |
| 3083 | accounts.credentialsPermissionNotificationIds.put(key, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3084 | } |
| 3085 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3086 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3089 | private NotificationId getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
| 3090 | NotificationId nId; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3091 | synchronized (accounts.signinRequiredNotificationIds) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3092 | nId = accounts.signinRequiredNotificationIds.get(account); |
| 3093 | if (nId == null) { |
| 3094 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN |
| 3095 | + ":" + account.hashCode(); |
| 3096 | int id = SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN; |
| 3097 | nId = new NotificationId(tag, id); |
| 3098 | accounts.signinRequiredNotificationIds.put(account, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3099 | } |
| 3100 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 3101 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 3102 | } |
| 3103 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3104 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3105 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3106 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3107 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3108 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3109 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3110 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3111 | + ", response " + response |
| 3112 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3113 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3114 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3115 | + ", caller's uid " + Binder.getCallingUid() |
| 3116 | + ", pid " + Binder.getCallingPid()); |
| 3117 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3118 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3119 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3120 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3121 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3122 | final int uid = Binder.getCallingUid(); |
| 3123 | final int userId = UserHandle.getUserId(uid); |
| 3124 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3125 | try { |
| 3126 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3127 | "User is not allowed to add an account!"); |
| 3128 | } catch (RemoteException re) { |
| 3129 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3130 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3131 | return; |
| 3132 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3133 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 3134 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3135 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3136 | "User cannot modify accounts of this type (policy)."); |
| 3137 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 3138 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3139 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3140 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3141 | return; |
| 3142 | } |
| 3143 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3144 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3145 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3146 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3147 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3148 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3149 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3150 | long identityToken = clearCallingIdentity(); |
| 3151 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3152 | UserAccounts accounts = getUserAccounts(usrId); |
| 3153 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3154 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3155 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3156 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3157 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3158 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3159 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3160 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 3161 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3162 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3163 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3164 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3165 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3166 | protected String toDebugString(long now) { |
| 3167 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3168 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3169 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3170 | } |
| 3171 | }.bind(); |
| 3172 | } finally { |
| 3173 | restoreCallingIdentity(identityToken); |
| 3174 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3177 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3178 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 3179 | final String authTokenType, final String[] requiredFeatures, |
| 3180 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3181 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3182 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3183 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3184 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3185 | + ", response " + response |
| 3186 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3187 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3188 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3189 | + ", caller's uid " + Binder.getCallingUid() |
| 3190 | + ", pid " + Binder.getCallingPid() |
| 3191 | + ", for user id " + userId); |
| 3192 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3193 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3194 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3195 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3196 | if (isCrossUser(callingUid, userId)) { |
| 3197 | throw new SecurityException( |
| 3198 | String.format( |
| 3199 | "User %s trying to add account for %s" , |
| 3200 | UserHandle.getCallingUserId(), |
| 3201 | userId)); |
| 3202 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3203 | |
| 3204 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3205 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3206 | try { |
| 3207 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3208 | "User is not allowed to add an account!"); |
| 3209 | } catch (RemoteException re) { |
| 3210 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3211 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3212 | return; |
| 3213 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3214 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3215 | try { |
| 3216 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3217 | "User cannot modify accounts of this type (policy)."); |
| 3218 | } catch (RemoteException re) { |
| 3219 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3220 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3221 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3222 | return; |
| 3223 | } |
| 3224 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3225 | final int pid = Binder.getCallingPid(); |
| 3226 | final int uid = Binder.getCallingUid(); |
| 3227 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3228 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3229 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3230 | |
| 3231 | long identityToken = clearCallingIdentity(); |
| 3232 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3233 | UserAccounts accounts = getUserAccounts(userId); |
| 3234 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3235 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3236 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3237 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3238 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3239 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3240 | @Override |
| 3241 | public void run() throws RemoteException { |
| 3242 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3243 | options); |
| 3244 | } |
| 3245 | |
| 3246 | @Override |
| 3247 | protected String toDebugString(long now) { |
| 3248 | return super.toDebugString(now) + ", addAccount" |
| 3249 | + ", accountType " + accountType |
| 3250 | + ", requiredFeatures " |
| 3251 | + (requiredFeatures != null |
| 3252 | ? TextUtils.join(",", requiredFeatures) |
| 3253 | : null); |
| 3254 | } |
| 3255 | }.bind(); |
| 3256 | } finally { |
| 3257 | restoreCallingIdentity(identityToken); |
| 3258 | } |
| 3259 | } |
| 3260 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3261 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3262 | public void startAddAccountSession( |
| 3263 | final IAccountManagerResponse response, |
| 3264 | final String accountType, |
| 3265 | final String authTokenType, |
| 3266 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3267 | final boolean expectActivityLaunch, |
| 3268 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3269 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3270 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3271 | Log.v(TAG, |
| 3272 | "startAddAccountSession: accountType " + accountType |
| 3273 | + ", response " + response |
| 3274 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3275 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3276 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3277 | + ", caller's uid " + Binder.getCallingUid() |
| 3278 | + ", pid " + Binder.getCallingPid()); |
| 3279 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3280 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3281 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3282 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3283 | final int uid = Binder.getCallingUid(); |
| 3284 | final int userId = UserHandle.getUserId(uid); |
| 3285 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3286 | try { |
| 3287 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3288 | "User is not allowed to add an account!"); |
| 3289 | } catch (RemoteException re) { |
| 3290 | } |
| 3291 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3292 | return; |
| 3293 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3294 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3295 | try { |
| 3296 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3297 | "User cannot modify accounts of this type (policy)."); |
| 3298 | } catch (RemoteException re) { |
| 3299 | } |
| 3300 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3301 | userId); |
| 3302 | return; |
| 3303 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3304 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3305 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3306 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3307 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3308 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3309 | // Check to see if the Password should be included to the caller. |
| 3310 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3311 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3312 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3313 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3314 | long identityToken = clearCallingIdentity(); |
| 3315 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3316 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3317 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3318 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3319 | new StartAccountSession( |
| 3320 | accounts, |
| 3321 | response, |
| 3322 | accountType, |
| 3323 | expectActivityLaunch, |
| 3324 | null /* accountName */, |
| 3325 | false /* authDetailsRequired */, |
| 3326 | true /* updateLastAuthenticationTime */, |
| 3327 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3328 | @Override |
| 3329 | public void run() throws RemoteException { |
| 3330 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3331 | requiredFeatures, options); |
| 3332 | } |
| 3333 | |
| 3334 | @Override |
| 3335 | protected String toDebugString(long now) { |
| 3336 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3337 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3338 | + accountType + ", requiredFeatures " |
| 3339 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3340 | } |
| 3341 | }.bind(); |
| 3342 | } finally { |
| 3343 | restoreCallingIdentity(identityToken); |
| 3344 | } |
| 3345 | } |
| 3346 | |
| 3347 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3348 | private abstract class StartAccountSession extends Session { |
| 3349 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3350 | private final boolean mIsPasswordForwardingAllowed; |
| 3351 | |
| 3352 | public StartAccountSession( |
| 3353 | UserAccounts accounts, |
| 3354 | IAccountManagerResponse response, |
| 3355 | String accountType, |
| 3356 | boolean expectActivityLaunch, |
| 3357 | String accountName, |
| 3358 | boolean authDetailsRequired, |
| 3359 | boolean updateLastAuthenticationTime, |
| 3360 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3361 | super(accounts, response, accountType, expectActivityLaunch, |
| 3362 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3363 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3364 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3365 | } |
| 3366 | |
| 3367 | @Override |
| 3368 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3369 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3370 | mNumResults++; |
| 3371 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3372 | if (result != null |
| 3373 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 3374 | if (!checkKeyIntent( |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3375 | Binder.getCallingUid(), |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 3376 | intent)) { |
| 3377 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3378 | "invalid intent in bundle returned"); |
| 3379 | return; |
| 3380 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3381 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3382 | IAccountManagerResponse response; |
| 3383 | if (mExpectActivityLaunch && result != null |
| 3384 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3385 | response = mResponse; |
| 3386 | } else { |
| 3387 | response = getResponseAndClose(); |
| 3388 | } |
| 3389 | if (response == null) { |
| 3390 | return; |
| 3391 | } |
| 3392 | if (result == null) { |
| 3393 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3394 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3395 | + response); |
| 3396 | } |
| 3397 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3398 | "null bundle returned"); |
| 3399 | return; |
| 3400 | } |
| 3401 | |
| 3402 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3403 | // All AccountManager error codes are greater |
| 3404 | // than 0 |
| 3405 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3406 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3407 | return; |
| 3408 | } |
| 3409 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3410 | // Omit passwords if the caller isn't permitted to see them. |
| 3411 | if (!mIsPasswordForwardingAllowed) { |
| 3412 | result.remove(AccountManager.KEY_PASSWORD); |
| 3413 | } |
| 3414 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3415 | // Strip auth token from result. |
| 3416 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3417 | |
| 3418 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3419 | Log.v(TAG, |
| 3420 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3421 | } |
| 3422 | |
| 3423 | // Get the session bundle created by authenticator. The |
| 3424 | // bundle contains data necessary for finishing the session |
| 3425 | // later. The session bundle will be encrypted here and |
| 3426 | // decrypted later when trying to finish the session. |
| 3427 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3428 | if (sessionBundle != null) { |
| 3429 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3430 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3431 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3432 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3433 | } |
| 3434 | // Add accountType info to session bundle. This will |
| 3435 | // override any value set by authenticator. |
| 3436 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3437 | |
| 3438 | // Encrypt session bundle before returning to caller. |
| 3439 | try { |
| 3440 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3441 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3442 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3443 | } catch (GeneralSecurityException e) { |
| 3444 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3445 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3446 | } |
| 3447 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3448 | "failed to encrypt session bundle"); |
| 3449 | return; |
| 3450 | } |
| 3451 | } |
| 3452 | |
| 3453 | sendResponse(response, result); |
| 3454 | } |
| 3455 | } |
| 3456 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3457 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3458 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3459 | @NonNull Bundle sessionBundle, |
| 3460 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3461 | Bundle appInfo, |
| 3462 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3463 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3464 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3465 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3466 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3467 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3468 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3469 | + ", caller's uid " + callingUid |
| 3470 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3471 | + ", pid " + Binder.getCallingPid() |
| 3472 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3473 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3474 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3475 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3476 | // Account type is added to it before encryption. |
| 3477 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3478 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3479 | } |
| 3480 | |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 3481 | // Only allow the system process to finish session for other users. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3482 | if (isCrossUser(callingUid, userId)) { |
| 3483 | throw new SecurityException( |
| 3484 | String.format( |
| 3485 | "User %s trying to finish session for %s without cross user permission", |
| 3486 | UserHandle.getCallingUserId(), |
| 3487 | userId)); |
| 3488 | } |
| 3489 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3490 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3491 | sendErrorResponse(response, |
| 3492 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3493 | "User is not allowed to add an account!"); |
| 3494 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3495 | return; |
| 3496 | } |
| 3497 | |
| 3498 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3499 | final Bundle decryptedBundle; |
| 3500 | final String accountType; |
| 3501 | // First decrypt session bundle to get account type for checking permission. |
| 3502 | try { |
| 3503 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3504 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3505 | if (decryptedBundle == null) { |
| 3506 | sendErrorResponse( |
| 3507 | response, |
| 3508 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3509 | "failed to decrypt session bundle"); |
| 3510 | return; |
| 3511 | } |
| 3512 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3513 | // Account type cannot be null. This should not happen if session bundle was created |
| 3514 | // properly by #StartAccountSession. |
| 3515 | if (TextUtils.isEmpty(accountType)) { |
| 3516 | sendErrorResponse( |
| 3517 | response, |
| 3518 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3519 | "accountType is empty"); |
| 3520 | return; |
| 3521 | } |
| 3522 | |
| 3523 | // If by any chances, decryptedBundle contains colliding keys with |
| 3524 | // system info |
| 3525 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3526 | // update credentials flow, we should replace with the new values of the current call. |
| 3527 | if (appInfo != null) { |
| 3528 | decryptedBundle.putAll(appInfo); |
| 3529 | } |
| 3530 | |
| 3531 | // 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] | 3532 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3533 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3534 | } catch (GeneralSecurityException e) { |
| 3535 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3536 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3537 | } |
| 3538 | sendErrorResponse( |
| 3539 | response, |
| 3540 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3541 | "failed to decrypt session bundle"); |
| 3542 | return; |
| 3543 | } |
| 3544 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3545 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3546 | sendErrorResponse( |
| 3547 | response, |
| 3548 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3549 | "User cannot modify accounts of this type (policy)."); |
| 3550 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3551 | userId); |
| 3552 | return; |
| 3553 | } |
| 3554 | |
| 3555 | long identityToken = clearCallingIdentity(); |
| 3556 | try { |
| 3557 | UserAccounts accounts = getUserAccounts(userId); |
| 3558 | logRecordWithUid( |
| 3559 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3560 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3561 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3562 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3563 | new Session( |
| 3564 | accounts, |
| 3565 | response, |
| 3566 | accountType, |
| 3567 | expectActivityLaunch, |
| 3568 | true /* stripAuthTokenFromResult */, |
| 3569 | null /* accountName */, |
| 3570 | false /* authDetailsRequired */, |
| 3571 | true /* updateLastAuthenticationTime */) { |
| 3572 | @Override |
| 3573 | public void run() throws RemoteException { |
| 3574 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3575 | } |
| 3576 | |
| 3577 | @Override |
| 3578 | protected String toDebugString(long now) { |
| 3579 | return super.toDebugString(now) |
| 3580 | + ", finishSession" |
| 3581 | + ", accountType " + accountType; |
| 3582 | } |
| 3583 | }.bind(); |
| 3584 | } finally { |
| 3585 | restoreCallingIdentity(identityToken); |
| 3586 | } |
| 3587 | } |
| 3588 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3589 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3590 | final DevicePolicyManagerInternal dpmi = |
| 3591 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3592 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3593 | if (dpmi == null) { |
| 3594 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3595 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3596 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3597 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3598 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3599 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3600 | } |
| 3601 | if (intent == null) { |
| 3602 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3603 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3604 | long identityToken = clearCallingIdentity(); |
| 3605 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3606 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3607 | } finally { |
| 3608 | restoreCallingIdentity(identityToken); |
| 3609 | } |
| 3610 | } |
| 3611 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3612 | /** |
| 3613 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3614 | */ |
| 3615 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3616 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3617 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3618 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3619 | return cantAddAccount; |
| 3620 | } |
| 3621 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3622 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3623 | public void confirmCredentialsAsUser( |
| 3624 | IAccountManagerResponse response, |
| 3625 | final Account account, |
| 3626 | final Bundle options, |
| 3627 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3628 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3629 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3630 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3631 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3632 | Log.v(TAG, "confirmCredentials: " + account |
| 3633 | + ", response " + response |
| 3634 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3635 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3636 | + ", pid " + Binder.getCallingPid()); |
| 3637 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3638 | // Only allow the system process to read accounts of other users |
| 3639 | if (isCrossUser(callingUid, userId)) { |
| 3640 | throw new SecurityException( |
| 3641 | String.format( |
| 3642 | "User %s trying to confirm account credentials for %s" , |
| 3643 | UserHandle.getCallingUserId(), |
| 3644 | userId)); |
| 3645 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3646 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3647 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3648 | long identityToken = clearCallingIdentity(); |
| 3649 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3650 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3651 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3652 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3653 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3654 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3655 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3656 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3657 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3658 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3659 | protected String toDebugString(long now) { |
| 3660 | return super.toDebugString(now) + ", confirmCredentials" |
| 3661 | + ", " + account; |
| 3662 | } |
| 3663 | }.bind(); |
| 3664 | } finally { |
| 3665 | restoreCallingIdentity(identityToken); |
| 3666 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3667 | } |
| 3668 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3669 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3670 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3671 | final String authTokenType, final boolean expectActivityLaunch, |
| 3672 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3673 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3674 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3675 | Log.v(TAG, "updateCredentials: " + account |
| 3676 | + ", response " + response |
| 3677 | + ", authTokenType " + authTokenType |
| 3678 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3679 | + ", caller's uid " + Binder.getCallingUid() |
| 3680 | + ", pid " + Binder.getCallingPid()); |
| 3681 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3682 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3683 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3684 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3685 | long identityToken = clearCallingIdentity(); |
| 3686 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3687 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3688 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3689 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3690 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3691 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3692 | public void run() throws RemoteException { |
| 3693 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3694 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3695 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3696 | protected String toDebugString(long now) { |
| 3697 | if (loginOptions != null) loginOptions.keySet(); |
| 3698 | return super.toDebugString(now) + ", updateCredentials" |
| 3699 | + ", " + account |
| 3700 | + ", authTokenType " + authTokenType |
| 3701 | + ", loginOptions " + loginOptions; |
| 3702 | } |
| 3703 | }.bind(); |
| 3704 | } finally { |
| 3705 | restoreCallingIdentity(identityToken); |
| 3706 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3707 | } |
| 3708 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3709 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3710 | public void startUpdateCredentialsSession( |
| 3711 | IAccountManagerResponse response, |
| 3712 | final Account account, |
| 3713 | final String authTokenType, |
| 3714 | final boolean expectActivityLaunch, |
| 3715 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3716 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3717 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3718 | Log.v(TAG, |
| 3719 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3720 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3721 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3722 | + ", pid " + Binder.getCallingPid()); |
| 3723 | } |
| 3724 | if (response == null) { |
| 3725 | throw new IllegalArgumentException("response is null"); |
| 3726 | } |
| 3727 | if (account == null) { |
| 3728 | throw new IllegalArgumentException("account is null"); |
| 3729 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3730 | |
| 3731 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3732 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3733 | |
| 3734 | // Check to see if the Password should be included to the caller. |
| 3735 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3736 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3737 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3738 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3739 | long identityToken = clearCallingIdentity(); |
| 3740 | try { |
| 3741 | UserAccounts accounts = getUserAccounts(userId); |
| 3742 | new StartAccountSession( |
| 3743 | accounts, |
| 3744 | response, |
| 3745 | account.type, |
| 3746 | expectActivityLaunch, |
| 3747 | account.name, |
| 3748 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3749 | true /* updateLastCredentialTime */, |
| 3750 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3751 | @Override |
| 3752 | public void run() throws RemoteException { |
| 3753 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3754 | loginOptions); |
| 3755 | } |
| 3756 | |
| 3757 | @Override |
| 3758 | protected String toDebugString(long now) { |
| 3759 | if (loginOptions != null) |
| 3760 | loginOptions.keySet(); |
| 3761 | return super.toDebugString(now) |
| 3762 | + ", startUpdateCredentialsSession" |
| 3763 | + ", " + account |
| 3764 | + ", authTokenType " + authTokenType |
| 3765 | + ", loginOptions " + loginOptions; |
| 3766 | } |
| 3767 | }.bind(); |
| 3768 | } finally { |
| 3769 | restoreCallingIdentity(identityToken); |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3774 | public void isCredentialsUpdateSuggested( |
| 3775 | IAccountManagerResponse response, |
| 3776 | final Account account, |
| 3777 | final String statusToken) { |
| 3778 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3779 | Log.v(TAG, |
| 3780 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3781 | + ", caller's uid " + Binder.getCallingUid() |
| 3782 | + ", pid " + Binder.getCallingPid()); |
| 3783 | } |
| 3784 | if (response == null) { |
| 3785 | throw new IllegalArgumentException("response is null"); |
| 3786 | } |
| 3787 | if (account == null) { |
| 3788 | throw new IllegalArgumentException("account is null"); |
| 3789 | } |
| 3790 | if (TextUtils.isEmpty(statusToken)) { |
| 3791 | throw new IllegalArgumentException("status token is empty"); |
| 3792 | } |
| 3793 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3794 | int usrId = UserHandle.getCallingUserId(); |
| 3795 | long identityToken = clearCallingIdentity(); |
| 3796 | try { |
| 3797 | UserAccounts accounts = getUserAccounts(usrId); |
| 3798 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3799 | false /* stripAuthTokenFromResult */, account.name, |
| 3800 | false /* authDetailsRequired */) { |
| 3801 | @Override |
| 3802 | protected String toDebugString(long now) { |
| 3803 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3804 | + ", " + account; |
| 3805 | } |
| 3806 | |
| 3807 | @Override |
| 3808 | public void run() throws RemoteException { |
| 3809 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3810 | } |
| 3811 | |
| 3812 | @Override |
| 3813 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3814 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3815 | IAccountManagerResponse response = getResponseAndClose(); |
| 3816 | if (response == null) { |
| 3817 | return; |
| 3818 | } |
| 3819 | |
| 3820 | if (result == null) { |
| 3821 | sendErrorResponse( |
| 3822 | response, |
| 3823 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3824 | "null bundle"); |
| 3825 | return; |
| 3826 | } |
| 3827 | |
| 3828 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3829 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3830 | + response); |
| 3831 | } |
| 3832 | // Check to see if an error occurred. We know if an error occurred because all |
| 3833 | // error codes are greater than 0. |
| 3834 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3835 | sendErrorResponse(response, |
| 3836 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3837 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3838 | return; |
| 3839 | } |
| 3840 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3841 | sendErrorResponse( |
| 3842 | response, |
| 3843 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3844 | "no result in response"); |
| 3845 | return; |
| 3846 | } |
| 3847 | final Bundle newResult = new Bundle(); |
| 3848 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3849 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3850 | sendResponse(response, newResult); |
| 3851 | } |
| 3852 | }.bind(); |
| 3853 | } finally { |
| 3854 | restoreCallingIdentity(identityToken); |
| 3855 | } |
| 3856 | } |
| 3857 | |
| 3858 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3859 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3860 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3861 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3862 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3863 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3864 | + ", response " + response |
| 3865 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3866 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3867 | + ", pid " + Binder.getCallingPid()); |
| 3868 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3869 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3870 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3871 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3872 | if (!isAccountManagedByCaller(accountType, callingUid, userId) |
| 3873 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3874 | String msg = String.format( |
| 3875 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3876 | callingUid, |
| 3877 | accountType); |
| 3878 | throw new SecurityException(msg); |
| 3879 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3880 | long identityToken = clearCallingIdentity(); |
| 3881 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3882 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3883 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3884 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3885 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3886 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3887 | public void run() throws RemoteException { |
| 3888 | mAuthenticator.editProperties(this, mAccountType); |
| 3889 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3890 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3891 | protected String toDebugString(long now) { |
| 3892 | return super.toDebugString(now) + ", editProperties" |
| 3893 | + ", accountType " + accountType; |
| 3894 | } |
| 3895 | }.bind(); |
| 3896 | } finally { |
| 3897 | restoreCallingIdentity(identityToken); |
| 3898 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3899 | } |
| 3900 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3901 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3902 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3903 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3904 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3905 | throw new SecurityException("Can be called only by system UID"); |
| 3906 | } |
| 3907 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3908 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3909 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3910 | |
| 3911 | final int userId = userHandle.getIdentifier(); |
| 3912 | |
| 3913 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3914 | |
| 3915 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3916 | int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3917 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3918 | } catch (NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3919 | Log.d(TAG, "Package not found " + e.getMessage()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3920 | return false; |
| 3921 | } |
| 3922 | } |
| 3923 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3924 | // Returns package with oldest target SDK for given UID. |
| 3925 | private String getPackageNameForUid(int uid) { |
| 3926 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3927 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3928 | return null; |
| 3929 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3930 | String packageName = packageNames[0]; |
Fyodor Kupolov | 892fc8d | 2017-03-22 12:57:04 -0700 | [diff] [blame] | 3931 | if (packageNames.length == 1) { |
| 3932 | return packageName; |
| 3933 | } |
| 3934 | // 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] | 3935 | int oldestVersion = Integer.MAX_VALUE; |
| 3936 | for (String name : packageNames) { |
| 3937 | try { |
| 3938 | ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(name, 0); |
| 3939 | if (applicationInfo != null) { |
| 3940 | int version = applicationInfo.targetSdkVersion; |
| 3941 | if (version < oldestVersion) { |
| 3942 | oldestVersion = version; |
| 3943 | packageName = name; |
| 3944 | } |
| 3945 | } |
| 3946 | } catch (NameNotFoundException e) { |
| 3947 | // skip |
| 3948 | } |
| 3949 | } |
| 3950 | return packageName; |
| 3951 | } |
| 3952 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3953 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3954 | int uid) { |
| 3955 | if (packageName == null) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3956 | packageName = getPackageNameForUid(uid); |
| 3957 | if (packageName == null) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3958 | return false; |
| 3959 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | // Use null token which means any token. Having a token means the package |
| 3963 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3964 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3965 | return true; |
| 3966 | } |
| 3967 | // 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] | 3968 | // 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] | 3969 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3970 | int visibility = resolveAccountVisibility(account, packageName, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3971 | getUserAccounts(UserHandle.getUserId(uid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3972 | return (visibility == AccountManager.VISIBILITY_VISIBLE |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3973 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | @Override |
| 3977 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3978 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3979 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3980 | throw new SecurityException("Can be called only by system UID"); |
| 3981 | } |
| 3982 | |
| 3983 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3984 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3985 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3986 | |
| 3987 | final int userId = userHandle.getIdentifier(); |
| 3988 | |
| 3989 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3990 | |
| 3991 | final int uid; |
| 3992 | try { |
| 3993 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 3994 | } catch (NameNotFoundException e) { |
| 3995 | Slog.e(TAG, "Unknown package " + packageName); |
| 3996 | return null; |
| 3997 | } |
| 3998 | |
| 3999 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 4000 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 4001 | final long identity = Binder.clearCallingIdentity(); |
| 4002 | try { |
| 4003 | return PendingIntent.getActivityAsUser( |
| 4004 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 4005 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 4006 | null, new UserHandle(userId)).getIntentSender(); |
| 4007 | } finally { |
| 4008 | Binder.restoreCallingIdentity(identity); |
| 4009 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 4013 | int uid, RemoteCallback callback) { |
| 4014 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 4015 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 4016 | @Override |
| 4017 | public void onResult(Bundle value) throws RemoteException { |
| 4018 | handleAuthenticatorResponse(true); |
| 4019 | } |
| 4020 | |
| 4021 | @Override |
| 4022 | public void onRequestContinued() { |
| 4023 | /* ignore */ |
| 4024 | } |
| 4025 | |
| 4026 | @Override |
| 4027 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 4028 | handleAuthenticatorResponse(false); |
| 4029 | } |
| 4030 | |
| 4031 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 4032 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 4033 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4034 | UserHandle.getUserHandleForUid(uid)); |
| 4035 | if (callback != null) { |
| 4036 | Bundle result = new Bundle(); |
| 4037 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 4038 | callback.sendResult(result); |
| 4039 | } |
| 4040 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 4041 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 4045 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 4046 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 4047 | throw new SecurityException("Only system can check for accounts across users"); |
| 4048 | } |
| 4049 | final long token = Binder.clearCallingIdentity(); |
| 4050 | try { |
| 4051 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 4052 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 4053 | if (allAccounts[i].account.equals(account)) { |
| 4054 | return true; |
| 4055 | } |
| 4056 | } |
| 4057 | return false; |
| 4058 | } finally { |
| 4059 | Binder.restoreCallingIdentity(token); |
| 4060 | } |
| 4061 | } |
| 4062 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4063 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 4064 | private final String[] mFeatures; |
| 4065 | private volatile Account[] mAccountsOfType = null; |
| 4066 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 4067 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4068 | private final int mCallingUid; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4069 | private final String mPackageName; |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4070 | private final boolean mIncludeManagedNotVisible; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4071 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4072 | public GetAccountsByTypeAndFeatureSession( |
| 4073 | UserAccounts accounts, |
| 4074 | IAccountManagerResponse response, |
| 4075 | String type, |
| 4076 | String[] features, |
| 4077 | int callingUid, |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4078 | String packageName, |
| 4079 | boolean includeManagedNotVisible) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4080 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4081 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 4082 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4083 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4084 | mFeatures = features; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4085 | mPackageName = packageName; |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4086 | mIncludeManagedNotVisible = includeManagedNotVisible; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4087 | } |
| 4088 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4089 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4090 | public void run() throws RemoteException { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4091 | mAccountsOfType = getAccountsFromCache(mAccounts, mAccountType, |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4092 | mCallingUid, mPackageName, mIncludeManagedNotVisible); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4093 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 4094 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4095 | mCurrentAccount = 0; |
| 4096 | |
| 4097 | checkAccount(); |
| 4098 | } |
| 4099 | |
| 4100 | public void checkAccount() { |
| 4101 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 4102 | sendResult(); |
| 4103 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4104 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4105 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 4106 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 4107 | if (accountAuthenticator == null) { |
| 4108 | // It is possible that the authenticator has died, which is indicated by |
| 4109 | // mAuthenticator being set to null. If this happens then just abort. |
| 4110 | // There is no need to send back a result or error in this case since |
| 4111 | // that already happened when mAuthenticator was cleared. |
| 4112 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4113 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 4114 | + " connected to the authenticator, " + toDebugString()); |
| 4115 | } |
| 4116 | return; |
| 4117 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4118 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 4119 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4120 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4121 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4125 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4126 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4127 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4128 | mNumResults++; |
| 4129 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4130 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4131 | return; |
| 4132 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4133 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4134 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 4135 | } |
| 4136 | mCurrentAccount++; |
| 4137 | checkAccount(); |
| 4138 | } |
| 4139 | |
| 4140 | public void sendResult() { |
| 4141 | IAccountManagerResponse response = getResponseAndClose(); |
| 4142 | if (response != null) { |
| 4143 | try { |
| 4144 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 4145 | for (int i = 0; i < accounts.length; i++) { |
| 4146 | accounts[i] = mAccountsWithFeatures.get(i); |
| 4147 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4148 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4149 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 4150 | + response); |
| 4151 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4152 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4153 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4154 | response.onResult(result); |
| 4155 | } catch (RemoteException e) { |
| 4156 | // if the caller is dead then there is no one to care about remote exceptions |
| 4157 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4158 | Log.v(TAG, "failure while notifying response", e); |
| 4159 | } |
| 4160 | } |
| 4161 | } |
| 4162 | } |
| 4163 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4164 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4165 | protected String toDebugString(long now) { |
| 4166 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 4167 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 4168 | } |
| 4169 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4170 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4171 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4172 | * 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] | 4173 | * @hide |
| 4174 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4175 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4176 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4177 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4178 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4179 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4180 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4181 | if (visibleAccountTypes.isEmpty()) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4182 | return EMPTY_ACCOUNT_ARRAY; |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4183 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4184 | long identityToken = clearCallingIdentity(); |
| 4185 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4186 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4187 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4188 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4189 | callingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4190 | opPackageName, |
| 4191 | visibleAccountTypes, |
| 4192 | false /* includeUserManagedNotVisible */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4193 | } finally { |
| 4194 | restoreCallingIdentity(identityToken); |
| 4195 | } |
| 4196 | } |
| 4197 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4198 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4199 | * Returns accounts for all running users, ignores visibility values. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4200 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4201 | * @hide |
| 4202 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4203 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4204 | public AccountAndUser[] getRunningAccounts() { |
| 4205 | final int[] runningUserIds; |
| 4206 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4207 | runningUserIds = ActivityManager.getService().getRunningUserIds(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4208 | } catch (RemoteException e) { |
| 4209 | // Running in system_server; should never happen |
| 4210 | throw new RuntimeException(e); |
| 4211 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4212 | return getAccounts(runningUserIds); |
| 4213 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4214 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4215 | /** |
| 4216 | * Returns accounts for all users, ignores visibility values. |
| 4217 | * |
| 4218 | * @hide |
| 4219 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4220 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4221 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 4222 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4223 | final int[] userIds = new int[users.size()]; |
| 4224 | for (int i = 0; i < userIds.length; i++) { |
| 4225 | userIds[i] = users.get(i).id; |
| 4226 | } |
| 4227 | return getAccounts(userIds); |
| 4228 | } |
| 4229 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4230 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4231 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4232 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4233 | for (int userId : userIds) { |
| 4234 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4235 | if (userAccounts == null) continue; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4236 | Account[] accounts = getAccountsFromCache( |
| 4237 | userAccounts, |
| 4238 | null /* type */, |
| 4239 | Binder.getCallingUid(), |
| 4240 | null /* packageName */, |
| 4241 | false /* include managed not visible*/); |
| 4242 | for (Account account : accounts) { |
| 4243 | runningAccounts.add(new AccountAndUser(account, userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4244 | } |
| 4245 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4246 | |
| 4247 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4248 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4251 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4252 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4253 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4254 | int callingUid = Binder.getCallingUid(); |
| 4255 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4256 | return getAccountsAsUserForPackage(type, userId, opPackageName /* callingPackage */, -1, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4257 | opPackageName, false /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4260 | @NonNull |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4261 | private Account[] getAccountsAsUserForPackage( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4262 | String type, |
| 4263 | int userId, |
| 4264 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4265 | int packageUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4266 | String opPackageName, |
| 4267 | boolean includeUserManagedNotVisible) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4268 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4269 | // Only allow the system process to read accounts of other users |
| 4270 | if (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4271 | && callingUid != Process.SYSTEM_UID |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4272 | && mContext.checkCallingOrSelfPermission( |
| 4273 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4274 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4275 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4276 | + " trying to get account for " + userId); |
| 4277 | } |
| 4278 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4279 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4280 | Log.v(TAG, "getAccounts: accountType " + type |
| 4281 | + ", caller's uid " + Binder.getCallingUid() |
| 4282 | + ", pid " + Binder.getCallingPid()); |
| 4283 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4284 | |
| 4285 | // If the original calling app was using account choosing activity |
| 4286 | // provided by the framework or authenticator we'll passing in |
| 4287 | // the original caller's uid here, which is what should be used for filtering. |
| 4288 | List<String> managedTypes = |
| 4289 | getTypesManagedByCaller(callingUid, UserHandle.getUserId(callingUid)); |
| 4290 | if (packageUid != -1 && |
| 4291 | ((UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 4292 | || (type != null && managedTypes.contains(type))))) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4293 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4294 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4295 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4296 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4297 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4298 | if (visibleAccountTypes.isEmpty() |
| 4299 | || (type != null && !visibleAccountTypes.contains(type))) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4300 | return EMPTY_ACCOUNT_ARRAY; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4301 | } else if (visibleAccountTypes.contains(type)) { |
| 4302 | // Prune the list down to just the requested type. |
| 4303 | visibleAccountTypes = new ArrayList<>(); |
| 4304 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4305 | } // else aggregate all the visible accounts (it won't matter if the |
| 4306 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4307 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4308 | long identityToken = clearCallingIdentity(); |
| 4309 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4310 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 4311 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4312 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4313 | callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4314 | opPackageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4315 | visibleAccountTypes, |
| 4316 | includeUserManagedNotVisible); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4317 | } finally { |
| 4318 | restoreCallingIdentity(identityToken); |
| 4319 | } |
| 4320 | } |
| 4321 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4322 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4323 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4324 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4325 | int callingUid, |
| 4326 | String callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4327 | List<String> visibleAccountTypes, |
| 4328 | boolean includeUserManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4329 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4330 | for (String visibleType : visibleAccountTypes) { |
| 4331 | Account[] accountsForType = getAccountsFromCache( |
| 4332 | userAccounts, visibleType, callingUid, callingPackage, |
| 4333 | includeUserManagedNotVisible); |
| 4334 | if (accountsForType != null) { |
| 4335 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4336 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4337 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4338 | Account[] result = new Account[visibleAccounts.size()]; |
| 4339 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4340 | result[i] = visibleAccounts.get(i); |
| 4341 | } |
| 4342 | return result; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4345 | @Override |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4346 | public void addSharedAccountsFromParentUser(int parentUserId, int userId, |
| 4347 | String opPackageName) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4348 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4349 | Account[] accounts = getAccountsAsUser(null, parentUserId, opPackageName); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4350 | for (Account account : accounts) { |
| 4351 | addSharedAccountAsUser(account, userId); |
| 4352 | } |
| 4353 | } |
| 4354 | |
| 4355 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4356 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4357 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4358 | accounts.accountsDb.deleteSharedAccount(account); |
| 4359 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4360 | if (accountId < 0) { |
| 4361 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4362 | + ", skipping the DB insert failed"); |
| 4363 | return false; |
| 4364 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4365 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4366 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4367 | return true; |
| 4368 | } |
| 4369 | |
| 4370 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4371 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4372 | userId = handleIncomingUser(userId); |
| 4373 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4374 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4375 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4376 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4377 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4378 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4379 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4380 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4381 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4382 | } |
| 4383 | return r > 0; |
| 4384 | } |
| 4385 | |
| 4386 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4387 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4388 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4389 | } |
| 4390 | |
| 4391 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4392 | userId = handleIncomingUser(userId); |
| 4393 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4394 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4395 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4396 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4397 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4398 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4399 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4400 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4401 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | @Override |
| 4405 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4406 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4407 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4408 | synchronized (accounts.dbLock) { |
| 4409 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
| 4410 | Account[] accountArray = new Account[accountList.size()]; |
| 4411 | accountList.toArray(accountArray); |
| 4412 | return accountArray; |
| 4413 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4417 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4418 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4419 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4420 | } |
| 4421 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4422 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4423 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4424 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4425 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4426 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4427 | // Don't do opPackageName check - caller is system. |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4428 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4429 | + callingUid + " with uid=" + uid); |
| 4430 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4431 | return getAccountsAsUserForPackage(null, UserHandle.getCallingUserId(), packageName, uid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4432 | opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4433 | } |
| 4434 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4435 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4436 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4437 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4438 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4439 | int callingUid = Binder.getCallingUid(); |
| 4440 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4441 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4442 | int packageUid = -1; |
| 4443 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4444 | packageUid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4445 | } catch (NameNotFoundException re) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4446 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4447 | return EMPTY_ACCOUNT_ARRAY; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4448 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4449 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4450 | && (type != null && !isAccountManagedByCaller(type, callingUid, userId))) { |
| 4451 | return EMPTY_ACCOUNT_ARRAY; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4452 | } |
sunjian | d62dc39 | 2017-06-01 12:05:59 -0700 | [diff] [blame] | 4453 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) && type == null) { |
| 4454 | return getAccountsAsUserForPackage(type, userId, |
| 4455 | packageName, packageUid, opPackageName, false /* includeUserManagedNotVisible */); |
| 4456 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4457 | return getAccountsAsUserForPackage(type, userId, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4458 | packageName, packageUid, opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4459 | } |
| 4460 | |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4461 | private boolean needToStartChooseAccountActivity(Account[] accounts, String callingPackage) { |
| 4462 | if (accounts.length < 1) return false; |
| 4463 | if (accounts.length > 1) return true; |
| 4464 | Account account = accounts[0]; |
| 4465 | UserAccounts userAccounts = getUserAccounts(UserHandle.getCallingUserId()); |
| 4466 | int visibility = resolveAccountVisibility(account, callingPackage, userAccounts); |
| 4467 | if (visibility == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE) return true; |
| 4468 | return false; |
| 4469 | } |
| 4470 | |
| 4471 | private void startChooseAccountActivityWithAccounts( |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 4472 | IAccountManagerResponse response, Account[] accounts, String callingPackage) { |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4473 | Intent intent = new Intent(mContext, ChooseAccountActivity.class); |
| 4474 | intent.putExtra(AccountManager.KEY_ACCOUNTS, accounts); |
| 4475 | intent.putExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE, |
| 4476 | new AccountManagerResponse(response)); |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 4477 | intent.putExtra(AccountManager.KEY_ANDROID_PACKAGE_NAME, callingPackage); |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4478 | |
| 4479 | mContext.startActivityAsUser(intent, UserHandle.of(UserHandle.getCallingUserId())); |
| 4480 | } |
| 4481 | |
| 4482 | private void handleGetAccountsResult( |
| 4483 | IAccountManagerResponse response, |
| 4484 | Account[] accounts, |
| 4485 | String callingPackage) { |
| 4486 | |
| 4487 | if (needToStartChooseAccountActivity(accounts, callingPackage)) { |
sunjian | bdabd40 | 2017-06-06 17:54:07 -0700 | [diff] [blame] | 4488 | startChooseAccountActivityWithAccounts(response, accounts, callingPackage); |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4489 | return; |
| 4490 | } |
| 4491 | if (accounts.length == 1) { |
| 4492 | Bundle bundle = new Bundle(); |
| 4493 | bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accounts[0].name); |
| 4494 | bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accounts[0].type); |
| 4495 | onResult(response, bundle); |
| 4496 | return; |
| 4497 | } |
| 4498 | // No qualified account exists, return an empty Bundle. |
| 4499 | onResult(response, new Bundle()); |
| 4500 | } |
| 4501 | |
| 4502 | @Override |
| 4503 | public void getAccountByTypeAndFeatures( |
| 4504 | IAccountManagerResponse response, |
| 4505 | String accountType, |
| 4506 | String[] features, |
| 4507 | String opPackageName) { |
| 4508 | |
| 4509 | int callingUid = Binder.getCallingUid(); |
| 4510 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 4511 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4512 | Log.v(TAG, "getAccount: accountType " + accountType |
| 4513 | + ", response " + response |
| 4514 | + ", features " + Arrays.toString(features) |
| 4515 | + ", caller's uid " + callingUid |
| 4516 | + ", pid " + Binder.getCallingPid()); |
| 4517 | } |
| 4518 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4519 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 4520 | |
| 4521 | int userId = UserHandle.getCallingUserId(); |
| 4522 | |
| 4523 | long identityToken = clearCallingIdentity(); |
| 4524 | try { |
| 4525 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4526 | if (ArrayUtils.isEmpty(features)) { |
| 4527 | Account[] accountsWithManagedNotVisible = getAccountsFromCache( |
| 4528 | userAccounts, accountType, callingUid, opPackageName, |
| 4529 | true /* include managed not visible */); |
| 4530 | handleGetAccountsResult( |
| 4531 | response, accountsWithManagedNotVisible, opPackageName); |
| 4532 | return; |
| 4533 | } |
| 4534 | |
| 4535 | IAccountManagerResponse retrieveAccountsResponse = |
| 4536 | new IAccountManagerResponse.Stub() { |
| 4537 | @Override |
| 4538 | public void onResult(Bundle value) throws RemoteException { |
| 4539 | Parcelable[] parcelables = value.getParcelableArray( |
| 4540 | AccountManager.KEY_ACCOUNTS); |
| 4541 | Account[] accounts = new Account[parcelables.length]; |
| 4542 | for (int i = 0; i < parcelables.length; i++) { |
| 4543 | accounts[i] = (Account) parcelables[i]; |
| 4544 | } |
| 4545 | handleGetAccountsResult( |
| 4546 | response, accounts, opPackageName); |
| 4547 | } |
| 4548 | |
| 4549 | @Override |
| 4550 | public void onError(int errorCode, String errorMessage) |
| 4551 | throws RemoteException { |
| 4552 | // Will not be called in this case. |
| 4553 | } |
| 4554 | }; |
| 4555 | new GetAccountsByTypeAndFeatureSession( |
| 4556 | userAccounts, |
| 4557 | retrieveAccountsResponse, |
| 4558 | accountType, |
| 4559 | features, |
| 4560 | callingUid, |
| 4561 | opPackageName, |
| 4562 | true /* include managed not visible */).bind(); |
| 4563 | } finally { |
| 4564 | restoreCallingIdentity(identityToken); |
| 4565 | } |
| 4566 | } |
| 4567 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4568 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4569 | public void getAccountsByFeatures( |
| 4570 | IAccountManagerResponse response, |
| 4571 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4572 | String[] features, |
| 4573 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4574 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4575 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4576 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4577 | Log.v(TAG, "getAccounts: accountType " + type |
| 4578 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4579 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4580 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4581 | + ", pid " + Binder.getCallingPid()); |
| 4582 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4583 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4584 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4585 | int userId = UserHandle.getCallingUserId(); |
| 4586 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4587 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4588 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4589 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4590 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4591 | // Need to return just the accounts that are from matching signatures. |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4592 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, EMPTY_ACCOUNT_ARRAY); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4593 | try { |
| 4594 | response.onResult(result); |
| 4595 | } catch (RemoteException e) { |
| 4596 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4597 | } |
| 4598 | return; |
| 4599 | } |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4600 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4601 | long identityToken = clearCallingIdentity(); |
| 4602 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4603 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4604 | if (features == null || features.length == 0) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4605 | Account[] accounts = getAccountsFromCache(userAccounts, type, callingUid, |
| 4606 | opPackageName, false); |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4607 | Bundle result = new Bundle(); |
| 4608 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4609 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4610 | return; |
| 4611 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4612 | new GetAccountsByTypeAndFeatureSession( |
| 4613 | userAccounts, |
| 4614 | response, |
| 4615 | type, |
| 4616 | features, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4617 | callingUid, |
sunjian | f29d549 | 2017-05-11 15:42:31 -0700 | [diff] [blame] | 4618 | opPackageName, |
| 4619 | false /* include managed not visible */).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4620 | } finally { |
| 4621 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4622 | } |
| 4623 | } |
| 4624 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4625 | @Override |
| 4626 | public void onAccountAccessed(String token) throws RemoteException { |
| 4627 | final int uid = Binder.getCallingUid(); |
| 4628 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4629 | return; |
| 4630 | } |
| 4631 | final int userId = UserHandle.getCallingUserId(); |
| 4632 | final long identity = Binder.clearCallingIdentity(); |
| 4633 | try { |
| 4634 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4635 | if (Objects.equals(account.getAccessId(), token)) { |
| 4636 | // An app just accessed the account. At this point it knows about |
| 4637 | // 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] | 4638 | // Do we need to update account visibility here? |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4639 | if (!hasAccountAccess(account, null, uid)) { |
| 4640 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4641 | uid, true); |
| 4642 | } |
| 4643 | } |
| 4644 | } |
| 4645 | } finally { |
| 4646 | Binder.restoreCallingIdentity(identity); |
| 4647 | } |
| 4648 | } |
| 4649 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4650 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4651 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4652 | IAccountManagerResponse mResponse; |
| 4653 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4654 | final boolean mExpectActivityLaunch; |
| 4655 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4656 | final String mAccountName; |
| 4657 | // Indicates if we need to add auth details(like last credential time) |
| 4658 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4659 | // If set, we need to update the last authenticated time. This is |
| 4660 | // currently |
| 4661 | // used on |
| 4662 | // successful confirming credentials. |
| 4663 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4664 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4665 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4666 | private int mNumRequestContinued = 0; |
| 4667 | private int mNumErrors = 0; |
| 4668 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4669 | IAccountAuthenticator mAuthenticator = null; |
| 4670 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4671 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4672 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4673 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4674 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4675 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4676 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4677 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4678 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4679 | } |
| 4680 | |
| 4681 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4682 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4683 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4684 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4685 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4686 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4687 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4688 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4689 | mResponse = response; |
| 4690 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4691 | mExpectActivityLaunch = expectActivityLaunch; |
| 4692 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4693 | mAccountName = accountName; |
| 4694 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4695 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4696 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4697 | synchronized (mSessions) { |
| 4698 | mSessions.put(toString(), this); |
| 4699 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4700 | if (response != null) { |
| 4701 | try { |
| 4702 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4703 | } catch (RemoteException e) { |
| 4704 | mResponse = null; |
| 4705 | binderDied(); |
| 4706 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4707 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4708 | } |
| 4709 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4710 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4711 | if (mResponse == null) { |
| 4712 | // this session has already been closed |
| 4713 | return null; |
| 4714 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4715 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4716 | 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] | 4717 | return response; |
| 4718 | } |
| 4719 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4720 | /** |
| 4721 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4722 | * security policy. |
| 4723 | * |
| 4724 | * 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] | 4725 | * 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] | 4726 | * supplied entries in the system Settings app. |
| 4727 | */ |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4728 | protected boolean checkKeyIntent(int authUid, Intent intent) { |
Jeff Sharkey | d722e78 | 2017-06-12 17:33:07 -0600 | [diff] [blame] | 4729 | intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_READ_URI_PERMISSION |
| 4730 | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
| 4731 | | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
| 4732 | | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION)); |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4733 | long bid = Binder.clearCallingIdentity(); |
| 4734 | try { |
| 4735 | PackageManager pm = mContext.getPackageManager(); |
| 4736 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4737 | if (resolveInfo == null) { |
| 4738 | return false; |
| 4739 | } |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4740 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4741 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame^] | 4742 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4743 | if (!isExportedSystemActivity(targetActivityInfo) |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame^] | 4744 | && !pmi.hasSignatureCapability( |
| 4745 | targetUid, authUid, |
| 4746 | PackageParser.SigningDetails.CertCapabilities.AUTH)) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4747 | String pkgName = targetActivityInfo.packageName; |
| 4748 | String activityName = targetActivityInfo.name; |
| 4749 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4750 | + "does not share a signature with the supplying authenticator (%s)."; |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4751 | Log.e(TAG, String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4752 | return false; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4753 | } |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4754 | return true; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4755 | } finally { |
| 4756 | Binder.restoreCallingIdentity(bid); |
| 4757 | } |
| 4758 | } |
| 4759 | |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4760 | private boolean isExportedSystemActivity(ActivityInfo activityInfo) { |
| 4761 | String className = activityInfo.name; |
| 4762 | return "android".equals(activityInfo.packageName) && |
| 4763 | (GrantCredentialsPermissionActivity.class.getName().equals(className) |
| 4764 | || CantAddAccountActivity.class.getName().equals(className)); |
| 4765 | } |
| 4766 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4767 | private void close() { |
| 4768 | synchronized (mSessions) { |
| 4769 | if (mSessions.remove(toString()) == null) { |
| 4770 | // the session was already closed, so bail out now |
| 4771 | return; |
| 4772 | } |
| 4773 | } |
| 4774 | if (mResponse != null) { |
| 4775 | // stop listening for response deaths |
| 4776 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4777 | |
| 4778 | // clear this so that we don't accidentally send any further results |
| 4779 | mResponse = null; |
| 4780 | } |
| 4781 | cancelTimeout(); |
| 4782 | unbind(); |
| 4783 | } |
| 4784 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4785 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4786 | public void binderDied() { |
| 4787 | mResponse = null; |
| 4788 | close(); |
| 4789 | } |
| 4790 | |
| 4791 | protected String toDebugString() { |
| 4792 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4793 | } |
| 4794 | |
| 4795 | protected String toDebugString(long now) { |
| 4796 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4797 | + ", connected " + (mAuthenticator != null) |
| 4798 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4799 | + "/" + mNumErrors + ")" |
| 4800 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4801 | } |
| 4802 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4803 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4804 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4805 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4806 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4807 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4808 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4809 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4810 | } |
| 4811 | } |
| 4812 | |
| 4813 | private void unbind() { |
| 4814 | if (mAuthenticator != null) { |
| 4815 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4816 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4817 | } |
| 4818 | } |
| 4819 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4820 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4821 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4822 | } |
| 4823 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4824 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4825 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4826 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4827 | try { |
| 4828 | run(); |
| 4829 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4830 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4831 | "remote exception"); |
| 4832 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4833 | } |
| 4834 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4835 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4836 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4837 | mAuthenticator = null; |
| 4838 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4839 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4840 | try { |
| 4841 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4842 | "disconnected"); |
| 4843 | } catch (RemoteException e) { |
| 4844 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4845 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4846 | + "caught RemoteException while responding", e); |
| 4847 | } |
| 4848 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4849 | } |
| 4850 | } |
| 4851 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4852 | public abstract void run() throws RemoteException; |
| 4853 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4854 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4855 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4856 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4857 | try { |
| 4858 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4859 | "timeout"); |
| 4860 | } catch (RemoteException e) { |
| 4861 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4862 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4863 | e); |
| 4864 | } |
| 4865 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4866 | } |
| 4867 | } |
| 4868 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4869 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4870 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4871 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4872 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4873 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4874 | if (result != null) { |
| 4875 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4876 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4877 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4878 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4879 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4880 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4881 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4882 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4883 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4884 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4885 | if (needUpdate || mAuthDetailsRequired) { |
| 4886 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4887 | if (needUpdate && accountPresent) { |
| 4888 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4889 | } |
| 4890 | if (mAuthDetailsRequired) { |
| 4891 | long lastAuthenticatedTime = -1; |
| 4892 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4893 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4894 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4895 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4896 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4897 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4898 | lastAuthenticatedTime); |
| 4899 | } |
| 4900 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4901 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4902 | if (result != null |
| 4903 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4904 | if (!checkKeyIntent( |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4905 | Binder.getCallingUid(), |
tiansiming | a8868bf | 2017-09-20 13:59:13 +0800 | [diff] [blame] | 4906 | intent)) { |
| 4907 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 4908 | "invalid intent in bundle returned"); |
| 4909 | return; |
| 4910 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4911 | } |
| 4912 | if (result != null |
| 4913 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4914 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4915 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4916 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4917 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4918 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4919 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4920 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4921 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4922 | IAccountManagerResponse response; |
| 4923 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4924 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4925 | response = mResponse; |
| 4926 | } else { |
| 4927 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4928 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4929 | if (response != null) { |
| 4930 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4931 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4932 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4933 | Log.v(TAG, getClass().getSimpleName() |
| 4934 | + " calling onError() on response " + response); |
| 4935 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4936 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4937 | "null bundle returned"); |
| 4938 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4939 | if (mStripAuthTokenFromResult) { |
| 4940 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4941 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4942 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4943 | Log.v(TAG, getClass().getSimpleName() |
| 4944 | + " calling onResult() on response " + response); |
| 4945 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4946 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4947 | (intent == null)) { |
| 4948 | // All AccountManager error codes are greater than 0 |
| 4949 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4950 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4951 | } else { |
| 4952 | response.onResult(result); |
| 4953 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4954 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4955 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4956 | // if the caller is dead then there is no one to care about remote exceptions |
| 4957 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4958 | Log.v(TAG, "failure while notifying response", e); |
| 4959 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4960 | } |
| 4961 | } |
| 4962 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4963 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4964 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4965 | public void onRequestContinued() { |
| 4966 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4967 | } |
| 4968 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4969 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4970 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4971 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4972 | IAccountManagerResponse response = getResponseAndClose(); |
| 4973 | if (response != null) { |
| 4974 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4975 | Log.v(TAG, getClass().getSimpleName() |
| 4976 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4977 | } |
| 4978 | try { |
| 4979 | response.onError(errorCode, errorMessage); |
| 4980 | } catch (RemoteException e) { |
| 4981 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4982 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4983 | } |
| 4984 | } |
| 4985 | } else { |
| 4986 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4987 | Log.v(TAG, "Session.onError: already closed"); |
| 4988 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4989 | } |
| 4990 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4991 | |
| 4992 | /** |
| 4993 | * find the component name for the authenticator and initiate a bind |
| 4994 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4995 | */ |
| 4996 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4997 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4998 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4999 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5000 | if (authenticatorInfo == null) { |
| 5001 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5002 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 5003 | + ", bailing out"); |
| 5004 | } |
| 5005 | return false; |
| 5006 | } |
| 5007 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 5008 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 5009 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 5010 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 5011 | + " which isn't encryption aware"); |
| 5012 | return false; |
| 5013 | } |
| 5014 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5015 | Intent intent = new Intent(); |
| 5016 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 5017 | intent.setComponent(authenticatorInfo.componentName); |
| 5018 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5019 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 5020 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 5021 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5022 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5023 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5024 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 5025 | } |
| 5026 | return false; |
| 5027 | } |
| 5028 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 5029 | return true; |
| 5030 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5031 | } |
| 5032 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5033 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5034 | MessageHandler(Looper looper) { |
| 5035 | super(looper); |
| 5036 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 5037 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 5038 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5039 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5040 | switch (msg.what) { |
| 5041 | case MESSAGE_TIMED_OUT: |
| 5042 | Session session = (Session)msg.obj; |
| 5043 | session.onTimedOut(); |
| 5044 | break; |
| 5045 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 5046 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 5047 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 5048 | break; |
| 5049 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5050 | default: |
| 5051 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 5052 | } |
| 5053 | } |
| 5054 | } |
| 5055 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5056 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5057 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5058 | } |
| 5059 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 5060 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5061 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 5062 | } |
| 5063 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5064 | /* |
| 5065 | * This function receives an opened writable database. |
| 5066 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5067 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5068 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5069 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5073 | * 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] | 5074 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5075 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5076 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5077 | |
| 5078 | class LogRecordTask implements Runnable { |
| 5079 | private final String action; |
| 5080 | private final String tableName; |
| 5081 | private final long accountId; |
| 5082 | private final UserAccounts userAccount; |
| 5083 | private final int callingUid; |
| 5084 | private final long userDebugDbInsertionPoint; |
| 5085 | |
| 5086 | LogRecordTask(final String action, |
| 5087 | final String tableName, |
| 5088 | final long accountId, |
| 5089 | final UserAccounts userAccount, |
| 5090 | final int callingUid, |
| 5091 | final long userDebugDbInsertionPoint) { |
| 5092 | this.action = action; |
| 5093 | this.tableName = tableName; |
| 5094 | this.accountId = accountId; |
| 5095 | this.userAccount = userAccount; |
| 5096 | this.callingUid = callingUid; |
| 5097 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 5098 | } |
| 5099 | |
Andrew Scull | c7770d6 | 2017-05-22 17:49:58 +0100 | [diff] [blame] | 5100 | @Override |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5101 | public void run() { |
| 5102 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 5103 | logStatement.bindLong(1, accountId); |
| 5104 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5105 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5106 | logStatement.bindLong(4, callingUid); |
| 5107 | logStatement.bindString(5, tableName); |
| 5108 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
Tetsutoki Shiozawa | be2d96a | 2017-10-24 18:44:00 +0900 | [diff] [blame] | 5109 | try { |
| 5110 | logStatement.execute(); |
| 5111 | } catch (IllegalStateException e) { |
| 5112 | // Guard against crash, DB can already be closed |
| 5113 | // since this statement is executed on a handler thread |
| 5114 | Slog.w(TAG, "Failed to insert a log record. accountId=" + accountId |
| 5115 | + " action=" + action + " tableName=" + tableName + " Error: " + e); |
| 5116 | } finally { |
| 5117 | logStatement.clearBindings(); |
| 5118 | } |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 5119 | } |
| 5120 | } |
| 5121 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 5122 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 5123 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5124 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5125 | % AccountsDb.MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 5126 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | /* |
| 5130 | * This should only be called once to compile the sql statement for logging |
| 5131 | * and to find the insertion point. |
| 5132 | */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5133 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(UserAccounts userAccount) { |
| 5134 | userAccount.debugDbInsertionPoint = userAccount.accountsDb |
| 5135 | .calculateDebugTableInsertionPoint(); |
| 5136 | userAccount.statementForLogging = userAccount.accountsDb.compileSqlStatementForLogging(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5137 | } |
| 5138 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5139 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5140 | return asBinder(); |
| 5141 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5142 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5143 | /** |
| 5144 | * Searches array of arguments for the specified string |
| 5145 | * @param args array of argument strings |
| 5146 | * @param value value to search for |
| 5147 | * @return true if the value is contained in the array |
| 5148 | */ |
| 5149 | private static boolean scanArgs(String[] args, String value) { |
| 5150 | if (args != null) { |
| 5151 | for (String arg : args) { |
| 5152 | if (value.equals(arg)) { |
| 5153 | return true; |
| 5154 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5155 | } |
| 5156 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5157 | return false; |
| 5158 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5159 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5160 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5161 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 5162 | if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5163 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5164 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 5165 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 5166 | final List<UserInfo> users = getUserManager().getUsers(); |
| 5167 | for (UserInfo user : users) { |
| 5168 | ipw.println("User " + user + ":"); |
| 5169 | ipw.increaseIndent(); |
| 5170 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 5171 | ipw.println(); |
| 5172 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5173 | } |
| 5174 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5175 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5176 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 5177 | String[] args, boolean isCheckinRequest) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5178 | if (isCheckinRequest) { |
| 5179 | // This is a checkin request. *Only* upload the account types and the count of |
| 5180 | // each. |
| 5181 | synchronized (userAccounts.dbLock) { |
| 5182 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
| 5183 | } |
| 5184 | } else { |
| 5185 | Account[] accounts = getAccountsFromCache(userAccounts, null /* type */, |
| 5186 | Process.SYSTEM_UID, null /* packageName */, false); |
| 5187 | fout.println("Accounts: " + accounts.length); |
| 5188 | for (Account account : accounts) { |
| 5189 | fout.println(" " + account); |
| 5190 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5191 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5192 | // Add debug information. |
| 5193 | fout.println(); |
| 5194 | synchronized (userAccounts.dbLock) { |
| 5195 | userAccounts.accountsDb.dumpDebugTable(fout); |
| 5196 | } |
| 5197 | fout.println(); |
| 5198 | synchronized (mSessions) { |
| 5199 | final long now = SystemClock.elapsedRealtime(); |
| 5200 | fout.println("Active Sessions: " + mSessions.size()); |
| 5201 | for (Session session : mSessions.values()) { |
| 5202 | fout.println(" " + session.toDebugString(now)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5203 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5204 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5205 | |
| 5206 | fout.println(); |
| 5207 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Tony Mak | 6d14d77 | 2017-07-13 17:49:46 +0800 | [diff] [blame] | 5208 | |
| 5209 | boolean isUserUnlocked; |
| 5210 | synchronized (mUsers) { |
| 5211 | isUserUnlocked = isLocalUnlockedUser(userAccounts.userId); |
| 5212 | } |
| 5213 | // Following logs are printed only when user is unlocked. |
| 5214 | if (!isUserUnlocked) { |
| 5215 | return; |
| 5216 | } |
| 5217 | fout.println(); |
| 5218 | synchronized (userAccounts.dbLock) { |
| 5219 | Map<Account, Map<String, Integer>> allVisibilityValues = |
| 5220 | userAccounts.accountsDb.findAllVisibilityValues(); |
| 5221 | fout.println("Account visibility:"); |
| 5222 | for (Account account : allVisibilityValues.keySet()) { |
| 5223 | fout.println(" " + account.name); |
| 5224 | Map<String, Integer> visibilities = allVisibilityValues.get(account); |
| 5225 | for (Entry<String, Integer> entry : visibilities.entrySet()) { |
| 5226 | fout.println(" " + entry.getKey() + ", " + entry.getValue()); |
| 5227 | } |
| 5228 | } |
| 5229 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 5230 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5231 | } |
| 5232 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5233 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5234 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5235 | long identityToken = clearCallingIdentity(); |
| 5236 | try { |
| 5237 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5238 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 5239 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5240 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5241 | if (intent.getComponent() != null && |
| 5242 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 5243 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5244 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5245 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5246 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5247 | final NotificationId id = getSigninRequiredNotificationId(accounts, account); |
| 5248 | intent.addCategory(id.mTag); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5249 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 5250 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5251 | contextForUser.getText(R.string.notification_title).toString(); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 5252 | Notification n = |
| 5253 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 5254 | .setWhen(0) |
| 5255 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 5256 | .setColor(contextForUser.getColor( |
| 5257 | com.android.internal.R.color.system_notification_accent_color)) |
| 5258 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 5259 | .setContentText(message) |
| 5260 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 5261 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5262 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 5263 | .build(); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5264 | installNotification(id, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5265 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5266 | } finally { |
| 5267 | restoreCallingIdentity(identityToken); |
| 5268 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5269 | } |
| 5270 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5271 | private void installNotification(NotificationId id, final Notification notification, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5272 | String packageName, int userId) { |
| 5273 | final long token = clearCallingIdentity(); |
| 5274 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5275 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5276 | try { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5277 | notificationManager.enqueueNotificationWithTag(packageName, packageName, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 5278 | id.mTag, id.mId, notification, userId); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5279 | } catch (RemoteException e) { |
| 5280 | /* ignore - local call */ |
| 5281 | } |
| 5282 | } finally { |
| 5283 | Binder.restoreCallingIdentity(token); |
| 5284 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5285 | } |
| 5286 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5287 | private void cancelNotification(NotificationId id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5288 | cancelNotification(id, mContext.getPackageName(), user); |
| 5289 | } |
| 5290 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5291 | private void cancelNotification(NotificationId id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5292 | long identityToken = clearCallingIdentity(); |
| 5293 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5294 | INotificationManager service = mInjector.getNotificationManager(); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5295 | service.cancelNotificationWithTag(packageName, id.mTag, id.mId, user.getIdentifier()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5296 | } catch (RemoteException e) { |
| 5297 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5298 | } finally { |
| 5299 | restoreCallingIdentity(identityToken); |
| 5300 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5301 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5302 | |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5303 | private boolean isPermittedForPackage(String packageName, int uid, int userId, |
| 5304 | String... permissions) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5305 | final long identity = Binder.clearCallingIdentity(); |
| 5306 | try { |
| 5307 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 5308 | for (String perm : permissions) { |
| 5309 | if (pm.checkPermission(perm, packageName, userId) |
| 5310 | == PackageManager.PERMISSION_GRANTED) { |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5311 | // Checks runtime permission revocation. |
| 5312 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
Tony Mak | 58f2815 | 2017-09-20 21:23:48 +0100 | [diff] [blame] | 5313 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOpNoThrow( |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5314 | opCode, uid, packageName) == AppOpsManager.MODE_ALLOWED) { |
| 5315 | return true; |
| 5316 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5317 | } |
| 5318 | } |
| 5319 | } catch (RemoteException e) { |
| 5320 | /* ignore - local call */ |
| 5321 | } finally { |
| 5322 | Binder.restoreCallingIdentity(identity); |
| 5323 | } |
| 5324 | return false; |
| 5325 | } |
| 5326 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5327 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 5328 | for (String perm : permissions) { |
| 5329 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 5330 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5331 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 5332 | } |
| 5333 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
Tony Mak | 58f2815 | 2017-09-20 21:23:48 +0100 | [diff] [blame] | 5334 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOpNoThrow( |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5335 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 5336 | return true; |
| 5337 | } |
| 5338 | } |
| 5339 | } |
| 5340 | return false; |
| 5341 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5342 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5343 | private int handleIncomingUser(int userId) { |
| 5344 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5345 | return ActivityManager.getService().handleIncomingUser( |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5346 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 5347 | } catch (RemoteException re) { |
| 5348 | // Shouldn't happen, local. |
| 5349 | } |
| 5350 | return userId; |
| 5351 | } |
| 5352 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 5353 | private boolean isPrivileged(int callingUid) { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5354 | String[] packages; |
| 5355 | long identityToken = Binder.clearCallingIdentity(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5356 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5357 | packages = mPackageManager.getPackagesForUid(callingUid); |
sunjian | 9ae597b6 | 2017-08-14 15:45:04 -0700 | [diff] [blame] | 5358 | if (packages == null) { |
| 5359 | Log.d(TAG, "No packages for callingUid " + callingUid); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5360 | return false; |
| 5361 | } |
sunjian | 9ae597b6 | 2017-08-14 15:45:04 -0700 | [diff] [blame] | 5362 | for (String name : packages) { |
| 5363 | try { |
| 5364 | PackageInfo packageInfo = |
| 5365 | mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5366 | if (packageInfo != null |
| 5367 | && (packageInfo.applicationInfo.privateFlags |
| 5368 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
| 5369 | return true; |
| 5370 | } |
| 5371 | } catch (PackageManager.NameNotFoundException e) { |
| 5372 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 5373 | } |
| 5374 | } |
| 5375 | } finally { |
| 5376 | Binder.restoreCallingIdentity(identityToken); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5377 | } |
| 5378 | return false; |
| 5379 | } |
| 5380 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5381 | private boolean permissionIsGranted( |
| 5382 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5383 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 5384 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5385 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 5386 | } |
| 5387 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5388 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5389 | |
| 5390 | if (isPrivileged(callerUid)) { |
| 5391 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5392 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5393 | + callerUid + " privileged"); |
| 5394 | } |
| 5395 | return true; |
| 5396 | } |
| 5397 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 5398 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5399 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5400 | + callerUid + " manages the account"); |
| 5401 | } |
| 5402 | return true; |
| 5403 | } |
| 5404 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5405 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5406 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5407 | + callerUid + " user granted access"); |
| 5408 | } |
| 5409 | return true; |
| 5410 | } |
| 5411 | |
| 5412 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5413 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5414 | } |
| 5415 | |
| 5416 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5417 | } |
| 5418 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5419 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5420 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5421 | if (accountType == null) { |
| 5422 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5423 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5424 | return getTypesVisibleToCaller(callingUid, userId, |
| 5425 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5426 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5429 | // Method checks visibility for applications targeing API level below {@link |
| 5430 | // android.os.Build.VERSION_CODES#O}, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5431 | // 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] | 5432 | private boolean checkGetAccountsPermission(String packageName, int uid, int userId) { |
| 5433 | return isPermittedForPackage(packageName, uid, userId, Manifest.permission.GET_ACCOUNTS, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5434 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 5435 | } |
| 5436 | |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5437 | private boolean checkReadContactsPermission(String packageName, int uid, int userId) { |
| 5438 | return isPermittedForPackage(packageName, uid, userId, Manifest.permission.READ_CONTACTS); |
| 5439 | } |
| 5440 | |
| 5441 | // Heuristic to check that account type may be associated with some contacts data and |
| 5442 | // therefore READ_CONTACTS permission grants the access to account by default. |
| 5443 | private boolean accountTypeManagesContacts(String accountType, int userId) { |
| 5444 | if (accountType == null) { |
| 5445 | return false; |
| 5446 | } |
| 5447 | long identityToken = Binder.clearCallingIdentity(); |
| 5448 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5449 | try { |
| 5450 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5451 | } finally { |
| 5452 | Binder.restoreCallingIdentity(identityToken); |
| 5453 | } |
| 5454 | // Check contacts related permissions for authenticator. |
| 5455 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5456 | : serviceInfos) { |
| 5457 | if (accountType.equals(serviceInfo.type.type)) { |
| 5458 | return isPermittedForPackage(serviceInfo.type.packageName, serviceInfo.uid, userId, |
| 5459 | Manifest.permission.WRITE_CONTACTS); |
| 5460 | } |
| 5461 | } |
| 5462 | return false; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | /** |
| 5466 | * Method checks package uid and signature with Authenticator which manages accountType. |
| 5467 | * |
| 5468 | * @return SIGNATURE_CHECK_UID_MATCH for uid match, SIGNATURE_CHECK_MATCH for signature match, |
| 5469 | * SIGNATURE_CHECK_MISMATCH otherwise. |
| 5470 | */ |
| 5471 | private int checkPackageSignature(String accountType, int callingUid, int userId) { |
| 5472 | if (accountType == null) { |
| 5473 | return SIGNATURE_CHECK_MISMATCH; |
| 5474 | } |
| 5475 | |
| 5476 | long identityToken = Binder.clearCallingIdentity(); |
| 5477 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5478 | try { |
| 5479 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5480 | } finally { |
| 5481 | Binder.restoreCallingIdentity(identityToken); |
| 5482 | } |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame^] | 5483 | // Check for signature match with Authenticator.LocalServices.getService(PackageManagerInternal.class); |
| 5484 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5485 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5486 | : serviceInfos) { |
| 5487 | if (accountType.equals(serviceInfo.type.type)) { |
| 5488 | if (serviceInfo.uid == callingUid) { |
| 5489 | return SIGNATURE_CHECK_UID_MATCH; |
| 5490 | } |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame^] | 5491 | if (pmi.hasSignatureCapability( |
| 5492 | serviceInfo.uid, callingUid, |
| 5493 | PackageParser.SigningDetails.CertCapabilities.AUTH)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5494 | return SIGNATURE_CHECK_MATCH; |
| 5495 | } |
| 5496 | } |
| 5497 | } |
| 5498 | return SIGNATURE_CHECK_MISMATCH; |
| 5499 | } |
| 5500 | |
| 5501 | // returns true for applications with the same signature as authenticator. |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5502 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5503 | if (accountType == null) { |
| 5504 | return false; |
| 5505 | } else { |
| 5506 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5507 | } |
| 5508 | } |
| 5509 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5510 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5511 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5512 | return getTypesForCaller(callingUid, userId, true /* isOtherwisePermitted*/); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5513 | } |
| 5514 | |
| 5515 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
Dmitry Dementyev | 2e22cfb | 2017-01-09 18:42:14 +0000 | [diff] [blame] | 5516 | return getTypesForCaller(callingUid, userId, false); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | private List<String> getTypesForCaller( |
| 5520 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5521 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5522 | long identityToken = Binder.clearCallingIdentity(); |
| 5523 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5524 | try { |
| 5525 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5526 | } finally { |
| 5527 | Binder.restoreCallingIdentity(identityToken); |
| 5528 | } |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame^] | 5529 | |
| 5530 | PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5531 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5532 | serviceInfos) { |
Dan Cashman | 303c4bb | 2018-04-10 07:41:16 -0700 | [diff] [blame^] | 5533 | if (isOtherwisePermitted || pmi.hasSignatureCapability( |
| 5534 | serviceInfo.uid, callingUid, |
| 5535 | PackageParser.SigningDetails.CertCapabilities.AUTH)) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5536 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5537 | } |
| 5538 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5539 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5540 | } |
| 5541 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5542 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5543 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5544 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5545 | if (account.name.equals(accountName)) { |
| 5546 | return true; |
| 5547 | } |
| 5548 | } |
| 5549 | } |
| 5550 | return false; |
| 5551 | } |
| 5552 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5553 | private static void checkManageUsersPermission(String message) { |
| 5554 | if (ActivityManager.checkComponentPermission( |
| 5555 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5556 | != PackageManager.PERMISSION_GRANTED) { |
| 5557 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5558 | } |
| 5559 | } |
| 5560 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5561 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5562 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5563 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5564 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5565 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5566 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5567 | + message); |
| 5568 | } |
| 5569 | } |
| 5570 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5571 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5572 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5573 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5574 | return true; |
| 5575 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5576 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5577 | synchronized (accounts.dbLock) { |
| 5578 | synchronized (accounts.cacheLock) { |
| 5579 | long grantsCount; |
| 5580 | if (authTokenType != null) { |
| 5581 | grantsCount = accounts.accountsDb |
| 5582 | .findMatchingGrantsCount(callerUid, authTokenType, account); |
| 5583 | } else { |
| 5584 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
| 5585 | account); |
| 5586 | } |
| 5587 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5588 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5589 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5590 | // TODO: Skip this check when running automated tests. Replace this |
| 5591 | // with a more general solution. |
| 5592 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
| 5593 | + authTokenType + " by uid " + callerUid |
| 5594 | + " but ignoring since device is in test harness."); |
| 5595 | return true; |
| 5596 | } |
| 5597 | return permissionGranted; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5598 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5599 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5600 | } |
| 5601 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5602 | private boolean isSystemUid(int callingUid) { |
| 5603 | String[] packages = null; |
| 5604 | long ident = Binder.clearCallingIdentity(); |
| 5605 | try { |
| 5606 | packages = mPackageManager.getPackagesForUid(callingUid); |
Alex Chau | 81a47cd | 2018-01-02 16:49:14 +0000 | [diff] [blame] | 5607 | if (packages != null) { |
| 5608 | for (String name : packages) { |
| 5609 | try { |
| 5610 | PackageInfo packageInfo = |
| 5611 | mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5612 | if (packageInfo != null |
| 5613 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5614 | != 0) { |
| 5615 | return true; |
| 5616 | } |
| 5617 | } catch (NameNotFoundException e) { |
| 5618 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5619 | } |
| 5620 | } |
| 5621 | } else { |
| 5622 | Log.w(TAG, "No known packages with uid " + callingUid); |
| 5623 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5624 | } finally { |
| 5625 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5626 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5627 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5630 | /** Succeeds if any of the specified permissions are granted. */ |
| 5631 | private void checkReadAccountsPermitted( |
| 5632 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5633 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5634 | int userId, |
| 5635 | String opPackageName) { |
| 5636 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5637 | String msg = String.format( |
| 5638 | "caller uid %s cannot access %s accounts", |
| 5639 | callingUid, |
| 5640 | accountType); |
| 5641 | Log.w(TAG, " " + msg); |
| 5642 | throw new SecurityException(msg); |
| 5643 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5644 | } |
| 5645 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5646 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5647 | // the managing app can always modify accounts |
| 5648 | if (isProfileOwner(callingUid)) { |
| 5649 | return true; |
| 5650 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5651 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5652 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5653 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5654 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5655 | return true; |
| 5656 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5657 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5658 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5659 | // the managing app can always modify accounts |
| 5660 | if (isProfileOwner(callingUid)) { |
| 5661 | return true; |
| 5662 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5663 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5664 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5665 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5666 | if (typesArray == null) { |
| 5667 | return true; |
| 5668 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5669 | for (String forbiddenType : typesArray) { |
| 5670 | if (forbiddenType.equals(accountType)) { |
| 5671 | return false; |
| 5672 | } |
| 5673 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5674 | return true; |
| 5675 | } |
| 5676 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5677 | private boolean isProfileOwner(int uid) { |
| 5678 | final DevicePolicyManagerInternal dpmi = |
| 5679 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5680 | return (dpmi != null) |
| 5681 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5682 | } |
| 5683 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5684 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5685 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5686 | throws RemoteException { |
| 5687 | final int callingUid = getCallingUid(); |
| 5688 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5689 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5690 | throw new SecurityException(); |
| 5691 | } |
| 5692 | |
| 5693 | if (value) { |
| 5694 | grantAppPermission(account, authTokenType, uid); |
| 5695 | } else { |
| 5696 | revokeAppPermission(account, authTokenType, uid); |
| 5697 | } |
| 5698 | } |
| 5699 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5700 | /** |
| 5701 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5702 | * <p> |
| 5703 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5704 | * which is in the system. This means we don't need to protect it with permissions. |
| 5705 | * @hide |
| 5706 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5707 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5708 | if (account == null || authTokenType == null) { |
| 5709 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5710 | return; |
| 5711 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5712 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5713 | synchronized (accounts.dbLock) { |
| 5714 | synchronized (accounts.cacheLock) { |
| 5715 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5716 | if (accountId >= 0) { |
| 5717 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
| 5718 | } |
| 5719 | cancelNotification( |
| 5720 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5721 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5722 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5723 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
| 5724 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5725 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5726 | |
| 5727 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5728 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5729 | : mAppPermissionChangeListeners) { |
| 5730 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5731 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5732 | } |
| 5733 | |
| 5734 | /** |
| 5735 | * Don't allow callers with the given uid permission to get credentials for |
| 5736 | * account/authTokenType. |
| 5737 | * <p> |
| 5738 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5739 | * which is in the system. This means we don't need to protect it with permissions. |
| 5740 | * @hide |
| 5741 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5742 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5743 | if (account == null || authTokenType == null) { |
| 5744 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5745 | return; |
| 5746 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5747 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5748 | synchronized (accounts.dbLock) { |
| 5749 | synchronized (accounts.cacheLock) { |
| 5750 | accounts.accountsDb.beginTransaction(); |
| 5751 | try { |
| 5752 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5753 | if (accountId >= 0) { |
| 5754 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5755 | accountId, authTokenType, uid); |
| 5756 | accounts.accountsDb.setTransactionSuccessful(); |
| 5757 | } |
| 5758 | } finally { |
| 5759 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5760 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5761 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5762 | cancelNotification( |
| 5763 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5764 | UserHandle.of(accounts.userId)); |
| 5765 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5766 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5767 | |
| 5768 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5769 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5770 | : mAppPermissionChangeListeners) { |
| 5771 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5772 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5773 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5774 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5775 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5776 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5777 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5778 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5779 | for (Account curAccount : oldAccountsForType) { |
| 5780 | if (!curAccount.equals(account)) { |
| 5781 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5782 | } |
| 5783 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5784 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5785 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5786 | } else { |
| 5787 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5788 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5789 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5790 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5791 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5792 | accounts.userDataCache.remove(account); |
| 5793 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5794 | accounts.previousNameCache.remove(account); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 5795 | accounts.visibilityCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5796 | } |
| 5797 | |
| 5798 | /** |
| 5799 | * 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] | 5800 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5801 | * processes and if you will return this account to apps you should return the result. |
| 5802 | * @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] | 5803 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5804 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5805 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5806 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5807 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5808 | if (accountsForType != null) { |
| 5809 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5810 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5811 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5812 | : UUID.randomUUID().toString(); |
| 5813 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5814 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5815 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5816 | } |
| 5817 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5818 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5819 | private Account[] filterAccounts(UserAccounts accounts, Account[] unfiltered, int callingUid, |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5820 | @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5821 | String visibilityFilterPackage = callingPackage; |
| 5822 | if (visibilityFilterPackage == null) { |
| 5823 | visibilityFilterPackage = getPackageNameForUid(callingUid); |
| 5824 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5825 | Map<Account, Integer> firstPass = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5826 | for (Account account : unfiltered) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5827 | int visibility = resolveAccountVisibility(account, visibilityFilterPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5828 | if ((visibility == AccountManager.VISIBILITY_VISIBLE |
| 5829 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) |
| 5830 | || (includeManagedNotVisible |
| 5831 | && (visibility |
| 5832 | == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE))) { |
| 5833 | firstPass.put(account, visibility); |
| 5834 | } |
| 5835 | } |
| 5836 | Map<Account, Integer> secondPass = |
| 5837 | filterSharedAccounts(accounts, firstPass, callingUid, callingPackage); |
| 5838 | |
| 5839 | Account[] filtered = new Account[secondPass.size()]; |
| 5840 | filtered = secondPass.keySet().toArray(filtered); |
| 5841 | return filtered; |
| 5842 | } |
| 5843 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5844 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5845 | private Map<Account, Integer> filterSharedAccounts(UserAccounts userAccounts, |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5846 | @NonNull Map<Account, Integer> unfiltered, int callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5847 | @Nullable String callingPackage) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5848 | // first part is to filter shared accounts. |
| 5849 | // unfiltered type check is not necessary. |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5850 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5851 | || callingUid == Process.SYSTEM_UID) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5852 | return unfiltered; |
| 5853 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5854 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5855 | if (user != null && user.isRestricted()) { |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5856 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5857 | if (packages == null) { |
| 5858 | packages = new String[] {}; |
| 5859 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5860 | // 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] | 5861 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5862 | // This might be a temporary way to specify a visible list |
| 5863 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5864 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5865 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5866 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5867 | return unfiltered; |
| 5868 | } |
| 5869 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5870 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5871 | if (ArrayUtils.isEmpty(sharedAccounts)) { |
| 5872 | return unfiltered; |
| 5873 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5874 | String requiredAccountType = ""; |
| 5875 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5876 | // If there's an explicit callingPackage specified, check if that package |
| 5877 | // opted in to see restricted accounts. |
| 5878 | if (callingPackage != null) { |
| 5879 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5880 | if (pi != null && pi.restrictedAccountType != null) { |
| 5881 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5882 | } |
| 5883 | } else { |
| 5884 | // Otherwise check if the callingUid has a package that has opted in |
| 5885 | for (String packageName : packages) { |
| 5886 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5887 | if (pi != null && pi.restrictedAccountType != null) { |
| 5888 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5889 | break; |
| 5890 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5891 | } |
| 5892 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5893 | } catch (NameNotFoundException e) { |
| 5894 | Log.d(TAG, "Package not found " + e.getMessage()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5895 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5896 | Map<Account, Integer> filtered = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5897 | for (Map.Entry<Account, Integer> entry : unfiltered.entrySet()) { |
| 5898 | Account account = entry.getKey(); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5899 | if (account.type.equals(requiredAccountType)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5900 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5901 | } else { |
| 5902 | boolean found = false; |
| 5903 | for (Account shared : sharedAccounts) { |
| 5904 | if (shared.equals(account)) { |
| 5905 | found = true; |
| 5906 | break; |
| 5907 | } |
| 5908 | } |
| 5909 | if (!found) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5910 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5911 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5912 | } |
| 5913 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5914 | return filtered; |
| 5915 | } else { |
| 5916 | return unfiltered; |
| 5917 | } |
| 5918 | } |
| 5919 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5920 | /* |
| 5921 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5922 | * that the package is not allowed to access. |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5923 | * |
| 5924 | * <p>The method shouldn't be called with UserAccounts#cacheLock held, otherwise it will cause a |
| 5925 | * deadlock |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5926 | */ |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5927 | @NonNull |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5928 | protected Account[] getAccountsFromCache(UserAccounts userAccounts, String accountType, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5929 | int callingUid, @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5930 | Preconditions.checkState(!Thread.holdsLock(userAccounts.cacheLock), |
| 5931 | "Method should not be called with cacheLock"); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5932 | if (accountType != null) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5933 | Account[] accounts; |
| 5934 | synchronized (userAccounts.cacheLock) { |
| 5935 | accounts = userAccounts.accountCache.get(accountType); |
| 5936 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5937 | if (accounts == null) { |
| 5938 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5939 | } else { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5940 | return filterAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
| 5941 | callingUid, callingPackage, includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5942 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5943 | } else { |
| 5944 | int totalLength = 0; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5945 | Account[] accountsArray; |
| 5946 | synchronized (userAccounts.cacheLock) { |
| 5947 | for (Account[] accounts : userAccounts.accountCache.values()) { |
| 5948 | totalLength += accounts.length; |
| 5949 | } |
| 5950 | if (totalLength == 0) { |
| 5951 | return EMPTY_ACCOUNT_ARRAY; |
| 5952 | } |
| 5953 | accountsArray = new Account[totalLength]; |
| 5954 | totalLength = 0; |
| 5955 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
| 5956 | System.arraycopy(accountsOfType, 0, accountsArray, totalLength, |
| 5957 | accountsOfType.length); |
| 5958 | totalLength += accountsOfType.length; |
| 5959 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5960 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5961 | return filterAccounts(userAccounts, accountsArray, callingUid, callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5962 | includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5963 | } |
| 5964 | } |
| 5965 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 5966 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5967 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5968 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5969 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5970 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5971 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5972 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5973 | } |
| 5974 | if (value == null) { |
| 5975 | userDataForAccount.remove(key); |
| 5976 | } else { |
| 5977 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5978 | } |
| 5979 | } |
| 5980 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5981 | protected String readCachedTokenInternal( |
| 5982 | UserAccounts accounts, |
| 5983 | Account account, |
| 5984 | String tokenType, |
| 5985 | String callingPackage, |
| 5986 | byte[] pkgSigDigest) { |
Dmitry Dementyev | 18f0ca9 | 2017-06-12 17:56:47 -0700 | [diff] [blame] | 5987 | synchronized (accounts.cacheLock) { |
| 5988 | return accounts.accountTokenCaches.get( |
| 5989 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5993 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5994 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5995 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5996 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5997 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5998 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5999 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 6000 | } |
| 6001 | if (value == null) { |
| 6002 | authTokensForAccount.remove(key); |
| 6003 | } else { |
| 6004 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6005 | } |
| 6006 | } |
| 6007 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 6008 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 6009 | String authTokenType) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 6010 | // Fast path - check if account is already cached |
| 6011 | synchronized (accounts.cacheLock) { |
| 6012 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 6013 | if (authTokensForAccount != null) { |
| 6014 | return authTokensForAccount.get(authTokenType); |
| 6015 | } |
| 6016 | } |
| 6017 | // 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] | 6018 | synchronized (accounts.dbLock) { |
| 6019 | synchronized (accounts.cacheLock) { |
| 6020 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 6021 | if (authTokensForAccount == null) { |
| 6022 | // need to populate the cache for this account |
| 6023 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
| 6024 | accounts.authTokenCache.put(account, authTokensForAccount); |
| 6025 | } |
| 6026 | return authTokensForAccount.get(authTokenType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6027 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6028 | } |
| 6029 | } |
| 6030 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 6031 | private String readUserDataInternal(UserAccounts accounts, Account account, String key) { |
| 6032 | Map<String, String> userDataForAccount; |
| 6033 | // Fast path - check if data is already cached |
| 6034 | synchronized (accounts.cacheLock) { |
| 6035 | userDataForAccount = accounts.userDataCache.get(account); |
| 6036 | } |
| 6037 | // 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] | 6038 | if (userDataForAccount == null) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 6039 | synchronized (accounts.dbLock) { |
| 6040 | synchronized (accounts.cacheLock) { |
| 6041 | userDataForAccount = accounts.userDataCache.get(account); |
| 6042 | if (userDataForAccount == null) { |
| 6043 | // need to populate the cache for this account |
| 6044 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
| 6045 | accounts.userDataCache.put(account, userDataForAccount); |
| 6046 | } |
| 6047 | } |
| 6048 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6049 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 6050 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 6051 | } |
| 6052 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 6053 | private Context getContextForUser(UserHandle user) { |
| 6054 | try { |
| 6055 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 6056 | } catch (NameNotFoundException e) { |
| 6057 | // Default to mContext, not finding the package system is running as is unlikely. |
| 6058 | return mContext; |
| 6059 | } |
| 6060 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 6061 | |
| 6062 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 6063 | try { |
| 6064 | response.onResult(result); |
| 6065 | } catch (RemoteException e) { |
| 6066 | // if the caller is dead then there is no one to care about remote |
| 6067 | // exceptions |
| 6068 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 6069 | Log.v(TAG, "failure while notifying response", e); |
| 6070 | } |
| 6071 | } |
| 6072 | } |
| 6073 | |
| 6074 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 6075 | String errorMessage) { |
| 6076 | try { |
| 6077 | response.onError(errorCode, errorMessage); |
| 6078 | } catch (RemoteException e) { |
| 6079 | // if the caller is dead then there is no one to care about remote |
| 6080 | // exceptions |
| 6081 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 6082 | Log.v(TAG, "failure while notifying response", e); |
| 6083 | } |
| 6084 | } |
| 6085 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 6086 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6087 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 6088 | private final Object mLock = new Object(); |
| 6089 | |
| 6090 | @GuardedBy("mLock") |
| 6091 | private AccountManagerBackupHelper mBackupHelper; |
| 6092 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6093 | @Override |
| 6094 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 6095 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 6096 | if (account == null) { |
| 6097 | Slog.w(TAG, "account cannot be null"); |
| 6098 | return; |
| 6099 | } |
| 6100 | if (packageName == null) { |
| 6101 | Slog.w(TAG, "packageName cannot be null"); |
| 6102 | return; |
| 6103 | } |
| 6104 | if (userId < UserHandle.USER_SYSTEM) { |
| 6105 | Slog.w(TAG, "user id must be concrete"); |
| 6106 | return; |
| 6107 | } |
| 6108 | if (callback == null) { |
| 6109 | Slog.w(TAG, "callback cannot be null"); |
| 6110 | return; |
| 6111 | } |
| 6112 | |
Dmitry Dementyev | 7b3ea13 | 2017-05-10 12:45:02 -0700 | [diff] [blame] | 6113 | int visibility = |
| 6114 | resolveAccountVisibility(account, packageName, getUserAccounts(userId)); |
| 6115 | if (visibility == AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 6116 | Slog.w(TAG, "requestAccountAccess: account is hidden"); |
| 6117 | return; |
| 6118 | } |
| 6119 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 6120 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 6121 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6122 | Bundle result = new Bundle(); |
| 6123 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 6124 | callback.sendResult(result); |
| 6125 | return; |
| 6126 | } |
| 6127 | |
| 6128 | final int uid; |
| 6129 | try { |
| 6130 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 6131 | } catch (NameNotFoundException e) { |
| 6132 | Slog.e(TAG, "Unknown package " + packageName); |
| 6133 | return; |
| 6134 | } |
| 6135 | |
| 6136 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 6137 | final UserAccounts userAccounts; |
| 6138 | synchronized (mUsers) { |
| 6139 | userAccounts = mUsers.get(userId); |
| 6140 | } |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 6141 | SystemNotificationChannels.createAccountChannelForPackage(packageName, uid, mContext); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 6142 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 6143 | } |
| 6144 | |
| 6145 | @Override |
| 6146 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 6147 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 6148 | mAppPermissionChangeListeners.add(listener); |
| 6149 | } |
| 6150 | |
| 6151 | @Override |
| 6152 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 6153 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 6154 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 6155 | |
| 6156 | @Override |
| 6157 | public byte[] backupAccountAccessPermissions(int userId) { |
| 6158 | synchronized (mLock) { |
| 6159 | if (mBackupHelper == null) { |
| 6160 | mBackupHelper = new AccountManagerBackupHelper( |
| 6161 | AccountManagerService.this, this); |
| 6162 | } |
| 6163 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 6164 | } |
| 6165 | } |
| 6166 | |
| 6167 | @Override |
| 6168 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 6169 | synchronized (mLock) { |
| 6170 | if (mBackupHelper == null) { |
| 6171 | mBackupHelper = new AccountManagerBackupHelper( |
| 6172 | AccountManagerService.this, this); |
| 6173 | } |
| 6174 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 6175 | } |
| 6176 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 6177 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6178 | |
| 6179 | @VisibleForTesting |
| 6180 | static class Injector { |
| 6181 | private final Context mContext; |
| 6182 | |
| 6183 | public Injector(Context context) { |
| 6184 | mContext = context; |
| 6185 | } |
| 6186 | |
| 6187 | Looper getMessageHandlerLooper() { |
| 6188 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 6189 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 6190 | serviceThread.start(); |
| 6191 | return serviceThread.getLooper(); |
| 6192 | } |
| 6193 | |
| 6194 | Context getContext() { |
| 6195 | return mContext; |
| 6196 | } |
| 6197 | |
| 6198 | void addLocalService(AccountManagerInternal service) { |
| 6199 | LocalServices.addService(AccountManagerInternal.class, service); |
| 6200 | } |
| 6201 | |
| 6202 | String getDeDatabaseName(int userId) { |
| 6203 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 6204 | AccountsDb.DE_DATABASE_NAME); |
| 6205 | return databaseFile.getPath(); |
| 6206 | } |
| 6207 | |
| 6208 | String getCeDatabaseName(int userId) { |
| 6209 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 6210 | AccountsDb.CE_DATABASE_NAME); |
| 6211 | return databaseFile.getPath(); |
| 6212 | } |
| 6213 | |
| 6214 | String getPreNDatabaseName(int userId) { |
| 6215 | File systemDir = Environment.getDataSystemDirectory(); |
| 6216 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 6217 | PRE_N_DATABASE_NAME); |
| 6218 | if (userId == 0) { |
| 6219 | // Migrate old file, if it exists, to the new location. |
| 6220 | // 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] | 6221 | // accidentally created in the old location, |
| 6222 | // causing the new one to become corrupted as well. |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6223 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 6224 | if (oldFile.exists() && !databaseFile.exists()) { |
| 6225 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 6226 | File userDir = Environment.getUserSystemDirectory(userId); |
| 6227 | if (!userDir.exists()) { |
| 6228 | if (!userDir.mkdirs()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 6229 | throw new IllegalStateException( |
| 6230 | "User dir cannot be created: " + userDir); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6231 | } |
| 6232 | } |
| 6233 | if (!oldFile.renameTo(databaseFile)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 6234 | throw new IllegalStateException( |
| 6235 | "User dir cannot be migrated: " + databaseFile); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 6236 | } |
| 6237 | } |
| 6238 | } |
| 6239 | return databaseFile.getPath(); |
| 6240 | } |
| 6241 | |
| 6242 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 6243 | return new AccountAuthenticatorCache(mContext); |
| 6244 | } |
| 6245 | |
| 6246 | INotificationManager getNotificationManager() { |
| 6247 | return NotificationManager.getService(); |
| 6248 | } |
| 6249 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 6250 | |
Andrew Scull | c7770d6 | 2017-05-22 17:49:58 +0100 | [diff] [blame] | 6251 | private static class NotificationId { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 6252 | final String mTag; |
| 6253 | private final int mId; |
| 6254 | |
| 6255 | NotificationId(String tag, int type) { |
| 6256 | mTag = tag; |
| 6257 | mId = type; |
| 6258 | } |
| 6259 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 6260 | } |