Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.accounts; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 18 | |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 20 | import android.accounts.AbstractAccountAuthenticator; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 21 | import android.accounts.Account; |
| 22 | import android.accounts.AccountAndUser; |
| 23 | import android.accounts.AccountAuthenticatorResponse; |
| 24 | import android.accounts.AccountManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 25 | import android.accounts.AccountManagerInternal; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 26 | import android.accounts.AuthenticatorDescription; |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 27 | import android.accounts.CantAddAccountActivity; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 28 | import android.accounts.GrantCredentialsPermissionActivity; |
| 29 | import android.accounts.IAccountAuthenticator; |
| 30 | import android.accounts.IAccountAuthenticatorResponse; |
| 31 | import android.accounts.IAccountManager; |
| 32 | import android.accounts.IAccountManagerResponse; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 33 | import android.annotation.IntRange; |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 34 | import android.annotation.NonNull; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 35 | import android.annotation.Nullable; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 36 | import android.app.ActivityManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 37 | import android.app.ActivityThread; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 38 | import android.app.AppOpsManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 39 | import android.app.INotificationManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 40 | import android.app.Notification; |
| 41 | import android.app.NotificationManager; |
| 42 | import android.app.PendingIntent; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 43 | import android.app.admin.DeviceAdminInfo; |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 44 | import android.app.admin.DevicePolicyManager; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 45 | import android.app.admin.DevicePolicyManagerInternal; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 46 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 47 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 48 | import android.content.Context; |
| 49 | import android.content.Intent; |
| 50 | import android.content.IntentFilter; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 51 | import android.content.IntentSender; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 52 | import android.content.ServiceConnection; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 53 | import android.content.pm.ActivityInfo; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 54 | import android.content.pm.ApplicationInfo; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 55 | import android.content.pm.IPackageManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 56 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 57 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 58 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 59 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 60 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 61 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 62 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 63 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 64 | import android.database.Cursor; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 65 | import android.database.sqlite.SQLiteStatement; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 66 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 67 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 68 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 69 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 70 | import android.os.IBinder; |
| 71 | import android.os.Looper; |
| 72 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 73 | import android.os.Parcel; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 74 | import android.os.Process; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 75 | import android.os.RemoteCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 76 | import android.os.RemoteException; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 77 | import android.os.StrictMode; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 78 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 79 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 80 | import android.os.UserManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 81 | import android.text.TextUtils; |
| 82 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 83 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 84 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 85 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 86 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 87 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 88 | import com.android.internal.R; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 89 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 90 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 91 | import com.android.internal.content.PackageMonitor; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 92 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 93 | import com.android.internal.notification.SystemNotificationChannels; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 94 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 95 | import com.android.internal.util.DumpUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 96 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 97 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 98 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 99 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 100 | import com.android.server.SystemService; |
| 101 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 102 | import com.google.android.collect.Lists; |
| 103 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 104 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 105 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 106 | import java.io.FileDescriptor; |
| 107 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 108 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 109 | import java.security.MessageDigest; |
| 110 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 111 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 112 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 113 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 114 | import java.util.Collection; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 115 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 116 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 117 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 118 | import java.util.LinkedHashMap; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 119 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 120 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 121 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 122 | import java.util.Objects; |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 123 | import java.util.Set; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 124 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 125 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 126 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 127 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 128 | /** |
| 129 | * A system service that provides account, password, and authtoken management for all |
| 130 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 131 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 132 | * 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] | 133 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 134 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 135 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 136 | public class AccountManagerService |
| 137 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 138 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 139 | private static final String TAG = "AccountManagerService"; |
| 140 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 141 | public static class Lifecycle extends SystemService { |
| 142 | private AccountManagerService mService; |
| 143 | |
| 144 | public Lifecycle(Context context) { |
| 145 | super(context); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onStart() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 150 | mService = new AccountManagerService(new Injector(getContext())); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 151 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 152 | } |
| 153 | |
| 154 | @Override |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 155 | public void onUnlockUser(int userHandle) { |
| 156 | mService.onUnlockUser(userHandle); |
| 157 | } |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 158 | |
| 159 | @Override |
| 160 | public void onCleanupUser(int userHandle) { |
| 161 | mService.onCleanupUser(userHandle); |
| 162 | } |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 163 | } |
| 164 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 165 | final Context mContext; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 166 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 167 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 168 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 169 | private UserManager mUserManager; |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 170 | private final Injector mInjector; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 171 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 172 | final MessageHandler mHandler; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 173 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 174 | // Messages that can be sent on mHandler |
| 175 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 176 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 177 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 178 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 179 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 180 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 181 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 182 | private static final int SIGNATURE_CHECK_MISMATCH = 0; |
| 183 | private static final int SIGNATURE_CHECK_MATCH = 1; |
| 184 | private static final int SIGNATURE_CHECK_UID_MATCH = 2; |
| 185 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 186 | static { |
| 187 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
Christopher Tate | bded68f | 2017-02-21 11:41:55 -0800 | [diff] [blame] | 188 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 189 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 190 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 191 | |
| 192 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 193 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 194 | static class UserAccounts { |
| 195 | private final int userId; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 196 | final AccountsDb accountsDb; |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 197 | private final HashMap<Pair<Pair<Account, String>, Integer>, NotificationId> |
| 198 | credentialsPermissionNotificationIds = new HashMap<>(); |
| 199 | private final HashMap<Account, NotificationId> signinRequiredNotificationIds |
| 200 | = new HashMap<>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 201 | final Object cacheLock = new Object(); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 202 | 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] | 203 | /** protected by the {@link #cacheLock} */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 204 | final HashMap<String, Account[]> accountCache = new LinkedHashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 205 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 206 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 207 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 208 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 209 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 210 | private final TokenCache accountTokenCaches = new TokenCache(); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 211 | /** protected by the {@link #cacheLock} */ |
| 212 | private final Map<Account, Map<String, Integer>> visibilityCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 213 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 214 | /** protected by the {@link #mReceiversForType}, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 215 | * type -> (packageName -> number of active receivers) |
| 216 | * type == null is used to get notifications about all account types |
| 217 | */ |
| 218 | private final Map<String, Map<String, Integer>> mReceiversForType = new HashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 219 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 220 | /** |
| 221 | * protected by the {@link #cacheLock} |
| 222 | * |
| 223 | * Caches the previous names associated with an account. Previous names |
| 224 | * should be cached because we expect that when an Account is renamed, |
| 225 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 226 | * want to know if the accounts they care about have been renamed. |
| 227 | * |
| 228 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 229 | * we can distinguish between those accounts with no previous names and |
| 230 | * those whose previous names haven't been cached (yet). |
| 231 | */ |
| 232 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 233 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 234 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 235 | private int debugDbInsertionPoint = -1; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 236 | private SQLiteStatement statementForLogging; // TODO Move to AccountsDb |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 237 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 238 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 239 | this.userId = userId; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 240 | synchronized (dbLock) { |
| 241 | synchronized (cacheLock) { |
| 242 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
| 243 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 248 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 249 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 250 | // Not thread-safe. Only use in synchronized context |
| 251 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 252 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 253 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 254 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 255 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 256 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 257 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 258 | /** |
| 259 | * This should only be called by system code. One should only call this after the service |
| 260 | * has started. |
| 261 | * @return a reference to the AccountManagerService instance |
| 262 | * @hide |
| 263 | */ |
| 264 | public static AccountManagerService getSingleton() { |
| 265 | return sThis.get(); |
| 266 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 267 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 268 | public AccountManagerService(Injector injector) { |
| 269 | mInjector = injector; |
| 270 | mContext = injector.getContext(); |
| 271 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 272 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 273 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 274 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 275 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 276 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 277 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 278 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 279 | IntentFilter intentFilter = new IntentFilter(); |
| 280 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 281 | intentFilter.addDataScheme("package"); |
| 282 | mContext.registerReceiver(new BroadcastReceiver() { |
| 283 | @Override |
| 284 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 285 | // Don't delete accounts when updating a authenticator's |
| 286 | // package. |
| 287 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 288 | /* Purging data requires file io, don't block the main thread. This is probably |
| 289 | * less than ideal because we are introducing a race condition where old grants |
| 290 | * could be exercised until they are purged. But that race condition existed |
| 291 | * anyway with the broadcast receiver. |
| 292 | * |
| 293 | * Ideally, we would completely clear the cache, purge data from the database, |
| 294 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 295 | * large at this point. |
| 296 | */ |
Dmitry Dementyev | 0b67642 | 2017-03-09 11:51:26 -0800 | [diff] [blame] | 297 | final String removedPackageName = intent.getData().getSchemeSpecificPart(); |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 298 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 299 | @Override |
| 300 | public void run() { |
| 301 | purgeOldGrantsAll(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 302 | // Notify authenticator about removed app? |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 303 | removeVisibilityValuesForPackage(removedPackageName); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 304 | } |
| 305 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 306 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 307 | } |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 308 | } |
| 309 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 310 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 311 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 312 | |
| 313 | // Need to cancel account request notifications if the update/install can access the account |
| 314 | new PackageMonitor() { |
| 315 | @Override |
| 316 | public void onPackageAdded(String packageName, int uid) { |
| 317 | // Called on a handler, and running as the system |
| 318 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 323 | // Called on a handler, and running as the system |
| 324 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 325 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 326 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 327 | |
| 328 | // Cancel account request notification if an app op was preventing the account access |
| 329 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 330 | new AppOpsManager.OnOpChangedInternalListener() { |
| 331 | @Override |
| 332 | public void onOpChanged(int op, String packageName) { |
| 333 | try { |
| 334 | final int userId = ActivityManager.getCurrentUser(); |
| 335 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 336 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 337 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 338 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 339 | final long identity = Binder.clearCallingIdentity(); |
| 340 | try { |
| 341 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 342 | } finally { |
| 343 | Binder.restoreCallingIdentity(identity); |
| 344 | } |
| 345 | } |
| 346 | } catch (NameNotFoundException e) { |
| 347 | /* ignore */ |
| 348 | } |
| 349 | } |
| 350 | }); |
| 351 | |
| 352 | // Cancel account request notification if a permission was preventing the account access |
| 353 | mPackageManager.addOnPermissionsChangeListener( |
| 354 | (int uid) -> { |
| 355 | Account[] accounts = null; |
| 356 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 357 | if (packageNames != null) { |
| 358 | final int userId = UserHandle.getUserId(uid); |
| 359 | final long identity = Binder.clearCallingIdentity(); |
| 360 | try { |
| 361 | for (String packageName : packageNames) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 362 | // if app asked for permission we need to cancel notification even |
| 363 | // for O+ applications. |
| 364 | if (mPackageManager.checkPermission( |
| 365 | Manifest.permission.GET_ACCOUNTS, |
| 366 | packageName) != PackageManager.PERMISSION_GRANTED) { |
| 367 | continue; |
| 368 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 369 | |
| 370 | if (accounts == null) { |
| 371 | accounts = getAccountsAsUser(null, userId, "android"); |
| 372 | if (ArrayUtils.isEmpty(accounts)) { |
| 373 | return; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | for (Account account : accounts) { |
| 378 | cancelAccountAccessRequestNotificationIfNeeded( |
| 379 | account, uid, packageName, true); |
| 380 | } |
| 381 | } |
| 382 | } finally { |
| 383 | Binder.restoreCallingIdentity(identity); |
| 384 | } |
| 385 | } |
| 386 | }); |
| 387 | } |
| 388 | |
| 389 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 390 | boolean checkAccess) { |
| 391 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 392 | for (Account account : accounts) { |
| 393 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 398 | boolean checkAccess) { |
| 399 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 400 | for (Account account : accounts) { |
| 401 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 406 | boolean checkAccess) { |
| 407 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 408 | if (packageNames != null) { |
| 409 | for (String packageName : packageNames) { |
| 410 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 411 | packageName, checkAccess); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 417 | int uid, String packageName, boolean checkAccess) { |
| 418 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 419 | UserHandle.getUserHandleForUid(uid))) { |
| 420 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 421 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 422 | UserHandle.getUserHandleForUid(uid)); |
| 423 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 426 | @Override |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 427 | public boolean addAccountExplicitlyWithVisibility(Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 428 | Bundle extras, Map packageToVisibility) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 429 | Bundle.setDefusable(extras, true); |
| 430 | |
| 431 | final int callingUid = Binder.getCallingUid(); |
| 432 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 433 | Log.v(TAG, "addAccountExplicitly: " + account + ", caller's uid " + callingUid |
| 434 | + ", pid " + Binder.getCallingPid()); |
| 435 | } |
| 436 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 437 | int userId = UserHandle.getCallingUserId(); |
| 438 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
| 439 | String msg = String.format("uid %s cannot explicitly add accounts of type: %s", |
| 440 | callingUid, account.type); |
| 441 | throw new SecurityException(msg); |
| 442 | } |
| 443 | /* |
| 444 | * Child users are not allowed to add accounts. Only the accounts that are shared by the |
| 445 | * parent profile can be added to child profile. |
| 446 | * |
| 447 | * TODO: Only allow accounts that were shared to be added by a limited user. |
| 448 | */ |
| 449 | // fails if the account already exists |
| 450 | long identityToken = clearCallingIdentity(); |
| 451 | try { |
| 452 | UserAccounts accounts = getUserAccounts(userId); |
| 453 | return addAccountInternal(accounts, account, password, extras, callingUid, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 454 | (Map<String, Integer>) packageToVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 455 | } finally { |
| 456 | restoreCallingIdentity(identityToken); |
| 457 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | @Override |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 461 | public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 462 | String accountType) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 463 | int callingUid = Binder.getCallingUid(); |
| 464 | boolean isSystemUid = UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); |
| 465 | List<String> managedTypes = |
| 466 | getTypesForCaller(callingUid, UserHandle.getUserId(callingUid), isSystemUid); |
| 467 | |
| 468 | if ((accountType != null && !managedTypes.contains(accountType)) |
| 469 | || (accountType == null && !isSystemUid)) { |
| 470 | throw new SecurityException( |
| 471 | "getAccountsAndVisibilityForPackage() called from unauthorized uid " |
| 472 | + callingUid + " with packageName=" + packageName); |
| 473 | } |
| 474 | if (accountType != null) { |
| 475 | managedTypes = new ArrayList<String>(); |
| 476 | managedTypes.add(accountType); |
| 477 | } |
| 478 | |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 479 | long identityToken = clearCallingIdentity(); |
| 480 | try { |
| 481 | return getAccountsAndVisibilityForPackage(packageName, managedTypes, callingUid, |
| 482 | getUserAccounts(UserHandle.getUserId(callingUid))); |
| 483 | } finally { |
| 484 | restoreCallingIdentity(identityToken); |
| 485 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* |
| 489 | * accountTypes may not be null |
| 490 | */ |
| 491 | private Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 492 | List<String> accountTypes, Integer callingUid, UserAccounts accounts) { |
| 493 | int uid = 0; |
| 494 | try { |
| 495 | uid = mPackageManager.getPackageUidAsUser(packageName, |
| 496 | UserHandle.getUserId(callingUid)); |
| 497 | } catch (NameNotFoundException e) { |
| 498 | Log.d(TAG, "Package not found " + e.getMessage()); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 499 | return new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 502 | Map<Account, Integer> result = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 503 | for (String accountType : accountTypes) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 504 | synchronized (accounts.dbLock) { |
| 505 | synchronized (accounts.cacheLock) { |
| 506 | final Account[] accountsOfType = accounts.accountCache.get(accountType); |
| 507 | if (accountsOfType != null) { |
| 508 | for (Account account : accountsOfType) { |
| 509 | result.put(account, |
| 510 | resolveAccountVisibility(account, packageName, accounts)); |
| 511 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | return filterSharedAccounts(accounts, result, callingUid, packageName); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | @Override |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 520 | public Map<String, Integer> getPackagesAndVisibilityForAccount(Account account) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 521 | Preconditions.checkNotNull(account, "account cannot be null"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 522 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 523 | int userId = UserHandle.getUserId(callingUid); |
| 524 | UserAccounts accounts = getUserAccounts(userId); |
| 525 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
| 526 | && !isSystemUid(callingUid)) { |
| 527 | String msg = |
| 528 | String.format("uid %s cannot get secrets for account %s", callingUid, account); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 529 | throw new SecurityException(msg); |
| 530 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 531 | synchronized (accounts.dbLock) { |
| 532 | synchronized (accounts.cacheLock) { |
| 533 | return getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 534 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 535 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /** |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 539 | * Returns Map with all package names and visibility values for given account. |
| 540 | * The method and returned map must be guarded by accounts.cacheLock |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 541 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 542 | * @param account Account to get visibility values. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 543 | * @param accounts UserAccount that currently hosts the account and application |
| 544 | * |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 545 | * @return Map with cache for package names to visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 546 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 547 | private @NonNull Map<String, Integer> getPackagesAndVisibilityForAccountLocked(Account account, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 548 | UserAccounts accounts) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 549 | Map<String, Integer> accountVisibility = accounts.visibilityCache.get(account); |
| 550 | if (accountVisibility == null) { |
| 551 | Log.d(TAG, "Visibility was not initialized"); |
| 552 | accountVisibility = new HashMap<>(); |
| 553 | accounts.visibilityCache.put(account, accountVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 554 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 555 | return accountVisibility; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 559 | public int getAccountVisibility(Account account, String packageName) { |
| 560 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 561 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 562 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 563 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
| 564 | if (!isAccountManagedByCaller(account.type, callingUid, accounts.userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 565 | && !isSystemUid(callingUid)) { |
| 566 | String msg = String.format( |
| 567 | "uid %s cannot get secrets for accounts of type: %s", |
| 568 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 569 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 570 | throw new SecurityException(msg); |
| 571 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 572 | return resolveAccountVisibility(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 576 | * Method returns visibility for given account and package name. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 577 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 578 | * @param account The account to check visibility. |
| 579 | * @param packageName Package name to check visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 580 | * @param accounts UserAccount that currently hosts the account and application |
| 581 | * |
| 582 | * @return Visibility value, AccountManager.VISIBILITY_UNDEFINED if no value was stored. |
| 583 | * |
| 584 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 585 | private int getAccountVisibilityFromCache(Account account, String packageName, |
| 586 | UserAccounts accounts) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 587 | synchronized (accounts.cacheLock) { |
| 588 | Map<String, Integer> accountVisibility = |
| 589 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 590 | Integer visibility = accountVisibility.get(packageName); |
| 591 | return visibility != null ? visibility : AccountManager.VISIBILITY_UNDEFINED; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Method which handles default values for Account visibility. |
| 597 | * |
| 598 | * @param account The account to check visibility. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 599 | * @param packageName Package name to check visibility |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 600 | * @param accounts UserAccount that currently hosts the account and application |
| 601 | * |
| 602 | * @return Visibility value, the method never returns AccountManager.VISIBILITY_UNDEFINED |
| 603 | * |
| 604 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 605 | private Integer resolveAccountVisibility(Account account, @NonNull String packageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 606 | UserAccounts accounts) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 607 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 608 | int uid = -1; |
| 609 | try { |
| 610 | long identityToken = clearCallingIdentity(); |
| 611 | try { |
| 612 | uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 613 | } finally { |
| 614 | restoreCallingIdentity(identityToken); |
| 615 | } |
| 616 | } catch (NameNotFoundException e) { |
| 617 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 618 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 619 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 620 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 621 | // System visibility can not be restricted. |
| 622 | if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) { |
| 623 | return AccountManager.VISIBILITY_VISIBLE; |
| 624 | } |
| 625 | |
| 626 | int signatureCheckResult = |
| 627 | checkPackageSignature(account.type, uid, accounts.userId); |
| 628 | |
| 629 | // Authenticator can not restrict visibility to itself. |
| 630 | if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) { |
| 631 | return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account |
| 632 | } |
| 633 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 634 | if (isSpecialPackageKey(packageName)) { |
| 635 | Log.d(TAG, "Package name is forbidden: " + packageName); |
| 636 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
| 637 | } |
| 638 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 639 | // Return stored value if it was set. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 640 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 641 | |
| 642 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 643 | return visibility; |
| 644 | } |
| 645 | |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 646 | boolean isPrivileged = isPermittedForPackage(packageName, accounts.userId, |
| 647 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 648 | |
| 649 | // Device/Profile owner gets visibility by default. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 650 | if (isProfileOwner(uid)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 651 | return AccountManager.VISIBILITY_VISIBLE; |
| 652 | } |
| 653 | // Apps with READ_CONTACTS permission get visibility by default even post O. |
| 654 | boolean canReadContacts = checkReadContactsPermission(packageName, accounts.userId); |
| 655 | |
| 656 | boolean preO = isPreOApplication(packageName); |
| 657 | if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH) |
| 658 | || (preO && checkGetAccountsPermission(packageName, accounts.userId)) |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 659 | || canReadContacts || isPrivileged) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 660 | // Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature |
| 661 | // match. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 662 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 663 | AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 664 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 665 | visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 666 | } |
| 667 | } else { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 668 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 669 | AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 670 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 671 | visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 672 | } |
| 673 | } |
| 674 | return visibility; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Checks targetSdk for a package; |
| 679 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 680 | * @param packageName Package name |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 681 | * |
| 682 | * @return True if package's target SDK is below {@link android.os.Build.VERSION_CODES#O}, or |
| 683 | * undefined |
| 684 | */ |
| 685 | private boolean isPreOApplication(String packageName) { |
| 686 | try { |
| 687 | long identityToken = clearCallingIdentity(); |
| 688 | ApplicationInfo applicationInfo; |
| 689 | try { |
| 690 | applicationInfo = mPackageManager.getApplicationInfo(packageName, 0); |
| 691 | } finally { |
| 692 | restoreCallingIdentity(identityToken); |
| 693 | } |
| 694 | |
| 695 | if (applicationInfo != null) { |
| 696 | int version = applicationInfo.targetSdkVersion; |
| 697 | return version < android.os.Build.VERSION_CODES.O; |
| 698 | } |
| 699 | return true; |
| 700 | } catch (NameNotFoundException e) { |
| 701 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 702 | return true; |
| 703 | } |
Dmitry Dementyev | 58fa8362 | 2016-12-20 18:08:51 -0800 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 707 | public boolean setAccountVisibility(Account account, String packageName, int newVisibility) { |
| 708 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 709 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 710 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 711 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
| 712 | if (!isAccountManagedByCaller(account.type, callingUid, accounts.userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 713 | && !isSystemUid(callingUid)) { |
| 714 | String msg = String.format( |
| 715 | "uid %s cannot get secrets for accounts of type: %s", |
| 716 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 717 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 718 | throw new SecurityException(msg); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 719 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 720 | return setAccountVisibility(account, packageName, newVisibility, true /* notify */, |
| 721 | accounts); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 725 | * Updates visibility for given account name and package. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 726 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 727 | * @param account Account to update visibility. |
| 728 | * @param packageName Package name for which visibility is updated. |
| 729 | * @param newVisibility New visibility calue |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 730 | * @param notify if the flag is set applications will get notification about visibility change |
| 731 | * @param accounts UserAccount that currently hosts the account and application |
| 732 | * |
| 733 | * @return True if account visibility was changed. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 734 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 735 | private boolean setAccountVisibility(Account account, String packageName, int newVisibility, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 736 | boolean notify, UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 737 | synchronized (accounts.dbLock) { |
| 738 | synchronized (accounts.cacheLock) { |
| 739 | Map<String, Integer> packagesToVisibility; |
| 740 | if (notify) { |
| 741 | if (isSpecialPackageKey(packageName)) { |
| 742 | packagesToVisibility = |
| 743 | getRequestingPackages(account, accounts); |
| 744 | } else { |
| 745 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 746 | return false; // package is not installed. |
| 747 | } |
| 748 | packagesToVisibility = new HashMap<>(); |
| 749 | packagesToVisibility.put(packageName, |
| 750 | resolveAccountVisibility(account, packageName, accounts)); |
| 751 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 752 | } else { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 753 | // Notifications will not be send. |
| 754 | if (!isSpecialPackageKey(packageName) && |
| 755 | !packageExistsForUser(packageName, accounts.userId)) { |
| 756 | // package is not installed and not meta value. |
| 757 | return false; |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 758 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 759 | packagesToVisibility = new HashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 760 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 761 | |
| 762 | if (!updateAccountVisibilityLocked(account, packageName, newVisibility, accounts)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 763 | return false; |
| 764 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 765 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 766 | if (notify) { |
| 767 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 768 | .entrySet()) { |
| 769 | if (packageToVisibility.getValue() |
| 770 | != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 771 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 772 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 773 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 774 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 775 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 776 | return true; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 777 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 778 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 781 | // Update account visibility in cache and database. |
| 782 | private boolean updateAccountVisibilityLocked(Account account, String packageName, |
| 783 | int newVisibility, UserAccounts accounts) { |
| 784 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 785 | if (accountId < 0) { |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
| 790 | try { |
| 791 | if (!accounts.accountsDb.setAccountVisibility(accountId, packageName, |
| 792 | newVisibility)) { |
| 793 | return false; |
| 794 | } |
| 795 | } finally { |
| 796 | StrictMode.setThreadPolicy(oldPolicy); |
| 797 | } |
| 798 | Map<String, Integer> accountVisibility = |
| 799 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 800 | accountVisibility.put(packageName, newVisibility); |
| 801 | return true; |
| 802 | } |
| 803 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 804 | @Override |
| 805 | public void registerAccountListener(String[] accountTypes, String opPackageName) { |
| 806 | int callingUid = Binder.getCallingUid(); |
| 807 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 808 | registerAccountListener(accountTypes, opPackageName, |
| 809 | getUserAccounts(UserHandle.getUserId(callingUid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 812 | private void registerAccountListener(String[] accountTypes, String opPackageName, |
| 813 | UserAccounts accounts) { |
| 814 | synchronized (accounts.mReceiversForType) { |
| 815 | if (accountTypes == null) { |
| 816 | // null for any type |
| 817 | accountTypes = new String[] {null}; |
| 818 | } |
| 819 | for (String type : accountTypes) { |
| 820 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 821 | if (receivers == null) { |
| 822 | receivers = new HashMap<>(); |
| 823 | accounts.mReceiversForType.put(type, receivers); |
| 824 | } |
| 825 | Integer cnt = receivers.get(opPackageName); |
| 826 | receivers.put(opPackageName, cnt != null ? cnt + 1 : 1); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 831 | @Override |
| 832 | public void unregisterAccountListener(String[] accountTypes, String opPackageName) { |
| 833 | int callingUid = Binder.getCallingUid(); |
| 834 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 835 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
| 836 | synchronized (accounts.mReceiversForType) { |
| 837 | if (accountTypes == null) { |
| 838 | // null for any type |
| 839 | accountTypes = new String[] {null}; |
| 840 | } |
| 841 | for (String type : accountTypes) { |
| 842 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 843 | if (receivers == null || receivers.get(opPackageName) == null) { |
| 844 | throw new IllegalArgumentException("attempt to unregister wrong receiver"); |
| 845 | } |
| 846 | Integer cnt = receivers.get(opPackageName); |
| 847 | if (cnt == 1) { |
| 848 | receivers.remove(opPackageName); |
| 849 | } else { |
| 850 | receivers.put(opPackageName, cnt - 1); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | // Send notification to all packages which can potentially see the account |
| 857 | private void sendNotificationAccountUpdated(Account account, UserAccounts accounts) { |
| 858 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, accounts); |
| 859 | // packages with VISIBILITY_USER_MANAGED_NOT_VISIBL still get notification. |
| 860 | // Should we notify VISIBILITY_NOT_VISIBLE packages when account is added? |
| 861 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
| 862 | if (packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 863 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * Sends a direct intent to a package, notifying it of account visibility change. |
| 870 | * |
| 871 | * @param packageName to send Account to |
| 872 | * @param accounts UserAccount that currently hosts the account |
| 873 | */ |
| 874 | private void notifyPackage(String packageName, UserAccounts accounts) { |
| 875 | Intent intent = new Intent(AccountManager.ACTION_VISIBLE_ACCOUNTS_CHANGED); |
| 876 | intent.setPackage(packageName); |
| 877 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 878 | mContext.sendBroadcastAsUser(intent, new UserHandle(accounts.userId)); |
| 879 | } |
| 880 | |
| 881 | // Returns a map from package name to visibility, for packages subscribed |
| 882 | // to notifications about any account type, or type of provided account |
| 883 | // account type or all types. |
| 884 | private Map<String, Integer> getRequestingPackages(Account account, UserAccounts accounts) { |
| 885 | Set<String> packages = new HashSet<>(); |
| 886 | synchronized (accounts.mReceiversForType) { |
| 887 | for (String type : new String[] {account.type, null}) { |
| 888 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 889 | if (receivers != null) { |
| 890 | packages.addAll(receivers.keySet()); |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | Map<String, Integer> result = new HashMap<>(); |
| 895 | for (String packageName : packages) { |
| 896 | result.put(packageName, resolveAccountVisibility(account, packageName, accounts)); |
| 897 | } |
| 898 | return result; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 901 | private boolean packageExistsForUser(String packageName, int userId) { |
| 902 | try { |
| 903 | long identityToken = clearCallingIdentity(); |
| 904 | try { |
| 905 | mPackageManager.getPackageUidAsUser(packageName, userId); |
| 906 | return true; // package exist |
| 907 | } finally { |
| 908 | restoreCallingIdentity(identityToken); |
| 909 | } |
| 910 | } catch (NameNotFoundException e) { |
| 911 | return false; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Returns true if packageName is one of special values. |
| 917 | */ |
| 918 | private boolean isSpecialPackageKey(String packageName) { |
| 919 | return (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName) |
| 920 | || AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)); |
| 921 | } |
| 922 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 923 | private void sendAccountsChangedBroadcast(int userId) { |
| 924 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 925 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
| 926 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 930 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 931 | throws RemoteException { |
| 932 | try { |
| 933 | return super.onTransact(code, data, reply, flags); |
| 934 | } catch (RuntimeException e) { |
| 935 | // The account manager only throws security exceptions, so let's |
| 936 | // log all others. |
| 937 | if (!(e instanceof SecurityException)) { |
| 938 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 939 | } |
| 940 | throw e; |
| 941 | } |
| 942 | } |
| 943 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 944 | private UserManager getUserManager() { |
| 945 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 946 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 947 | } |
| 948 | return mUserManager; |
| 949 | } |
| 950 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 951 | /** |
| 952 | * Validate internal set of accounts against installed authenticators for |
| 953 | * given user. Clears cached authenticators before validating. |
| 954 | */ |
| 955 | public void validateAccounts(int userId) { |
| 956 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 957 | // Invalidate user-specific cache to make sure we catch any |
| 958 | // removed authenticators. |
| 959 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 960 | } |
| 961 | |
| 962 | /** |
| 963 | * Validate internal set of accounts against installed authenticators for |
| 964 | * given user. Clear cached authenticators before validating when requested. |
| 965 | */ |
| 966 | private void validateAccountsInternal( |
| 967 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 968 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 969 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 970 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 971 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 972 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 973 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 974 | if (invalidateAuthenticatorCache) { |
| 975 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 976 | } |
| 977 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 978 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 979 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 980 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 981 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 982 | synchronized (accounts.dbLock) { |
| 983 | synchronized (accounts.cacheLock) { |
| 984 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 985 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 986 | // Get a map of stored authenticator types to UID |
| 987 | final AccountsDb accountsDb = accounts.accountsDb; |
| 988 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
| 989 | // Create a list of authenticator type whose previous uid no longer exists |
| 990 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
| 991 | SparseBooleanArray knownUids = null; |
| 992 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 993 | String type = authToUidEntry.getKey(); |
| 994 | int uid = authToUidEntry.getValue(); |
| 995 | Integer knownUid = knownAuth.get(type); |
| 996 | if (knownUid != null && uid == knownUid) { |
| 997 | // Remove it from the knownAuth list if it's unchanged. |
| 998 | knownAuth.remove(type); |
| 999 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1000 | /* |
| 1001 | * The authenticator is presently not cached and should only be triggered |
| 1002 | * when we think an authenticator has been removed (or is being updated). |
| 1003 | * But we still want to check if any data with the associated uid is |
| 1004 | * around. This is an (imperfect) signal that the package may be updating. |
| 1005 | * |
| 1006 | * A side effect of this is that an authenticator sharing a uid with |
| 1007 | * multiple apps won't get its credentials wiped as long as some app with |
| 1008 | * that uid is still on the device. But I suspect that this is a rare case. |
| 1009 | * And it isn't clear to me how an attacker could really exploit that |
| 1010 | * feature. |
| 1011 | * |
| 1012 | * The upshot is that we don't have to worry about accounts getting |
| 1013 | * uninstalled while the authenticator's package is being updated. |
| 1014 | * |
| 1015 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1016 | if (knownUids == null) { |
| 1017 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
| 1018 | } |
| 1019 | if (!knownUids.get(uid)) { |
| 1020 | // The authenticator is not presently available to the cache. And the |
| 1021 | // package no longer has a data directory (so we surmise it isn't |
| 1022 | // updating). So purge its data from the account databases. |
| 1023 | obsoleteAuthType.add(type); |
| 1024 | // And delete it from the TABLE_META |
| 1025 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
| 1026 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1027 | } |
| 1028 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1029 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1030 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
| 1031 | // been re-enabled (after being updated for example), then we just overwrite the old |
| 1032 | // values. |
| 1033 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
| 1034 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
| 1035 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1036 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1037 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
| 1038 | try { |
| 1039 | accounts.accountCache.clear(); |
| 1040 | final HashMap<String, ArrayList<String>> accountNamesByType |
| 1041 | = new LinkedHashMap<>(); |
| 1042 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1043 | final long accountId = accountEntry.getKey(); |
| 1044 | final Account account = accountEntry.getValue(); |
| 1045 | if (obsoleteAuthType.contains(account.type)) { |
| 1046 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 1047 | + account.type |
| 1048 | + "'s registered authenticator no longer exist."); |
| 1049 | Map<String, Integer> packagesToVisibility = |
| 1050 | getRequestingPackages(account, accounts); |
| 1051 | accountsDb.beginTransaction(); |
| 1052 | try { |
| 1053 | accountsDb.deleteDeAccount(accountId); |
| 1054 | // Also delete from CE table if user is unlocked; if user is |
| 1055 | // currently locked the account will be removed later by |
| 1056 | // syncDeCeAccountsLocked |
| 1057 | if (userUnlocked) { |
| 1058 | accountsDb.deleteCeAccount(accountId); |
| 1059 | } |
| 1060 | accountsDb.setTransactionSuccessful(); |
| 1061 | } finally { |
| 1062 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1063 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1064 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1065 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1066 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1067 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1068 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1069 | accounts.userDataCache.remove(account); |
| 1070 | accounts.authTokenCache.remove(account); |
| 1071 | accounts.accountTokenCaches.remove(account); |
| 1072 | accounts.visibilityCache.remove(account); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1073 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1074 | for (Entry<String, Integer> packageToVisibility : |
| 1075 | packagesToVisibility.entrySet()) { |
| 1076 | if (packageToVisibility.getValue() |
| 1077 | != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 1078 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 1079 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1080 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1081 | } else { |
| 1082 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
| 1083 | if (accountNames == null) { |
| 1084 | accountNames = new ArrayList<>(); |
| 1085 | accountNamesByType.put(account.type, accountNames); |
| 1086 | } |
| 1087 | accountNames.add(account.name); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1088 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1089 | } |
| 1090 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
| 1091 | final String accountType = cur.getKey(); |
| 1092 | final ArrayList<String> accountNames = cur.getValue(); |
| 1093 | final Account[] accountsForType = new Account[accountNames.size()]; |
| 1094 | for (int i = 0; i < accountsForType.length; i++) { |
| 1095 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
| 1096 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1097 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1098 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1099 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1100 | accounts.visibilityCache.putAll(accountsDb.findAllVisibilityValues()); |
| 1101 | } finally { |
| 1102 | if (accountDeleted) { |
| 1103 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1104 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1105 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1106 | } |
| 1107 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1110 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1111 | // Get the UIDs of all apps that might have data on the device. We want |
| 1112 | // to preserve user data if the app might otherwise be storing data. |
| 1113 | List<PackageInfo> pkgsWithData = |
| 1114 | mPackageManager.getInstalledPackagesAsUser( |
| 1115 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1116 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1117 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1118 | if (pkgInfo.applicationInfo != null |
| 1119 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1120 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1121 | } |
| 1122 | } |
| 1123 | return knownUids; |
| 1124 | } |
| 1125 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1126 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1127 | Context context, |
| 1128 | int userId) { |
| 1129 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1130 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1131 | } |
| 1132 | |
| 1133 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1134 | IAccountAuthenticatorCache authCache, |
| 1135 | int userId) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 1136 | HashMap<String, Integer> knownAuth = new LinkedHashMap<>(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1137 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1138 | .getAllServices(userId)) { |
| 1139 | knownAuth.put(service.type.type, service.uid); |
| 1140 | } |
| 1141 | return knownAuth; |
| 1142 | } |
| 1143 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1144 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1145 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | protected UserAccounts getUserAccounts(int userId) { |
| 1149 | synchronized (mUsers) { |
| 1150 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1151 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1152 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1153 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1154 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1155 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1156 | initializeDebugDbSizeAndCompileSqlStatementForLogging(accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1157 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1158 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1159 | validateAccounts = true; |
| 1160 | } |
| 1161 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1162 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1163 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1164 | synchronized (accounts.dbLock) { |
| 1165 | synchronized (accounts.cacheLock) { |
| 1166 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
| 1167 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
| 1168 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1169 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1170 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1171 | } |
| 1172 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1173 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1174 | } |
| 1175 | return accounts; |
| 1176 | } |
| 1177 | } |
| 1178 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1179 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1180 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1181 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1182 | if (!accountsToRemove.isEmpty()) { |
| 1183 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1184 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1185 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1186 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1187 | |
| 1188 | for (Account account : accountsToRemove) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1189 | removeAccountInternal(accounts, account, Process.SYSTEM_UID); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1194 | private void purgeOldGrantsAll() { |
| 1195 | synchronized (mUsers) { |
| 1196 | for (int i = 0; i < mUsers.size(); i++) { |
| 1197 | purgeOldGrants(mUsers.valueAt(i)); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | private void purgeOldGrants(UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1203 | synchronized (accounts.dbLock) { |
| 1204 | synchronized (accounts.cacheLock) { |
| 1205 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
| 1206 | for (int uid : uids) { |
| 1207 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1208 | if (packageExists) { |
| 1209 | continue; |
| 1210 | } |
| 1211 | Log.d(TAG, "deleting grants for UID " + uid |
| 1212 | + " because its package is no longer installed"); |
| 1213 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1214 | } |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1219 | private void removeVisibilityValuesForPackage(String packageName) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1220 | if (isSpecialPackageKey(packageName)) { |
| 1221 | return; |
| 1222 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1223 | synchronized (mUsers) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1224 | int numberOfUsers = mUsers.size(); |
| 1225 | for (int i = 0; i < numberOfUsers; i++) { |
| 1226 | UserAccounts accounts = mUsers.valueAt(i); |
| 1227 | try { |
| 1228 | mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 1229 | } catch (NameNotFoundException e) { |
| 1230 | // package does not exist - remove visibility values |
| 1231 | accounts.accountsDb.deleteAccountVisibilityForPackage(packageName); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1232 | synchronized (accounts.dbLock) { |
| 1233 | synchronized (accounts.cacheLock) { |
| 1234 | for (Account account : accounts.visibilityCache.keySet()) { |
| 1235 | Map<String, Integer> accountVisibility = |
| 1236 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 1237 | accountVisibility.remove(packageName); |
| 1238 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1246 | |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 1247 | private void onCleanupUser(int userId) { |
| 1248 | Log.i(TAG, "onCleanupUser " + userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1249 | UserAccounts accounts; |
| 1250 | synchronized (mUsers) { |
| 1251 | accounts = mUsers.get(userId); |
| 1252 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1253 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1254 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1255 | if (accounts != null) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1256 | synchronized (accounts.dbLock) { |
| 1257 | synchronized (accounts.cacheLock) { |
| 1258 | accounts.accountsDb.close(); |
| 1259 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1260 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1261 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1264 | @VisibleForTesting |
| 1265 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1266 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1267 | } |
| 1268 | |
| 1269 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1270 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1271 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1272 | } |
| 1273 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1274 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1275 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1276 | if (userId < 1) return; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1277 | syncSharedAccounts(userId); |
| 1278 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1279 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1280 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1281 | // Check if there's a shared account that needs to be created as an account |
| 1282 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1283 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1284 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1285 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1286 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1287 | : UserHandle.USER_SYSTEM; |
| 1288 | if (parentUserId < 0) { |
| 1289 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1290 | return; |
| 1291 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1292 | for (Account sa : sharedAccounts) { |
| 1293 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1294 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1295 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1299 | @Override |
| 1300 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1301 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1304 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1305 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1306 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1307 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1308 | Log.v(TAG, "getPassword: " + account |
| 1309 | + ", caller's uid " + Binder.getCallingUid() |
| 1310 | + ", pid " + Binder.getCallingPid()); |
| 1311 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1312 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1313 | int userId = UserHandle.getCallingUserId(); |
| 1314 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1315 | String msg = String.format( |
| 1316 | "uid %s cannot get secrets for accounts of type: %s", |
| 1317 | callingUid, |
| 1318 | account.type); |
| 1319 | throw new SecurityException(msg); |
| 1320 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1321 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1322 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1323 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1324 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1325 | } finally { |
| 1326 | restoreCallingIdentity(identityToken); |
| 1327 | } |
| 1328 | } |
| 1329 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1330 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1331 | if (account == null) { |
| 1332 | return null; |
| 1333 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1334 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1335 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1336 | return null; |
| 1337 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1338 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1339 | synchronized (accounts.dbLock) { |
| 1340 | synchronized (accounts.cacheLock) { |
| 1341 | return accounts.accountsDb |
| 1342 | .findAccountPasswordByNameAndType(account.name, account.type); |
| 1343 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1347 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1348 | public String getPreviousName(Account account) { |
| 1349 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1350 | Log.v(TAG, "getPreviousName: " + account |
| 1351 | + ", caller's uid " + Binder.getCallingUid() |
| 1352 | + ", pid " + Binder.getCallingPid()); |
| 1353 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1354 | Preconditions.checkNotNull(account, "account cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1355 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1356 | long identityToken = clearCallingIdentity(); |
| 1357 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1358 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1359 | return readPreviousNameInternal(accounts, account); |
| 1360 | } finally { |
| 1361 | restoreCallingIdentity(identityToken); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1366 | if (account == null) { |
| 1367 | return null; |
| 1368 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1369 | synchronized (accounts.dbLock) { |
| 1370 | synchronized (accounts.cacheLock) { |
| 1371 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1372 | if (previousNameRef == null) { |
| 1373 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
| 1374 | previousNameRef = new AtomicReference<>(previousName); |
| 1375 | accounts.previousNameCache.put(account, previousNameRef); |
| 1376 | return previousName; |
| 1377 | } else { |
| 1378 | return previousNameRef.get(); |
| 1379 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1385 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1386 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1387 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1388 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1389 | account, key, callingUid, Binder.getCallingPid()); |
| 1390 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1391 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1392 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 1393 | Preconditions.checkNotNull(key, "key cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1394 | int userId = UserHandle.getCallingUserId(); |
| 1395 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1396 | String msg = String.format( |
| 1397 | "uid %s cannot get user data for accounts of type: %s", |
| 1398 | callingUid, |
| 1399 | account.type); |
| 1400 | throw new SecurityException(msg); |
| 1401 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1402 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1403 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1404 | return null; |
| 1405 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1406 | long identityToken = clearCallingIdentity(); |
| 1407 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1408 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1409 | if (!accountExistsCache(accounts, account)) { |
| 1410 | return null; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1411 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1412 | return readUserDataInternal(accounts, account, key); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1413 | } finally { |
| 1414 | restoreCallingIdentity(identityToken); |
| 1415 | } |
| 1416 | } |
| 1417 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1418 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1419 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1420 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1421 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1422 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1423 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1424 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1425 | + ", pid " + Binder.getCallingPid()); |
| 1426 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1427 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1428 | if (isCrossUser(callingUid, userId)) { |
| 1429 | throw new SecurityException( |
| 1430 | String.format( |
| 1431 | "User %s tying to get authenticator types for %s" , |
| 1432 | UserHandle.getCallingUserId(), |
| 1433 | userId)); |
| 1434 | } |
| 1435 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1436 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1437 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1438 | return getAuthenticatorTypesInternal(userId); |
| 1439 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1440 | } finally { |
| 1441 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1442 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1445 | /** |
| 1446 | * Should only be called inside of a clearCallingIdentity block. |
| 1447 | */ |
| 1448 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1449 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1450 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1451 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1452 | AuthenticatorDescription[] types = |
| 1453 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1454 | int i = 0; |
| 1455 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1456 | : authenticatorCollection) { |
| 1457 | types[i] = authenticator.type; |
| 1458 | i++; |
| 1459 | } |
| 1460 | return types; |
| 1461 | } |
| 1462 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1463 | private boolean isCrossUser(int callingUid, int userId) { |
| 1464 | return (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1465 | && callingUid != Process.SYSTEM_UID |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1466 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1467 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1468 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1469 | } |
| 1470 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1471 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1472 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1473 | return addAccountExplicitlyWithVisibility(account, password, extras, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1476 | @Override |
| 1477 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1478 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1479 | int callingUid = Binder.getCallingUid(); |
| 1480 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1481 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1482 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1483 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1484 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1485 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1486 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1487 | if (response != null) { |
| 1488 | Bundle result = new Bundle(); |
| 1489 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1490 | try { |
| 1491 | response.onResult(result); |
| 1492 | } catch (RemoteException e) { |
| 1493 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1494 | } |
| 1495 | } |
| 1496 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1499 | Slog.d(TAG, "Copying account " + account.name |
| 1500 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1501 | long identityToken = clearCallingIdentity(); |
| 1502 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1503 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1504 | false /* stripAuthTokenFromResult */, account.name, |
| 1505 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1506 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1507 | protected String toDebugString(long now) { |
| 1508 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1509 | + ", " + account.type; |
| 1510 | } |
| 1511 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1512 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1513 | public void run() throws RemoteException { |
| 1514 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1515 | } |
| 1516 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1517 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1518 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1519 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1520 | if (result != null |
| 1521 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1522 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1523 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1524 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1525 | super.onResult(result); |
| 1526 | } |
| 1527 | } |
| 1528 | }.bind(); |
| 1529 | } finally { |
| 1530 | restoreCallingIdentity(identityToken); |
| 1531 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1532 | } |
| 1533 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1534 | @Override |
| 1535 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1536 | final int callingUid = Binder.getCallingUid(); |
| 1537 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1538 | String msg = String.format( |
| 1539 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1540 | account, |
| 1541 | callingUid); |
| 1542 | Log.v(TAG, msg); |
| 1543 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1544 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1545 | int userId = UserHandle.getCallingUserId(); |
| 1546 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1547 | String msg = String.format( |
| 1548 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1549 | callingUid, |
| 1550 | account.type); |
| 1551 | throw new SecurityException(msg); |
| 1552 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1553 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1554 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1555 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1556 | return false; |
| 1557 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1558 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1559 | long identityToken = clearCallingIdentity(); |
| 1560 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1561 | UserAccounts accounts = getUserAccounts(userId); |
| 1562 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1563 | } finally { |
| 1564 | restoreCallingIdentity(identityToken); |
| 1565 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1569 | final UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1570 | synchronized (accounts.dbLock) { |
| 1571 | synchronized (accounts.cacheLock) { |
| 1572 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
| 1573 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1574 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1575 | } |
| 1576 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1577 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1578 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1579 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1580 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1581 | long id = clearCallingIdentity(); |
| 1582 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1583 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1584 | false /* stripAuthTokenFromResult */, account.name, |
| 1585 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1586 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1587 | protected String toDebugString(long now) { |
| 1588 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1589 | + ", " + account.type; |
| 1590 | } |
| 1591 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1592 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1593 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1594 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 16bedd4 | 2017-03-30 10:00:49 -0700 | [diff] [blame] | 1595 | for (Account acc : getAccounts(parentUserId, mContext.getOpPackageName())) { |
| 1596 | if (acc.equals(account)) { |
| 1597 | mAuthenticator.addAccountFromCredentials( |
| 1598 | this, account, accountCredentials); |
| 1599 | break; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1600 | } |
| 1601 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1602 | } |
| 1603 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1604 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1605 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1606 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1607 | // TODO: Anything to do if if succedded? |
| 1608 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1609 | // account to avoid retries? |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1610 | // TODO: what we do with the visibility? |
| 1611 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1612 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1613 | } |
| 1614 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1615 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1616 | public void onError(int errorCode, String errorMessage) { |
| 1617 | super.onError(errorCode, errorMessage); |
| 1618 | // TODO: Show error notification to user |
| 1619 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1620 | } |
| 1621 | |
| 1622 | }.bind(); |
| 1623 | } finally { |
| 1624 | restoreCallingIdentity(id); |
| 1625 | } |
| 1626 | } |
| 1627 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1628 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1629 | Bundle extras, int callingUid, Map<String, Integer> packageToVisibility) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1630 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1631 | if (account == null) { |
| 1632 | return false; |
| 1633 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1634 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1635 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1636 | + " is locked. callingUid=" + callingUid); |
| 1637 | return false; |
| 1638 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1639 | synchronized (accounts.dbLock) { |
| 1640 | synchronized (accounts.cacheLock) { |
| 1641 | accounts.accountsDb.beginTransaction(); |
| 1642 | try { |
| 1643 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
| 1644 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1645 | + ", skipping since the account already exists"); |
| 1646 | return false; |
| 1647 | } |
| 1648 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
| 1649 | if (accountId < 0) { |
| 1650 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1651 | + ", skipping the DB insert failed"); |
| 1652 | return false; |
| 1653 | } |
| 1654 | // Insert into DE table |
| 1655 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
| 1656 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1657 | + ", skipping the DB insert failed"); |
| 1658 | return false; |
| 1659 | } |
| 1660 | if (extras != null) { |
| 1661 | for (String key : extras.keySet()) { |
| 1662 | final String value = extras.getString(key); |
| 1663 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
| 1664 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1665 | + ", skipping since insertExtra failed for key " + key); |
| 1666 | return false; |
| 1667 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1668 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1669 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1670 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1671 | if (packageToVisibility != null) { |
| 1672 | for (Entry<String, Integer> entry : packageToVisibility.entrySet()) { |
| 1673 | setAccountVisibility(account, entry.getKey() /* package */, |
| 1674 | entry.getValue() /* visibility */, false /* notify */, |
| 1675 | accounts); |
| 1676 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1677 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1678 | accounts.accountsDb.setTransactionSuccessful(); |
| 1679 | |
| 1680 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 1681 | accountId, |
| 1682 | accounts, callingUid); |
| 1683 | |
| 1684 | insertAccountIntoCacheLocked(accounts, account); |
| 1685 | } finally { |
| 1686 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1687 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1688 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1689 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1690 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1691 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1692 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1693 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1694 | sendNotificationAccountUpdated(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1695 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1696 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1697 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1698 | return true; |
| 1699 | } |
| 1700 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1701 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1702 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1703 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1704 | } |
| 1705 | } |
| 1706 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1707 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1708 | * 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] | 1709 | * running, then clone the account too. |
| 1710 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1711 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1712 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1713 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1714 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1715 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1716 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1717 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1718 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1719 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1720 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1721 | } |
| 1722 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1726 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1727 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1728 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1729 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1730 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1731 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1732 | Log.v(TAG, "hasFeatures: " + account |
| 1733 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1734 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1735 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1736 | + ", pid " + Binder.getCallingPid()); |
| 1737 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1738 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1739 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1740 | Preconditions.checkArgument(features != null, "features cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1741 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1742 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1743 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1744 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1745 | long identityToken = clearCallingIdentity(); |
| 1746 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1747 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1748 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1749 | } finally { |
| 1750 | restoreCallingIdentity(identityToken); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | private class TestFeaturesSession extends Session { |
| 1755 | private final String[] mFeatures; |
| 1756 | private final Account mAccount; |
| 1757 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1758 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1759 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1760 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1761 | true /* stripAuthTokenFromResult */, account.name, |
| 1762 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1763 | mFeatures = features; |
| 1764 | mAccount = account; |
| 1765 | } |
| 1766 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1767 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1768 | public void run() throws RemoteException { |
| 1769 | try { |
| 1770 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1771 | } catch (RemoteException e) { |
| 1772 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1773 | } |
| 1774 | } |
| 1775 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1776 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1777 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1778 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1779 | IAccountManagerResponse response = getResponseAndClose(); |
| 1780 | if (response != null) { |
| 1781 | try { |
| 1782 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1783 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1784 | return; |
| 1785 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1786 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1787 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1788 | + response); |
| 1789 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1790 | final Bundle newResult = new Bundle(); |
| 1791 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1792 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1793 | response.onResult(newResult); |
| 1794 | } catch (RemoteException e) { |
| 1795 | // if the caller is dead then there is no one to care about remote exceptions |
| 1796 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1797 | Log.v(TAG, "failure while notifying response", e); |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | } |
| 1802 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1803 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1804 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1805 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1806 | + ", " + mAccount |
| 1807 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1808 | } |
| 1809 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1810 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1811 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1812 | public void renameAccount( |
| 1813 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1814 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1815 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1816 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1817 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1818 | + ", pid " + Binder.getCallingPid()); |
| 1819 | } |
| 1820 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1821 | int userId = UserHandle.getCallingUserId(); |
| 1822 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1823 | String msg = String.format( |
| 1824 | "uid %s cannot rename accounts of type: %s", |
| 1825 | callingUid, |
| 1826 | accountToRename.type); |
| 1827 | throw new SecurityException(msg); |
| 1828 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1829 | long identityToken = clearCallingIdentity(); |
| 1830 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1831 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1832 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1833 | Bundle result = new Bundle(); |
| 1834 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1835 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1836 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1837 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1838 | try { |
| 1839 | response.onResult(result); |
| 1840 | } catch (RemoteException e) { |
| 1841 | Log.w(TAG, e.getMessage()); |
| 1842 | } |
| 1843 | } finally { |
| 1844 | restoreCallingIdentity(identityToken); |
| 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1849 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1850 | Account resultAccount = null; |
| 1851 | /* |
| 1852 | * Cancel existing notifications. Let authenticators |
| 1853 | * re-post notifications as required. But we don't know if |
| 1854 | * the authenticators have bound their notifications to |
| 1855 | * now stale account name data. |
| 1856 | * |
| 1857 | * With a rename api, we might not need to do this anymore but it |
| 1858 | * shouldn't hurt. |
| 1859 | */ |
| 1860 | cancelNotification( |
| 1861 | getSigninRequiredNotificationId(accounts, accountToRename), |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 1862 | new UserHandle(accounts.userId)); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1863 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1864 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1865 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1866 | if (accountToRename.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 1867 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1868 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1869 | } |
| 1870 | } |
| 1871 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1872 | synchronized (accounts.dbLock) { |
| 1873 | synchronized (accounts.cacheLock) { |
| 1874 | accounts.accountsDb.beginTransaction(); |
| 1875 | Account renamedAccount = new Account(newName, accountToRename.type); |
| 1876 | if ((accounts.accountsDb.findCeAccountId(renamedAccount) >= 0)) { |
| 1877 | Log.e(TAG, "renameAccount failed - account with new name already exists"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1878 | return null; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1879 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1880 | try { |
| 1881 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
| 1882 | if (accountId >= 0) { |
| 1883 | accounts.accountsDb.renameCeAccount(accountId, newName); |
| 1884 | if (accounts.accountsDb.renameDeAccount( |
| 1885 | accountId, newName, accountToRename.name)) { |
| 1886 | accounts.accountsDb.setTransactionSuccessful(); |
| 1887 | } else { |
| 1888 | Log.e(TAG, "renameAccount failed"); |
| 1889 | return null; |
| 1890 | } |
| 1891 | } else { |
| 1892 | Log.e(TAG, "renameAccount failed - old account does not exist"); |
| 1893 | return null; |
| 1894 | } |
| 1895 | } finally { |
| 1896 | accounts.accountsDb.endTransaction(); |
| 1897 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1898 | /* |
| 1899 | * Database transaction was successful. Clean up cached |
| 1900 | * data associated with the account in the user profile. |
| 1901 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1902 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1903 | /* |
| 1904 | * Extract the data and token caches before removing the |
| 1905 | * old account to preserve the user data associated with |
| 1906 | * the account. |
| 1907 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1908 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1909 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
| 1910 | Map<String, Integer> tmpVisibility = accounts.visibilityCache.get(accountToRename); |
| 1911 | removeAccountFromCacheLocked(accounts, accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1912 | /* |
| 1913 | * Update the cached data associated with the renamed |
| 1914 | * account. |
| 1915 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1916 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1917 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 1918 | accounts.visibilityCache.put(renamedAccount, tmpVisibility); |
| 1919 | accounts.previousNameCache.put( |
| 1920 | renamedAccount, |
| 1921 | new AtomicReference<>(accountToRename.name)); |
| 1922 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1923 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1924 | int parentUserId = accounts.userId; |
| 1925 | if (canHaveProfile(parentUserId)) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1926 | /* |
| 1927 | * Owner or system user account was renamed, rename the account for |
| 1928 | * those users with which the account was shared. |
| 1929 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1930 | List<UserInfo> users = getUserManager().getUsers(true); |
| 1931 | for (UserInfo user : users) { |
| 1932 | if (user.isRestricted() |
| 1933 | && (user.restrictedProfileParentId == parentUserId)) { |
| 1934 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
| 1935 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1936 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1937 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1938 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1939 | sendNotificationAccountUpdated(resultAccount, accounts); |
| 1940 | sendAccountsChangedBroadcast(accounts.userId); |
| 1941 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1942 | } |
| 1943 | return resultAccount; |
| 1944 | } |
| 1945 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1946 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1947 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1948 | return userInfo != null && userInfo.canHaveProfile(); |
| 1949 | } |
| 1950 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1951 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1952 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1953 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1954 | removeAccountAsUser( |
| 1955 | response, |
| 1956 | account, |
| 1957 | expectActivityLaunch, |
| 1958 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | @Override |
| 1962 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1963 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1964 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1965 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1966 | Log.v(TAG, "removeAccount: " + account |
| 1967 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1968 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1969 | + ", pid " + Binder.getCallingPid() |
| 1970 | + ", for user id " + userId); |
| 1971 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1972 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1973 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1974 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1975 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1976 | if (isCrossUser(callingUid, userId)) { |
| 1977 | throw new SecurityException( |
| 1978 | String.format( |
| 1979 | "User %s tying remove account for %s" , |
| 1980 | UserHandle.getCallingUserId(), |
| 1981 | userId)); |
| 1982 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1983 | /* |
| 1984 | * Only the system or authenticator should be allowed to remove accounts for that |
| 1985 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 1986 | * arbitrary applications (like competing authenticators). |
| 1987 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1988 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 1989 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 1990 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1991 | String msg = String.format( |
| 1992 | "uid %s cannot remove accounts of type: %s", |
| 1993 | callingUid, |
| 1994 | account.type); |
| 1995 | throw new SecurityException(msg); |
| 1996 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1997 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1998 | try { |
| 1999 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2000 | "User cannot modify accounts"); |
| 2001 | } catch (RemoteException re) { |
| 2002 | } |
| 2003 | return; |
| 2004 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2005 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2006 | try { |
| 2007 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2008 | "User cannot modify accounts of this type (policy)."); |
| 2009 | } catch (RemoteException re) { |
| 2010 | } |
| 2011 | return; |
| 2012 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2013 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2014 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2015 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2016 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2017 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2018 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2019 | if (account.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 2020 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2021 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2022 | } |
| 2023 | } |
| 2024 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2025 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2026 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2027 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2028 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2029 | accountId, |
| 2030 | accounts, |
| 2031 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2032 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2033 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2034 | } finally { |
| 2035 | restoreCallingIdentity(identityToken); |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | @Override |
| 2040 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2041 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2042 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2043 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2044 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2045 | + ", pid " + Binder.getCallingPid()); |
| 2046 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2047 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2048 | if (account == null) { |
| 2049 | /* |
| 2050 | * Null accounts should result in returning false, as per |
| 2051 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2052 | */ |
| 2053 | Log.e(TAG, "account is null"); |
| 2054 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2055 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2056 | String msg = String.format( |
| 2057 | "uid %s cannot explicitly add accounts of type: %s", |
| 2058 | callingUid, |
| 2059 | account.type); |
| 2060 | throw new SecurityException(msg); |
| 2061 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2062 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2063 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2064 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2065 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2066 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2067 | accountId, |
| 2068 | accounts, |
| 2069 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2070 | long identityToken = clearCallingIdentity(); |
| 2071 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2072 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2073 | } finally { |
| 2074 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2075 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2078 | private class RemoveAccountSession extends Session { |
| 2079 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2080 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2081 | Account account, boolean expectActivityLaunch) { |
| 2082 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2083 | true /* stripAuthTokenFromResult */, account.name, |
| 2084 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2085 | mAccount = account; |
| 2086 | } |
| 2087 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2088 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2089 | protected String toDebugString(long now) { |
| 2090 | return super.toDebugString(now) + ", removeAccount" |
| 2091 | + ", account " + mAccount; |
| 2092 | } |
| 2093 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2094 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2095 | public void run() throws RemoteException { |
| 2096 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2097 | } |
| 2098 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2099 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2100 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2101 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2102 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2103 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2104 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2105 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2106 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2107 | } |
| 2108 | IAccountManagerResponse response = getResponseAndClose(); |
| 2109 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2110 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2111 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2112 | + response); |
| 2113 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2114 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2115 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2116 | try { |
| 2117 | response.onResult(result2); |
| 2118 | } catch (RemoteException e) { |
| 2119 | // ignore |
| 2120 | } |
| 2121 | } |
| 2122 | } |
| 2123 | super.onResult(result); |
| 2124 | } |
| 2125 | } |
| 2126 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2127 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2128 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2129 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2130 | } |
| 2131 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2132 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2133 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2134 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2135 | if (!userUnlocked) { |
| 2136 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2137 | + " is still locked. CE data will be removed later"); |
| 2138 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2139 | synchronized (accounts.dbLock) { |
| 2140 | synchronized (accounts.cacheLock) { |
| 2141 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, |
| 2142 | accounts); |
| 2143 | accounts.accountsDb.beginTransaction(); |
| 2144 | // Set to a dummy value, this will only be used if the database |
| 2145 | // transaction succeeds. |
| 2146 | long accountId = -1; |
| 2147 | try { |
| 2148 | accountId = accounts.accountsDb.findDeAccountId(account); |
| 2149 | if (accountId >= 0) { |
| 2150 | isChanged = accounts.accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2151 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2152 | // always delete from CE table if CE storage is available |
| 2153 | // DE account could be removed while CE was locked |
| 2154 | if (userUnlocked) { |
| 2155 | long ceAccountId = accounts.accountsDb.findCeAccountId(account); |
| 2156 | if (ceAccountId >= 0) { |
| 2157 | accounts.accountsDb.deleteCeAccount(ceAccountId); |
| 2158 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2159 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2160 | accounts.accountsDb.setTransactionSuccessful(); |
| 2161 | } finally { |
| 2162 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2163 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2164 | if (isChanged) { |
| 2165 | removeAccountFromCacheLocked(accounts, account); |
| 2166 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 2167 | .entrySet()) { |
| 2168 | if (packageToVisibility.getValue() |
| 2169 | != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 2170 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 2171 | } |
| 2172 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2173 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2174 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occurred. |
| 2175 | sendAccountsChangedBroadcast(accounts.userId); |
| 2176 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2177 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2178 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
| 2179 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2180 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2181 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2182 | long id = Binder.clearCallingIdentity(); |
| 2183 | try { |
| 2184 | int parentUserId = accounts.userId; |
| 2185 | if (canHaveProfile(parentUserId)) { |
| 2186 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2187 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2188 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2189 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2190 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2191 | } |
| 2192 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2193 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2194 | } finally { |
| 2195 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2196 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2197 | |
| 2198 | if (isChanged) { |
| 2199 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2200 | for (Pair<Pair<Account, String>, Integer> key |
| 2201 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2202 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2203 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2204 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2205 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2206 | account, uid, false)); |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2212 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2215 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2216 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2217 | int callerUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2218 | Preconditions.checkNotNull(accountType, "accountType cannot be null"); |
| 2219 | Preconditions.checkNotNull(authToken, "authToken cannot be null"); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2220 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2221 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2222 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2223 | + ", pid " + Binder.getCallingPid()); |
| 2224 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2225 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2226 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2227 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2228 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2229 | List<Pair<Account, String>> deletedTokens; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2230 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2231 | accounts.accountsDb.beginTransaction(); |
| 2232 | try { |
| 2233 | deletedTokens = invalidateAuthTokenLocked(accounts, accountType, authToken); |
| 2234 | accounts.accountsDb.setTransactionSuccessful(); |
| 2235 | } finally { |
| 2236 | accounts.accountsDb.endTransaction(); |
| 2237 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2238 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2239 | for (Pair<Account, String> tokenInfo : deletedTokens) { |
| 2240 | Account act = tokenInfo.first; |
| 2241 | String tokenType = tokenInfo.second; |
| 2242 | writeAuthTokenIntoCacheLocked(accounts, act, tokenType, null); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2243 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2244 | // wipe out cached token in memory. |
| 2245 | accounts.accountTokenCaches.remove(accountType, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2246 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2247 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2248 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2249 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2250 | } |
| 2251 | } |
| 2252 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2253 | private List<Pair<Account, String>> invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2254 | String authToken) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2255 | // TODO Move to AccountsDB |
| 2256 | List<Pair<Account, String>> results = new ArrayList<>(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2257 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2258 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2259 | try { |
| 2260 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2261 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2262 | String accountName = cursor.getString(1); |
| 2263 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2264 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2265 | results.add(Pair.create(new Account(accountName, accountType), authTokenType)); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2266 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2267 | } finally { |
| 2268 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2269 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2270 | return results; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2271 | } |
| 2272 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2273 | private void saveCachedToken( |
| 2274 | UserAccounts accounts, |
| 2275 | Account account, |
| 2276 | String callerPkg, |
| 2277 | byte[] callerSigDigest, |
| 2278 | String tokenType, |
| 2279 | String token, |
| 2280 | long expiryMillis) { |
| 2281 | |
| 2282 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2283 | return; |
| 2284 | } |
| 2285 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2286 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2287 | synchronized (accounts.cacheLock) { |
| 2288 | accounts.accountTokenCaches.put( |
| 2289 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2293 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2294 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2295 | if (account == null || type == null) { |
| 2296 | return false; |
| 2297 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2298 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2299 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2300 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2301 | accounts.accountsDb.beginTransaction(); |
| 2302 | boolean updateCache = false; |
| 2303 | try { |
| 2304 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2305 | if (accountId < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2306 | return false; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2307 | } |
| 2308 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2309 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2310 | accounts.accountsDb.setTransactionSuccessful(); |
| 2311 | updateCache = true; |
| 2312 | return true; |
| 2313 | } |
| 2314 | return false; |
| 2315 | } finally { |
| 2316 | accounts.accountsDb.endTransaction(); |
| 2317 | if (updateCache) { |
| 2318 | synchronized (accounts.cacheLock) { |
| 2319 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
| 2320 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2321 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2322 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2323 | } |
| 2324 | } |
| 2325 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2326 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2327 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2328 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2329 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2330 | Log.v(TAG, "peekAuthToken: " + account |
| 2331 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2332 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2333 | + ", pid " + Binder.getCallingPid()); |
| 2334 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2335 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2336 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2337 | int userId = UserHandle.getCallingUserId(); |
| 2338 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2339 | String msg = String.format( |
| 2340 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2341 | callingUid, |
| 2342 | account.type); |
| 2343 | throw new SecurityException(msg); |
| 2344 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2345 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2346 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2347 | + callingUid); |
| 2348 | return null; |
| 2349 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2350 | long identityToken = clearCallingIdentity(); |
| 2351 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2352 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2353 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2354 | } finally { |
| 2355 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2356 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2359 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2360 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2361 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2362 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2363 | Log.v(TAG, "setAuthToken: " + account |
| 2364 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2365 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2366 | + ", pid " + Binder.getCallingPid()); |
| 2367 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2368 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2369 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2370 | int userId = UserHandle.getCallingUserId(); |
| 2371 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2372 | String msg = String.format( |
| 2373 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2374 | callingUid, |
| 2375 | account.type); |
| 2376 | throw new SecurityException(msg); |
| 2377 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2378 | long identityToken = clearCallingIdentity(); |
| 2379 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2380 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2381 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2382 | } finally { |
| 2383 | restoreCallingIdentity(identityToken); |
| 2384 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2385 | } |
| 2386 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2387 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2388 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2389 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2390 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2391 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2392 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2393 | + ", pid " + Binder.getCallingPid()); |
| 2394 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2395 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2396 | int userId = UserHandle.getCallingUserId(); |
| 2397 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2398 | String msg = String.format( |
| 2399 | "uid %s cannot set secrets for accounts of type: %s", |
| 2400 | callingUid, |
| 2401 | account.type); |
| 2402 | throw new SecurityException(msg); |
| 2403 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2404 | long identityToken = clearCallingIdentity(); |
| 2405 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2406 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2407 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2408 | } finally { |
| 2409 | restoreCallingIdentity(identityToken); |
| 2410 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2411 | } |
| 2412 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2413 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2414 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2415 | if (account == null) { |
| 2416 | return; |
| 2417 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2418 | boolean isChanged = false; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2419 | synchronized (accounts.dbLock) { |
| 2420 | synchronized (accounts.cacheLock) { |
| 2421 | accounts.accountsDb.beginTransaction(); |
| 2422 | try { |
| 2423 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2424 | if (accountId >= 0) { |
| 2425 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2426 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
| 2427 | accounts.authTokenCache.remove(account); |
| 2428 | accounts.accountTokenCaches.remove(account); |
| 2429 | accounts.accountsDb.setTransactionSuccessful(); |
| 2430 | // If there is an account whose password will be updated and the database |
| 2431 | // transactions succeed, then we say that a change has occured. Even if the |
| 2432 | // new password is the same as the old and there were no authtokens to |
| 2433 | // delete. |
| 2434 | isChanged = true; |
| 2435 | String action = (password == null || password.length() == 0) ? |
| 2436 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2437 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2438 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, |
| 2439 | callingUid); |
| 2440 | } |
| 2441 | } finally { |
| 2442 | accounts.accountsDb.endTransaction(); |
| 2443 | if (isChanged) { |
| 2444 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
| 2445 | sendNotificationAccountUpdated(account, accounts); |
| 2446 | sendAccountsChangedBroadcast(accounts.userId); |
| 2447 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2448 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2449 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2450 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2451 | } |
| 2452 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2453 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2454 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2455 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2456 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2457 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2458 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2459 | + ", pid " + Binder.getCallingPid()); |
| 2460 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2461 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2462 | int userId = UserHandle.getCallingUserId(); |
| 2463 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2464 | String msg = String.format( |
| 2465 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2466 | callingUid, |
| 2467 | account.type); |
| 2468 | throw new SecurityException(msg); |
| 2469 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2470 | long identityToken = clearCallingIdentity(); |
| 2471 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2472 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2473 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2474 | } finally { |
| 2475 | restoreCallingIdentity(identityToken); |
| 2476 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2477 | } |
| 2478 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2479 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2480 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2481 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2482 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2483 | Log.v(TAG, "setUserData: " + account |
| 2484 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2485 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2486 | + ", pid " + Binder.getCallingPid()); |
| 2487 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2488 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2489 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2490 | int userId = UserHandle.getCallingUserId(); |
| 2491 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2492 | String msg = String.format( |
| 2493 | "uid %s cannot set user data for accounts of type: %s", |
| 2494 | callingUid, |
| 2495 | account.type); |
| 2496 | throw new SecurityException(msg); |
| 2497 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2498 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2499 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2500 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2501 | if (!accountExistsCache(accounts, account)) { |
| 2502 | return; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2503 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2504 | setUserdataInternal(accounts, account, key, value); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2505 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2506 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2510 | private boolean accountExistsCache(UserAccounts accounts, Account account) { |
| 2511 | synchronized (accounts.cacheLock) { |
| 2512 | if (accounts.accountCache.containsKey(account.type)) { |
| 2513 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2514 | if (acc.name.equals(account.name)) { |
| 2515 | return true; |
| 2516 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2517 | } |
| 2518 | } |
| 2519 | } |
| 2520 | return false; |
| 2521 | } |
| 2522 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2523 | private void setUserdataInternal(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2524 | String value) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2525 | synchronized (accounts.dbLock) { |
| 2526 | accounts.accountsDb.beginTransaction(); |
| 2527 | try { |
| 2528 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2529 | if (accountId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2530 | return; |
| 2531 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2532 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
| 2533 | if (extrasId < 0) { |
| 2534 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
| 2535 | if (extrasId < 0) { |
| 2536 | return; |
| 2537 | } |
| 2538 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
| 2539 | return; |
| 2540 | } |
| 2541 | accounts.accountsDb.setTransactionSuccessful(); |
| 2542 | } finally { |
| 2543 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2544 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2545 | synchronized (accounts.cacheLock) { |
| 2546 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2547 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2548 | } |
| 2549 | } |
| 2550 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2551 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2552 | if (result == null) { |
| 2553 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2554 | } |
| 2555 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2556 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2557 | + response); |
| 2558 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2559 | try { |
| 2560 | response.onResult(result); |
| 2561 | } catch (RemoteException e) { |
| 2562 | // if the caller is dead then there is no one to care about remote |
| 2563 | // exceptions |
| 2564 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2565 | Log.v(TAG, "failure while notifying response", e); |
| 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2570 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2571 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2572 | final String authTokenType) |
| 2573 | throws RemoteException { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2574 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
| 2575 | Preconditions.checkArgument(authTokenType != null, "authTokenType cannot be null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2576 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2577 | final int callingUid = getCallingUid(); |
| 2578 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2579 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2580 | throw new SecurityException("can only call from system"); |
| 2581 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2582 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2583 | long identityToken = clearCallingIdentity(); |
| 2584 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2585 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2586 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2587 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2588 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2589 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2590 | protected String toDebugString(long now) { |
| 2591 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2592 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2593 | + ", authTokenType " + authTokenType; |
| 2594 | } |
| 2595 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2596 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2597 | public void run() throws RemoteException { |
| 2598 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2599 | } |
| 2600 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2601 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2602 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2603 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2604 | if (result != null) { |
| 2605 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2606 | Bundle bundle = new Bundle(); |
| 2607 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2608 | super.onResult(bundle); |
| 2609 | return; |
| 2610 | } else { |
| 2611 | super.onResult(result); |
| 2612 | } |
| 2613 | } |
| 2614 | }.bind(); |
| 2615 | } finally { |
| 2616 | restoreCallingIdentity(identityToken); |
| 2617 | } |
| 2618 | } |
| 2619 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2620 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2621 | public void getAuthToken( |
| 2622 | IAccountManagerResponse response, |
| 2623 | final Account account, |
| 2624 | final String authTokenType, |
| 2625 | final boolean notifyOnAuthFailure, |
| 2626 | final boolean expectActivityLaunch, |
| 2627 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2628 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2629 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2630 | Log.v(TAG, "getAuthToken: " + account |
| 2631 | + ", response " + response |
| 2632 | + ", authTokenType " + authTokenType |
| 2633 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2634 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2635 | + ", caller's uid " + Binder.getCallingUid() |
| 2636 | + ", pid " + Binder.getCallingPid()); |
| 2637 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2638 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2639 | try { |
| 2640 | if (account == null) { |
| 2641 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2642 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2643 | return; |
| 2644 | } |
| 2645 | if (authTokenType == null) { |
| 2646 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2647 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2648 | return; |
| 2649 | } |
| 2650 | } catch (RemoteException e) { |
| 2651 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2652 | return; |
| 2653 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2654 | int userId = UserHandle.getCallingUserId(); |
| 2655 | long ident = Binder.clearCallingIdentity(); |
| 2656 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2657 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2658 | try { |
| 2659 | accounts = getUserAccounts(userId); |
| 2660 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2661 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2662 | } finally { |
| 2663 | Binder.restoreCallingIdentity(ident); |
| 2664 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2665 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2666 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2667 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2668 | |
| 2669 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2670 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2671 | final boolean permissionGranted = |
| 2672 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2673 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2674 | // Get the calling package. We will use it for the purpose of caching. |
| 2675 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2676 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2677 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2678 | try { |
| 2679 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2680 | } finally { |
| 2681 | Binder.restoreCallingIdentity(ident); |
| 2682 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2683 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2684 | String msg = String.format( |
| 2685 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2686 | callerUid, |
| 2687 | callerPkg); |
| 2688 | throw new SecurityException(msg); |
| 2689 | } |
| 2690 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2691 | // let authenticator know the identity of the caller |
| 2692 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2693 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2694 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2695 | if (notifyOnAuthFailure) { |
| 2696 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2697 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2698 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2699 | long identityToken = clearCallingIdentity(); |
| 2700 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2701 | // Distill the caller's package signatures into a single digest. |
| 2702 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2703 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2704 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2705 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2706 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2707 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2708 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2709 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2710 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2711 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2712 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2713 | onResult(response, result); |
| 2714 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2715 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2718 | if (customTokens) { |
| 2719 | /* |
| 2720 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2721 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2722 | * ANDORID_PACKAGE_NAME. |
| 2723 | */ |
| 2724 | String token = readCachedTokenInternal( |
| 2725 | accounts, |
| 2726 | account, |
| 2727 | authTokenType, |
| 2728 | callerPkg, |
| 2729 | callerPkgSigDigest); |
| 2730 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2731 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2732 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2733 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2734 | Bundle result = new Bundle(); |
| 2735 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2736 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2737 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2738 | onResult(response, result); |
| 2739 | return; |
| 2740 | } |
| 2741 | } |
| 2742 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2743 | new Session( |
| 2744 | accounts, |
| 2745 | response, |
| 2746 | account.type, |
| 2747 | expectActivityLaunch, |
| 2748 | false /* stripAuthTokenFromResult */, |
| 2749 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2750 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2751 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2752 | protected String toDebugString(long now) { |
| 2753 | if (loginOptions != null) loginOptions.keySet(); |
| 2754 | return super.toDebugString(now) + ", getAuthToken" |
| 2755 | + ", " + account |
| 2756 | + ", authTokenType " + authTokenType |
| 2757 | + ", loginOptions " + loginOptions |
| 2758 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2759 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2760 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2761 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2762 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2763 | // If the caller doesn't have permission then create and return the |
| 2764 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2765 | if (!permissionGranted) { |
| 2766 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2767 | } else { |
| 2768 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2769 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2772 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2773 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2774 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2775 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2776 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2777 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2778 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2779 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2780 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2781 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2782 | authTokenType, |
| 2783 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2784 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2785 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2786 | onResult(bundle); |
| 2787 | return; |
| 2788 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2789 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2790 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2791 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2792 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2793 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2794 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2795 | "the type and name should not be empty"); |
| 2796 | return; |
| 2797 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2798 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2799 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2800 | saveAuthTokenToDatabase( |
| 2801 | mAccounts, |
| 2802 | resultAccount, |
| 2803 | authTokenType, |
| 2804 | authToken); |
| 2805 | } |
| 2806 | long expiryMillis = result.getLong( |
| 2807 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2808 | if (customTokens |
| 2809 | && expiryMillis > System.currentTimeMillis()) { |
| 2810 | saveCachedToken( |
| 2811 | mAccounts, |
| 2812 | account, |
| 2813 | callerPkg, |
| 2814 | callerPkgSigDigest, |
| 2815 | authTokenType, |
| 2816 | authToken, |
| 2817 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2818 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2819 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2820 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2821 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2822 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2823 | /* |
| 2824 | * Make sure that the supplied intent is owned by the authenticator |
| 2825 | * giving it to the system. Otherwise a malicious authenticator could |
| 2826 | * have users launching arbitrary activities by tricking users to |
| 2827 | * interact with malicious notifications. |
| 2828 | */ |
| 2829 | checkKeyIntent( |
| 2830 | Binder.getCallingUid(), |
| 2831 | intent); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2832 | doNotification( |
| 2833 | mAccounts, |
| 2834 | account, |
| 2835 | result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2836 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2837 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2838 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2839 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2840 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2841 | }.bind(); |
| 2842 | } finally { |
| 2843 | restoreCallingIdentity(identityToken); |
| 2844 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2845 | } |
| 2846 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2847 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2848 | MessageDigest digester; |
| 2849 | try { |
| 2850 | digester = MessageDigest.getInstance("SHA-256"); |
| 2851 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2852 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2853 | for (Signature sig : pkgInfo.signatures) { |
| 2854 | digester.update(sig.toByteArray()); |
| 2855 | } |
| 2856 | } catch (NoSuchAlgorithmException x) { |
| 2857 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2858 | digester = null; |
| 2859 | } catch (NameNotFoundException e) { |
| 2860 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2861 | digester = null; |
| 2862 | } |
| 2863 | return (digester == null) ? null : digester.digest(); |
| 2864 | } |
| 2865 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2866 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2867 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2868 | int uid = intent.getIntExtra( |
| 2869 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2870 | String authTokenType = intent.getStringExtra( |
| 2871 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2872 | final String titleAndSubtitle = |
| 2873 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2874 | account.name); |
| 2875 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2876 | String title = titleAndSubtitle; |
| 2877 | String subtitle = ""; |
| 2878 | if (index > 0) { |
| 2879 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2880 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2881 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2882 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2883 | Context contextForUser = getContextForUser(user); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 2884 | Notification n = |
| 2885 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
| 2886 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2887 | .setWhen(0) |
| 2888 | .setColor(contextForUser.getColor( |
| 2889 | com.android.internal.R.color.system_notification_accent_color)) |
| 2890 | .setContentTitle(title) |
| 2891 | .setContentText(subtitle) |
| 2892 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2893 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2894 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2895 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2896 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2897 | } |
| 2898 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2899 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 2900 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 2901 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2902 | |
| 2903 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2904 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2905 | if (startInNewTask) { |
| 2906 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 2907 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2908 | // the intent from a non-Activity context. This is the default behavior. |
| 2909 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2910 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 2911 | intent.addCategory(getCredentialPermissionNotificationId(account, |
| 2912 | authTokenType, uid).mTag + (packageName != null ? packageName : "")); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2913 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2914 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2915 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2916 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2917 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2918 | return intent; |
| 2919 | } |
| 2920 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 2921 | private NotificationId getCredentialPermissionNotificationId(Account account, |
| 2922 | String authTokenType, int uid) { |
| 2923 | NotificationId nId; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2924 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2925 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2926 | final Pair<Pair<Account, String>, Integer> key = |
| 2927 | new Pair<Pair<Account, String>, Integer>( |
| 2928 | new Pair<Account, String>(account, authTokenType), uid); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 2929 | nId = accounts.credentialsPermissionNotificationIds.get(key); |
| 2930 | if (nId == null) { |
| 2931 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION |
| 2932 | + ":" + account.hashCode() + ":" + authTokenType.hashCode(); |
| 2933 | int id = SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION; |
| 2934 | nId = new NotificationId(tag, id); |
| 2935 | accounts.credentialsPermissionNotificationIds.put(key, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2936 | } |
| 2937 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 2938 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 2941 | private NotificationId getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
| 2942 | NotificationId nId; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2943 | synchronized (accounts.signinRequiredNotificationIds) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 2944 | nId = accounts.signinRequiredNotificationIds.get(account); |
| 2945 | if (nId == null) { |
| 2946 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN |
| 2947 | + ":" + account.hashCode(); |
| 2948 | int id = SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN; |
| 2949 | nId = new NotificationId(tag, id); |
| 2950 | accounts.signinRequiredNotificationIds.put(account, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2951 | } |
| 2952 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 2953 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2954 | } |
| 2955 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2956 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2957 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2958 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2959 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2960 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2961 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2962 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2963 | + ", response " + response |
| 2964 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2965 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2966 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2967 | + ", caller's uid " + Binder.getCallingUid() |
| 2968 | + ", pid " + Binder.getCallingPid()); |
| 2969 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2970 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2971 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2972 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2973 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2974 | final int uid = Binder.getCallingUid(); |
| 2975 | final int userId = UserHandle.getUserId(uid); |
| 2976 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2977 | try { |
| 2978 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2979 | "User is not allowed to add an account!"); |
| 2980 | } catch (RemoteException re) { |
| 2981 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2982 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2983 | return; |
| 2984 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2985 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2986 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2987 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2988 | "User cannot modify accounts of this type (policy)."); |
| 2989 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2990 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2991 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2992 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2993 | return; |
| 2994 | } |
| 2995 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2996 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2997 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2998 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2999 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3000 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3001 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3002 | long identityToken = clearCallingIdentity(); |
| 3003 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3004 | UserAccounts accounts = getUserAccounts(usrId); |
| 3005 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3006 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3007 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3008 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3009 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3010 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3011 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3012 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 3013 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3014 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3015 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3016 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3017 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3018 | protected String toDebugString(long now) { |
| 3019 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3020 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3021 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3022 | } |
| 3023 | }.bind(); |
| 3024 | } finally { |
| 3025 | restoreCallingIdentity(identityToken); |
| 3026 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3029 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3030 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 3031 | final String authTokenType, final String[] requiredFeatures, |
| 3032 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3033 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3034 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3035 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3036 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3037 | + ", response " + response |
| 3038 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3039 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3040 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3041 | + ", caller's uid " + Binder.getCallingUid() |
| 3042 | + ", pid " + Binder.getCallingPid() |
| 3043 | + ", for user id " + userId); |
| 3044 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3045 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3046 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3047 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3048 | if (isCrossUser(callingUid, userId)) { |
| 3049 | throw new SecurityException( |
| 3050 | String.format( |
| 3051 | "User %s trying to add account for %s" , |
| 3052 | UserHandle.getCallingUserId(), |
| 3053 | userId)); |
| 3054 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3055 | |
| 3056 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3057 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3058 | try { |
| 3059 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3060 | "User is not allowed to add an account!"); |
| 3061 | } catch (RemoteException re) { |
| 3062 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3063 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3064 | return; |
| 3065 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3066 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3067 | try { |
| 3068 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3069 | "User cannot modify accounts of this type (policy)."); |
| 3070 | } catch (RemoteException re) { |
| 3071 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3072 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3073 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3074 | return; |
| 3075 | } |
| 3076 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3077 | final int pid = Binder.getCallingPid(); |
| 3078 | final int uid = Binder.getCallingUid(); |
| 3079 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3080 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3081 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3082 | |
| 3083 | long identityToken = clearCallingIdentity(); |
| 3084 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3085 | UserAccounts accounts = getUserAccounts(userId); |
| 3086 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3087 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3088 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3089 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3090 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3091 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3092 | @Override |
| 3093 | public void run() throws RemoteException { |
| 3094 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3095 | options); |
| 3096 | } |
| 3097 | |
| 3098 | @Override |
| 3099 | protected String toDebugString(long now) { |
| 3100 | return super.toDebugString(now) + ", addAccount" |
| 3101 | + ", accountType " + accountType |
| 3102 | + ", requiredFeatures " |
| 3103 | + (requiredFeatures != null |
| 3104 | ? TextUtils.join(",", requiredFeatures) |
| 3105 | : null); |
| 3106 | } |
| 3107 | }.bind(); |
| 3108 | } finally { |
| 3109 | restoreCallingIdentity(identityToken); |
| 3110 | } |
| 3111 | } |
| 3112 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3113 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3114 | public void startAddAccountSession( |
| 3115 | final IAccountManagerResponse response, |
| 3116 | final String accountType, |
| 3117 | final String authTokenType, |
| 3118 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3119 | final boolean expectActivityLaunch, |
| 3120 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3121 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3122 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3123 | Log.v(TAG, |
| 3124 | "startAddAccountSession: accountType " + accountType |
| 3125 | + ", response " + response |
| 3126 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3127 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3128 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3129 | + ", caller's uid " + Binder.getCallingUid() |
| 3130 | + ", pid " + Binder.getCallingPid()); |
| 3131 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3132 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3133 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3134 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3135 | final int uid = Binder.getCallingUid(); |
| 3136 | final int userId = UserHandle.getUserId(uid); |
| 3137 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3138 | try { |
| 3139 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3140 | "User is not allowed to add an account!"); |
| 3141 | } catch (RemoteException re) { |
| 3142 | } |
| 3143 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3144 | return; |
| 3145 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3146 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3147 | try { |
| 3148 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3149 | "User cannot modify accounts of this type (policy)."); |
| 3150 | } catch (RemoteException re) { |
| 3151 | } |
| 3152 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3153 | userId); |
| 3154 | return; |
| 3155 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3156 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3157 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3158 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3159 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3160 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3161 | // Check to see if the Password should be included to the caller. |
| 3162 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3163 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3164 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3165 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3166 | long identityToken = clearCallingIdentity(); |
| 3167 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3168 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3169 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3170 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3171 | new StartAccountSession( |
| 3172 | accounts, |
| 3173 | response, |
| 3174 | accountType, |
| 3175 | expectActivityLaunch, |
| 3176 | null /* accountName */, |
| 3177 | false /* authDetailsRequired */, |
| 3178 | true /* updateLastAuthenticationTime */, |
| 3179 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3180 | @Override |
| 3181 | public void run() throws RemoteException { |
| 3182 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3183 | requiredFeatures, options); |
| 3184 | } |
| 3185 | |
| 3186 | @Override |
| 3187 | protected String toDebugString(long now) { |
| 3188 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3189 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3190 | + accountType + ", requiredFeatures " |
| 3191 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3192 | } |
| 3193 | }.bind(); |
| 3194 | } finally { |
| 3195 | restoreCallingIdentity(identityToken); |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3200 | private abstract class StartAccountSession extends Session { |
| 3201 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3202 | private final boolean mIsPasswordForwardingAllowed; |
| 3203 | |
| 3204 | public StartAccountSession( |
| 3205 | UserAccounts accounts, |
| 3206 | IAccountManagerResponse response, |
| 3207 | String accountType, |
| 3208 | boolean expectActivityLaunch, |
| 3209 | String accountName, |
| 3210 | boolean authDetailsRequired, |
| 3211 | boolean updateLastAuthenticationTime, |
| 3212 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3213 | super(accounts, response, accountType, expectActivityLaunch, |
| 3214 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3215 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3216 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | @Override |
| 3220 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3221 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3222 | mNumResults++; |
| 3223 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3224 | if (result != null |
| 3225 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3226 | checkKeyIntent( |
| 3227 | Binder.getCallingUid(), |
| 3228 | intent); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3229 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3230 | IAccountManagerResponse response; |
| 3231 | if (mExpectActivityLaunch && result != null |
| 3232 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3233 | response = mResponse; |
| 3234 | } else { |
| 3235 | response = getResponseAndClose(); |
| 3236 | } |
| 3237 | if (response == null) { |
| 3238 | return; |
| 3239 | } |
| 3240 | if (result == null) { |
| 3241 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3242 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3243 | + response); |
| 3244 | } |
| 3245 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3246 | "null bundle returned"); |
| 3247 | return; |
| 3248 | } |
| 3249 | |
| 3250 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3251 | // All AccountManager error codes are greater |
| 3252 | // than 0 |
| 3253 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3254 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3255 | return; |
| 3256 | } |
| 3257 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3258 | // Omit passwords if the caller isn't permitted to see them. |
| 3259 | if (!mIsPasswordForwardingAllowed) { |
| 3260 | result.remove(AccountManager.KEY_PASSWORD); |
| 3261 | } |
| 3262 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3263 | // Strip auth token from result. |
| 3264 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3265 | |
| 3266 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3267 | Log.v(TAG, |
| 3268 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3269 | } |
| 3270 | |
| 3271 | // Get the session bundle created by authenticator. The |
| 3272 | // bundle contains data necessary for finishing the session |
| 3273 | // later. The session bundle will be encrypted here and |
| 3274 | // decrypted later when trying to finish the session. |
| 3275 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3276 | if (sessionBundle != null) { |
| 3277 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3278 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3279 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3280 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3281 | } |
| 3282 | // Add accountType info to session bundle. This will |
| 3283 | // override any value set by authenticator. |
| 3284 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3285 | |
| 3286 | // Encrypt session bundle before returning to caller. |
| 3287 | try { |
| 3288 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3289 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3290 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3291 | } catch (GeneralSecurityException e) { |
| 3292 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3293 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3294 | } |
| 3295 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3296 | "failed to encrypt session bundle"); |
| 3297 | return; |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | sendResponse(response, result); |
| 3302 | } |
| 3303 | } |
| 3304 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3305 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3306 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3307 | @NonNull Bundle sessionBundle, |
| 3308 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3309 | Bundle appInfo, |
| 3310 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3311 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3312 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3313 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3314 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3315 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3316 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3317 | + ", caller's uid " + callingUid |
| 3318 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3319 | + ", pid " + Binder.getCallingPid() |
| 3320 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3321 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3322 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3323 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3324 | // Account type is added to it before encryption. |
| 3325 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3326 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3327 | } |
| 3328 | |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 3329 | // Only allow the system process to finish session for other users. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3330 | if (isCrossUser(callingUid, userId)) { |
| 3331 | throw new SecurityException( |
| 3332 | String.format( |
| 3333 | "User %s trying to finish session for %s without cross user permission", |
| 3334 | UserHandle.getCallingUserId(), |
| 3335 | userId)); |
| 3336 | } |
| 3337 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3338 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3339 | sendErrorResponse(response, |
| 3340 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3341 | "User is not allowed to add an account!"); |
| 3342 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3343 | return; |
| 3344 | } |
| 3345 | |
| 3346 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3347 | final Bundle decryptedBundle; |
| 3348 | final String accountType; |
| 3349 | // First decrypt session bundle to get account type for checking permission. |
| 3350 | try { |
| 3351 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3352 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3353 | if (decryptedBundle == null) { |
| 3354 | sendErrorResponse( |
| 3355 | response, |
| 3356 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3357 | "failed to decrypt session bundle"); |
| 3358 | return; |
| 3359 | } |
| 3360 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3361 | // Account type cannot be null. This should not happen if session bundle was created |
| 3362 | // properly by #StartAccountSession. |
| 3363 | if (TextUtils.isEmpty(accountType)) { |
| 3364 | sendErrorResponse( |
| 3365 | response, |
| 3366 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3367 | "accountType is empty"); |
| 3368 | return; |
| 3369 | } |
| 3370 | |
| 3371 | // If by any chances, decryptedBundle contains colliding keys with |
| 3372 | // system info |
| 3373 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3374 | // update credentials flow, we should replace with the new values of the current call. |
| 3375 | if (appInfo != null) { |
| 3376 | decryptedBundle.putAll(appInfo); |
| 3377 | } |
| 3378 | |
| 3379 | // 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] | 3380 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3381 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3382 | } catch (GeneralSecurityException e) { |
| 3383 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3384 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3385 | } |
| 3386 | sendErrorResponse( |
| 3387 | response, |
| 3388 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3389 | "failed to decrypt session bundle"); |
| 3390 | return; |
| 3391 | } |
| 3392 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3393 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3394 | sendErrorResponse( |
| 3395 | response, |
| 3396 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3397 | "User cannot modify accounts of this type (policy)."); |
| 3398 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3399 | userId); |
| 3400 | return; |
| 3401 | } |
| 3402 | |
| 3403 | long identityToken = clearCallingIdentity(); |
| 3404 | try { |
| 3405 | UserAccounts accounts = getUserAccounts(userId); |
| 3406 | logRecordWithUid( |
| 3407 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3408 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3409 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3410 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3411 | new Session( |
| 3412 | accounts, |
| 3413 | response, |
| 3414 | accountType, |
| 3415 | expectActivityLaunch, |
| 3416 | true /* stripAuthTokenFromResult */, |
| 3417 | null /* accountName */, |
| 3418 | false /* authDetailsRequired */, |
| 3419 | true /* updateLastAuthenticationTime */) { |
| 3420 | @Override |
| 3421 | public void run() throws RemoteException { |
| 3422 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3423 | } |
| 3424 | |
| 3425 | @Override |
| 3426 | protected String toDebugString(long now) { |
| 3427 | return super.toDebugString(now) |
| 3428 | + ", finishSession" |
| 3429 | + ", accountType " + accountType; |
| 3430 | } |
| 3431 | }.bind(); |
| 3432 | } finally { |
| 3433 | restoreCallingIdentity(identityToken); |
| 3434 | } |
| 3435 | } |
| 3436 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3437 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3438 | final DevicePolicyManagerInternal dpmi = |
| 3439 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3440 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3441 | if (dpmi == null) { |
| 3442 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3443 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3444 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3445 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3446 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3447 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3448 | } |
| 3449 | if (intent == null) { |
| 3450 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3451 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3452 | long identityToken = clearCallingIdentity(); |
| 3453 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3454 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3455 | } finally { |
| 3456 | restoreCallingIdentity(identityToken); |
| 3457 | } |
| 3458 | } |
| 3459 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3460 | /** |
| 3461 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3462 | */ |
| 3463 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3464 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3465 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3466 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3467 | return cantAddAccount; |
| 3468 | } |
| 3469 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3470 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3471 | public void confirmCredentialsAsUser( |
| 3472 | IAccountManagerResponse response, |
| 3473 | final Account account, |
| 3474 | final Bundle options, |
| 3475 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3476 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3477 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3478 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3479 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3480 | Log.v(TAG, "confirmCredentials: " + account |
| 3481 | + ", response " + response |
| 3482 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3483 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3484 | + ", pid " + Binder.getCallingPid()); |
| 3485 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3486 | // Only allow the system process to read accounts of other users |
| 3487 | if (isCrossUser(callingUid, userId)) { |
| 3488 | throw new SecurityException( |
| 3489 | String.format( |
| 3490 | "User %s trying to confirm account credentials for %s" , |
| 3491 | UserHandle.getCallingUserId(), |
| 3492 | userId)); |
| 3493 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3494 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3495 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3496 | long identityToken = clearCallingIdentity(); |
| 3497 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3498 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3499 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3500 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3501 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3502 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3503 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3504 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3505 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3506 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3507 | protected String toDebugString(long now) { |
| 3508 | return super.toDebugString(now) + ", confirmCredentials" |
| 3509 | + ", " + account; |
| 3510 | } |
| 3511 | }.bind(); |
| 3512 | } finally { |
| 3513 | restoreCallingIdentity(identityToken); |
| 3514 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3515 | } |
| 3516 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3517 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3518 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3519 | final String authTokenType, final boolean expectActivityLaunch, |
| 3520 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3521 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3522 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3523 | Log.v(TAG, "updateCredentials: " + account |
| 3524 | + ", response " + response |
| 3525 | + ", authTokenType " + authTokenType |
| 3526 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3527 | + ", caller's uid " + Binder.getCallingUid() |
| 3528 | + ", pid " + Binder.getCallingPid()); |
| 3529 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3530 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3531 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3532 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3533 | long identityToken = clearCallingIdentity(); |
| 3534 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3535 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3536 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3537 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3538 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3539 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3540 | public void run() throws RemoteException { |
| 3541 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3542 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3543 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3544 | protected String toDebugString(long now) { |
| 3545 | if (loginOptions != null) loginOptions.keySet(); |
| 3546 | return super.toDebugString(now) + ", updateCredentials" |
| 3547 | + ", " + account |
| 3548 | + ", authTokenType " + authTokenType |
| 3549 | + ", loginOptions " + loginOptions; |
| 3550 | } |
| 3551 | }.bind(); |
| 3552 | } finally { |
| 3553 | restoreCallingIdentity(identityToken); |
| 3554 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3555 | } |
| 3556 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3557 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3558 | public void startUpdateCredentialsSession( |
| 3559 | IAccountManagerResponse response, |
| 3560 | final Account account, |
| 3561 | final String authTokenType, |
| 3562 | final boolean expectActivityLaunch, |
| 3563 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3564 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3565 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3566 | Log.v(TAG, |
| 3567 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3568 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3569 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3570 | + ", pid " + Binder.getCallingPid()); |
| 3571 | } |
| 3572 | if (response == null) { |
| 3573 | throw new IllegalArgumentException("response is null"); |
| 3574 | } |
| 3575 | if (account == null) { |
| 3576 | throw new IllegalArgumentException("account is null"); |
| 3577 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3578 | |
| 3579 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3580 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3581 | |
| 3582 | // Check to see if the Password should be included to the caller. |
| 3583 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3584 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3585 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3586 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3587 | long identityToken = clearCallingIdentity(); |
| 3588 | try { |
| 3589 | UserAccounts accounts = getUserAccounts(userId); |
| 3590 | new StartAccountSession( |
| 3591 | accounts, |
| 3592 | response, |
| 3593 | account.type, |
| 3594 | expectActivityLaunch, |
| 3595 | account.name, |
| 3596 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3597 | true /* updateLastCredentialTime */, |
| 3598 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3599 | @Override |
| 3600 | public void run() throws RemoteException { |
| 3601 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3602 | loginOptions); |
| 3603 | } |
| 3604 | |
| 3605 | @Override |
| 3606 | protected String toDebugString(long now) { |
| 3607 | if (loginOptions != null) |
| 3608 | loginOptions.keySet(); |
| 3609 | return super.toDebugString(now) |
| 3610 | + ", startUpdateCredentialsSession" |
| 3611 | + ", " + account |
| 3612 | + ", authTokenType " + authTokenType |
| 3613 | + ", loginOptions " + loginOptions; |
| 3614 | } |
| 3615 | }.bind(); |
| 3616 | } finally { |
| 3617 | restoreCallingIdentity(identityToken); |
| 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3622 | public void isCredentialsUpdateSuggested( |
| 3623 | IAccountManagerResponse response, |
| 3624 | final Account account, |
| 3625 | final String statusToken) { |
| 3626 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3627 | Log.v(TAG, |
| 3628 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3629 | + ", caller's uid " + Binder.getCallingUid() |
| 3630 | + ", pid " + Binder.getCallingPid()); |
| 3631 | } |
| 3632 | if (response == null) { |
| 3633 | throw new IllegalArgumentException("response is null"); |
| 3634 | } |
| 3635 | if (account == null) { |
| 3636 | throw new IllegalArgumentException("account is null"); |
| 3637 | } |
| 3638 | if (TextUtils.isEmpty(statusToken)) { |
| 3639 | throw new IllegalArgumentException("status token is empty"); |
| 3640 | } |
| 3641 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3642 | int usrId = UserHandle.getCallingUserId(); |
| 3643 | long identityToken = clearCallingIdentity(); |
| 3644 | try { |
| 3645 | UserAccounts accounts = getUserAccounts(usrId); |
| 3646 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3647 | false /* stripAuthTokenFromResult */, account.name, |
| 3648 | false /* authDetailsRequired */) { |
| 3649 | @Override |
| 3650 | protected String toDebugString(long now) { |
| 3651 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3652 | + ", " + account; |
| 3653 | } |
| 3654 | |
| 3655 | @Override |
| 3656 | public void run() throws RemoteException { |
| 3657 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3658 | } |
| 3659 | |
| 3660 | @Override |
| 3661 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3662 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3663 | IAccountManagerResponse response = getResponseAndClose(); |
| 3664 | if (response == null) { |
| 3665 | return; |
| 3666 | } |
| 3667 | |
| 3668 | if (result == null) { |
| 3669 | sendErrorResponse( |
| 3670 | response, |
| 3671 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3672 | "null bundle"); |
| 3673 | return; |
| 3674 | } |
| 3675 | |
| 3676 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3677 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3678 | + response); |
| 3679 | } |
| 3680 | // Check to see if an error occurred. We know if an error occurred because all |
| 3681 | // error codes are greater than 0. |
| 3682 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3683 | sendErrorResponse(response, |
| 3684 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3685 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3686 | return; |
| 3687 | } |
| 3688 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3689 | sendErrorResponse( |
| 3690 | response, |
| 3691 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3692 | "no result in response"); |
| 3693 | return; |
| 3694 | } |
| 3695 | final Bundle newResult = new Bundle(); |
| 3696 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3697 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3698 | sendResponse(response, newResult); |
| 3699 | } |
| 3700 | }.bind(); |
| 3701 | } finally { |
| 3702 | restoreCallingIdentity(identityToken); |
| 3703 | } |
| 3704 | } |
| 3705 | |
| 3706 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3707 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3708 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3709 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3710 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3711 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3712 | + ", response " + response |
| 3713 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3714 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3715 | + ", pid " + Binder.getCallingPid()); |
| 3716 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3717 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3718 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3719 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3720 | if (!isAccountManagedByCaller(accountType, callingUid, userId) |
| 3721 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3722 | String msg = String.format( |
| 3723 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3724 | callingUid, |
| 3725 | accountType); |
| 3726 | throw new SecurityException(msg); |
| 3727 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3728 | long identityToken = clearCallingIdentity(); |
| 3729 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3730 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3731 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3732 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3733 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3734 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3735 | public void run() throws RemoteException { |
| 3736 | mAuthenticator.editProperties(this, mAccountType); |
| 3737 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3738 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3739 | protected String toDebugString(long now) { |
| 3740 | return super.toDebugString(now) + ", editProperties" |
| 3741 | + ", accountType " + accountType; |
| 3742 | } |
| 3743 | }.bind(); |
| 3744 | } finally { |
| 3745 | restoreCallingIdentity(identityToken); |
| 3746 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3747 | } |
| 3748 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3749 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3750 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3751 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3752 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3753 | throw new SecurityException("Can be called only by system UID"); |
| 3754 | } |
| 3755 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3756 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3757 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3758 | |
| 3759 | final int userId = userHandle.getIdentifier(); |
| 3760 | |
| 3761 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3762 | |
| 3763 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3764 | int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3765 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3766 | } catch (NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3767 | Log.d(TAG, "Package not found " + e.getMessage()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3768 | return false; |
| 3769 | } |
| 3770 | } |
| 3771 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3772 | // Returns package with oldest target SDK for given UID. |
| 3773 | private String getPackageNameForUid(int uid) { |
| 3774 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3775 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3776 | return null; |
| 3777 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3778 | String packageName = packageNames[0]; |
Fyodor Kupolov | 892fc8d | 2017-03-22 12:57:04 -0700 | [diff] [blame] | 3779 | if (packageNames.length == 1) { |
| 3780 | return packageName; |
| 3781 | } |
| 3782 | // 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] | 3783 | int oldestVersion = Integer.MAX_VALUE; |
| 3784 | for (String name : packageNames) { |
| 3785 | try { |
| 3786 | ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(name, 0); |
| 3787 | if (applicationInfo != null) { |
| 3788 | int version = applicationInfo.targetSdkVersion; |
| 3789 | if (version < oldestVersion) { |
| 3790 | oldestVersion = version; |
| 3791 | packageName = name; |
| 3792 | } |
| 3793 | } |
| 3794 | } catch (NameNotFoundException e) { |
| 3795 | // skip |
| 3796 | } |
| 3797 | } |
| 3798 | return packageName; |
| 3799 | } |
| 3800 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3801 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3802 | int uid) { |
| 3803 | if (packageName == null) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3804 | packageName = getPackageNameForUid(uid); |
| 3805 | if (packageName == null) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3806 | return false; |
| 3807 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | // Use null token which means any token. Having a token means the package |
| 3811 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3812 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3813 | return true; |
| 3814 | } |
| 3815 | // 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] | 3816 | // 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] | 3817 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3818 | int visibility = resolveAccountVisibility(account, packageName, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3819 | getUserAccounts(UserHandle.getUserId(uid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3820 | return (visibility == AccountManager.VISIBILITY_VISIBLE |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3821 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | @Override |
| 3825 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3826 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3827 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3828 | throw new SecurityException("Can be called only by system UID"); |
| 3829 | } |
| 3830 | |
| 3831 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3832 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3833 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3834 | |
| 3835 | final int userId = userHandle.getIdentifier(); |
| 3836 | |
| 3837 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3838 | |
| 3839 | final int uid; |
| 3840 | try { |
| 3841 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 3842 | } catch (NameNotFoundException e) { |
| 3843 | Slog.e(TAG, "Unknown package " + packageName); |
| 3844 | return null; |
| 3845 | } |
| 3846 | |
| 3847 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 3848 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3849 | final long identity = Binder.clearCallingIdentity(); |
| 3850 | try { |
| 3851 | return PendingIntent.getActivityAsUser( |
| 3852 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 3853 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 3854 | null, new UserHandle(userId)).getIntentSender(); |
| 3855 | } finally { |
| 3856 | Binder.restoreCallingIdentity(identity); |
| 3857 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 3861 | int uid, RemoteCallback callback) { |
| 3862 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 3863 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 3864 | @Override |
| 3865 | public void onResult(Bundle value) throws RemoteException { |
| 3866 | handleAuthenticatorResponse(true); |
| 3867 | } |
| 3868 | |
| 3869 | @Override |
| 3870 | public void onRequestContinued() { |
| 3871 | /* ignore */ |
| 3872 | } |
| 3873 | |
| 3874 | @Override |
| 3875 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 3876 | handleAuthenticatorResponse(false); |
| 3877 | } |
| 3878 | |
| 3879 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 3880 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3881 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3882 | UserHandle.getUserHandleForUid(uid)); |
| 3883 | if (callback != null) { |
| 3884 | Bundle result = new Bundle(); |
| 3885 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 3886 | callback.sendResult(result); |
| 3887 | } |
| 3888 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3889 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3893 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 3894 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 3895 | throw new SecurityException("Only system can check for accounts across users"); |
| 3896 | } |
| 3897 | final long token = Binder.clearCallingIdentity(); |
| 3898 | try { |
| 3899 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 3900 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 3901 | if (allAccounts[i].account.equals(account)) { |
| 3902 | return true; |
| 3903 | } |
| 3904 | } |
| 3905 | return false; |
| 3906 | } finally { |
| 3907 | Binder.restoreCallingIdentity(token); |
| 3908 | } |
| 3909 | } |
| 3910 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3911 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 3912 | private final String[] mFeatures; |
| 3913 | private volatile Account[] mAccountsOfType = null; |
| 3914 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 3915 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3916 | private final int mCallingUid; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3917 | private final String mPackageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3918 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3919 | public GetAccountsByTypeAndFeatureSession( |
| 3920 | UserAccounts accounts, |
| 3921 | IAccountManagerResponse response, |
| 3922 | String type, |
| 3923 | String[] features, |
| 3924 | int callingUid, |
| 3925 | String packageName) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3926 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3927 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3928 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3929 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3930 | mFeatures = features; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3931 | mPackageName = packageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3932 | } |
| 3933 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3934 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3935 | public void run() throws RemoteException { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 3936 | mAccountsOfType = getAccountsFromCache(mAccounts, mAccountType, |
| 3937 | mCallingUid, mPackageName, false /* include managed not visible*/); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3938 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 3939 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3940 | mCurrentAccount = 0; |
| 3941 | |
| 3942 | checkAccount(); |
| 3943 | } |
| 3944 | |
| 3945 | public void checkAccount() { |
| 3946 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 3947 | sendResult(); |
| 3948 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3949 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3950 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3951 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 3952 | if (accountAuthenticator == null) { |
| 3953 | // It is possible that the authenticator has died, which is indicated by |
| 3954 | // mAuthenticator being set to null. If this happens then just abort. |
| 3955 | // There is no need to send back a result or error in this case since |
| 3956 | // that already happened when mAuthenticator was cleared. |
| 3957 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3958 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 3959 | + " connected to the authenticator, " + toDebugString()); |
| 3960 | } |
| 3961 | return; |
| 3962 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3963 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3964 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3965 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3966 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3967 | } |
| 3968 | } |
| 3969 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3970 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3971 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3972 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3973 | mNumResults++; |
| 3974 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3975 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3976 | return; |
| 3977 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3978 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3979 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 3980 | } |
| 3981 | mCurrentAccount++; |
| 3982 | checkAccount(); |
| 3983 | } |
| 3984 | |
| 3985 | public void sendResult() { |
| 3986 | IAccountManagerResponse response = getResponseAndClose(); |
| 3987 | if (response != null) { |
| 3988 | try { |
| 3989 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 3990 | for (int i = 0; i < accounts.length; i++) { |
| 3991 | accounts[i] = mAccountsWithFeatures.get(i); |
| 3992 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3993 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3994 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3995 | + response); |
| 3996 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3997 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3998 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3999 | response.onResult(result); |
| 4000 | } catch (RemoteException e) { |
| 4001 | // if the caller is dead then there is no one to care about remote exceptions |
| 4002 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4003 | Log.v(TAG, "failure while notifying response", e); |
| 4004 | } |
| 4005 | } |
| 4006 | } |
| 4007 | } |
| 4008 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4009 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4010 | protected String toDebugString(long now) { |
| 4011 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 4012 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 4013 | } |
| 4014 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4015 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4016 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4017 | * 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] | 4018 | * @hide |
| 4019 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4020 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4021 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4022 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4023 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4024 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4025 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4026 | if (visibleAccountTypes.isEmpty()) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4027 | return EMPTY_ACCOUNT_ARRAY; |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4028 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4029 | long identityToken = clearCallingIdentity(); |
| 4030 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4031 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4032 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4033 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4034 | callingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4035 | opPackageName, |
| 4036 | visibleAccountTypes, |
| 4037 | false /* includeUserManagedNotVisible */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4038 | } finally { |
| 4039 | restoreCallingIdentity(identityToken); |
| 4040 | } |
| 4041 | } |
| 4042 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4043 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4044 | * Returns accounts for all running users, ignores visibility values. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4045 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4046 | * @hide |
| 4047 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4048 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4049 | public AccountAndUser[] getRunningAccounts() { |
| 4050 | final int[] runningUserIds; |
| 4051 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4052 | runningUserIds = ActivityManager.getService().getRunningUserIds(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4053 | } catch (RemoteException e) { |
| 4054 | // Running in system_server; should never happen |
| 4055 | throw new RuntimeException(e); |
| 4056 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4057 | return getAccounts(runningUserIds); |
| 4058 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4059 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4060 | /** |
| 4061 | * Returns accounts for all users, ignores visibility values. |
| 4062 | * |
| 4063 | * @hide |
| 4064 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4065 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4066 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 4067 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4068 | final int[] userIds = new int[users.size()]; |
| 4069 | for (int i = 0; i < userIds.length; i++) { |
| 4070 | userIds[i] = users.get(i).id; |
| 4071 | } |
| 4072 | return getAccounts(userIds); |
| 4073 | } |
| 4074 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4075 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4076 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4077 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4078 | for (int userId : userIds) { |
| 4079 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4080 | if (userAccounts == null) continue; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4081 | Account[] accounts = getAccountsFromCache( |
| 4082 | userAccounts, |
| 4083 | null /* type */, |
| 4084 | Binder.getCallingUid(), |
| 4085 | null /* packageName */, |
| 4086 | false /* include managed not visible*/); |
| 4087 | for (Account account : accounts) { |
| 4088 | runningAccounts.add(new AccountAndUser(account, userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4089 | } |
| 4090 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4091 | |
| 4092 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4093 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4094 | } |
| 4095 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4096 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4097 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4098 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4099 | int callingUid = Binder.getCallingUid(); |
| 4100 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4101 | return getAccountsAsUserForPackage(type, userId, opPackageName /* callingPackage */, -1, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4102 | opPackageName, false /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4103 | } |
| 4104 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4105 | @NonNull |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4106 | private Account[] getAccountsAsUserForPackage( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4107 | String type, |
| 4108 | int userId, |
| 4109 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4110 | int packageUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4111 | String opPackageName, |
| 4112 | boolean includeUserManagedNotVisible) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4113 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4114 | // Only allow the system process to read accounts of other users |
| 4115 | if (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4116 | && callingUid != Process.SYSTEM_UID |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4117 | && mContext.checkCallingOrSelfPermission( |
| 4118 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4119 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4120 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4121 | + " trying to get account for " + userId); |
| 4122 | } |
| 4123 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4124 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4125 | Log.v(TAG, "getAccounts: accountType " + type |
| 4126 | + ", caller's uid " + Binder.getCallingUid() |
| 4127 | + ", pid " + Binder.getCallingPid()); |
| 4128 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4129 | |
| 4130 | // If the original calling app was using account choosing activity |
| 4131 | // provided by the framework or authenticator we'll passing in |
| 4132 | // the original caller's uid here, which is what should be used for filtering. |
| 4133 | List<String> managedTypes = |
| 4134 | getTypesManagedByCaller(callingUid, UserHandle.getUserId(callingUid)); |
| 4135 | if (packageUid != -1 && |
| 4136 | ((UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 4137 | || (type != null && managedTypes.contains(type))))) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4138 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4139 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4140 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4141 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4142 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4143 | if (visibleAccountTypes.isEmpty() |
| 4144 | || (type != null && !visibleAccountTypes.contains(type))) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4145 | return EMPTY_ACCOUNT_ARRAY; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4146 | } else if (visibleAccountTypes.contains(type)) { |
| 4147 | // Prune the list down to just the requested type. |
| 4148 | visibleAccountTypes = new ArrayList<>(); |
| 4149 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4150 | } // else aggregate all the visible accounts (it won't matter if the |
| 4151 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4152 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4153 | long identityToken = clearCallingIdentity(); |
| 4154 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4155 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 4156 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4157 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4158 | callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4159 | opPackageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4160 | visibleAccountTypes, |
| 4161 | includeUserManagedNotVisible); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4162 | } finally { |
| 4163 | restoreCallingIdentity(identityToken); |
| 4164 | } |
| 4165 | } |
| 4166 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4167 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4168 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4169 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4170 | int callingUid, |
| 4171 | String callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4172 | List<String> visibleAccountTypes, |
| 4173 | boolean includeUserManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4174 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4175 | for (String visibleType : visibleAccountTypes) { |
| 4176 | Account[] accountsForType = getAccountsFromCache( |
| 4177 | userAccounts, visibleType, callingUid, callingPackage, |
| 4178 | includeUserManagedNotVisible); |
| 4179 | if (accountsForType != null) { |
| 4180 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4181 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4182 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4183 | Account[] result = new Account[visibleAccounts.size()]; |
| 4184 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4185 | result[i] = visibleAccounts.get(i); |
| 4186 | } |
| 4187 | return result; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4190 | @Override |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4191 | public void addSharedAccountsFromParentUser(int parentUserId, int userId, |
| 4192 | String opPackageName) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4193 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4194 | Account[] accounts = getAccountsAsUser(null, parentUserId, opPackageName); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4195 | for (Account account : accounts) { |
| 4196 | addSharedAccountAsUser(account, userId); |
| 4197 | } |
| 4198 | } |
| 4199 | |
| 4200 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4201 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4202 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4203 | accounts.accountsDb.deleteSharedAccount(account); |
| 4204 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4205 | if (accountId < 0) { |
| 4206 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4207 | + ", skipping the DB insert failed"); |
| 4208 | return false; |
| 4209 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4210 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4211 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4212 | return true; |
| 4213 | } |
| 4214 | |
| 4215 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4216 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4217 | userId = handleIncomingUser(userId); |
| 4218 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4219 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4220 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4221 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4222 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4223 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4224 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4225 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4226 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4227 | } |
| 4228 | return r > 0; |
| 4229 | } |
| 4230 | |
| 4231 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4232 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4233 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4234 | } |
| 4235 | |
| 4236 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4237 | userId = handleIncomingUser(userId); |
| 4238 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4239 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4240 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4241 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4242 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4243 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4244 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4245 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4246 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4247 | } |
| 4248 | |
| 4249 | @Override |
| 4250 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4251 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4252 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4253 | synchronized (accounts.dbLock) { |
| 4254 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
| 4255 | Account[] accountArray = new Account[accountList.size()]; |
| 4256 | accountList.toArray(accountArray); |
| 4257 | return accountArray; |
| 4258 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4262 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4263 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4264 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4265 | } |
| 4266 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4267 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4268 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4269 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4270 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4271 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4272 | // Don't do opPackageName check - caller is system. |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4273 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4274 | + callingUid + " with uid=" + uid); |
| 4275 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4276 | return getAccountsAsUserForPackage(null, UserHandle.getCallingUserId(), packageName, uid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4277 | opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4278 | } |
| 4279 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4280 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4281 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4282 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4283 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4284 | int callingUid = Binder.getCallingUid(); |
| 4285 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4286 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4287 | int packageUid = -1; |
| 4288 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4289 | packageUid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4290 | } catch (NameNotFoundException re) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4291 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4292 | return EMPTY_ACCOUNT_ARRAY; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4293 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4294 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4295 | && (type != null && !isAccountManagedByCaller(type, callingUid, userId))) { |
| 4296 | return EMPTY_ACCOUNT_ARRAY; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4297 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4298 | return getAccountsAsUserForPackage(type, userId, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4299 | packageName, packageUid, opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4300 | } |
| 4301 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4302 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4303 | public void getAccountsByFeatures( |
| 4304 | IAccountManagerResponse response, |
| 4305 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4306 | String[] features, |
| 4307 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4308 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4309 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4310 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4311 | Log.v(TAG, "getAccounts: accountType " + type |
| 4312 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4313 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4314 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4315 | + ", pid " + Binder.getCallingPid()); |
| 4316 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4317 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4318 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4319 | int userId = UserHandle.getCallingUserId(); |
| 4320 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4321 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4322 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4323 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4324 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4325 | // Need to return just the accounts that are from matching signatures. |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4326 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, EMPTY_ACCOUNT_ARRAY); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4327 | try { |
| 4328 | response.onResult(result); |
| 4329 | } catch (RemoteException e) { |
| 4330 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4331 | } |
| 4332 | return; |
| 4333 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4334 | long identityToken = clearCallingIdentity(); |
| 4335 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4336 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4337 | if (features == null || features.length == 0) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4338 | Account[] accounts = getAccountsFromCache(userAccounts, type, callingUid, |
| 4339 | opPackageName, false); |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4340 | Bundle result = new Bundle(); |
| 4341 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4342 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4343 | return; |
| 4344 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4345 | new GetAccountsByTypeAndFeatureSession( |
| 4346 | userAccounts, |
| 4347 | response, |
| 4348 | type, |
| 4349 | features, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4350 | callingUid, |
| 4351 | opPackageName).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4352 | } finally { |
| 4353 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4354 | } |
| 4355 | } |
| 4356 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4357 | @Override |
| 4358 | public void onAccountAccessed(String token) throws RemoteException { |
| 4359 | final int uid = Binder.getCallingUid(); |
| 4360 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4361 | return; |
| 4362 | } |
| 4363 | final int userId = UserHandle.getCallingUserId(); |
| 4364 | final long identity = Binder.clearCallingIdentity(); |
| 4365 | try { |
| 4366 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4367 | if (Objects.equals(account.getAccessId(), token)) { |
| 4368 | // An app just accessed the account. At this point it knows about |
| 4369 | // 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] | 4370 | // Do we need to update account visibility here? |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4371 | if (!hasAccountAccess(account, null, uid)) { |
| 4372 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4373 | uid, true); |
| 4374 | } |
| 4375 | } |
| 4376 | } |
| 4377 | } finally { |
| 4378 | Binder.restoreCallingIdentity(identity); |
| 4379 | } |
| 4380 | } |
| 4381 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4382 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4383 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4384 | IAccountManagerResponse mResponse; |
| 4385 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4386 | final boolean mExpectActivityLaunch; |
| 4387 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4388 | final String mAccountName; |
| 4389 | // Indicates if we need to add auth details(like last credential time) |
| 4390 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4391 | // If set, we need to update the last authenticated time. This is |
| 4392 | // currently |
| 4393 | // used on |
| 4394 | // successful confirming credentials. |
| 4395 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4396 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4397 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4398 | private int mNumRequestContinued = 0; |
| 4399 | private int mNumErrors = 0; |
| 4400 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4401 | IAccountAuthenticator mAuthenticator = null; |
| 4402 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4403 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4404 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4405 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4406 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4407 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4408 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4409 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4410 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4411 | } |
| 4412 | |
| 4413 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4414 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4415 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4416 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4417 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4418 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4419 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4420 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4421 | mResponse = response; |
| 4422 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4423 | mExpectActivityLaunch = expectActivityLaunch; |
| 4424 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4425 | mAccountName = accountName; |
| 4426 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4427 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4428 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4429 | synchronized (mSessions) { |
| 4430 | mSessions.put(toString(), this); |
| 4431 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4432 | if (response != null) { |
| 4433 | try { |
| 4434 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4435 | } catch (RemoteException e) { |
| 4436 | mResponse = null; |
| 4437 | binderDied(); |
| 4438 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4439 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4440 | } |
| 4441 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4442 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4443 | if (mResponse == null) { |
| 4444 | // this session has already been closed |
| 4445 | return null; |
| 4446 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4447 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4448 | 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] | 4449 | return response; |
| 4450 | } |
| 4451 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4452 | /** |
| 4453 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4454 | * security policy. |
| 4455 | * |
| 4456 | * 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] | 4457 | * 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] | 4458 | * supplied entries in the system Settings app. |
| 4459 | */ |
| 4460 | protected void checkKeyIntent( |
| 4461 | int authUid, |
| 4462 | Intent intent) throws SecurityException { |
| 4463 | long bid = Binder.clearCallingIdentity(); |
| 4464 | try { |
| 4465 | PackageManager pm = mContext.getPackageManager(); |
| 4466 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 4467 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4468 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4469 | if (!isExportedSystemActivity(targetActivityInfo) |
| 4470 | && (PackageManager.SIGNATURE_MATCH != pm.checkSignatures(authUid, |
| 4471 | targetUid))) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4472 | String pkgName = targetActivityInfo.packageName; |
| 4473 | String activityName = targetActivityInfo.name; |
| 4474 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4475 | + "does not share a signature with the supplying authenticator (%s)."; |
| 4476 | throw new SecurityException( |
| 4477 | String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4478 | } |
| 4479 | } finally { |
| 4480 | Binder.restoreCallingIdentity(bid); |
| 4481 | } |
| 4482 | } |
| 4483 | |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4484 | private boolean isExportedSystemActivity(ActivityInfo activityInfo) { |
| 4485 | String className = activityInfo.name; |
| 4486 | return "android".equals(activityInfo.packageName) && |
| 4487 | (GrantCredentialsPermissionActivity.class.getName().equals(className) |
| 4488 | || CantAddAccountActivity.class.getName().equals(className)); |
| 4489 | } |
| 4490 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4491 | private void close() { |
| 4492 | synchronized (mSessions) { |
| 4493 | if (mSessions.remove(toString()) == null) { |
| 4494 | // the session was already closed, so bail out now |
| 4495 | return; |
| 4496 | } |
| 4497 | } |
| 4498 | if (mResponse != null) { |
| 4499 | // stop listening for response deaths |
| 4500 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4501 | |
| 4502 | // clear this so that we don't accidentally send any further results |
| 4503 | mResponse = null; |
| 4504 | } |
| 4505 | cancelTimeout(); |
| 4506 | unbind(); |
| 4507 | } |
| 4508 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4509 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4510 | public void binderDied() { |
| 4511 | mResponse = null; |
| 4512 | close(); |
| 4513 | } |
| 4514 | |
| 4515 | protected String toDebugString() { |
| 4516 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4517 | } |
| 4518 | |
| 4519 | protected String toDebugString(long now) { |
| 4520 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4521 | + ", connected " + (mAuthenticator != null) |
| 4522 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4523 | + "/" + mNumErrors + ")" |
| 4524 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4525 | } |
| 4526 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4527 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4528 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4529 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4530 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4531 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4532 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4533 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | private void unbind() { |
| 4538 | if (mAuthenticator != null) { |
| 4539 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4540 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4541 | } |
| 4542 | } |
| 4543 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4544 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4545 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4546 | } |
| 4547 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4548 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4549 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4550 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4551 | try { |
| 4552 | run(); |
| 4553 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4554 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4555 | "remote exception"); |
| 4556 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4557 | } |
| 4558 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4559 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4560 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4561 | mAuthenticator = null; |
| 4562 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4563 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4564 | try { |
| 4565 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4566 | "disconnected"); |
| 4567 | } catch (RemoteException e) { |
| 4568 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4569 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4570 | + "caught RemoteException while responding", e); |
| 4571 | } |
| 4572 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4573 | } |
| 4574 | } |
| 4575 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4576 | public abstract void run() throws RemoteException; |
| 4577 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4578 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4579 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4580 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4581 | try { |
| 4582 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4583 | "timeout"); |
| 4584 | } catch (RemoteException e) { |
| 4585 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4586 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4587 | e); |
| 4588 | } |
| 4589 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4590 | } |
| 4591 | } |
| 4592 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4593 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4594 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4595 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4596 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4597 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4598 | if (result != null) { |
| 4599 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4600 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4601 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4602 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4603 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4604 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4605 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4606 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4607 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4608 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4609 | if (needUpdate || mAuthDetailsRequired) { |
| 4610 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4611 | if (needUpdate && accountPresent) { |
| 4612 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4613 | } |
| 4614 | if (mAuthDetailsRequired) { |
| 4615 | long lastAuthenticatedTime = -1; |
| 4616 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4617 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4618 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4619 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4620 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4621 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4622 | lastAuthenticatedTime); |
| 4623 | } |
| 4624 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4625 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4626 | if (result != null |
| 4627 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4628 | checkKeyIntent( |
| 4629 | Binder.getCallingUid(), |
| 4630 | intent); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4631 | } |
| 4632 | if (result != null |
| 4633 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4634 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4635 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4636 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4637 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4638 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4639 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4640 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4641 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4642 | IAccountManagerResponse response; |
| 4643 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4644 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4645 | response = mResponse; |
| 4646 | } else { |
| 4647 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4648 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4649 | if (response != null) { |
| 4650 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4651 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4652 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4653 | Log.v(TAG, getClass().getSimpleName() |
| 4654 | + " calling onError() on response " + response); |
| 4655 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4656 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4657 | "null bundle returned"); |
| 4658 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4659 | if (mStripAuthTokenFromResult) { |
| 4660 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4661 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4662 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4663 | Log.v(TAG, getClass().getSimpleName() |
| 4664 | + " calling onResult() on response " + response); |
| 4665 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4666 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4667 | (intent == null)) { |
| 4668 | // All AccountManager error codes are greater than 0 |
| 4669 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4670 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4671 | } else { |
| 4672 | response.onResult(result); |
| 4673 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4674 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4675 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4676 | // if the caller is dead then there is no one to care about remote exceptions |
| 4677 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4678 | Log.v(TAG, "failure while notifying response", e); |
| 4679 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4680 | } |
| 4681 | } |
| 4682 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4683 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4684 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4685 | public void onRequestContinued() { |
| 4686 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4687 | } |
| 4688 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4689 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4690 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4691 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4692 | IAccountManagerResponse response = getResponseAndClose(); |
| 4693 | if (response != null) { |
| 4694 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4695 | Log.v(TAG, getClass().getSimpleName() |
| 4696 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4697 | } |
| 4698 | try { |
| 4699 | response.onError(errorCode, errorMessage); |
| 4700 | } catch (RemoteException e) { |
| 4701 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4702 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4703 | } |
| 4704 | } |
| 4705 | } else { |
| 4706 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4707 | Log.v(TAG, "Session.onError: already closed"); |
| 4708 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4709 | } |
| 4710 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4711 | |
| 4712 | /** |
| 4713 | * find the component name for the authenticator and initiate a bind |
| 4714 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4715 | */ |
| 4716 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4717 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4718 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4719 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4720 | if (authenticatorInfo == null) { |
| 4721 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4722 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 4723 | + ", bailing out"); |
| 4724 | } |
| 4725 | return false; |
| 4726 | } |
| 4727 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 4728 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 4729 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 4730 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 4731 | + " which isn't encryption aware"); |
| 4732 | return false; |
| 4733 | } |
| 4734 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4735 | Intent intent = new Intent(); |
| 4736 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 4737 | intent.setComponent(authenticatorInfo.componentName); |
| 4738 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4739 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 4740 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 4741 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4742 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4743 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4744 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 4745 | } |
| 4746 | return false; |
| 4747 | } |
| 4748 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4749 | return true; |
| 4750 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4751 | } |
| 4752 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 4753 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4754 | MessageHandler(Looper looper) { |
| 4755 | super(looper); |
| 4756 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4757 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4758 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4759 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4760 | switch (msg.what) { |
| 4761 | case MESSAGE_TIMED_OUT: |
| 4762 | Session session = (Session)msg.obj; |
| 4763 | session.onTimedOut(); |
| 4764 | break; |
| 4765 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4766 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 4767 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4768 | break; |
| 4769 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4770 | default: |
| 4771 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 4772 | } |
| 4773 | } |
| 4774 | } |
| 4775 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4776 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4777 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4778 | } |
| 4779 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4780 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4781 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4782 | } |
| 4783 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4784 | /* |
| 4785 | * This function receives an opened writable database. |
| 4786 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4787 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4788 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4789 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4793 | * 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] | 4794 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4795 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4796 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4797 | |
| 4798 | class LogRecordTask implements Runnable { |
| 4799 | private final String action; |
| 4800 | private final String tableName; |
| 4801 | private final long accountId; |
| 4802 | private final UserAccounts userAccount; |
| 4803 | private final int callingUid; |
| 4804 | private final long userDebugDbInsertionPoint; |
| 4805 | |
| 4806 | LogRecordTask(final String action, |
| 4807 | final String tableName, |
| 4808 | final long accountId, |
| 4809 | final UserAccounts userAccount, |
| 4810 | final int callingUid, |
| 4811 | final long userDebugDbInsertionPoint) { |
| 4812 | this.action = action; |
| 4813 | this.tableName = tableName; |
| 4814 | this.accountId = accountId; |
| 4815 | this.userAccount = userAccount; |
| 4816 | this.callingUid = callingUid; |
| 4817 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 4818 | } |
| 4819 | |
| 4820 | public void run() { |
| 4821 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 4822 | logStatement.bindLong(1, accountId); |
| 4823 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4824 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4825 | logStatement.bindLong(4, callingUid); |
| 4826 | logStatement.bindString(5, tableName); |
| 4827 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 4828 | logStatement.execute(); |
| 4829 | logStatement.clearBindings(); |
| 4830 | } |
| 4831 | } |
| 4832 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4833 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 4834 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4835 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4836 | % AccountsDb.MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4837 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4838 | } |
| 4839 | |
| 4840 | /* |
| 4841 | * This should only be called once to compile the sql statement for logging |
| 4842 | * and to find the insertion point. |
| 4843 | */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4844 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(UserAccounts userAccount) { |
| 4845 | userAccount.debugDbInsertionPoint = userAccount.accountsDb |
| 4846 | .calculateDebugTableInsertionPoint(); |
| 4847 | userAccount.statementForLogging = userAccount.accountsDb.compileSqlStatementForLogging(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4848 | } |
| 4849 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4850 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4851 | return asBinder(); |
| 4852 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4853 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4854 | /** |
| 4855 | * Searches array of arguments for the specified string |
| 4856 | * @param args array of argument strings |
| 4857 | * @param value value to search for |
| 4858 | * @return true if the value is contained in the array |
| 4859 | */ |
| 4860 | private static boolean scanArgs(String[] args, String value) { |
| 4861 | if (args != null) { |
| 4862 | for (String arg : args) { |
| 4863 | if (value.equals(arg)) { |
| 4864 | return true; |
| 4865 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4866 | } |
| 4867 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4868 | return false; |
| 4869 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4870 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4871 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4872 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 4873 | if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4874 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4875 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4876 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4877 | final List<UserInfo> users = getUserManager().getUsers(); |
| 4878 | for (UserInfo user : users) { |
| 4879 | ipw.println("User " + user + ":"); |
| 4880 | ipw.increaseIndent(); |
| 4881 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 4882 | ipw.println(); |
| 4883 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4884 | } |
| 4885 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4886 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4887 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 4888 | String[] args, boolean isCheckinRequest) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4889 | if (isCheckinRequest) { |
| 4890 | // This is a checkin request. *Only* upload the account types and the count of |
| 4891 | // each. |
| 4892 | synchronized (userAccounts.dbLock) { |
| 4893 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
| 4894 | } |
| 4895 | } else { |
| 4896 | Account[] accounts = getAccountsFromCache(userAccounts, null /* type */, |
| 4897 | Process.SYSTEM_UID, null /* packageName */, false); |
| 4898 | fout.println("Accounts: " + accounts.length); |
| 4899 | for (Account account : accounts) { |
| 4900 | fout.println(" " + account); |
| 4901 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4902 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4903 | // Add debug information. |
| 4904 | fout.println(); |
| 4905 | synchronized (userAccounts.dbLock) { |
| 4906 | userAccounts.accountsDb.dumpDebugTable(fout); |
| 4907 | } |
| 4908 | fout.println(); |
| 4909 | synchronized (mSessions) { |
| 4910 | final long now = SystemClock.elapsedRealtime(); |
| 4911 | fout.println("Active Sessions: " + mSessions.size()); |
| 4912 | for (Session session : mSessions.values()) { |
| 4913 | fout.println(" " + session.toDebugString(now)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 4914 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4915 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4916 | |
| 4917 | fout.println(); |
| 4918 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4919 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4920 | } |
| 4921 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4922 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4923 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4924 | long identityToken = clearCallingIdentity(); |
| 4925 | try { |
| 4926 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4927 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 4928 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4929 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4930 | if (intent.getComponent() != null && |
| 4931 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 4932 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4933 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4934 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4935 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 4936 | final NotificationId id = getSigninRequiredNotificationId(accounts, account); |
| 4937 | intent.addCategory(id.mTag); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4938 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4939 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4940 | contextForUser.getText(R.string.notification_title).toString(); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 4941 | Notification n = |
| 4942 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4943 | .setWhen(0) |
| 4944 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 4945 | .setColor(contextForUser.getColor( |
| 4946 | com.android.internal.R.color.system_notification_accent_color)) |
| 4947 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 4948 | .setContentText(message) |
| 4949 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 4950 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4951 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4952 | .build(); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 4953 | installNotification(id, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4954 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4955 | } finally { |
| 4956 | restoreCallingIdentity(identityToken); |
| 4957 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4958 | } |
| 4959 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 4960 | private void installNotification(NotificationId id, final Notification notification, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4961 | String packageName, int userId) { |
| 4962 | final long token = clearCallingIdentity(); |
| 4963 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4964 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4965 | try { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 4966 | notificationManager.enqueueNotificationWithTag(packageName, packageName, |
| 4967 | id.mTag, id.mId, notification, new int[1], userId); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4968 | } catch (RemoteException e) { |
| 4969 | /* ignore - local call */ |
| 4970 | } |
| 4971 | } finally { |
| 4972 | Binder.restoreCallingIdentity(token); |
| 4973 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4974 | } |
| 4975 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 4976 | private void cancelNotification(NotificationId id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4977 | cancelNotification(id, mContext.getPackageName(), user); |
| 4978 | } |
| 4979 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 4980 | private void cancelNotification(NotificationId id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4981 | long identityToken = clearCallingIdentity(); |
| 4982 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4983 | INotificationManager service = mInjector.getNotificationManager(); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 4984 | service.cancelNotificationWithTag(packageName, id.mTag, id.mId, user.getIdentifier()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4985 | } catch (RemoteException e) { |
| 4986 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4987 | } finally { |
| 4988 | restoreCallingIdentity(identityToken); |
| 4989 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4990 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4991 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4992 | private boolean isPermittedForPackage(String packageName, int userId, String... permissions) { |
| 4993 | final long identity = Binder.clearCallingIdentity(); |
| 4994 | try { |
| 4995 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 4996 | for (String perm : permissions) { |
| 4997 | if (pm.checkPermission(perm, packageName, userId) |
| 4998 | == PackageManager.PERMISSION_GRANTED) { |
| 4999 | return true; |
| 5000 | } |
| 5001 | } |
| 5002 | } catch (RemoteException e) { |
| 5003 | /* ignore - local call */ |
| 5004 | } finally { |
| 5005 | Binder.restoreCallingIdentity(identity); |
| 5006 | } |
| 5007 | return false; |
| 5008 | } |
| 5009 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5010 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 5011 | for (String perm : permissions) { |
| 5012 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 5013 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5014 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 5015 | } |
| 5016 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 5017 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 5018 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 5019 | return true; |
| 5020 | } |
| 5021 | } |
| 5022 | } |
| 5023 | return false; |
| 5024 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5025 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5026 | private int handleIncomingUser(int userId) { |
| 5027 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5028 | return ActivityManager.getService().handleIncomingUser( |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5029 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 5030 | } catch (RemoteException re) { |
| 5031 | // Shouldn't happen, local. |
| 5032 | } |
| 5033 | return userId; |
| 5034 | } |
| 5035 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 5036 | private boolean isPrivileged(int callingUid) { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5037 | String[] packages; |
| 5038 | long identityToken = Binder.clearCallingIdentity(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5039 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5040 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5041 | } finally { |
| 5042 | Binder.restoreCallingIdentity(identityToken); |
| 5043 | } |
| 5044 | if (packages == null) { |
| 5045 | Log.d(TAG, "No packages for callingUid " + callingUid); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5046 | return false; |
| 5047 | } |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5048 | for (String name : packages) { |
| 5049 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5050 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5051 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 5052 | && (packageInfo.applicationInfo.privateFlags |
| 5053 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5054 | return true; |
| 5055 | } |
| 5056 | } catch (PackageManager.NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5057 | Log.d(TAG, "Package not found " + e.getMessage()); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5058 | return false; |
| 5059 | } |
| 5060 | } |
| 5061 | return false; |
| 5062 | } |
| 5063 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5064 | private boolean permissionIsGranted( |
| 5065 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5066 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 5067 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5068 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 5069 | } |
| 5070 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5071 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5072 | |
| 5073 | if (isPrivileged(callerUid)) { |
| 5074 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5075 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5076 | + callerUid + " privileged"); |
| 5077 | } |
| 5078 | return true; |
| 5079 | } |
| 5080 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 5081 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5082 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5083 | + callerUid + " manages the account"); |
| 5084 | } |
| 5085 | return true; |
| 5086 | } |
| 5087 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5088 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5089 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5090 | + callerUid + " user granted access"); |
| 5091 | } |
| 5092 | return true; |
| 5093 | } |
| 5094 | |
| 5095 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5096 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5097 | } |
| 5098 | |
| 5099 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5100 | } |
| 5101 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5102 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5103 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5104 | if (accountType == null) { |
| 5105 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5106 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5107 | return getTypesVisibleToCaller(callingUid, userId, |
| 5108 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5109 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5110 | } |
| 5111 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5112 | // Method checks visibility for applications targeing API level below {@link |
| 5113 | // android.os.Build.VERSION_CODES#O}, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5114 | // returns true if the the app has GET_ACCOUNTS or GET_ACCOUNTS_PRIVILEGED permission. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5115 | private boolean checkGetAccountsPermission(String packageName, int userId) { |
| 5116 | return isPermittedForPackage(packageName, userId, Manifest.permission.GET_ACCOUNTS, |
| 5117 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 5118 | } |
| 5119 | |
| 5120 | private boolean checkReadContactsPermission(String packageName, int userId) { |
| 5121 | return isPermittedForPackage(packageName, userId, Manifest.permission.READ_CONTACTS); |
| 5122 | } |
| 5123 | |
| 5124 | /** |
| 5125 | * Method checks package uid and signature with Authenticator which manages accountType. |
| 5126 | * |
| 5127 | * @return SIGNATURE_CHECK_UID_MATCH for uid match, SIGNATURE_CHECK_MATCH for signature match, |
| 5128 | * SIGNATURE_CHECK_MISMATCH otherwise. |
| 5129 | */ |
| 5130 | private int checkPackageSignature(String accountType, int callingUid, int userId) { |
| 5131 | if (accountType == null) { |
| 5132 | return SIGNATURE_CHECK_MISMATCH; |
| 5133 | } |
| 5134 | |
| 5135 | long identityToken = Binder.clearCallingIdentity(); |
| 5136 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5137 | try { |
| 5138 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5139 | } finally { |
| 5140 | Binder.restoreCallingIdentity(identityToken); |
| 5141 | } |
| 5142 | // Check for signature match with Authenticator. |
| 5143 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5144 | : serviceInfos) { |
| 5145 | if (accountType.equals(serviceInfo.type.type)) { |
| 5146 | if (serviceInfo.uid == callingUid) { |
| 5147 | return SIGNATURE_CHECK_UID_MATCH; |
| 5148 | } |
| 5149 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 5150 | if (sigChk == PackageManager.SIGNATURE_MATCH) { |
| 5151 | return SIGNATURE_CHECK_MATCH; |
| 5152 | } |
| 5153 | } |
| 5154 | } |
| 5155 | return SIGNATURE_CHECK_MISMATCH; |
| 5156 | } |
| 5157 | |
| 5158 | // returns true for applications with the same signature as authenticator. |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5159 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5160 | if (accountType == null) { |
| 5161 | return false; |
| 5162 | } else { |
| 5163 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5164 | } |
| 5165 | } |
| 5166 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5167 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5168 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5169 | return getTypesForCaller(callingUid, userId, true /* isOtherwisePermitted*/); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
Dmitry Dementyev | 2e22cfb | 2017-01-09 18:42:14 +0000 | [diff] [blame] | 5173 | return getTypesForCaller(callingUid, userId, false); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | private List<String> getTypesForCaller( |
| 5177 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5178 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5179 | long identityToken = Binder.clearCallingIdentity(); |
| 5180 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5181 | try { |
| 5182 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5183 | } finally { |
| 5184 | Binder.restoreCallingIdentity(identityToken); |
| 5185 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5186 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5187 | serviceInfos) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5188 | if (isOtherwisePermitted || (mPackageManager.checkSignatures(serviceInfo.uid, |
| 5189 | callingUid) == PackageManager.SIGNATURE_MATCH)) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5190 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5191 | } |
| 5192 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5193 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5194 | } |
| 5195 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5196 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5197 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5198 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5199 | if (account.name.equals(accountName)) { |
| 5200 | return true; |
| 5201 | } |
| 5202 | } |
| 5203 | } |
| 5204 | return false; |
| 5205 | } |
| 5206 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5207 | private static void checkManageUsersPermission(String message) { |
| 5208 | if (ActivityManager.checkComponentPermission( |
| 5209 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5210 | != PackageManager.PERMISSION_GRANTED) { |
| 5211 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5212 | } |
| 5213 | } |
| 5214 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5215 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5216 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5217 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5218 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5219 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5220 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5221 | + message); |
| 5222 | } |
| 5223 | } |
| 5224 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5225 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5226 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5227 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5228 | return true; |
| 5229 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5230 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5231 | synchronized (accounts.dbLock) { |
| 5232 | synchronized (accounts.cacheLock) { |
| 5233 | long grantsCount; |
| 5234 | if (authTokenType != null) { |
| 5235 | grantsCount = accounts.accountsDb |
| 5236 | .findMatchingGrantsCount(callerUid, authTokenType, account); |
| 5237 | } else { |
| 5238 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
| 5239 | account); |
| 5240 | } |
| 5241 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5242 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5243 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5244 | // TODO: Skip this check when running automated tests. Replace this |
| 5245 | // with a more general solution. |
| 5246 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
| 5247 | + authTokenType + " by uid " + callerUid |
| 5248 | + " but ignoring since device is in test harness."); |
| 5249 | return true; |
| 5250 | } |
| 5251 | return permissionGranted; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5252 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5253 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5254 | } |
| 5255 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5256 | private boolean isSystemUid(int callingUid) { |
| 5257 | String[] packages = null; |
| 5258 | long ident = Binder.clearCallingIdentity(); |
| 5259 | try { |
| 5260 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5261 | } finally { |
| 5262 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5263 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5264 | if (packages != null) { |
| 5265 | for (String name : packages) { |
| 5266 | try { |
| 5267 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5268 | if (packageInfo != null |
| 5269 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5270 | != 0) { |
| 5271 | return true; |
| 5272 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5273 | } catch (NameNotFoundException e) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5274 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5275 | } |
| 5276 | } |
| 5277 | } else { |
| 5278 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5279 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5280 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5281 | } |
| 5282 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5283 | /** Succeeds if any of the specified permissions are granted. */ |
| 5284 | private void checkReadAccountsPermitted( |
| 5285 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5286 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5287 | int userId, |
| 5288 | String opPackageName) { |
| 5289 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5290 | String msg = String.format( |
| 5291 | "caller uid %s cannot access %s accounts", |
| 5292 | callingUid, |
| 5293 | accountType); |
| 5294 | Log.w(TAG, " " + msg); |
| 5295 | throw new SecurityException(msg); |
| 5296 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5297 | } |
| 5298 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5299 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5300 | // the managing app can always modify accounts |
| 5301 | if (isProfileOwner(callingUid)) { |
| 5302 | return true; |
| 5303 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5304 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5305 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5306 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5307 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5308 | return true; |
| 5309 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5310 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5311 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5312 | // the managing app can always modify accounts |
| 5313 | if (isProfileOwner(callingUid)) { |
| 5314 | return true; |
| 5315 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5316 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5317 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5318 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5319 | if (typesArray == null) { |
| 5320 | return true; |
| 5321 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5322 | for (String forbiddenType : typesArray) { |
| 5323 | if (forbiddenType.equals(accountType)) { |
| 5324 | return false; |
| 5325 | } |
| 5326 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5327 | return true; |
| 5328 | } |
| 5329 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5330 | private boolean isProfileOwner(int uid) { |
| 5331 | final DevicePolicyManagerInternal dpmi = |
| 5332 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5333 | return (dpmi != null) |
| 5334 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5335 | } |
| 5336 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5337 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5338 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5339 | throws RemoteException { |
| 5340 | final int callingUid = getCallingUid(); |
| 5341 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5342 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5343 | throw new SecurityException(); |
| 5344 | } |
| 5345 | |
| 5346 | if (value) { |
| 5347 | grantAppPermission(account, authTokenType, uid); |
| 5348 | } else { |
| 5349 | revokeAppPermission(account, authTokenType, uid); |
| 5350 | } |
| 5351 | } |
| 5352 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5353 | /** |
| 5354 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5355 | * <p> |
| 5356 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5357 | * which is in the system. This means we don't need to protect it with permissions. |
| 5358 | * @hide |
| 5359 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5360 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5361 | if (account == null || authTokenType == null) { |
| 5362 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5363 | return; |
| 5364 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5365 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5366 | synchronized (accounts.dbLock) { |
| 5367 | synchronized (accounts.cacheLock) { |
| 5368 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5369 | if (accountId >= 0) { |
| 5370 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
| 5371 | } |
| 5372 | cancelNotification( |
| 5373 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5374 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5375 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5376 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
| 5377 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5378 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5379 | |
| 5380 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5381 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5382 | : mAppPermissionChangeListeners) { |
| 5383 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5384 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5385 | } |
| 5386 | |
| 5387 | /** |
| 5388 | * Don't allow callers with the given uid permission to get credentials for |
| 5389 | * account/authTokenType. |
| 5390 | * <p> |
| 5391 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5392 | * which is in the system. This means we don't need to protect it with permissions. |
| 5393 | * @hide |
| 5394 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5395 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5396 | if (account == null || authTokenType == null) { |
| 5397 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5398 | return; |
| 5399 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5400 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5401 | synchronized (accounts.dbLock) { |
| 5402 | synchronized (accounts.cacheLock) { |
| 5403 | accounts.accountsDb.beginTransaction(); |
| 5404 | try { |
| 5405 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5406 | if (accountId >= 0) { |
| 5407 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5408 | accountId, authTokenType, uid); |
| 5409 | accounts.accountsDb.setTransactionSuccessful(); |
| 5410 | } |
| 5411 | } finally { |
| 5412 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5413 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5414 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5415 | cancelNotification( |
| 5416 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5417 | UserHandle.of(accounts.userId)); |
| 5418 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5419 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5420 | |
| 5421 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5422 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5423 | : mAppPermissionChangeListeners) { |
| 5424 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5425 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5426 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5427 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5428 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5429 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5430 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5431 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5432 | for (Account curAccount : oldAccountsForType) { |
| 5433 | if (!curAccount.equals(account)) { |
| 5434 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5435 | } |
| 5436 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5437 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5438 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5439 | } else { |
| 5440 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5441 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5442 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5443 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5444 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5445 | accounts.userDataCache.remove(account); |
| 5446 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5447 | accounts.previousNameCache.remove(account); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 5448 | accounts.visibilityCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | /** |
| 5452 | * 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] | 5453 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5454 | * processes and if you will return this account to apps you should return the result. |
| 5455 | * @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] | 5456 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5457 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5458 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5459 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5460 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5461 | if (accountsForType != null) { |
| 5462 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5463 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5464 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5465 | : UUID.randomUUID().toString(); |
| 5466 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5467 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5468 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5469 | } |
| 5470 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5471 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5472 | private Account[] filterAccounts(UserAccounts accounts, Account[] unfiltered, int callingUid, |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5473 | @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5474 | String visibilityFilterPackage = callingPackage; |
| 5475 | if (visibilityFilterPackage == null) { |
| 5476 | visibilityFilterPackage = getPackageNameForUid(callingUid); |
| 5477 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5478 | Map<Account, Integer> firstPass = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5479 | for (Account account : unfiltered) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5480 | int visibility = resolveAccountVisibility(account, visibilityFilterPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5481 | if ((visibility == AccountManager.VISIBILITY_VISIBLE |
| 5482 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) |
| 5483 | || (includeManagedNotVisible |
| 5484 | && (visibility |
| 5485 | == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE))) { |
| 5486 | firstPass.put(account, visibility); |
| 5487 | } |
| 5488 | } |
| 5489 | Map<Account, Integer> secondPass = |
| 5490 | filterSharedAccounts(accounts, firstPass, callingUid, callingPackage); |
| 5491 | |
| 5492 | Account[] filtered = new Account[secondPass.size()]; |
| 5493 | filtered = secondPass.keySet().toArray(filtered); |
| 5494 | return filtered; |
| 5495 | } |
| 5496 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5497 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5498 | private Map<Account, Integer> filterSharedAccounts(UserAccounts userAccounts, |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5499 | @NonNull Map<Account, Integer> unfiltered, int callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5500 | @Nullable String callingPackage) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5501 | // first part is to filter shared accounts. |
| 5502 | // unfiltered type check is not necessary. |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5503 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5504 | || callingUid == Process.SYSTEM_UID) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5505 | return unfiltered; |
| 5506 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5507 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5508 | if (user != null && user.isRestricted()) { |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5509 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5510 | if (packages == null) { |
| 5511 | packages = new String[] {}; |
| 5512 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5513 | // 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] | 5514 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5515 | // This might be a temporary way to specify a visible list |
| 5516 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5517 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5518 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5519 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5520 | return unfiltered; |
| 5521 | } |
| 5522 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5523 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5524 | if (ArrayUtils.isEmpty(sharedAccounts)) { |
| 5525 | return unfiltered; |
| 5526 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5527 | String requiredAccountType = ""; |
| 5528 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5529 | // If there's an explicit callingPackage specified, check if that package |
| 5530 | // opted in to see restricted accounts. |
| 5531 | if (callingPackage != null) { |
| 5532 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5533 | if (pi != null && pi.restrictedAccountType != null) { |
| 5534 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5535 | } |
| 5536 | } else { |
| 5537 | // Otherwise check if the callingUid has a package that has opted in |
| 5538 | for (String packageName : packages) { |
| 5539 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5540 | if (pi != null && pi.restrictedAccountType != null) { |
| 5541 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5542 | break; |
| 5543 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5544 | } |
| 5545 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5546 | } catch (NameNotFoundException e) { |
| 5547 | Log.d(TAG, "Package not found " + e.getMessage()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5548 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5549 | Map<Account, Integer> filtered = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5550 | for (Map.Entry<Account, Integer> entry : unfiltered.entrySet()) { |
| 5551 | Account account = entry.getKey(); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5552 | if (account.type.equals(requiredAccountType)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5553 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5554 | } else { |
| 5555 | boolean found = false; |
| 5556 | for (Account shared : sharedAccounts) { |
| 5557 | if (shared.equals(account)) { |
| 5558 | found = true; |
| 5559 | break; |
| 5560 | } |
| 5561 | } |
| 5562 | if (!found) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5563 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5564 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5565 | } |
| 5566 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5567 | return filtered; |
| 5568 | } else { |
| 5569 | return unfiltered; |
| 5570 | } |
| 5571 | } |
| 5572 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5573 | /* |
| 5574 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5575 | * that the package is not allowed to access. |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5576 | * |
| 5577 | * <p>The method shouldn't be called with UserAccounts#cacheLock held, otherwise it will cause a |
| 5578 | * deadlock |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5579 | */ |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5580 | @NonNull |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5581 | protected Account[] getAccountsFromCache(UserAccounts userAccounts, String accountType, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5582 | int callingUid, @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5583 | Preconditions.checkState(!Thread.holdsLock(userAccounts.cacheLock), |
| 5584 | "Method should not be called with cacheLock"); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5585 | if (accountType != null) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5586 | Account[] accounts; |
| 5587 | synchronized (userAccounts.cacheLock) { |
| 5588 | accounts = userAccounts.accountCache.get(accountType); |
| 5589 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5590 | if (accounts == null) { |
| 5591 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5592 | } else { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5593 | return filterAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
| 5594 | callingUid, callingPackage, includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5595 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5596 | } else { |
| 5597 | int totalLength = 0; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5598 | Account[] accountsArray; |
| 5599 | synchronized (userAccounts.cacheLock) { |
| 5600 | for (Account[] accounts : userAccounts.accountCache.values()) { |
| 5601 | totalLength += accounts.length; |
| 5602 | } |
| 5603 | if (totalLength == 0) { |
| 5604 | return EMPTY_ACCOUNT_ARRAY; |
| 5605 | } |
| 5606 | accountsArray = new Account[totalLength]; |
| 5607 | totalLength = 0; |
| 5608 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
| 5609 | System.arraycopy(accountsOfType, 0, accountsArray, totalLength, |
| 5610 | accountsOfType.length); |
| 5611 | totalLength += accountsOfType.length; |
| 5612 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5613 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5614 | return filterAccounts(userAccounts, accountsArray, callingUid, callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5615 | includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5616 | } |
| 5617 | } |
| 5618 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 5619 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5620 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5621 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5622 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5623 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5624 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5625 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5626 | } |
| 5627 | if (value == null) { |
| 5628 | userDataForAccount.remove(key); |
| 5629 | } else { |
| 5630 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5631 | } |
| 5632 | } |
| 5633 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5634 | protected String readCachedTokenInternal( |
| 5635 | UserAccounts accounts, |
| 5636 | Account account, |
| 5637 | String tokenType, |
| 5638 | String callingPackage, |
| 5639 | byte[] pkgSigDigest) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5640 | synchronized (accounts.dbLock) { |
| 5641 | synchronized (accounts.cacheLock) { |
| 5642 | return accounts.accountTokenCaches.get( |
| 5643 | account, tokenType, callingPackage, pkgSigDigest); |
| 5644 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5645 | } |
| 5646 | } |
| 5647 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5648 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5649 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5650 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5651 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5652 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5653 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5654 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5655 | } |
| 5656 | if (value == null) { |
| 5657 | authTokensForAccount.remove(key); |
| 5658 | } else { |
| 5659 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5660 | } |
| 5661 | } |
| 5662 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5663 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 5664 | String authTokenType) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5665 | // Fast path - check if account is already cached |
| 5666 | synchronized (accounts.cacheLock) { |
| 5667 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 5668 | if (authTokensForAccount != null) { |
| 5669 | return authTokensForAccount.get(authTokenType); |
| 5670 | } |
| 5671 | } |
| 5672 | // 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] | 5673 | synchronized (accounts.dbLock) { |
| 5674 | synchronized (accounts.cacheLock) { |
| 5675 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 5676 | if (authTokensForAccount == null) { |
| 5677 | // need to populate the cache for this account |
| 5678 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
| 5679 | accounts.authTokenCache.put(account, authTokensForAccount); |
| 5680 | } |
| 5681 | return authTokensForAccount.get(authTokenType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5682 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5683 | } |
| 5684 | } |
| 5685 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 5686 | private String readUserDataInternal(UserAccounts accounts, Account account, String key) { |
| 5687 | Map<String, String> userDataForAccount; |
| 5688 | // Fast path - check if data is already cached |
| 5689 | synchronized (accounts.cacheLock) { |
| 5690 | userDataForAccount = accounts.userDataCache.get(account); |
| 5691 | } |
| 5692 | // 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] | 5693 | if (userDataForAccount == null) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 5694 | synchronized (accounts.dbLock) { |
| 5695 | synchronized (accounts.cacheLock) { |
| 5696 | userDataForAccount = accounts.userDataCache.get(account); |
| 5697 | if (userDataForAccount == null) { |
| 5698 | // need to populate the cache for this account |
| 5699 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
| 5700 | accounts.userDataCache.put(account, userDataForAccount); |
| 5701 | } |
| 5702 | } |
| 5703 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5704 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5705 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5706 | } |
| 5707 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5708 | private Context getContextForUser(UserHandle user) { |
| 5709 | try { |
| 5710 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 5711 | } catch (NameNotFoundException e) { |
| 5712 | // Default to mContext, not finding the package system is running as is unlikely. |
| 5713 | return mContext; |
| 5714 | } |
| 5715 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 5716 | |
| 5717 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 5718 | try { |
| 5719 | response.onResult(result); |
| 5720 | } catch (RemoteException e) { |
| 5721 | // if the caller is dead then there is no one to care about remote |
| 5722 | // exceptions |
| 5723 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5724 | Log.v(TAG, "failure while notifying response", e); |
| 5725 | } |
| 5726 | } |
| 5727 | } |
| 5728 | |
| 5729 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 5730 | String errorMessage) { |
| 5731 | try { |
| 5732 | response.onError(errorCode, errorMessage); |
| 5733 | } catch (RemoteException e) { |
| 5734 | // if the caller is dead then there is no one to care about remote |
| 5735 | // exceptions |
| 5736 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5737 | Log.v(TAG, "failure while notifying response", e); |
| 5738 | } |
| 5739 | } |
| 5740 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5741 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5742 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5743 | private final Object mLock = new Object(); |
| 5744 | |
| 5745 | @GuardedBy("mLock") |
| 5746 | private AccountManagerBackupHelper mBackupHelper; |
| 5747 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5748 | @Override |
| 5749 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 5750 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 5751 | if (account == null) { |
| 5752 | Slog.w(TAG, "account cannot be null"); |
| 5753 | return; |
| 5754 | } |
| 5755 | if (packageName == null) { |
| 5756 | Slog.w(TAG, "packageName cannot be null"); |
| 5757 | return; |
| 5758 | } |
| 5759 | if (userId < UserHandle.USER_SYSTEM) { |
| 5760 | Slog.w(TAG, "user id must be concrete"); |
| 5761 | return; |
| 5762 | } |
| 5763 | if (callback == null) { |
| 5764 | Slog.w(TAG, "callback cannot be null"); |
| 5765 | return; |
| 5766 | } |
| 5767 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5768 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 5769 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5770 | Bundle result = new Bundle(); |
| 5771 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 5772 | callback.sendResult(result); |
| 5773 | return; |
| 5774 | } |
| 5775 | |
| 5776 | final int uid; |
| 5777 | try { |
| 5778 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 5779 | } catch (NameNotFoundException e) { |
| 5780 | Slog.e(TAG, "Unknown package " + packageName); |
| 5781 | return; |
| 5782 | } |
| 5783 | |
| 5784 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5785 | final UserAccounts userAccounts; |
| 5786 | synchronized (mUsers) { |
| 5787 | userAccounts = mUsers.get(userId); |
| 5788 | } |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 5789 | SystemNotificationChannels.createAccountChannelForPackage(packageName, uid, mContext); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5790 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 5791 | } |
| 5792 | |
| 5793 | @Override |
| 5794 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 5795 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5796 | mAppPermissionChangeListeners.add(listener); |
| 5797 | } |
| 5798 | |
| 5799 | @Override |
| 5800 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 5801 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5802 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5803 | |
| 5804 | @Override |
| 5805 | public byte[] backupAccountAccessPermissions(int userId) { |
| 5806 | synchronized (mLock) { |
| 5807 | if (mBackupHelper == null) { |
| 5808 | mBackupHelper = new AccountManagerBackupHelper( |
| 5809 | AccountManagerService.this, this); |
| 5810 | } |
| 5811 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 5812 | } |
| 5813 | } |
| 5814 | |
| 5815 | @Override |
| 5816 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 5817 | synchronized (mLock) { |
| 5818 | if (mBackupHelper == null) { |
| 5819 | mBackupHelper = new AccountManagerBackupHelper( |
| 5820 | AccountManagerService.this, this); |
| 5821 | } |
| 5822 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 5823 | } |
| 5824 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5825 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5826 | |
| 5827 | @VisibleForTesting |
| 5828 | static class Injector { |
| 5829 | private final Context mContext; |
| 5830 | |
| 5831 | public Injector(Context context) { |
| 5832 | mContext = context; |
| 5833 | } |
| 5834 | |
| 5835 | Looper getMessageHandlerLooper() { |
| 5836 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 5837 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 5838 | serviceThread.start(); |
| 5839 | return serviceThread.getLooper(); |
| 5840 | } |
| 5841 | |
| 5842 | Context getContext() { |
| 5843 | return mContext; |
| 5844 | } |
| 5845 | |
| 5846 | void addLocalService(AccountManagerInternal service) { |
| 5847 | LocalServices.addService(AccountManagerInternal.class, service); |
| 5848 | } |
| 5849 | |
| 5850 | String getDeDatabaseName(int userId) { |
| 5851 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 5852 | AccountsDb.DE_DATABASE_NAME); |
| 5853 | return databaseFile.getPath(); |
| 5854 | } |
| 5855 | |
| 5856 | String getCeDatabaseName(int userId) { |
| 5857 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 5858 | AccountsDb.CE_DATABASE_NAME); |
| 5859 | return databaseFile.getPath(); |
| 5860 | } |
| 5861 | |
| 5862 | String getPreNDatabaseName(int userId) { |
| 5863 | File systemDir = Environment.getDataSystemDirectory(); |
| 5864 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 5865 | PRE_N_DATABASE_NAME); |
| 5866 | if (userId == 0) { |
| 5867 | // Migrate old file, if it exists, to the new location. |
| 5868 | // 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] | 5869 | // accidentally created in the old location, |
| 5870 | // causing the new one to become corrupted as well. |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5871 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 5872 | if (oldFile.exists() && !databaseFile.exists()) { |
| 5873 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 5874 | File userDir = Environment.getUserSystemDirectory(userId); |
| 5875 | if (!userDir.exists()) { |
| 5876 | if (!userDir.mkdirs()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5877 | throw new IllegalStateException( |
| 5878 | "User dir cannot be created: " + userDir); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5879 | } |
| 5880 | } |
| 5881 | if (!oldFile.renameTo(databaseFile)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5882 | throw new IllegalStateException( |
| 5883 | "User dir cannot be migrated: " + databaseFile); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5884 | } |
| 5885 | } |
| 5886 | } |
| 5887 | return databaseFile.getPath(); |
| 5888 | } |
| 5889 | |
| 5890 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 5891 | return new AccountAuthenticatorCache(mContext); |
| 5892 | } |
| 5893 | |
| 5894 | INotificationManager getNotificationManager() { |
| 5895 | return NotificationManager.getService(); |
| 5896 | } |
| 5897 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame^] | 5898 | |
| 5899 | private class NotificationId { |
| 5900 | final String mTag; |
| 5901 | private final int mId; |
| 5902 | |
| 5903 | NotificationId(String tag, int type) { |
| 5904 | mTag = tag; |
| 5905 | mId = type; |
| 5906 | } |
| 5907 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5908 | } |