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; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 92 | import com.android.internal.notification.SystemNotificationChannels; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 93 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 94 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 95 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 96 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 97 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 98 | import com.android.server.SystemService; |
| 99 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 100 | import com.google.android.collect.Lists; |
| 101 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 102 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 103 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 104 | import java.io.FileDescriptor; |
| 105 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 106 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 107 | import java.security.MessageDigest; |
| 108 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 109 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 110 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 111 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 112 | import java.util.Collection; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 113 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 114 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 115 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 116 | import java.util.LinkedHashMap; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 117 | import java.util.LinkedHashSet; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 118 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 119 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 120 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 121 | import java.util.Objects; |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 122 | import java.util.Set; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 123 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 124 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 125 | import java.util.concurrent.atomic.AtomicInteger; |
| 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 | private final AtomicInteger mNotificationIds = new AtomicInteger(1); |
| 194 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 195 | static class UserAccounts { |
| 196 | private final int userId; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 197 | final AccountsDb accountsDb; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 198 | private final HashMap<Pair<Pair<Account, String>, Integer>, Integer> |
| 199 | credentialsPermissionNotificationIds = |
| 200 | new HashMap<Pair<Pair<Account, String>, Integer>, Integer>(); |
| 201 | private final HashMap<Account, Integer> signinRequiredNotificationIds = |
| 202 | new HashMap<Account, Integer>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 203 | final Object cacheLock = new Object(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 204 | /** protected by the {@link #cacheLock} */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 205 | final HashMap<String, Account[]> accountCache = new LinkedHashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 206 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 207 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 208 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 209 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 210 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 211 | private final TokenCache accountTokenCaches = new TokenCache(); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 212 | /** protected by the {@link #cacheLock} */ |
| 213 | private final Map<Account, Map<String, Integer>> visibilityCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 214 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 215 | /** protected by the {@link #mReceiversForType}, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 216 | * type -> (packageName -> number of active receivers) |
| 217 | * type == null is used to get notifications about all account types |
| 218 | */ |
| 219 | private final Map<String, Map<String, Integer>> mReceiversForType = new HashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 220 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 221 | /** |
| 222 | * protected by the {@link #cacheLock} |
| 223 | * |
| 224 | * Caches the previous names associated with an account. Previous names |
| 225 | * should be cached because we expect that when an Account is renamed, |
| 226 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 227 | * want to know if the accounts they care about have been renamed. |
| 228 | * |
| 229 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 230 | * we can distinguish between those accounts with no previous names and |
| 231 | * those whose previous names haven't been cached (yet). |
| 232 | */ |
| 233 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 234 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 235 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 236 | private int debugDbInsertionPoint = -1; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 237 | private SQLiteStatement statementForLogging; // TODO Move to AccountsDb |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 238 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 239 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 240 | this.userId = userId; |
| 241 | synchronized (cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 242 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 247 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 248 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 249 | // Not thread-safe. Only use in synchronized context |
| 250 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 251 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 252 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 253 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 254 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 255 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 256 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 257 | /** |
| 258 | * This should only be called by system code. One should only call this after the service |
| 259 | * has started. |
| 260 | * @return a reference to the AccountManagerService instance |
| 261 | * @hide |
| 262 | */ |
| 263 | public static AccountManagerService getSingleton() { |
| 264 | return sThis.get(); |
| 265 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 266 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 267 | public AccountManagerService(Injector injector) { |
| 268 | mInjector = injector; |
| 269 | mContext = injector.getContext(); |
| 270 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 271 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 272 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 273 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 274 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 275 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 276 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 277 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 278 | IntentFilter intentFilter = new IntentFilter(); |
| 279 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 280 | intentFilter.addDataScheme("package"); |
| 281 | mContext.registerReceiver(new BroadcastReceiver() { |
| 282 | @Override |
| 283 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 284 | // Don't delete accounts when updating a authenticator's |
| 285 | // package. |
| 286 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 287 | /* Purging data requires file io, don't block the main thread. This is probably |
| 288 | * less than ideal because we are introducing a race condition where old grants |
| 289 | * could be exercised until they are purged. But that race condition existed |
| 290 | * anyway with the broadcast receiver. |
| 291 | * |
| 292 | * Ideally, we would completely clear the cache, purge data from the database, |
| 293 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 294 | * large at this point. |
| 295 | */ |
Dmitry Dementyev | 0b67642 | 2017-03-09 11:51:26 -0800 | [diff] [blame] | 296 | final String removedPackageName = intent.getData().getSchemeSpecificPart(); |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 297 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 298 | @Override |
| 299 | public void run() { |
| 300 | purgeOldGrantsAll(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 301 | // Notify authenticator about removed app? |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 302 | removeVisibilityValuesForPackage(removedPackageName); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 303 | } |
| 304 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 305 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 306 | } |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 307 | } |
| 308 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 309 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 310 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 311 | |
| 312 | // Need to cancel account request notifications if the update/install can access the account |
| 313 | new PackageMonitor() { |
| 314 | @Override |
| 315 | public void onPackageAdded(String packageName, int uid) { |
| 316 | // Called on a handler, and running as the system |
| 317 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 322 | // Called on a handler, and running as the system |
| 323 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 324 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 325 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 326 | |
| 327 | // Cancel account request notification if an app op was preventing the account access |
| 328 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 329 | new AppOpsManager.OnOpChangedInternalListener() { |
| 330 | @Override |
| 331 | public void onOpChanged(int op, String packageName) { |
| 332 | try { |
| 333 | final int userId = ActivityManager.getCurrentUser(); |
| 334 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 335 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 336 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 337 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 338 | final long identity = Binder.clearCallingIdentity(); |
| 339 | try { |
| 340 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 341 | } finally { |
| 342 | Binder.restoreCallingIdentity(identity); |
| 343 | } |
| 344 | } |
| 345 | } catch (NameNotFoundException e) { |
| 346 | /* ignore */ |
| 347 | } |
| 348 | } |
| 349 | }); |
| 350 | |
| 351 | // Cancel account request notification if a permission was preventing the account access |
| 352 | mPackageManager.addOnPermissionsChangeListener( |
| 353 | (int uid) -> { |
| 354 | Account[] accounts = null; |
| 355 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 356 | if (packageNames != null) { |
| 357 | final int userId = UserHandle.getUserId(uid); |
| 358 | final long identity = Binder.clearCallingIdentity(); |
| 359 | try { |
| 360 | for (String packageName : packageNames) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 361 | // if app asked for permission we need to cancel notification even |
| 362 | // for O+ applications. |
| 363 | if (mPackageManager.checkPermission( |
| 364 | Manifest.permission.GET_ACCOUNTS, |
| 365 | packageName) != PackageManager.PERMISSION_GRANTED) { |
| 366 | continue; |
| 367 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 368 | |
| 369 | if (accounts == null) { |
| 370 | accounts = getAccountsAsUser(null, userId, "android"); |
| 371 | if (ArrayUtils.isEmpty(accounts)) { |
| 372 | return; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | for (Account account : accounts) { |
| 377 | cancelAccountAccessRequestNotificationIfNeeded( |
| 378 | account, uid, packageName, true); |
| 379 | } |
| 380 | } |
| 381 | } finally { |
| 382 | Binder.restoreCallingIdentity(identity); |
| 383 | } |
| 384 | } |
| 385 | }); |
| 386 | } |
| 387 | |
| 388 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 389 | boolean checkAccess) { |
| 390 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 391 | for (Account account : accounts) { |
| 392 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 397 | boolean checkAccess) { |
| 398 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 399 | for (Account account : accounts) { |
| 400 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 405 | boolean checkAccess) { |
| 406 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 407 | if (packageNames != null) { |
| 408 | for (String packageName : packageNames) { |
| 409 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 410 | packageName, checkAccess); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 416 | int uid, String packageName, boolean checkAccess) { |
| 417 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 418 | UserHandle.getUserHandleForUid(uid))) { |
| 419 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 420 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 421 | UserHandle.getUserHandleForUid(uid)); |
| 422 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 425 | @Override |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 426 | public boolean addAccountExplicitlyWithVisibility(Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 427 | Bundle extras, Map packageToVisibility) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 428 | Bundle.setDefusable(extras, true); |
| 429 | |
| 430 | final int callingUid = Binder.getCallingUid(); |
| 431 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 432 | Log.v(TAG, "addAccountExplicitly: " + account + ", caller's uid " + callingUid |
| 433 | + ", pid " + Binder.getCallingPid()); |
| 434 | } |
| 435 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 436 | int userId = UserHandle.getCallingUserId(); |
| 437 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
| 438 | String msg = String.format("uid %s cannot explicitly add accounts of type: %s", |
| 439 | callingUid, account.type); |
| 440 | throw new SecurityException(msg); |
| 441 | } |
| 442 | /* |
| 443 | * Child users are not allowed to add accounts. Only the accounts that are shared by the |
| 444 | * parent profile can be added to child profile. |
| 445 | * |
| 446 | * TODO: Only allow accounts that were shared to be added by a limited user. |
| 447 | */ |
| 448 | // fails if the account already exists |
| 449 | long identityToken = clearCallingIdentity(); |
| 450 | try { |
| 451 | UserAccounts accounts = getUserAccounts(userId); |
| 452 | return addAccountInternal(accounts, account, password, extras, callingUid, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 453 | (Map<String, Integer>) packageToVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 454 | } finally { |
| 455 | restoreCallingIdentity(identityToken); |
| 456 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | @Override |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 460 | public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 461 | String accountType) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 462 | int callingUid = Binder.getCallingUid(); |
| 463 | boolean isSystemUid = UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); |
| 464 | List<String> managedTypes = |
| 465 | getTypesForCaller(callingUid, UserHandle.getUserId(callingUid), isSystemUid); |
| 466 | |
| 467 | if ((accountType != null && !managedTypes.contains(accountType)) |
| 468 | || (accountType == null && !isSystemUid)) { |
| 469 | throw new SecurityException( |
| 470 | "getAccountsAndVisibilityForPackage() called from unauthorized uid " |
| 471 | + callingUid + " with packageName=" + packageName); |
| 472 | } |
| 473 | if (accountType != null) { |
| 474 | managedTypes = new ArrayList<String>(); |
| 475 | managedTypes.add(accountType); |
| 476 | } |
| 477 | |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 478 | long identityToken = clearCallingIdentity(); |
| 479 | try { |
| 480 | return getAccountsAndVisibilityForPackage(packageName, managedTypes, callingUid, |
| 481 | getUserAccounts(UserHandle.getUserId(callingUid))); |
| 482 | } finally { |
| 483 | restoreCallingIdentity(identityToken); |
| 484 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* |
| 488 | * accountTypes may not be null |
| 489 | */ |
| 490 | private Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 491 | List<String> accountTypes, Integer callingUid, UserAccounts accounts) { |
| 492 | int uid = 0; |
| 493 | try { |
| 494 | uid = mPackageManager.getPackageUidAsUser(packageName, |
| 495 | UserHandle.getUserId(callingUid)); |
| 496 | } catch (NameNotFoundException e) { |
| 497 | Log.d(TAG, "Package not found " + e.getMessage()); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 498 | return new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 501 | Map<Account, Integer> result = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 502 | for (String accountType : accountTypes) { |
| 503 | synchronized (accounts.cacheLock) { |
| 504 | final Account[] accountsOfType = accounts.accountCache.get(accountType); |
| 505 | if (accountsOfType != null) { |
| 506 | for (Account account : accountsOfType) { |
| 507 | result.put(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 508 | resolveAccountVisibility(account, packageName, accounts)); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | return filterSharedAccounts(accounts, result, callingUid, packageName); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | @Override |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 517 | public Map<String, Integer> getPackagesAndVisibilityForAccount(Account account) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 518 | Preconditions.checkNotNull(account, "account cannot be null"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 519 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 520 | int userId = UserHandle.getUserId(callingUid); |
| 521 | UserAccounts accounts = getUserAccounts(userId); |
| 522 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
| 523 | && !isSystemUid(callingUid)) { |
| 524 | String msg = |
| 525 | String.format("uid %s cannot get secrets for account %s", callingUid, account); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 526 | throw new SecurityException(msg); |
| 527 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 528 | synchronized (accounts.cacheLock) { |
| 529 | return getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 530 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /** |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 534 | * Returns Map with all package names and visibility values for given account. |
| 535 | * The method and returned map must be guarded by accounts.cacheLock |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 536 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 537 | * @param account Account to get visibility values. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 538 | * @param accounts UserAccount that currently hosts the account and application |
| 539 | * |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 540 | * @return Map with cache for package names to visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 541 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 542 | private @NonNull Map<String, Integer> getPackagesAndVisibilityForAccountLocked(Account account, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 543 | UserAccounts accounts) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 544 | Map<String, Integer> accountVisibility = accounts.visibilityCache.get(account); |
| 545 | if (accountVisibility == null) { |
| 546 | Log.d(TAG, "Visibility was not initialized"); |
| 547 | accountVisibility = new HashMap<>(); |
| 548 | accounts.visibilityCache.put(account, accountVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 549 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 550 | return accountVisibility; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 554 | public int getAccountVisibility(Account account, String packageName) { |
| 555 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 556 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 557 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 558 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
| 559 | if (!isAccountManagedByCaller(account.type, callingUid, accounts.userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 560 | && !isSystemUid(callingUid)) { |
| 561 | String msg = String.format( |
| 562 | "uid %s cannot get secrets for accounts of type: %s", |
| 563 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 564 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 565 | throw new SecurityException(msg); |
| 566 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 567 | return resolveAccountVisibility(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 571 | * Method returns visibility for given account and package name. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 572 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 573 | * @param account The account to check visibility. |
| 574 | * @param packageName Package name to check visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 575 | * @param accounts UserAccount that currently hosts the account and application |
| 576 | * |
| 577 | * @return Visibility value, AccountManager.VISIBILITY_UNDEFINED if no value was stored. |
| 578 | * |
| 579 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 580 | private int getAccountVisibilityFromCache(Account account, String packageName, |
| 581 | UserAccounts accounts) { |
| 582 | synchronized (accounts.cacheLock) { |
| 583 | Map<String, Integer> accountVisibility = |
| 584 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 585 | Integer visibility = accountVisibility.get(packageName); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 586 | return visibility != null ? visibility : AccountManager.VISIBILITY_UNDEFINED; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Method which handles default values for Account visibility. |
| 592 | * |
| 593 | * @param account The account to check visibility. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 594 | * @param packageName Package name to check visibility |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 595 | * @param accounts UserAccount that currently hosts the account and application |
| 596 | * |
| 597 | * @return Visibility value, the method never returns AccountManager.VISIBILITY_UNDEFINED |
| 598 | * |
| 599 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 600 | private Integer resolveAccountVisibility(Account account, @NonNull String packageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 601 | UserAccounts accounts) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 602 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 603 | int uid = -1; |
| 604 | try { |
| 605 | long identityToken = clearCallingIdentity(); |
| 606 | try { |
| 607 | uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 608 | } finally { |
| 609 | restoreCallingIdentity(identityToken); |
| 610 | } |
| 611 | } catch (NameNotFoundException e) { |
| 612 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 613 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 614 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 615 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 616 | // System visibility can not be restricted. |
| 617 | if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) { |
| 618 | return AccountManager.VISIBILITY_VISIBLE; |
| 619 | } |
| 620 | |
| 621 | int signatureCheckResult = |
| 622 | checkPackageSignature(account.type, uid, accounts.userId); |
| 623 | |
| 624 | // Authenticator can not restrict visibility to itself. |
| 625 | if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) { |
| 626 | return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account |
| 627 | } |
| 628 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 629 | if (isSpecialPackageKey(packageName)) { |
| 630 | Log.d(TAG, "Package name is forbidden: " + packageName); |
| 631 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
| 632 | } |
| 633 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 634 | // Return stored value if it was set. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 635 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 636 | |
| 637 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 638 | return visibility; |
| 639 | } |
| 640 | |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 641 | boolean isPrivileged = isPermittedForPackage(packageName, accounts.userId, |
| 642 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 643 | |
| 644 | // Device/Profile owner gets visibility by default. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 645 | if (isProfileOwner(uid)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 646 | return AccountManager.VISIBILITY_VISIBLE; |
| 647 | } |
| 648 | // Apps with READ_CONTACTS permission get visibility by default even post O. |
| 649 | boolean canReadContacts = checkReadContactsPermission(packageName, accounts.userId); |
| 650 | |
| 651 | boolean preO = isPreOApplication(packageName); |
| 652 | if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH) |
| 653 | || (preO && checkGetAccountsPermission(packageName, accounts.userId)) |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 654 | || canReadContacts || isPrivileged) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 655 | // Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature |
| 656 | // match. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 657 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 658 | AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 659 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 660 | visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 661 | } |
| 662 | } else { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 663 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 664 | AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 665 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 666 | visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 667 | } |
| 668 | } |
| 669 | return visibility; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Checks targetSdk for a package; |
| 674 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 675 | * @param packageName Package name |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 676 | * |
| 677 | * @return True if package's target SDK is below {@link android.os.Build.VERSION_CODES#O}, or |
| 678 | * undefined |
| 679 | */ |
| 680 | private boolean isPreOApplication(String packageName) { |
| 681 | try { |
| 682 | long identityToken = clearCallingIdentity(); |
| 683 | ApplicationInfo applicationInfo; |
| 684 | try { |
| 685 | applicationInfo = mPackageManager.getApplicationInfo(packageName, 0); |
| 686 | } finally { |
| 687 | restoreCallingIdentity(identityToken); |
| 688 | } |
| 689 | |
| 690 | if (applicationInfo != null) { |
| 691 | int version = applicationInfo.targetSdkVersion; |
| 692 | return version < android.os.Build.VERSION_CODES.O; |
| 693 | } |
| 694 | return true; |
| 695 | } catch (NameNotFoundException e) { |
| 696 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 697 | return true; |
| 698 | } |
Dmitry Dementyev | 58fa8362 | 2016-12-20 18:08:51 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 702 | public boolean setAccountVisibility(Account account, String packageName, int newVisibility) { |
| 703 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 704 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 705 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 706 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
| 707 | if (!isAccountManagedByCaller(account.type, callingUid, accounts.userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 708 | && !isSystemUid(callingUid)) { |
| 709 | String msg = String.format( |
| 710 | "uid %s cannot get secrets for accounts of type: %s", |
| 711 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 712 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 713 | throw new SecurityException(msg); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 714 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 715 | return setAccountVisibility(account, packageName, newVisibility, true /* notify */, |
| 716 | accounts); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 720 | * Updates visibility for given account name and package. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 721 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 722 | * @param account Account to update visibility. |
| 723 | * @param packageName Package name for which visibility is updated. |
| 724 | * @param newVisibility New visibility calue |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 725 | * @param notify if the flag is set applications will get notification about visibility change |
| 726 | * @param accounts UserAccount that currently hosts the account and application |
| 727 | * |
| 728 | * @return True if account visibility was changed. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 729 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 730 | private boolean setAccountVisibility(Account account, String packageName, int newVisibility, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 731 | boolean notify, UserAccounts accounts) { |
| 732 | synchronized (accounts.cacheLock) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 733 | Map<String, Integer> packagesToVisibility; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 734 | if (notify) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 735 | if (isSpecialPackageKey(packageName)) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 736 | packagesToVisibility = |
| 737 | getRequestingPackages(account, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 738 | } else { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 739 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 740 | return false; // package is not installed. |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 741 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 742 | packagesToVisibility = new HashMap<>(); |
| 743 | packagesToVisibility.put(packageName, |
| 744 | resolveAccountVisibility(account, packageName, accounts)); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 745 | } |
| 746 | } else { |
| 747 | // Notifications will not be send. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 748 | if (!isSpecialPackageKey(packageName) && |
| 749 | !packageExistsForUser(packageName, accounts.userId)) { |
| 750 | // package is not installed and not meta value. |
| 751 | return false; |
| 752 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 753 | packagesToVisibility = new HashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 754 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 755 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 756 | if (!updateAccountVisibilityLocked(account, packageName, newVisibility, accounts)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 757 | return false; |
| 758 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 759 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 760 | if (notify) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 761 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
| 762 | if (packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 763 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 764 | } |
| 765 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 766 | sendAccountsChangedBroadcast(accounts.userId); |
| 767 | } |
| 768 | return true; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 769 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 772 | // Update account visibility in cache and database. |
| 773 | private boolean updateAccountVisibilityLocked(Account account, String packageName, |
| 774 | int newVisibility, UserAccounts accounts) { |
| 775 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 776 | if (accountId < 0) { |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
| 781 | try { |
| 782 | if (!accounts.accountsDb.setAccountVisibility(accountId, packageName, |
| 783 | newVisibility)) { |
| 784 | return false; |
| 785 | } |
| 786 | } finally { |
| 787 | StrictMode.setThreadPolicy(oldPolicy); |
| 788 | } |
| 789 | Map<String, Integer> accountVisibility = |
| 790 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 791 | accountVisibility.put(packageName, newVisibility); |
| 792 | return true; |
| 793 | } |
| 794 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 795 | @Override |
| 796 | public void registerAccountListener(String[] accountTypes, String opPackageName) { |
| 797 | int callingUid = Binder.getCallingUid(); |
| 798 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 799 | registerAccountListener(accountTypes, opPackageName, |
| 800 | getUserAccounts(UserHandle.getUserId(callingUid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 803 | private void registerAccountListener(String[] accountTypes, String opPackageName, |
| 804 | UserAccounts accounts) { |
| 805 | synchronized (accounts.mReceiversForType) { |
| 806 | if (accountTypes == null) { |
| 807 | // null for any type |
| 808 | accountTypes = new String[] {null}; |
| 809 | } |
| 810 | for (String type : accountTypes) { |
| 811 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 812 | if (receivers == null) { |
| 813 | receivers = new HashMap<>(); |
| 814 | accounts.mReceiversForType.put(type, receivers); |
| 815 | } |
| 816 | Integer cnt = receivers.get(opPackageName); |
| 817 | receivers.put(opPackageName, cnt != null ? cnt + 1 : 1); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 822 | @Override |
| 823 | public void unregisterAccountListener(String[] accountTypes, String opPackageName) { |
| 824 | int callingUid = Binder.getCallingUid(); |
| 825 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 826 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
| 827 | synchronized (accounts.mReceiversForType) { |
| 828 | if (accountTypes == null) { |
| 829 | // null for any type |
| 830 | accountTypes = new String[] {null}; |
| 831 | } |
| 832 | for (String type : accountTypes) { |
| 833 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 834 | if (receivers == null || receivers.get(opPackageName) == null) { |
| 835 | throw new IllegalArgumentException("attempt to unregister wrong receiver"); |
| 836 | } |
| 837 | Integer cnt = receivers.get(opPackageName); |
| 838 | if (cnt == 1) { |
| 839 | receivers.remove(opPackageName); |
| 840 | } else { |
| 841 | receivers.put(opPackageName, cnt - 1); |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | // Send notification to all packages which can potentially see the account |
| 848 | private void sendNotificationAccountUpdated(Account account, UserAccounts accounts) { |
| 849 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, accounts); |
| 850 | // packages with VISIBILITY_USER_MANAGED_NOT_VISIBL still get notification. |
| 851 | // Should we notify VISIBILITY_NOT_VISIBLE packages when account is added? |
| 852 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
| 853 | if (packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 854 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Sends a direct intent to a package, notifying it of account visibility change. |
| 861 | * |
| 862 | * @param packageName to send Account to |
| 863 | * @param accounts UserAccount that currently hosts the account |
| 864 | */ |
| 865 | private void notifyPackage(String packageName, UserAccounts accounts) { |
| 866 | Intent intent = new Intent(AccountManager.ACTION_VISIBLE_ACCOUNTS_CHANGED); |
| 867 | intent.setPackage(packageName); |
| 868 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 869 | mContext.sendBroadcastAsUser(intent, new UserHandle(accounts.userId)); |
| 870 | } |
| 871 | |
| 872 | // Returns a map from package name to visibility, for packages subscribed |
| 873 | // to notifications about any account type, or type of provided account |
| 874 | // account type or all types. |
| 875 | private Map<String, Integer> getRequestingPackages(Account account, UserAccounts accounts) { |
| 876 | Set<String> packages = new HashSet<>(); |
| 877 | synchronized (accounts.mReceiversForType) { |
| 878 | for (String type : new String[] {account.type, null}) { |
| 879 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 880 | if (receivers != null) { |
| 881 | packages.addAll(receivers.keySet()); |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | Map<String, Integer> result = new HashMap<>(); |
| 886 | for (String packageName : packages) { |
| 887 | result.put(packageName, resolveAccountVisibility(account, packageName, accounts)); |
| 888 | } |
| 889 | return result; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 890 | } |
| 891 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 892 | private boolean packageExistsForUser(String packageName, int userId) { |
| 893 | try { |
| 894 | long identityToken = clearCallingIdentity(); |
| 895 | try { |
| 896 | mPackageManager.getPackageUidAsUser(packageName, userId); |
| 897 | return true; // package exist |
| 898 | } finally { |
| 899 | restoreCallingIdentity(identityToken); |
| 900 | } |
| 901 | } catch (NameNotFoundException e) { |
| 902 | return false; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Returns true if packageName is one of special values. |
| 908 | */ |
| 909 | private boolean isSpecialPackageKey(String packageName) { |
| 910 | return (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName) |
| 911 | || AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)); |
| 912 | } |
| 913 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 914 | private void sendAccountsChangedBroadcast(int userId) { |
| 915 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 916 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
| 917 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 921 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 922 | throws RemoteException { |
| 923 | try { |
| 924 | return super.onTransact(code, data, reply, flags); |
| 925 | } catch (RuntimeException e) { |
| 926 | // The account manager only throws security exceptions, so let's |
| 927 | // log all others. |
| 928 | if (!(e instanceof SecurityException)) { |
| 929 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 930 | } |
| 931 | throw e; |
| 932 | } |
| 933 | } |
| 934 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 935 | private UserManager getUserManager() { |
| 936 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 937 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 938 | } |
| 939 | return mUserManager; |
| 940 | } |
| 941 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 942 | /** |
| 943 | * Validate internal set of accounts against installed authenticators for |
| 944 | * given user. Clears cached authenticators before validating. |
| 945 | */ |
| 946 | public void validateAccounts(int userId) { |
| 947 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 948 | // Invalidate user-specific cache to make sure we catch any |
| 949 | // removed authenticators. |
| 950 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * Validate internal set of accounts against installed authenticators for |
| 955 | * given user. Clear cached authenticators before validating when requested. |
| 956 | */ |
| 957 | private void validateAccountsInternal( |
| 958 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 959 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 960 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 961 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 962 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 963 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 964 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 965 | if (invalidateAuthenticatorCache) { |
| 966 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 967 | } |
| 968 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 969 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 970 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 971 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 972 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 973 | synchronized (accounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 974 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 975 | |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 976 | // Get a map of stored authenticator types to UID |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 977 | final AccountsDb accountsDb = accounts.accountsDb; |
| 978 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 979 | // Create a list of authenticator type whose previous uid no longer exists |
| 980 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 981 | SparseBooleanArray knownUids = null; |
| 982 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 983 | String type = authToUidEntry.getKey(); |
| 984 | int uid = authToUidEntry.getValue(); |
| 985 | Integer knownUid = knownAuth.get(type); |
| 986 | if (knownUid != null && uid == knownUid) { |
| 987 | // Remove it from the knownAuth list if it's unchanged. |
| 988 | knownAuth.remove(type); |
| 989 | } else { |
| 990 | /* |
| 991 | * The authenticator is presently not cached and should only be triggered |
| 992 | * when we think an authenticator has been removed (or is being updated). |
| 993 | * But we still want to check if any data with the associated uid is |
| 994 | * around. This is an (imperfect) signal that the package may be updating. |
| 995 | * |
| 996 | * A side effect of this is that an authenticator sharing a uid with |
| 997 | * multiple apps won't get its credentials wiped as long as some app with |
| 998 | * that uid is still on the device. But I suspect that this is a rare case. |
| 999 | * And it isn't clear to me how an attacker could really exploit that |
| 1000 | * feature. |
| 1001 | * |
| 1002 | * The upshot is that we don't have to worry about accounts getting |
| 1003 | * uninstalled while the authenticator's package is being updated. |
| 1004 | * |
| 1005 | */ |
| 1006 | if (knownUids == null) { |
| 1007 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1008 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1009 | if (!knownUids.get(uid)) { |
| 1010 | // The authenticator is not presently available to the cache. And the |
| 1011 | // package no longer has a data directory (so we surmise it isn't updating). |
| 1012 | // So purge its data from the account databases. |
| 1013 | obsoleteAuthType.add(type); |
| 1014 | // And delete it from the TABLE_META |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1015 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1016 | } |
| 1017 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1020 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1021 | // been re-enabled (after being updated for example), then we just overwrite the old |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1022 | // values. |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1023 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1024 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1027 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1028 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1029 | accounts.accountCache.clear(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1030 | final HashMap<String, ArrayList<String>> accountNamesByType = new LinkedHashMap<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1031 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1032 | final long accountId = accountEntry.getKey(); |
| 1033 | final Account account = accountEntry.getValue(); |
| 1034 | if (obsoleteAuthType.contains(account.type)) { |
| 1035 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 1036 | + account.type + "'s registered authenticator no longer exist."); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1037 | Map<String, Integer> packagesToVisibility = |
| 1038 | getRequestingPackages(account, accounts); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1039 | accountsDb.beginTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1040 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1041 | accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1042 | // Also delete from CE table if user is unlocked; if user is currently |
| 1043 | // locked the account will be removed later by syncDeCeAccountsLocked |
| 1044 | if (userUnlocked) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1045 | accountsDb.deleteCeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1046 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1047 | accountsDb.setTransactionSuccessful(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1048 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1049 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1050 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1051 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1052 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1053 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1054 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1055 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1056 | accounts.userDataCache.remove(account); |
| 1057 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1058 | accounts.accountTokenCaches.remove(account); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 1059 | accounts.visibilityCache.remove(account); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1060 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 1061 | for (Entry<String, Integer> packageToVisibility : |
| 1062 | packagesToVisibility.entrySet()) { |
| 1063 | if (packageToVisibility.getValue() |
| 1064 | != AccountManager.VISIBILITY_NOT_VISIBLE) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1065 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 1066 | } |
| 1067 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1068 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1069 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1070 | if (accountNames == null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1071 | accountNames = new ArrayList<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1072 | accountNamesByType.put(account.type, accountNames); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1073 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1074 | accountNames.add(account.name); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1077 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1078 | final String accountType = cur.getKey(); |
| 1079 | final ArrayList<String> accountNames = cur.getValue(); |
| 1080 | final Account[] accountsForType = new Account[accountNames.size()]; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1081 | for (int i = 0; i < accountsForType.length; i++) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1082 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1083 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1084 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1085 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1086 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 1087 | accounts.visibilityCache.putAll(accountsDb.findAllVisibilityValues()); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1088 | } finally { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1089 | if (accountDeleted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1090 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1091 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1092 | } |
| 1093 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1096 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1097 | // Get the UIDs of all apps that might have data on the device. We want |
| 1098 | // to preserve user data if the app might otherwise be storing data. |
| 1099 | List<PackageInfo> pkgsWithData = |
| 1100 | mPackageManager.getInstalledPackagesAsUser( |
| 1101 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1102 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1103 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1104 | if (pkgInfo.applicationInfo != null |
| 1105 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1106 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1107 | } |
| 1108 | } |
| 1109 | return knownUids; |
| 1110 | } |
| 1111 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1112 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1113 | Context context, |
| 1114 | int userId) { |
| 1115 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1116 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1117 | } |
| 1118 | |
| 1119 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1120 | IAccountAuthenticatorCache authCache, |
| 1121 | int userId) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 1122 | HashMap<String, Integer> knownAuth = new LinkedHashMap<>(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1123 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1124 | .getAllServices(userId)) { |
| 1125 | knownAuth.put(service.type.type, service.uid); |
| 1126 | } |
| 1127 | return knownAuth; |
| 1128 | } |
| 1129 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1130 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1131 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | protected UserAccounts getUserAccounts(int userId) { |
| 1135 | synchronized (mUsers) { |
| 1136 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1137 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1138 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1139 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1140 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1141 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1142 | initializeDebugDbSizeAndCompileSqlStatementForLogging(accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1143 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1144 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1145 | validateAccounts = true; |
| 1146 | } |
| 1147 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1148 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1149 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
| 1150 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1151 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1152 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1153 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1154 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1155 | } |
| 1156 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1157 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1158 | } |
| 1159 | return accounts; |
| 1160 | } |
| 1161 | } |
| 1162 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1163 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1164 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1165 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1166 | if (!accountsToRemove.isEmpty()) { |
| 1167 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1168 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1169 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1170 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1171 | |
| 1172 | for (Account account : accountsToRemove) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1173 | removeAccountInternal(accounts, account, Process.SYSTEM_UID); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1178 | private void purgeOldGrantsAll() { |
| 1179 | synchronized (mUsers) { |
| 1180 | for (int i = 0; i < mUsers.size(); i++) { |
| 1181 | purgeOldGrants(mUsers.valueAt(i)); |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | private void purgeOldGrants(UserAccounts accounts) { |
| 1187 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1188 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1189 | for (int uid : uids) { |
| 1190 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1191 | if (packageExists) { |
| 1192 | continue; |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1193 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1194 | Log.d(TAG, "deleting grants for UID " + uid |
| 1195 | + " because its package is no longer installed"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1196 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1201 | private void removeVisibilityValuesForPackage(String packageName) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 1202 | if (isSpecialPackageKey(packageName)) { |
| 1203 | return; |
| 1204 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1205 | synchronized (mUsers) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 1206 | int numberOfUsers = mUsers.size(); |
| 1207 | for (int i = 0; i < numberOfUsers; i++) { |
| 1208 | UserAccounts accounts = mUsers.valueAt(i); |
| 1209 | try { |
| 1210 | mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 1211 | } catch (NameNotFoundException e) { |
| 1212 | // package does not exist - remove visibility values |
| 1213 | accounts.accountsDb.deleteAccountVisibilityForPackage(packageName); |
| 1214 | synchronized(accounts.cacheLock) { |
| 1215 | for (Account account : accounts.visibilityCache.keySet()) { |
| 1216 | Map<String, Integer> accountVisibility = |
| 1217 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 1218 | accountVisibility.remove(packageName); |
| 1219 | } |
| 1220 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 1226 | |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 1227 | private void onCleanupUser(int userId) { |
| 1228 | Log.i(TAG, "onCleanupUser " + userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1229 | UserAccounts accounts; |
| 1230 | synchronized (mUsers) { |
| 1231 | accounts = mUsers.get(userId); |
| 1232 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1233 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1234 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1235 | if (accounts != null) { |
| 1236 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1237 | accounts.accountsDb.close(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1238 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1239 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1242 | @VisibleForTesting |
| 1243 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1244 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1245 | } |
| 1246 | |
| 1247 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1248 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1249 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1250 | } |
| 1251 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1252 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1253 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1254 | if (userId < 1) return; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1255 | syncSharedAccounts(userId); |
| 1256 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1257 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1258 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1259 | // Check if there's a shared account that needs to be created as an account |
| 1260 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1261 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1262 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1263 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1264 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1265 | : UserHandle.USER_SYSTEM; |
| 1266 | if (parentUserId < 0) { |
| 1267 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1268 | return; |
| 1269 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1270 | for (Account sa : sharedAccounts) { |
| 1271 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1272 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1273 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1277 | @Override |
| 1278 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1279 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1282 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1283 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1284 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1285 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1286 | Log.v(TAG, "getPassword: " + account |
| 1287 | + ", caller's uid " + Binder.getCallingUid() |
| 1288 | + ", pid " + Binder.getCallingPid()); |
| 1289 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1290 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1291 | int userId = UserHandle.getCallingUserId(); |
| 1292 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1293 | String msg = String.format( |
| 1294 | "uid %s cannot get secrets for accounts of type: %s", |
| 1295 | callingUid, |
| 1296 | account.type); |
| 1297 | throw new SecurityException(msg); |
| 1298 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1299 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1300 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1301 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1302 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1303 | } finally { |
| 1304 | restoreCallingIdentity(identityToken); |
| 1305 | } |
| 1306 | } |
| 1307 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1308 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1309 | if (account == null) { |
| 1310 | return null; |
| 1311 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1312 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1313 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1314 | return null; |
| 1315 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1316 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1317 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1318 | return accounts.accountsDb.findAccountPasswordByNameAndType(account.name, account.type); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1322 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1323 | public String getPreviousName(Account account) { |
| 1324 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1325 | Log.v(TAG, "getPreviousName: " + account |
| 1326 | + ", caller's uid " + Binder.getCallingUid() |
| 1327 | + ", pid " + Binder.getCallingPid()); |
| 1328 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1329 | Preconditions.checkNotNull(account, "account cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1330 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1331 | long identityToken = clearCallingIdentity(); |
| 1332 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1333 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1334 | return readPreviousNameInternal(accounts, account); |
| 1335 | } finally { |
| 1336 | restoreCallingIdentity(identityToken); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1341 | if (account == null) { |
| 1342 | return null; |
| 1343 | } |
| 1344 | synchronized (accounts.cacheLock) { |
| 1345 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1346 | if (previousNameRef == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1347 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1348 | previousNameRef = new AtomicReference<>(previousName); |
| 1349 | accounts.previousNameCache.put(account, previousNameRef); |
| 1350 | return previousName; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1351 | } else { |
| 1352 | return previousNameRef.get(); |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1358 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1359 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1360 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1361 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1362 | account, key, callingUid, Binder.getCallingPid()); |
| 1363 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1364 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1365 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 1366 | Preconditions.checkNotNull(key, "key cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1367 | int userId = UserHandle.getCallingUserId(); |
| 1368 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1369 | String msg = String.format( |
| 1370 | "uid %s cannot get user data for accounts of type: %s", |
| 1371 | callingUid, |
| 1372 | account.type); |
| 1373 | throw new SecurityException(msg); |
| 1374 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1375 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1376 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1377 | return null; |
| 1378 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1379 | long identityToken = clearCallingIdentity(); |
| 1380 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1381 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1382 | synchronized (accounts.cacheLock) { |
| 1383 | if (!accountExistsCacheLocked(accounts, account)) { |
| 1384 | return null; |
| 1385 | } |
| 1386 | return readUserDataInternalLocked(accounts, account, key); |
| 1387 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1388 | } finally { |
| 1389 | restoreCallingIdentity(identityToken); |
| 1390 | } |
| 1391 | } |
| 1392 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1393 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1394 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1395 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1396 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1397 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1398 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1399 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1400 | + ", pid " + Binder.getCallingPid()); |
| 1401 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1402 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1403 | if (isCrossUser(callingUid, userId)) { |
| 1404 | throw new SecurityException( |
| 1405 | String.format( |
| 1406 | "User %s tying to get authenticator types for %s" , |
| 1407 | UserHandle.getCallingUserId(), |
| 1408 | userId)); |
| 1409 | } |
| 1410 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1411 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1412 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1413 | return getAuthenticatorTypesInternal(userId); |
| 1414 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1415 | } finally { |
| 1416 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1417 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1420 | /** |
| 1421 | * Should only be called inside of a clearCallingIdentity block. |
| 1422 | */ |
| 1423 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1424 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1425 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1426 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1427 | AuthenticatorDescription[] types = |
| 1428 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1429 | int i = 0; |
| 1430 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1431 | : authenticatorCollection) { |
| 1432 | types[i] = authenticator.type; |
| 1433 | i++; |
| 1434 | } |
| 1435 | return types; |
| 1436 | } |
| 1437 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1438 | private boolean isCrossUser(int callingUid, int userId) { |
| 1439 | return (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1440 | && callingUid != Process.SYSTEM_UID |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1441 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1442 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1443 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1444 | } |
| 1445 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1446 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1447 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1448 | return addAccountExplicitlyWithVisibility(account, password, extras, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1451 | @Override |
| 1452 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1453 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1454 | int callingUid = Binder.getCallingUid(); |
| 1455 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1456 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1457 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1458 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1459 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1460 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1461 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1462 | if (response != null) { |
| 1463 | Bundle result = new Bundle(); |
| 1464 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1465 | try { |
| 1466 | response.onResult(result); |
| 1467 | } catch (RemoteException e) { |
| 1468 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1469 | } |
| 1470 | } |
| 1471 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1472 | } |
| 1473 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1474 | Slog.d(TAG, "Copying account " + account.name |
| 1475 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1476 | long identityToken = clearCallingIdentity(); |
| 1477 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1478 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1479 | false /* stripAuthTokenFromResult */, account.name, |
| 1480 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1481 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1482 | protected String toDebugString(long now) { |
| 1483 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1484 | + ", " + account.type; |
| 1485 | } |
| 1486 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1487 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1488 | public void run() throws RemoteException { |
| 1489 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1490 | } |
| 1491 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1492 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1493 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1494 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1495 | if (result != null |
| 1496 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1497 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1498 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1499 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1500 | super.onResult(result); |
| 1501 | } |
| 1502 | } |
| 1503 | }.bind(); |
| 1504 | } finally { |
| 1505 | restoreCallingIdentity(identityToken); |
| 1506 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1509 | @Override |
| 1510 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1511 | final int callingUid = Binder.getCallingUid(); |
| 1512 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1513 | String msg = String.format( |
| 1514 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1515 | account, |
| 1516 | callingUid); |
| 1517 | Log.v(TAG, msg); |
| 1518 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1519 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1520 | int userId = UserHandle.getCallingUserId(); |
| 1521 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1522 | String msg = String.format( |
| 1523 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1524 | callingUid, |
| 1525 | account.type); |
| 1526 | throw new SecurityException(msg); |
| 1527 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1528 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1529 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1530 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1531 | return false; |
| 1532 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1533 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1534 | long identityToken = clearCallingIdentity(); |
| 1535 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1536 | UserAccounts accounts = getUserAccounts(userId); |
| 1537 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1538 | } finally { |
| 1539 | restoreCallingIdentity(identityToken); |
| 1540 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1544 | final UserAccounts accounts = getUserAccountsForCaller(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1545 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1546 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1547 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1550 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1551 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1552 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1553 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1554 | long id = clearCallingIdentity(); |
| 1555 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1556 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1557 | false /* stripAuthTokenFromResult */, account.name, |
| 1558 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1559 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1560 | protected String toDebugString(long now) { |
| 1561 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1562 | + ", " + account.type; |
| 1563 | } |
| 1564 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1565 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1566 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1567 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1568 | UserAccounts owner = getUserAccounts(parentUserId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1569 | synchronized (owner.cacheLock) { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1570 | for (Account acc : getAccounts(parentUserId, |
| 1571 | mContext.getOpPackageName())) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1572 | if (acc.equals(account)) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1573 | mAuthenticator.addAccountFromCredentials( |
| 1574 | this, account, accountCredentials); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1575 | break; |
| 1576 | } |
| 1577 | } |
| 1578 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1581 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1582 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1583 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1584 | // TODO: Anything to do if if succedded? |
| 1585 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1586 | // account to avoid retries? |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1587 | // TODO: what we do with the visibility? |
| 1588 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1589 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 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 onError(int errorCode, String errorMessage) { |
| 1594 | super.onError(errorCode, errorMessage); |
| 1595 | // TODO: Show error notification to user |
| 1596 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1597 | } |
| 1598 | |
| 1599 | }.bind(); |
| 1600 | } finally { |
| 1601 | restoreCallingIdentity(id); |
| 1602 | } |
| 1603 | } |
| 1604 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1605 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1606 | Bundle extras, int callingUid, Map<String, Integer> packageToVisibility) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1607 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1608 | if (account == null) { |
| 1609 | return false; |
| 1610 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1611 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1612 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1613 | + " is locked. callingUid=" + callingUid); |
| 1614 | return false; |
| 1615 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1616 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1617 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1618 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1619 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1620 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1621 | + ", skipping since the account already exists"); |
| 1622 | return false; |
| 1623 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1624 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1625 | if (accountId < 0) { |
| 1626 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1627 | + ", skipping the DB insert failed"); |
| 1628 | return false; |
| 1629 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1630 | // Insert into DE table |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1631 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1632 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1633 | + ", skipping the DB insert failed"); |
| 1634 | return false; |
| 1635 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1636 | if (extras != null) { |
| 1637 | for (String key : extras.keySet()) { |
| 1638 | final String value = extras.getString(key); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1639 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1640 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1641 | + ", skipping since insertExtra failed for key " + key); |
| 1642 | return false; |
| 1643 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1644 | } |
| 1645 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1646 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1647 | if (packageToVisibility != null) { |
| 1648 | for (Entry<String, Integer> entry : packageToVisibility.entrySet()) { |
| 1649 | setAccountVisibility(account, entry.getKey() /* package */, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1650 | entry.getValue() /* visibility */, false /* notify */, accounts); |
| 1651 | } |
| 1652 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1653 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1654 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1655 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, accountId, |
| 1656 | accounts, callingUid); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1657 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1658 | insertAccountIntoCacheLocked(accounts, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1659 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1660 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1661 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1662 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1663 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1664 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1665 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1666 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1667 | sendNotificationAccountUpdated(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1668 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1669 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1670 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1671 | return true; |
| 1672 | } |
| 1673 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1674 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1675 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1676 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1680 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1681 | * 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] | 1682 | * running, then clone the account too. |
| 1683 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1684 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1685 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1686 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1687 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1688 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1689 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1690 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1691 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1692 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1693 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1694 | } |
| 1695 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1696 | } |
| 1697 | } |
| 1698 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1699 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1700 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1701 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1702 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1703 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1704 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1705 | Log.v(TAG, "hasFeatures: " + account |
| 1706 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1707 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1708 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1709 | + ", pid " + Binder.getCallingPid()); |
| 1710 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1711 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1712 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1713 | Preconditions.checkArgument(features != null, "features cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1714 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1715 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1716 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1717 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1718 | long identityToken = clearCallingIdentity(); |
| 1719 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1720 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1721 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1722 | } finally { |
| 1723 | restoreCallingIdentity(identityToken); |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | private class TestFeaturesSession extends Session { |
| 1728 | private final String[] mFeatures; |
| 1729 | private final Account mAccount; |
| 1730 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1731 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1732 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1733 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1734 | true /* stripAuthTokenFromResult */, account.name, |
| 1735 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1736 | mFeatures = features; |
| 1737 | mAccount = account; |
| 1738 | } |
| 1739 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1740 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1741 | public void run() throws RemoteException { |
| 1742 | try { |
| 1743 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1744 | } catch (RemoteException e) { |
| 1745 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1746 | } |
| 1747 | } |
| 1748 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1749 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1750 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1751 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1752 | IAccountManagerResponse response = getResponseAndClose(); |
| 1753 | if (response != null) { |
| 1754 | try { |
| 1755 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1756 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1757 | return; |
| 1758 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1759 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1760 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1761 | + response); |
| 1762 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1763 | final Bundle newResult = new Bundle(); |
| 1764 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1765 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1766 | response.onResult(newResult); |
| 1767 | } catch (RemoteException e) { |
| 1768 | // if the caller is dead then there is no one to care about remote exceptions |
| 1769 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1770 | Log.v(TAG, "failure while notifying response", e); |
| 1771 | } |
| 1772 | } |
| 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 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1778 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1779 | + ", " + mAccount |
| 1780 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1781 | } |
| 1782 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1783 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1784 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1785 | public void renameAccount( |
| 1786 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1787 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1788 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1789 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1790 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1791 | + ", pid " + Binder.getCallingPid()); |
| 1792 | } |
| 1793 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1794 | int userId = UserHandle.getCallingUserId(); |
| 1795 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1796 | String msg = String.format( |
| 1797 | "uid %s cannot rename accounts of type: %s", |
| 1798 | callingUid, |
| 1799 | accountToRename.type); |
| 1800 | throw new SecurityException(msg); |
| 1801 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1802 | long identityToken = clearCallingIdentity(); |
| 1803 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1804 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1805 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1806 | Bundle result = new Bundle(); |
| 1807 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1808 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1809 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1810 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1811 | try { |
| 1812 | response.onResult(result); |
| 1813 | } catch (RemoteException e) { |
| 1814 | Log.w(TAG, e.getMessage()); |
| 1815 | } |
| 1816 | } finally { |
| 1817 | restoreCallingIdentity(identityToken); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1822 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1823 | Account resultAccount = null; |
| 1824 | /* |
| 1825 | * Cancel existing notifications. Let authenticators |
| 1826 | * re-post notifications as required. But we don't know if |
| 1827 | * the authenticators have bound their notifications to |
| 1828 | * now stale account name data. |
| 1829 | * |
| 1830 | * With a rename api, we might not need to do this anymore but it |
| 1831 | * shouldn't hurt. |
| 1832 | */ |
| 1833 | cancelNotification( |
| 1834 | getSigninRequiredNotificationId(accounts, accountToRename), |
| 1835 | new UserHandle(accounts.userId)); |
| 1836 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1837 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1838 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1839 | if (accountToRename.equals(pair.first.first)) { |
| 1840 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
| 1841 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1842 | } |
| 1843 | } |
| 1844 | } |
| 1845 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1846 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1847 | Account renamedAccount = new Account(newName, accountToRename.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1848 | if ((accounts.accountsDb.findCeAccountId(renamedAccount) >= 0)) { |
| 1849 | Log.e(TAG, "renameAccount failed - account with new name already exists"); |
| 1850 | return null; |
| 1851 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1852 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1853 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1854 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1855 | accounts.accountsDb.renameCeAccount(accountId, newName); |
Dmitry Dementyev | cf50dcf | 2016-11-17 14:14:11 -0800 | [diff] [blame] | 1856 | if (accounts.accountsDb.renameDeAccount( |
| 1857 | accountId, newName, accountToRename.name)) { |
| 1858 | accounts.accountsDb.setTransactionSuccessful(); |
| 1859 | } else { |
| 1860 | Log.e(TAG, "renameAccount failed"); |
| 1861 | return null; |
| 1862 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1863 | } else { |
| 1864 | Log.e(TAG, "renameAccount failed - old account does not exist"); |
| 1865 | return null; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1866 | } |
| 1867 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1868 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1869 | } |
| 1870 | /* |
| 1871 | * Database transaction was successful. Clean up cached |
| 1872 | * data associated with the account in the user profile. |
| 1873 | */ |
Svet Ganov | 5c4a512 | 2016-09-23 19:29:11 -0700 | [diff] [blame] | 1874 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1875 | /* |
| 1876 | * Extract the data and token caches before removing the |
| 1877 | * old account to preserve the user data associated with |
| 1878 | * the account. |
| 1879 | */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1880 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1881 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 1882 | Map<String, Integer> tmpVisibility = accounts.visibilityCache.get(accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1883 | removeAccountFromCacheLocked(accounts, accountToRename); |
| 1884 | /* |
| 1885 | * Update the cached data associated with the renamed |
| 1886 | * account. |
| 1887 | */ |
| 1888 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1889 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 1890 | accounts.visibilityCache.put(renamedAccount, tmpVisibility); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1891 | accounts.previousNameCache.put( |
| 1892 | renamedAccount, |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1893 | new AtomicReference<>(accountToRename.name)); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1894 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1895 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1896 | int parentUserId = accounts.userId; |
| 1897 | if (canHaveProfile(parentUserId)) { |
| 1898 | /* |
| 1899 | * Owner or system user account was renamed, rename the account for |
| 1900 | * those users with which the account was shared. |
| 1901 | */ |
| 1902 | List<UserInfo> users = getUserManager().getUsers(true); |
| 1903 | for (UserInfo user : users) { |
| 1904 | if (user.isRestricted() |
| 1905 | && (user.restrictedProfileParentId == parentUserId)) { |
| 1906 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1907 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1908 | } |
| 1909 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1910 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1911 | sendNotificationAccountUpdated(resultAccount, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1912 | sendAccountsChangedBroadcast(accounts.userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1913 | } |
| 1914 | return resultAccount; |
| 1915 | } |
| 1916 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1917 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1918 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1919 | return userInfo != null && userInfo.canHaveProfile(); |
| 1920 | } |
| 1921 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1922 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1923 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1924 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1925 | removeAccountAsUser( |
| 1926 | response, |
| 1927 | account, |
| 1928 | expectActivityLaunch, |
| 1929 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | @Override |
| 1933 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1934 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1935 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1936 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1937 | Log.v(TAG, "removeAccount: " + account |
| 1938 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1939 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1940 | + ", pid " + Binder.getCallingPid() |
| 1941 | + ", for user id " + userId); |
| 1942 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1943 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1944 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1945 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1946 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1947 | if (isCrossUser(callingUid, userId)) { |
| 1948 | throw new SecurityException( |
| 1949 | String.format( |
| 1950 | "User %s tying remove account for %s" , |
| 1951 | UserHandle.getCallingUserId(), |
| 1952 | userId)); |
| 1953 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1954 | /* |
| 1955 | * Only the system or authenticator should be allowed to remove accounts for that |
| 1956 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 1957 | * arbitrary applications (like competing authenticators). |
| 1958 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1959 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 1960 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 1961 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1962 | String msg = String.format( |
| 1963 | "uid %s cannot remove accounts of type: %s", |
| 1964 | callingUid, |
| 1965 | account.type); |
| 1966 | throw new SecurityException(msg); |
| 1967 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1968 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1969 | try { |
| 1970 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 1971 | "User cannot modify accounts"); |
| 1972 | } catch (RemoteException re) { |
| 1973 | } |
| 1974 | return; |
| 1975 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1976 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1977 | try { |
| 1978 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 1979 | "User cannot modify accounts of this type (policy)."); |
| 1980 | } catch (RemoteException re) { |
| 1981 | } |
| 1982 | return; |
| 1983 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1984 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1985 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1986 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1987 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1988 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1989 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1990 | if (account.equals(pair.first.first)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1991 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 1992 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1993 | } |
| 1994 | } |
| 1995 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1996 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 1997 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1998 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 1999 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2000 | accountId, |
| 2001 | accounts, |
| 2002 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2003 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2004 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2005 | } finally { |
| 2006 | restoreCallingIdentity(identityToken); |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | @Override |
| 2011 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2012 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2013 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2014 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2015 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2016 | + ", pid " + Binder.getCallingPid()); |
| 2017 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2018 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2019 | if (account == null) { |
| 2020 | /* |
| 2021 | * Null accounts should result in returning false, as per |
| 2022 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2023 | */ |
| 2024 | Log.e(TAG, "account is null"); |
| 2025 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2026 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2027 | String msg = String.format( |
| 2028 | "uid %s cannot explicitly add accounts of type: %s", |
| 2029 | callingUid, |
| 2030 | account.type); |
| 2031 | throw new SecurityException(msg); |
| 2032 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2033 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2034 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2035 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2036 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2037 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2038 | accountId, |
| 2039 | accounts, |
| 2040 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2041 | long identityToken = clearCallingIdentity(); |
| 2042 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2043 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2044 | } finally { |
| 2045 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2046 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2049 | private class RemoveAccountSession extends Session { |
| 2050 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2051 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2052 | Account account, boolean expectActivityLaunch) { |
| 2053 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2054 | true /* stripAuthTokenFromResult */, account.name, |
| 2055 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2056 | mAccount = account; |
| 2057 | } |
| 2058 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2059 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2060 | protected String toDebugString(long now) { |
| 2061 | return super.toDebugString(now) + ", removeAccount" |
| 2062 | + ", account " + mAccount; |
| 2063 | } |
| 2064 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2065 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2066 | public void run() throws RemoteException { |
| 2067 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2068 | } |
| 2069 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2070 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2071 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2072 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2073 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2074 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2075 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2076 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2077 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2078 | } |
| 2079 | IAccountManagerResponse response = getResponseAndClose(); |
| 2080 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2081 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2082 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2083 | + response); |
| 2084 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2085 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2086 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2087 | try { |
| 2088 | response.onResult(result2); |
| 2089 | } catch (RemoteException e) { |
| 2090 | // ignore |
| 2091 | } |
| 2092 | } |
| 2093 | } |
| 2094 | super.onResult(result); |
| 2095 | } |
| 2096 | } |
| 2097 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2098 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2099 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2100 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2101 | } |
| 2102 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2103 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2104 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2105 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2106 | if (!userUnlocked) { |
| 2107 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2108 | + " is still locked. CE data will be removed later"); |
| 2109 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2110 | synchronized (accounts.cacheLock) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2111 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, accounts); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2112 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2113 | // Set to a dummy value, this will only be used if the database |
| 2114 | // transaction succeeds. |
| 2115 | long accountId = -1; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2116 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2117 | accountId = accounts.accountsDb.findDeAccountId(account); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2118 | if (accountId >= 0) { |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2119 | isChanged = accounts.accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2120 | } |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2121 | // always delete from CE table if CE storage is available |
| 2122 | // DE account could be removed while CE was locked |
| 2123 | if (userUnlocked) { |
| 2124 | long ceAccountId = accounts.accountsDb.findCeAccountId(account); |
| 2125 | if (ceAccountId >= 0) { |
| 2126 | accounts.accountsDb.deleteCeAccount(ceAccountId); |
| 2127 | } |
| 2128 | } |
| 2129 | accounts.accountsDb.setTransactionSuccessful(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2130 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2131 | accounts.accountsDb.endTransaction(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2132 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2133 | if (isChanged) { |
| 2134 | removeAccountFromCacheLocked(accounts, account); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2135 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
| 2136 | if (packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 2137 | notifyPackage(packageToVisibility.getKey(), accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2138 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2139 | } |
| 2140 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2141 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occurred. |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2142 | sendAccountsChangedBroadcast(accounts.userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2143 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2144 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2145 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2146 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2147 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2148 | long id = Binder.clearCallingIdentity(); |
| 2149 | try { |
| 2150 | int parentUserId = accounts.userId; |
| 2151 | if (canHaveProfile(parentUserId)) { |
| 2152 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2153 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2154 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2155 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2156 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2157 | } |
| 2158 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2159 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2160 | } finally { |
| 2161 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2162 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2163 | |
| 2164 | if (isChanged) { |
| 2165 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2166 | for (Pair<Pair<Account, String>, Integer> key |
| 2167 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2168 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2169 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2170 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2171 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2172 | account, uid, false)); |
| 2173 | } |
| 2174 | } |
| 2175 | } |
| 2176 | } |
| 2177 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2178 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2181 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2182 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2183 | int callerUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2184 | Preconditions.checkNotNull(accountType, "accountType cannot be null"); |
| 2185 | Preconditions.checkNotNull(authToken, "authToken cannot be null"); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2186 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2187 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2188 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2189 | + ", pid " + Binder.getCallingPid()); |
| 2190 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2191 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2192 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2193 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2194 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2195 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2196 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2197 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2198 | invalidateAuthTokenLocked(accounts, accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2199 | invalidateCustomTokenLocked(accounts, accountType, authToken); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2200 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2201 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2202 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2203 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2204 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2205 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2206 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2207 | } |
| 2208 | } |
| 2209 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2210 | private void invalidateCustomTokenLocked( |
| 2211 | UserAccounts accounts, |
| 2212 | String accountType, |
| 2213 | String authToken) { |
| 2214 | if (authToken == null || accountType == null) { |
| 2215 | return; |
| 2216 | } |
| 2217 | // Also wipe out cached token in memory. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2218 | accounts.accountTokenCaches.remove(accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2221 | private void invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
| 2222 | String authToken) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2223 | if (authToken == null || accountType == null) { |
| 2224 | return; |
| 2225 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2226 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2227 | try { |
| 2228 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2229 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2230 | String accountName = cursor.getString(1); |
| 2231 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2232 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2233 | writeAuthTokenIntoCacheLocked( |
| 2234 | accounts, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2235 | new Account(accountName, accountType), |
| 2236 | authTokenType, |
| 2237 | null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2238 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2239 | } finally { |
| 2240 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2244 | private void saveCachedToken( |
| 2245 | UserAccounts accounts, |
| 2246 | Account account, |
| 2247 | String callerPkg, |
| 2248 | byte[] callerSigDigest, |
| 2249 | String tokenType, |
| 2250 | String token, |
| 2251 | long expiryMillis) { |
| 2252 | |
| 2253 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2254 | return; |
| 2255 | } |
| 2256 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2257 | UserHandle.of(accounts.userId)); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2258 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2259 | accounts.accountTokenCaches.put( |
| 2260 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2261 | } |
| 2262 | } |
| 2263 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2264 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2265 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2266 | if (account == null || type == null) { |
| 2267 | return false; |
| 2268 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2269 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2270 | UserHandle.of(accounts.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2271 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2272 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2273 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2274 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2275 | if (accountId < 0) { |
| 2276 | return false; |
| 2277 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2278 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2279 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2280 | accounts.accountsDb.setTransactionSuccessful(); |
| 2281 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2282 | return true; |
| 2283 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2284 | return false; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2285 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2286 | accounts.accountsDb.endTransaction(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2287 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2288 | } |
| 2289 | } |
| 2290 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2291 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2292 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2293 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2294 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2295 | Log.v(TAG, "peekAuthToken: " + account |
| 2296 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2297 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2298 | + ", pid " + Binder.getCallingPid()); |
| 2299 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2300 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2301 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2302 | int userId = UserHandle.getCallingUserId(); |
| 2303 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2304 | String msg = String.format( |
| 2305 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2306 | callingUid, |
| 2307 | account.type); |
| 2308 | throw new SecurityException(msg); |
| 2309 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2310 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2311 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2312 | + callingUid); |
| 2313 | return null; |
| 2314 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2315 | long identityToken = clearCallingIdentity(); |
| 2316 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2317 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2318 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2319 | } finally { |
| 2320 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2321 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2324 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2325 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2326 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2327 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2328 | Log.v(TAG, "setAuthToken: " + account |
| 2329 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2330 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2331 | + ", pid " + Binder.getCallingPid()); |
| 2332 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2333 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2334 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2335 | int userId = UserHandle.getCallingUserId(); |
| 2336 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2337 | String msg = String.format( |
| 2338 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2339 | callingUid, |
| 2340 | account.type); |
| 2341 | throw new SecurityException(msg); |
| 2342 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2343 | long identityToken = clearCallingIdentity(); |
| 2344 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2345 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2346 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2347 | } finally { |
| 2348 | restoreCallingIdentity(identityToken); |
| 2349 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2350 | } |
| 2351 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2352 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2353 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2354 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2355 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2356 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2357 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2358 | + ", pid " + Binder.getCallingPid()); |
| 2359 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2360 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2361 | int userId = UserHandle.getCallingUserId(); |
| 2362 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2363 | String msg = String.format( |
| 2364 | "uid %s cannot set secrets for accounts of type: %s", |
| 2365 | callingUid, |
| 2366 | account.type); |
| 2367 | throw new SecurityException(msg); |
| 2368 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2369 | long identityToken = clearCallingIdentity(); |
| 2370 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2371 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2372 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2373 | } finally { |
| 2374 | restoreCallingIdentity(identityToken); |
| 2375 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2376 | } |
| 2377 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2378 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2379 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2380 | if (account == null) { |
| 2381 | return; |
| 2382 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2383 | boolean isChanged = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2384 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2385 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2386 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2387 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2388 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2389 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2390 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2391 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2392 | accounts.accountTokenCaches.remove(account); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2393 | accounts.accountsDb.setTransactionSuccessful(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2394 | // If there is an account whose password will be updated and the database |
| 2395 | // transactions succeed, then we say that a change has occured. Even if the |
| 2396 | // new password is the same as the old and there were no authtokens to delete. |
| 2397 | isChanged = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2398 | String action = (password == null || password.length() == 0) ? |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2399 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2400 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2401 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, callingUid); |
Costin Manolache | f5ffe89 | 2011-01-19 09:35:32 -0800 | [diff] [blame] | 2402 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2403 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2404 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2405 | if (isChanged) { |
| 2406 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2407 | sendNotificationAccountUpdated(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2408 | sendAccountsChangedBroadcast(accounts.userId); |
| 2409 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2410 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2411 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2414 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2415 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2416 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2417 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2418 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2419 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2420 | + ", pid " + Binder.getCallingPid()); |
| 2421 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2422 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2423 | int userId = UserHandle.getCallingUserId(); |
| 2424 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2425 | String msg = String.format( |
| 2426 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2427 | callingUid, |
| 2428 | account.type); |
| 2429 | throw new SecurityException(msg); |
| 2430 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2431 | long identityToken = clearCallingIdentity(); |
| 2432 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2433 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2434 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2435 | } finally { |
| 2436 | restoreCallingIdentity(identityToken); |
| 2437 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2440 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2441 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2442 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2443 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2444 | Log.v(TAG, "setUserData: " + account |
| 2445 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2446 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2447 | + ", pid " + Binder.getCallingPid()); |
| 2448 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2449 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2450 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2451 | int userId = UserHandle.getCallingUserId(); |
| 2452 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2453 | String msg = String.format( |
| 2454 | "uid %s cannot set user data for accounts of type: %s", |
| 2455 | callingUid, |
| 2456 | account.type); |
| 2457 | throw new SecurityException(msg); |
| 2458 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2459 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2460 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2461 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2462 | synchronized (accounts.cacheLock) { |
| 2463 | if (!accountExistsCacheLocked(accounts, account)) { |
| 2464 | return; |
| 2465 | } |
| 2466 | setUserdataInternalLocked(accounts, account, key, value); |
| 2467 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2468 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2469 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2470 | } |
| 2471 | } |
| 2472 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2473 | private boolean accountExistsCacheLocked(UserAccounts accounts, Account account) { |
| 2474 | if (accounts.accountCache.containsKey(account.type)) { |
| 2475 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2476 | if (acc.name.equals(account.name)) { |
| 2477 | return true; |
| 2478 | } |
| 2479 | } |
| 2480 | } |
| 2481 | return false; |
| 2482 | } |
| 2483 | |
| 2484 | private void setUserdataInternalLocked(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2485 | String value) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2486 | if (account == null || key == null) { |
| 2487 | return; |
| 2488 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2489 | accounts.accountsDb.beginTransaction(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2490 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2491 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2492 | if (accountId < 0) { |
| 2493 | return; |
| 2494 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2495 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2496 | if (extrasId < 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2497 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2498 | if (extrasId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2499 | return; |
| 2500 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2501 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2502 | return; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2503 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2504 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2505 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2506 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2507 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2508 | } |
| 2509 | } |
| 2510 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2511 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2512 | if (result == null) { |
| 2513 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2514 | } |
| 2515 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2516 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2517 | + response); |
| 2518 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2519 | try { |
| 2520 | response.onResult(result); |
| 2521 | } catch (RemoteException e) { |
| 2522 | // if the caller is dead then there is no one to care about remote |
| 2523 | // exceptions |
| 2524 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2525 | Log.v(TAG, "failure while notifying response", e); |
| 2526 | } |
| 2527 | } |
| 2528 | } |
| 2529 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2530 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2531 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2532 | final String authTokenType) |
| 2533 | throws RemoteException { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2534 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
| 2535 | Preconditions.checkArgument(authTokenType != null, "authTokenType cannot be null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2536 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2537 | final int callingUid = getCallingUid(); |
| 2538 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2539 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2540 | throw new SecurityException("can only call from system"); |
| 2541 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2542 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2543 | long identityToken = clearCallingIdentity(); |
| 2544 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2545 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2546 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2547 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2548 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2549 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2550 | protected String toDebugString(long now) { |
| 2551 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2552 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2553 | + ", authTokenType " + authTokenType; |
| 2554 | } |
| 2555 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2556 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2557 | public void run() throws RemoteException { |
| 2558 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2559 | } |
| 2560 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2561 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2562 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2563 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2564 | if (result != null) { |
| 2565 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2566 | Bundle bundle = new Bundle(); |
| 2567 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2568 | super.onResult(bundle); |
| 2569 | return; |
| 2570 | } else { |
| 2571 | super.onResult(result); |
| 2572 | } |
| 2573 | } |
| 2574 | }.bind(); |
| 2575 | } finally { |
| 2576 | restoreCallingIdentity(identityToken); |
| 2577 | } |
| 2578 | } |
| 2579 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2580 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2581 | public void getAuthToken( |
| 2582 | IAccountManagerResponse response, |
| 2583 | final Account account, |
| 2584 | final String authTokenType, |
| 2585 | final boolean notifyOnAuthFailure, |
| 2586 | final boolean expectActivityLaunch, |
| 2587 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2588 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2589 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2590 | Log.v(TAG, "getAuthToken: " + account |
| 2591 | + ", response " + response |
| 2592 | + ", authTokenType " + authTokenType |
| 2593 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2594 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2595 | + ", caller's uid " + Binder.getCallingUid() |
| 2596 | + ", pid " + Binder.getCallingPid()); |
| 2597 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2598 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2599 | try { |
| 2600 | if (account == null) { |
| 2601 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2602 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2603 | return; |
| 2604 | } |
| 2605 | if (authTokenType == null) { |
| 2606 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2607 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2608 | return; |
| 2609 | } |
| 2610 | } catch (RemoteException e) { |
| 2611 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2612 | return; |
| 2613 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2614 | int userId = UserHandle.getCallingUserId(); |
| 2615 | long ident = Binder.clearCallingIdentity(); |
| 2616 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2617 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2618 | try { |
| 2619 | accounts = getUserAccounts(userId); |
| 2620 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2621 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2622 | } finally { |
| 2623 | Binder.restoreCallingIdentity(ident); |
| 2624 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2625 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2626 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2627 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2628 | |
| 2629 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2630 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2631 | final boolean permissionGranted = |
| 2632 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2633 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2634 | // Get the calling package. We will use it for the purpose of caching. |
| 2635 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2636 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2637 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2638 | try { |
| 2639 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2640 | } finally { |
| 2641 | Binder.restoreCallingIdentity(ident); |
| 2642 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2643 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2644 | String msg = String.format( |
| 2645 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2646 | callerUid, |
| 2647 | callerPkg); |
| 2648 | throw new SecurityException(msg); |
| 2649 | } |
| 2650 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2651 | // let authenticator know the identity of the caller |
| 2652 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2653 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2654 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2655 | if (notifyOnAuthFailure) { |
| 2656 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2657 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2658 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2659 | long identityToken = clearCallingIdentity(); |
| 2660 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2661 | // Distill the caller's package signatures into a single digest. |
| 2662 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2663 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2664 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2665 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2666 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2667 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2668 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2669 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2670 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2671 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2672 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2673 | onResult(response, result); |
| 2674 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2675 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2678 | if (customTokens) { |
| 2679 | /* |
| 2680 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2681 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2682 | * ANDORID_PACKAGE_NAME. |
| 2683 | */ |
| 2684 | String token = readCachedTokenInternal( |
| 2685 | accounts, |
| 2686 | account, |
| 2687 | authTokenType, |
| 2688 | callerPkg, |
| 2689 | callerPkgSigDigest); |
| 2690 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2691 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2692 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2693 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2694 | Bundle result = new Bundle(); |
| 2695 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2696 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2697 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2698 | onResult(response, result); |
| 2699 | return; |
| 2700 | } |
| 2701 | } |
| 2702 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2703 | new Session( |
| 2704 | accounts, |
| 2705 | response, |
| 2706 | account.type, |
| 2707 | expectActivityLaunch, |
| 2708 | false /* stripAuthTokenFromResult */, |
| 2709 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2710 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2711 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2712 | protected String toDebugString(long now) { |
| 2713 | if (loginOptions != null) loginOptions.keySet(); |
| 2714 | return super.toDebugString(now) + ", getAuthToken" |
| 2715 | + ", " + account |
| 2716 | + ", authTokenType " + authTokenType |
| 2717 | + ", loginOptions " + loginOptions |
| 2718 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2719 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2720 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2721 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2722 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2723 | // If the caller doesn't have permission then create and return the |
| 2724 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2725 | if (!permissionGranted) { |
| 2726 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2727 | } else { |
| 2728 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2729 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2730 | } |
| 2731 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2732 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2733 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2734 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2735 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2736 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2737 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2738 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2739 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2740 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2741 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2742 | authTokenType, |
| 2743 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2744 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2745 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2746 | onResult(bundle); |
| 2747 | return; |
| 2748 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2749 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2750 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2751 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2752 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2753 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2754 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2755 | "the type and name should not be empty"); |
| 2756 | return; |
| 2757 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2758 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2759 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2760 | saveAuthTokenToDatabase( |
| 2761 | mAccounts, |
| 2762 | resultAccount, |
| 2763 | authTokenType, |
| 2764 | authToken); |
| 2765 | } |
| 2766 | long expiryMillis = result.getLong( |
| 2767 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2768 | if (customTokens |
| 2769 | && expiryMillis > System.currentTimeMillis()) { |
| 2770 | saveCachedToken( |
| 2771 | mAccounts, |
| 2772 | account, |
| 2773 | callerPkg, |
| 2774 | callerPkgSigDigest, |
| 2775 | authTokenType, |
| 2776 | authToken, |
| 2777 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2778 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2779 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2780 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2781 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2782 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2783 | /* |
| 2784 | * Make sure that the supplied intent is owned by the authenticator |
| 2785 | * giving it to the system. Otherwise a malicious authenticator could |
| 2786 | * have users launching arbitrary activities by tricking users to |
| 2787 | * interact with malicious notifications. |
| 2788 | */ |
| 2789 | checkKeyIntent( |
| 2790 | Binder.getCallingUid(), |
| 2791 | intent); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2792 | doNotification( |
| 2793 | mAccounts, |
| 2794 | account, |
| 2795 | result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2796 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2797 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2798 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2799 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2800 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2801 | }.bind(); |
| 2802 | } finally { |
| 2803 | restoreCallingIdentity(identityToken); |
| 2804 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2805 | } |
| 2806 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2807 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2808 | MessageDigest digester; |
| 2809 | try { |
| 2810 | digester = MessageDigest.getInstance("SHA-256"); |
| 2811 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2812 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2813 | for (Signature sig : pkgInfo.signatures) { |
| 2814 | digester.update(sig.toByteArray()); |
| 2815 | } |
| 2816 | } catch (NoSuchAlgorithmException x) { |
| 2817 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2818 | digester = null; |
| 2819 | } catch (NameNotFoundException e) { |
| 2820 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2821 | digester = null; |
| 2822 | } |
| 2823 | return (digester == null) ? null : digester.digest(); |
| 2824 | } |
| 2825 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2826 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2827 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2828 | int uid = intent.getIntExtra( |
| 2829 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2830 | String authTokenType = intent.getStringExtra( |
| 2831 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2832 | final String titleAndSubtitle = |
| 2833 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2834 | account.name); |
| 2835 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2836 | String title = titleAndSubtitle; |
| 2837 | String subtitle = ""; |
| 2838 | if (index > 0) { |
| 2839 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2840 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2841 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2842 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2843 | Context contextForUser = getContextForUser(user); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 2844 | Notification n = |
| 2845 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
| 2846 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2847 | .setWhen(0) |
| 2848 | .setColor(contextForUser.getColor( |
| 2849 | com.android.internal.R.color.system_notification_accent_color)) |
| 2850 | .setContentTitle(title) |
| 2851 | .setContentText(subtitle) |
| 2852 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2853 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2854 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2855 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2856 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2859 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 2860 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 2861 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2862 | |
| 2863 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2864 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2865 | if (startInNewTask) { |
| 2866 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 2867 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2868 | // the intent from a non-Activity context. This is the default behavior. |
| 2869 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2870 | } |
| 2871 | intent.addCategory(String.valueOf(getCredentialPermissionNotificationId(account, |
| 2872 | authTokenType, uid) + (packageName != null ? packageName : ""))); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2873 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2874 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2875 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2876 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2877 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2878 | return intent; |
| 2879 | } |
| 2880 | |
| 2881 | private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, |
| 2882 | int uid) { |
| 2883 | Integer id; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2884 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2885 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2886 | final Pair<Pair<Account, String>, Integer> key = |
| 2887 | new Pair<Pair<Account, String>, Integer>( |
| 2888 | new Pair<Account, String>(account, authTokenType), uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2889 | id = accounts.credentialsPermissionNotificationIds.get(key); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2890 | if (id == null) { |
| 2891 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2892 | accounts.credentialsPermissionNotificationIds.put(key, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2893 | } |
| 2894 | } |
| 2895 | return id; |
| 2896 | } |
| 2897 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2898 | private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2899 | Integer id; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2900 | synchronized (accounts.signinRequiredNotificationIds) { |
| 2901 | id = accounts.signinRequiredNotificationIds.get(account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2902 | if (id == null) { |
| 2903 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2904 | accounts.signinRequiredNotificationIds.put(account, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2905 | } |
| 2906 | } |
| 2907 | return id; |
| 2908 | } |
| 2909 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2910 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2911 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2912 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2913 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2914 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2915 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2916 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2917 | + ", response " + response |
| 2918 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2919 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2920 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2921 | + ", caller's uid " + Binder.getCallingUid() |
| 2922 | + ", pid " + Binder.getCallingPid()); |
| 2923 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2924 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2925 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2926 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2927 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2928 | final int uid = Binder.getCallingUid(); |
| 2929 | final int userId = UserHandle.getUserId(uid); |
| 2930 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2931 | try { |
| 2932 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2933 | "User is not allowed to add an account!"); |
| 2934 | } catch (RemoteException re) { |
| 2935 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2936 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2937 | return; |
| 2938 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2939 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2940 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2941 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2942 | "User cannot modify accounts of this type (policy)."); |
| 2943 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2944 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2945 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2946 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2947 | return; |
| 2948 | } |
| 2949 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2950 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2951 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2952 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2953 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2954 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2955 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2956 | long identityToken = clearCallingIdentity(); |
| 2957 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2958 | UserAccounts accounts = getUserAccounts(usrId); |
| 2959 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2960 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 2961 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2962 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2963 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2964 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2965 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2966 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2967 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2968 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2969 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2970 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2971 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2972 | protected String toDebugString(long now) { |
| 2973 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2974 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2975 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2976 | } |
| 2977 | }.bind(); |
| 2978 | } finally { |
| 2979 | restoreCallingIdentity(identityToken); |
| 2980 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2983 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2984 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 2985 | final String authTokenType, final String[] requiredFeatures, |
| 2986 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2987 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2988 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2989 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2990 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2991 | + ", response " + response |
| 2992 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2993 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2994 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2995 | + ", caller's uid " + Binder.getCallingUid() |
| 2996 | + ", pid " + Binder.getCallingPid() |
| 2997 | + ", for user id " + userId); |
| 2998 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2999 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3000 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3001 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3002 | if (isCrossUser(callingUid, userId)) { |
| 3003 | throw new SecurityException( |
| 3004 | String.format( |
| 3005 | "User %s trying to add account for %s" , |
| 3006 | UserHandle.getCallingUserId(), |
| 3007 | userId)); |
| 3008 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3009 | |
| 3010 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3011 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3012 | try { |
| 3013 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3014 | "User is not allowed to add an account!"); |
| 3015 | } catch (RemoteException re) { |
| 3016 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3017 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3018 | return; |
| 3019 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3020 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3021 | try { |
| 3022 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3023 | "User cannot modify accounts of this type (policy)."); |
| 3024 | } catch (RemoteException re) { |
| 3025 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3026 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3027 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3028 | return; |
| 3029 | } |
| 3030 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3031 | final int pid = Binder.getCallingPid(); |
| 3032 | final int uid = Binder.getCallingUid(); |
| 3033 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3034 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3035 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3036 | |
| 3037 | long identityToken = clearCallingIdentity(); |
| 3038 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3039 | UserAccounts accounts = getUserAccounts(userId); |
| 3040 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3041 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3042 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3043 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3044 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3045 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3046 | @Override |
| 3047 | public void run() throws RemoteException { |
| 3048 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3049 | options); |
| 3050 | } |
| 3051 | |
| 3052 | @Override |
| 3053 | protected String toDebugString(long now) { |
| 3054 | return super.toDebugString(now) + ", addAccount" |
| 3055 | + ", accountType " + accountType |
| 3056 | + ", requiredFeatures " |
| 3057 | + (requiredFeatures != null |
| 3058 | ? TextUtils.join(",", requiredFeatures) |
| 3059 | : null); |
| 3060 | } |
| 3061 | }.bind(); |
| 3062 | } finally { |
| 3063 | restoreCallingIdentity(identityToken); |
| 3064 | } |
| 3065 | } |
| 3066 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3067 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3068 | public void startAddAccountSession( |
| 3069 | final IAccountManagerResponse response, |
| 3070 | final String accountType, |
| 3071 | final String authTokenType, |
| 3072 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3073 | final boolean expectActivityLaunch, |
| 3074 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3075 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3076 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3077 | Log.v(TAG, |
| 3078 | "startAddAccountSession: accountType " + accountType |
| 3079 | + ", response " + response |
| 3080 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3081 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3082 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3083 | + ", caller's uid " + Binder.getCallingUid() |
| 3084 | + ", pid " + Binder.getCallingPid()); |
| 3085 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3086 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3087 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3088 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3089 | final int uid = Binder.getCallingUid(); |
| 3090 | final int userId = UserHandle.getUserId(uid); |
| 3091 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3092 | try { |
| 3093 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3094 | "User is not allowed to add an account!"); |
| 3095 | } catch (RemoteException re) { |
| 3096 | } |
| 3097 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3098 | return; |
| 3099 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3100 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3101 | try { |
| 3102 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3103 | "User cannot modify accounts of this type (policy)."); |
| 3104 | } catch (RemoteException re) { |
| 3105 | } |
| 3106 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3107 | userId); |
| 3108 | return; |
| 3109 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3110 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3111 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3112 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3113 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3114 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3115 | // Check to see if the Password should be included to the caller. |
| 3116 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3117 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3118 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3119 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3120 | long identityToken = clearCallingIdentity(); |
| 3121 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3122 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3123 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3124 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3125 | new StartAccountSession( |
| 3126 | accounts, |
| 3127 | response, |
| 3128 | accountType, |
| 3129 | expectActivityLaunch, |
| 3130 | null /* accountName */, |
| 3131 | false /* authDetailsRequired */, |
| 3132 | true /* updateLastAuthenticationTime */, |
| 3133 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3134 | @Override |
| 3135 | public void run() throws RemoteException { |
| 3136 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3137 | requiredFeatures, options); |
| 3138 | } |
| 3139 | |
| 3140 | @Override |
| 3141 | protected String toDebugString(long now) { |
| 3142 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3143 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3144 | + accountType + ", requiredFeatures " |
| 3145 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3146 | } |
| 3147 | }.bind(); |
| 3148 | } finally { |
| 3149 | restoreCallingIdentity(identityToken); |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3154 | private abstract class StartAccountSession extends Session { |
| 3155 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3156 | private final boolean mIsPasswordForwardingAllowed; |
| 3157 | |
| 3158 | public StartAccountSession( |
| 3159 | UserAccounts accounts, |
| 3160 | IAccountManagerResponse response, |
| 3161 | String accountType, |
| 3162 | boolean expectActivityLaunch, |
| 3163 | String accountName, |
| 3164 | boolean authDetailsRequired, |
| 3165 | boolean updateLastAuthenticationTime, |
| 3166 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3167 | super(accounts, response, accountType, expectActivityLaunch, |
| 3168 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3169 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3170 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | @Override |
| 3174 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3175 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3176 | mNumResults++; |
| 3177 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3178 | if (result != null |
| 3179 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3180 | checkKeyIntent( |
| 3181 | Binder.getCallingUid(), |
| 3182 | intent); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3183 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3184 | IAccountManagerResponse response; |
| 3185 | if (mExpectActivityLaunch && result != null |
| 3186 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3187 | response = mResponse; |
| 3188 | } else { |
| 3189 | response = getResponseAndClose(); |
| 3190 | } |
| 3191 | if (response == null) { |
| 3192 | return; |
| 3193 | } |
| 3194 | if (result == null) { |
| 3195 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3196 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3197 | + response); |
| 3198 | } |
| 3199 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3200 | "null bundle returned"); |
| 3201 | return; |
| 3202 | } |
| 3203 | |
| 3204 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3205 | // All AccountManager error codes are greater |
| 3206 | // than 0 |
| 3207 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3208 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3209 | return; |
| 3210 | } |
| 3211 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3212 | // Omit passwords if the caller isn't permitted to see them. |
| 3213 | if (!mIsPasswordForwardingAllowed) { |
| 3214 | result.remove(AccountManager.KEY_PASSWORD); |
| 3215 | } |
| 3216 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3217 | // Strip auth token from result. |
| 3218 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3219 | |
| 3220 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3221 | Log.v(TAG, |
| 3222 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3223 | } |
| 3224 | |
| 3225 | // Get the session bundle created by authenticator. The |
| 3226 | // bundle contains data necessary for finishing the session |
| 3227 | // later. The session bundle will be encrypted here and |
| 3228 | // decrypted later when trying to finish the session. |
| 3229 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3230 | if (sessionBundle != null) { |
| 3231 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3232 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3233 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3234 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3235 | } |
| 3236 | // Add accountType info to session bundle. This will |
| 3237 | // override any value set by authenticator. |
| 3238 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3239 | |
| 3240 | // Encrypt session bundle before returning to caller. |
| 3241 | try { |
| 3242 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3243 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3244 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3245 | } catch (GeneralSecurityException e) { |
| 3246 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3247 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3248 | } |
| 3249 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3250 | "failed to encrypt session bundle"); |
| 3251 | return; |
| 3252 | } |
| 3253 | } |
| 3254 | |
| 3255 | sendResponse(response, result); |
| 3256 | } |
| 3257 | } |
| 3258 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3259 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3260 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3261 | @NonNull Bundle sessionBundle, |
| 3262 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3263 | Bundle appInfo, |
| 3264 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3265 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3266 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3267 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3268 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3269 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3270 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3271 | + ", caller's uid " + callingUid |
| 3272 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3273 | + ", pid " + Binder.getCallingPid() |
| 3274 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3275 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3276 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3277 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3278 | // Account type is added to it before encryption. |
| 3279 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3280 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3281 | } |
| 3282 | |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 3283 | // Only allow the system process to finish session for other users. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3284 | if (isCrossUser(callingUid, userId)) { |
| 3285 | throw new SecurityException( |
| 3286 | String.format( |
| 3287 | "User %s trying to finish session for %s without cross user permission", |
| 3288 | UserHandle.getCallingUserId(), |
| 3289 | userId)); |
| 3290 | } |
| 3291 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3292 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3293 | sendErrorResponse(response, |
| 3294 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3295 | "User is not allowed to add an account!"); |
| 3296 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3297 | return; |
| 3298 | } |
| 3299 | |
| 3300 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3301 | final Bundle decryptedBundle; |
| 3302 | final String accountType; |
| 3303 | // First decrypt session bundle to get account type for checking permission. |
| 3304 | try { |
| 3305 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3306 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3307 | if (decryptedBundle == null) { |
| 3308 | sendErrorResponse( |
| 3309 | response, |
| 3310 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3311 | "failed to decrypt session bundle"); |
| 3312 | return; |
| 3313 | } |
| 3314 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3315 | // Account type cannot be null. This should not happen if session bundle was created |
| 3316 | // properly by #StartAccountSession. |
| 3317 | if (TextUtils.isEmpty(accountType)) { |
| 3318 | sendErrorResponse( |
| 3319 | response, |
| 3320 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3321 | "accountType is empty"); |
| 3322 | return; |
| 3323 | } |
| 3324 | |
| 3325 | // If by any chances, decryptedBundle contains colliding keys with |
| 3326 | // system info |
| 3327 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3328 | // update credentials flow, we should replace with the new values of the current call. |
| 3329 | if (appInfo != null) { |
| 3330 | decryptedBundle.putAll(appInfo); |
| 3331 | } |
| 3332 | |
| 3333 | // 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] | 3334 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3335 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3336 | } catch (GeneralSecurityException e) { |
| 3337 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3338 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3339 | } |
| 3340 | sendErrorResponse( |
| 3341 | response, |
| 3342 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3343 | "failed to decrypt session bundle"); |
| 3344 | return; |
| 3345 | } |
| 3346 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3347 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3348 | sendErrorResponse( |
| 3349 | response, |
| 3350 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3351 | "User cannot modify accounts of this type (policy)."); |
| 3352 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3353 | userId); |
| 3354 | return; |
| 3355 | } |
| 3356 | |
| 3357 | long identityToken = clearCallingIdentity(); |
| 3358 | try { |
| 3359 | UserAccounts accounts = getUserAccounts(userId); |
| 3360 | logRecordWithUid( |
| 3361 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3362 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3363 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3364 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3365 | new Session( |
| 3366 | accounts, |
| 3367 | response, |
| 3368 | accountType, |
| 3369 | expectActivityLaunch, |
| 3370 | true /* stripAuthTokenFromResult */, |
| 3371 | null /* accountName */, |
| 3372 | false /* authDetailsRequired */, |
| 3373 | true /* updateLastAuthenticationTime */) { |
| 3374 | @Override |
| 3375 | public void run() throws RemoteException { |
| 3376 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3377 | } |
| 3378 | |
| 3379 | @Override |
| 3380 | protected String toDebugString(long now) { |
| 3381 | return super.toDebugString(now) |
| 3382 | + ", finishSession" |
| 3383 | + ", accountType " + accountType; |
| 3384 | } |
| 3385 | }.bind(); |
| 3386 | } finally { |
| 3387 | restoreCallingIdentity(identityToken); |
| 3388 | } |
| 3389 | } |
| 3390 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3391 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3392 | final DevicePolicyManagerInternal dpmi = |
| 3393 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3394 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3395 | if (dpmi == null) { |
| 3396 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3397 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3398 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3399 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3400 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3401 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3402 | } |
| 3403 | if (intent == null) { |
| 3404 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3405 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3406 | long identityToken = clearCallingIdentity(); |
| 3407 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3408 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3409 | } finally { |
| 3410 | restoreCallingIdentity(identityToken); |
| 3411 | } |
| 3412 | } |
| 3413 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3414 | /** |
| 3415 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3416 | */ |
| 3417 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3418 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3419 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3420 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3421 | return cantAddAccount; |
| 3422 | } |
| 3423 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3424 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3425 | public void confirmCredentialsAsUser( |
| 3426 | IAccountManagerResponse response, |
| 3427 | final Account account, |
| 3428 | final Bundle options, |
| 3429 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3430 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3431 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3432 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3433 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3434 | Log.v(TAG, "confirmCredentials: " + account |
| 3435 | + ", response " + response |
| 3436 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3437 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3438 | + ", pid " + Binder.getCallingPid()); |
| 3439 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3440 | // Only allow the system process to read accounts of other users |
| 3441 | if (isCrossUser(callingUid, userId)) { |
| 3442 | throw new SecurityException( |
| 3443 | String.format( |
| 3444 | "User %s trying to confirm account credentials for %s" , |
| 3445 | UserHandle.getCallingUserId(), |
| 3446 | userId)); |
| 3447 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3448 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3449 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3450 | long identityToken = clearCallingIdentity(); |
| 3451 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3452 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3453 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3454 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3455 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3456 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3457 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3458 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3459 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3460 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3461 | protected String toDebugString(long now) { |
| 3462 | return super.toDebugString(now) + ", confirmCredentials" |
| 3463 | + ", " + account; |
| 3464 | } |
| 3465 | }.bind(); |
| 3466 | } finally { |
| 3467 | restoreCallingIdentity(identityToken); |
| 3468 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3469 | } |
| 3470 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3471 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3472 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3473 | final String authTokenType, final boolean expectActivityLaunch, |
| 3474 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3475 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3476 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3477 | Log.v(TAG, "updateCredentials: " + account |
| 3478 | + ", response " + response |
| 3479 | + ", authTokenType " + authTokenType |
| 3480 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3481 | + ", caller's uid " + Binder.getCallingUid() |
| 3482 | + ", pid " + Binder.getCallingPid()); |
| 3483 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3484 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3485 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3486 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3487 | long identityToken = clearCallingIdentity(); |
| 3488 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3489 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3490 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3491 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3492 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3493 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3494 | public void run() throws RemoteException { |
| 3495 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3496 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3497 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3498 | protected String toDebugString(long now) { |
| 3499 | if (loginOptions != null) loginOptions.keySet(); |
| 3500 | return super.toDebugString(now) + ", updateCredentials" |
| 3501 | + ", " + account |
| 3502 | + ", authTokenType " + authTokenType |
| 3503 | + ", loginOptions " + loginOptions; |
| 3504 | } |
| 3505 | }.bind(); |
| 3506 | } finally { |
| 3507 | restoreCallingIdentity(identityToken); |
| 3508 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3509 | } |
| 3510 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3511 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3512 | public void startUpdateCredentialsSession( |
| 3513 | IAccountManagerResponse response, |
| 3514 | final Account account, |
| 3515 | final String authTokenType, |
| 3516 | final boolean expectActivityLaunch, |
| 3517 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3518 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3519 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3520 | Log.v(TAG, |
| 3521 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3522 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3523 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3524 | + ", pid " + Binder.getCallingPid()); |
| 3525 | } |
| 3526 | if (response == null) { |
| 3527 | throw new IllegalArgumentException("response is null"); |
| 3528 | } |
| 3529 | if (account == null) { |
| 3530 | throw new IllegalArgumentException("account is null"); |
| 3531 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3532 | |
| 3533 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3534 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3535 | |
| 3536 | // Check to see if the Password should be included to the caller. |
| 3537 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3538 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3539 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3540 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3541 | long identityToken = clearCallingIdentity(); |
| 3542 | try { |
| 3543 | UserAccounts accounts = getUserAccounts(userId); |
| 3544 | new StartAccountSession( |
| 3545 | accounts, |
| 3546 | response, |
| 3547 | account.type, |
| 3548 | expectActivityLaunch, |
| 3549 | account.name, |
| 3550 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3551 | true /* updateLastCredentialTime */, |
| 3552 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3553 | @Override |
| 3554 | public void run() throws RemoteException { |
| 3555 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3556 | loginOptions); |
| 3557 | } |
| 3558 | |
| 3559 | @Override |
| 3560 | protected String toDebugString(long now) { |
| 3561 | if (loginOptions != null) |
| 3562 | loginOptions.keySet(); |
| 3563 | return super.toDebugString(now) |
| 3564 | + ", startUpdateCredentialsSession" |
| 3565 | + ", " + account |
| 3566 | + ", authTokenType " + authTokenType |
| 3567 | + ", loginOptions " + loginOptions; |
| 3568 | } |
| 3569 | }.bind(); |
| 3570 | } finally { |
| 3571 | restoreCallingIdentity(identityToken); |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3576 | public void isCredentialsUpdateSuggested( |
| 3577 | IAccountManagerResponse response, |
| 3578 | final Account account, |
| 3579 | final String statusToken) { |
| 3580 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3581 | Log.v(TAG, |
| 3582 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3583 | + ", caller's uid " + Binder.getCallingUid() |
| 3584 | + ", pid " + Binder.getCallingPid()); |
| 3585 | } |
| 3586 | if (response == null) { |
| 3587 | throw new IllegalArgumentException("response is null"); |
| 3588 | } |
| 3589 | if (account == null) { |
| 3590 | throw new IllegalArgumentException("account is null"); |
| 3591 | } |
| 3592 | if (TextUtils.isEmpty(statusToken)) { |
| 3593 | throw new IllegalArgumentException("status token is empty"); |
| 3594 | } |
| 3595 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3596 | int usrId = UserHandle.getCallingUserId(); |
| 3597 | long identityToken = clearCallingIdentity(); |
| 3598 | try { |
| 3599 | UserAccounts accounts = getUserAccounts(usrId); |
| 3600 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3601 | false /* stripAuthTokenFromResult */, account.name, |
| 3602 | false /* authDetailsRequired */) { |
| 3603 | @Override |
| 3604 | protected String toDebugString(long now) { |
| 3605 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3606 | + ", " + account; |
| 3607 | } |
| 3608 | |
| 3609 | @Override |
| 3610 | public void run() throws RemoteException { |
| 3611 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3612 | } |
| 3613 | |
| 3614 | @Override |
| 3615 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3616 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3617 | IAccountManagerResponse response = getResponseAndClose(); |
| 3618 | if (response == null) { |
| 3619 | return; |
| 3620 | } |
| 3621 | |
| 3622 | if (result == null) { |
| 3623 | sendErrorResponse( |
| 3624 | response, |
| 3625 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3626 | "null bundle"); |
| 3627 | return; |
| 3628 | } |
| 3629 | |
| 3630 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3631 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3632 | + response); |
| 3633 | } |
| 3634 | // Check to see if an error occurred. We know if an error occurred because all |
| 3635 | // error codes are greater than 0. |
| 3636 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3637 | sendErrorResponse(response, |
| 3638 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3639 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3640 | return; |
| 3641 | } |
| 3642 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3643 | sendErrorResponse( |
| 3644 | response, |
| 3645 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3646 | "no result in response"); |
| 3647 | return; |
| 3648 | } |
| 3649 | final Bundle newResult = new Bundle(); |
| 3650 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3651 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3652 | sendResponse(response, newResult); |
| 3653 | } |
| 3654 | }.bind(); |
| 3655 | } finally { |
| 3656 | restoreCallingIdentity(identityToken); |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3661 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3662 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3663 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3664 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3665 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3666 | + ", response " + response |
| 3667 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3668 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3669 | + ", pid " + Binder.getCallingPid()); |
| 3670 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3671 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3672 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3673 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3674 | if (!isAccountManagedByCaller(accountType, callingUid, userId) |
| 3675 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3676 | String msg = String.format( |
| 3677 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3678 | callingUid, |
| 3679 | accountType); |
| 3680 | throw new SecurityException(msg); |
| 3681 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3682 | long identityToken = clearCallingIdentity(); |
| 3683 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3684 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3685 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3686 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3687 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3688 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3689 | public void run() throws RemoteException { |
| 3690 | mAuthenticator.editProperties(this, mAccountType); |
| 3691 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3692 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3693 | protected String toDebugString(long now) { |
| 3694 | return super.toDebugString(now) + ", editProperties" |
| 3695 | + ", accountType " + accountType; |
| 3696 | } |
| 3697 | }.bind(); |
| 3698 | } finally { |
| 3699 | restoreCallingIdentity(identityToken); |
| 3700 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3701 | } |
| 3702 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3703 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3704 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3705 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3706 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3707 | throw new SecurityException("Can be called only by system UID"); |
| 3708 | } |
| 3709 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3710 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3711 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3712 | |
| 3713 | final int userId = userHandle.getIdentifier(); |
| 3714 | |
| 3715 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3716 | |
| 3717 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3718 | int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3719 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3720 | } catch (NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3721 | Log.d(TAG, "Package not found " + e.getMessage()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3722 | return false; |
| 3723 | } |
| 3724 | } |
| 3725 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3726 | // Returns package with oldest target SDK for given UID. |
| 3727 | private String getPackageNameForUid(int uid) { |
| 3728 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3729 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3730 | return null; |
| 3731 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3732 | String packageName = packageNames[0]; |
Fyodor Kupolov | 892fc8d | 2017-03-22 12:57:04 -0700 | [diff] [blame] | 3733 | if (packageNames.length == 1) { |
| 3734 | return packageName; |
| 3735 | } |
| 3736 | // 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] | 3737 | int oldestVersion = Integer.MAX_VALUE; |
| 3738 | for (String name : packageNames) { |
| 3739 | try { |
| 3740 | ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(name, 0); |
| 3741 | if (applicationInfo != null) { |
| 3742 | int version = applicationInfo.targetSdkVersion; |
| 3743 | if (version < oldestVersion) { |
| 3744 | oldestVersion = version; |
| 3745 | packageName = name; |
| 3746 | } |
| 3747 | } |
| 3748 | } catch (NameNotFoundException e) { |
| 3749 | // skip |
| 3750 | } |
| 3751 | } |
| 3752 | return packageName; |
| 3753 | } |
| 3754 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3755 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3756 | int uid) { |
| 3757 | if (packageName == null) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3758 | packageName = getPackageNameForUid(uid); |
| 3759 | if (packageName == null) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3760 | return false; |
| 3761 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | // Use null token which means any token. Having a token means the package |
| 3765 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3766 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3767 | return true; |
| 3768 | } |
| 3769 | // 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] | 3770 | // 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] | 3771 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3772 | int visibility = resolveAccountVisibility(account, packageName, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3773 | getUserAccounts(UserHandle.getUserId(uid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3774 | return (visibility == AccountManager.VISIBILITY_VISIBLE |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3775 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | @Override |
| 3779 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3780 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3781 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3782 | throw new SecurityException("Can be called only by system UID"); |
| 3783 | } |
| 3784 | |
| 3785 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3786 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3787 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3788 | |
| 3789 | final int userId = userHandle.getIdentifier(); |
| 3790 | |
| 3791 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3792 | |
| 3793 | final int uid; |
| 3794 | try { |
| 3795 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 3796 | } catch (NameNotFoundException e) { |
| 3797 | Slog.e(TAG, "Unknown package " + packageName); |
| 3798 | return null; |
| 3799 | } |
| 3800 | |
| 3801 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 3802 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3803 | final long identity = Binder.clearCallingIdentity(); |
| 3804 | try { |
| 3805 | return PendingIntent.getActivityAsUser( |
| 3806 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 3807 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 3808 | null, new UserHandle(userId)).getIntentSender(); |
| 3809 | } finally { |
| 3810 | Binder.restoreCallingIdentity(identity); |
| 3811 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 3815 | int uid, RemoteCallback callback) { |
| 3816 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 3817 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 3818 | @Override |
| 3819 | public void onResult(Bundle value) throws RemoteException { |
| 3820 | handleAuthenticatorResponse(true); |
| 3821 | } |
| 3822 | |
| 3823 | @Override |
| 3824 | public void onRequestContinued() { |
| 3825 | /* ignore */ |
| 3826 | } |
| 3827 | |
| 3828 | @Override |
| 3829 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 3830 | handleAuthenticatorResponse(false); |
| 3831 | } |
| 3832 | |
| 3833 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 3834 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3835 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3836 | UserHandle.getUserHandleForUid(uid)); |
| 3837 | if (callback != null) { |
| 3838 | Bundle result = new Bundle(); |
| 3839 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 3840 | callback.sendResult(result); |
| 3841 | } |
| 3842 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3843 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3847 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 3848 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 3849 | throw new SecurityException("Only system can check for accounts across users"); |
| 3850 | } |
| 3851 | final long token = Binder.clearCallingIdentity(); |
| 3852 | try { |
| 3853 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 3854 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 3855 | if (allAccounts[i].account.equals(account)) { |
| 3856 | return true; |
| 3857 | } |
| 3858 | } |
| 3859 | return false; |
| 3860 | } finally { |
| 3861 | Binder.restoreCallingIdentity(token); |
| 3862 | } |
| 3863 | } |
| 3864 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3865 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 3866 | private final String[] mFeatures; |
| 3867 | private volatile Account[] mAccountsOfType = null; |
| 3868 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 3869 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3870 | private final int mCallingUid; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3871 | private final String mPackageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3872 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3873 | public GetAccountsByTypeAndFeatureSession( |
| 3874 | UserAccounts accounts, |
| 3875 | IAccountManagerResponse response, |
| 3876 | String type, |
| 3877 | String[] features, |
| 3878 | int callingUid, |
| 3879 | String packageName) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3880 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3881 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3882 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3883 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3884 | mFeatures = features; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3885 | mPackageName = packageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3886 | } |
| 3887 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3888 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3889 | public void run() throws RemoteException { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3890 | synchronized (mAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3891 | mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType, mCallingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3892 | mPackageName, false /* include managed not visible*/); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3893 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3894 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 3895 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3896 | mCurrentAccount = 0; |
| 3897 | |
| 3898 | checkAccount(); |
| 3899 | } |
| 3900 | |
| 3901 | public void checkAccount() { |
| 3902 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 3903 | sendResult(); |
| 3904 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3905 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3906 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3907 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 3908 | if (accountAuthenticator == null) { |
| 3909 | // It is possible that the authenticator has died, which is indicated by |
| 3910 | // mAuthenticator being set to null. If this happens then just abort. |
| 3911 | // There is no need to send back a result or error in this case since |
| 3912 | // that already happened when mAuthenticator was cleared. |
| 3913 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3914 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 3915 | + " connected to the authenticator, " + toDebugString()); |
| 3916 | } |
| 3917 | return; |
| 3918 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3919 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3920 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3921 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3922 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3926 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3927 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3928 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3929 | mNumResults++; |
| 3930 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3931 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3932 | return; |
| 3933 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3934 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3935 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 3936 | } |
| 3937 | mCurrentAccount++; |
| 3938 | checkAccount(); |
| 3939 | } |
| 3940 | |
| 3941 | public void sendResult() { |
| 3942 | IAccountManagerResponse response = getResponseAndClose(); |
| 3943 | if (response != null) { |
| 3944 | try { |
| 3945 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 3946 | for (int i = 0; i < accounts.length; i++) { |
| 3947 | accounts[i] = mAccountsWithFeatures.get(i); |
| 3948 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3949 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3950 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3951 | + response); |
| 3952 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3953 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3954 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3955 | response.onResult(result); |
| 3956 | } catch (RemoteException e) { |
| 3957 | // if the caller is dead then there is no one to care about remote exceptions |
| 3958 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3959 | Log.v(TAG, "failure while notifying response", e); |
| 3960 | } |
| 3961 | } |
| 3962 | } |
| 3963 | } |
| 3964 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3965 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3966 | protected String toDebugString(long now) { |
| 3967 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 3968 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 3969 | } |
| 3970 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 3971 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3972 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3973 | * 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] | 3974 | * @hide |
| 3975 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3976 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3977 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3978 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3979 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3980 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 3981 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3982 | if (visibleAccountTypes.isEmpty()) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 3983 | return EMPTY_ACCOUNT_ARRAY; |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3984 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3985 | long identityToken = clearCallingIdentity(); |
| 3986 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3987 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3988 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3989 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3990 | callingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3991 | opPackageName, |
| 3992 | visibleAccountTypes, |
| 3993 | false /* includeUserManagedNotVisible */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3994 | } finally { |
| 3995 | restoreCallingIdentity(identityToken); |
| 3996 | } |
| 3997 | } |
| 3998 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3999 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4000 | * Returns accounts for all running users, ignores visibility values. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4001 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4002 | * @hide |
| 4003 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4004 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4005 | public AccountAndUser[] getRunningAccounts() { |
| 4006 | final int[] runningUserIds; |
| 4007 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4008 | runningUserIds = ActivityManager.getService().getRunningUserIds(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4009 | } catch (RemoteException e) { |
| 4010 | // Running in system_server; should never happen |
| 4011 | throw new RuntimeException(e); |
| 4012 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4013 | return getAccounts(runningUserIds); |
| 4014 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4015 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4016 | /** |
| 4017 | * Returns accounts for all users, ignores visibility values. |
| 4018 | * |
| 4019 | * @hide |
| 4020 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4021 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4022 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 4023 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4024 | final int[] userIds = new int[users.size()]; |
| 4025 | for (int i = 0; i < userIds.length; i++) { |
| 4026 | userIds[i] = users.get(i).id; |
| 4027 | } |
| 4028 | return getAccounts(userIds); |
| 4029 | } |
| 4030 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4031 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4032 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4033 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4034 | for (int userId : userIds) { |
| 4035 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4036 | if (userAccounts == null) continue; |
| 4037 | synchronized (userAccounts.cacheLock) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4038 | Account[] accounts = getAccountsFromCacheLocked( |
| 4039 | userAccounts, |
| 4040 | null /* type */, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4041 | Binder.getCallingUid(), |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4042 | null /* packageName */, |
| 4043 | false /* include managed not visible*/); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4044 | for (int a = 0; a < accounts.length; a++) { |
| 4045 | runningAccounts.add(new AccountAndUser(accounts[a], userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4046 | } |
| 4047 | } |
| 4048 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4049 | |
| 4050 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4051 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4052 | } |
| 4053 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4054 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4055 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4056 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4057 | int callingUid = Binder.getCallingUid(); |
| 4058 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4059 | return getAccountsAsUserForPackage(type, userId, opPackageName /* callingPackage */, -1, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4060 | opPackageName, false /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4063 | @NonNull |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4064 | private Account[] getAccountsAsUserForPackage( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4065 | String type, |
| 4066 | int userId, |
| 4067 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4068 | int packageUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4069 | String opPackageName, |
| 4070 | boolean includeUserManagedNotVisible) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4071 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4072 | // Only allow the system process to read accounts of other users |
| 4073 | if (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4074 | && callingUid != Process.SYSTEM_UID |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4075 | && mContext.checkCallingOrSelfPermission( |
| 4076 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4077 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4078 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4079 | + " trying to get account for " + userId); |
| 4080 | } |
| 4081 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4082 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4083 | Log.v(TAG, "getAccounts: accountType " + type |
| 4084 | + ", caller's uid " + Binder.getCallingUid() |
| 4085 | + ", pid " + Binder.getCallingPid()); |
| 4086 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4087 | |
| 4088 | // If the original calling app was using account choosing activity |
| 4089 | // provided by the framework or authenticator we'll passing in |
| 4090 | // the original caller's uid here, which is what should be used for filtering. |
| 4091 | List<String> managedTypes = |
| 4092 | getTypesManagedByCaller(callingUid, UserHandle.getUserId(callingUid)); |
| 4093 | if (packageUid != -1 && |
| 4094 | ((UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 4095 | || (type != null && managedTypes.contains(type))))) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4096 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4097 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4098 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4099 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4100 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4101 | if (visibleAccountTypes.isEmpty() |
| 4102 | || (type != null && !visibleAccountTypes.contains(type))) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4103 | return EMPTY_ACCOUNT_ARRAY; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4104 | } else if (visibleAccountTypes.contains(type)) { |
| 4105 | // Prune the list down to just the requested type. |
| 4106 | visibleAccountTypes = new ArrayList<>(); |
| 4107 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4108 | } // else aggregate all the visible accounts (it won't matter if the |
| 4109 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4110 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4111 | long identityToken = clearCallingIdentity(); |
| 4112 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4113 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 4114 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4115 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4116 | callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4117 | opPackageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4118 | visibleAccountTypes, |
| 4119 | includeUserManagedNotVisible); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4120 | } finally { |
| 4121 | restoreCallingIdentity(identityToken); |
| 4122 | } |
| 4123 | } |
| 4124 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4125 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4126 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4127 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4128 | int callingUid, |
| 4129 | String callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4130 | List<String> visibleAccountTypes, |
| 4131 | boolean includeUserManagedNotVisible) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4132 | synchronized (userAccounts.cacheLock) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4133 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4134 | for (String visibleType : visibleAccountTypes) { |
| 4135 | Account[] accountsForType = getAccountsFromCacheLocked( |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4136 | userAccounts, visibleType, callingUid, callingPackage, |
| 4137 | includeUserManagedNotVisible); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4138 | if (accountsForType != null) { |
| 4139 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
| 4140 | } |
| 4141 | } |
| 4142 | Account[] result = new Account[visibleAccounts.size()]; |
| 4143 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4144 | result[i] = visibleAccounts.get(i); |
| 4145 | } |
| 4146 | return result; |
| 4147 | } |
| 4148 | } |
| 4149 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4150 | @Override |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4151 | public void addSharedAccountsFromParentUser(int parentUserId, int userId, |
| 4152 | String opPackageName) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4153 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4154 | Account[] accounts = getAccountsAsUser(null, parentUserId, opPackageName); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4155 | for (Account account : accounts) { |
| 4156 | addSharedAccountAsUser(account, userId); |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4161 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4162 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4163 | accounts.accountsDb.deleteSharedAccount(account); |
| 4164 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4165 | if (accountId < 0) { |
| 4166 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4167 | + ", skipping the DB insert failed"); |
| 4168 | return false; |
| 4169 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4170 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4171 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4172 | return true; |
| 4173 | } |
| 4174 | |
| 4175 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4176 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4177 | userId = handleIncomingUser(userId); |
| 4178 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4179 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4180 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4181 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4182 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4183 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4184 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4185 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4186 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4187 | } |
| 4188 | return r > 0; |
| 4189 | } |
| 4190 | |
| 4191 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4192 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4193 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4194 | } |
| 4195 | |
| 4196 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4197 | userId = handleIncomingUser(userId); |
| 4198 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4199 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4200 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4201 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4202 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4203 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4204 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4205 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4206 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | @Override |
| 4210 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4211 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4212 | UserAccounts accounts = getUserAccounts(userId); |
| 4213 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4214 | Account[] accountArray = new Account[accountList.size()]; |
| 4215 | accountList.toArray(accountArray); |
| 4216 | return accountArray; |
| 4217 | } |
| 4218 | |
| 4219 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4220 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4221 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4222 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4223 | } |
| 4224 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4225 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4226 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4227 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4228 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4229 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4230 | // Don't do opPackageName check - caller is system. |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4231 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4232 | + callingUid + " with uid=" + uid); |
| 4233 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4234 | return getAccountsAsUserForPackage(null, UserHandle.getCallingUserId(), packageName, uid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4235 | opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4236 | } |
| 4237 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4238 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4239 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4240 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4241 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4242 | int callingUid = Binder.getCallingUid(); |
| 4243 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4244 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4245 | int packageUid = -1; |
| 4246 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4247 | packageUid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4248 | } catch (NameNotFoundException re) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4249 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4250 | return EMPTY_ACCOUNT_ARRAY; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4251 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4252 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4253 | && (type != null && !isAccountManagedByCaller(type, callingUid, userId))) { |
| 4254 | return EMPTY_ACCOUNT_ARRAY; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4255 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4256 | return getAccountsAsUserForPackage(type, userId, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4257 | packageName, packageUid, opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4260 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4261 | public void getAccountsByFeatures( |
| 4262 | IAccountManagerResponse response, |
| 4263 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4264 | String[] features, |
| 4265 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4266 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4267 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4268 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4269 | Log.v(TAG, "getAccounts: accountType " + type |
| 4270 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4271 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4272 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4273 | + ", pid " + Binder.getCallingPid()); |
| 4274 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4275 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4276 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4277 | int userId = UserHandle.getCallingUserId(); |
| 4278 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4279 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4280 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4281 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4282 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4283 | // Need to return just the accounts that are from matching signatures. |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4284 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, EMPTY_ACCOUNT_ARRAY); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4285 | try { |
| 4286 | response.onResult(result); |
| 4287 | } catch (RemoteException e) { |
| 4288 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4289 | } |
| 4290 | return; |
| 4291 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4292 | long identityToken = clearCallingIdentity(); |
| 4293 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4294 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4295 | if (features == null || features.length == 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4296 | Account[] accounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4297 | synchronized (userAccounts.cacheLock) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4298 | accounts = getAccountsFromCacheLocked( |
| 4299 | userAccounts, type, callingUid, opPackageName, false); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4300 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4301 | Bundle result = new Bundle(); |
| 4302 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4303 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4304 | return; |
| 4305 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4306 | new GetAccountsByTypeAndFeatureSession( |
| 4307 | userAccounts, |
| 4308 | response, |
| 4309 | type, |
| 4310 | features, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4311 | callingUid, |
| 4312 | opPackageName).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4313 | } finally { |
| 4314 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4315 | } |
| 4316 | } |
| 4317 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4318 | @Override |
| 4319 | public void onAccountAccessed(String token) throws RemoteException { |
| 4320 | final int uid = Binder.getCallingUid(); |
| 4321 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4322 | return; |
| 4323 | } |
| 4324 | final int userId = UserHandle.getCallingUserId(); |
| 4325 | final long identity = Binder.clearCallingIdentity(); |
| 4326 | try { |
| 4327 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4328 | if (Objects.equals(account.getAccessId(), token)) { |
| 4329 | // An app just accessed the account. At this point it knows about |
| 4330 | // 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] | 4331 | // Do we need to update account visibility here? |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4332 | if (!hasAccountAccess(account, null, uid)) { |
| 4333 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4334 | uid, true); |
| 4335 | } |
| 4336 | } |
| 4337 | } |
| 4338 | } finally { |
| 4339 | Binder.restoreCallingIdentity(identity); |
| 4340 | } |
| 4341 | } |
| 4342 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4343 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4344 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4345 | IAccountManagerResponse mResponse; |
| 4346 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4347 | final boolean mExpectActivityLaunch; |
| 4348 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4349 | final String mAccountName; |
| 4350 | // Indicates if we need to add auth details(like last credential time) |
| 4351 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4352 | // If set, we need to update the last authenticated time. This is |
| 4353 | // currently |
| 4354 | // used on |
| 4355 | // successful confirming credentials. |
| 4356 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4357 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4358 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4359 | private int mNumRequestContinued = 0; |
| 4360 | private int mNumErrors = 0; |
| 4361 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4362 | IAccountAuthenticator mAuthenticator = null; |
| 4363 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4364 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4365 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4366 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4367 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4368 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4369 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4370 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4371 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4372 | } |
| 4373 | |
| 4374 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4375 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4376 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4377 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4378 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4379 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4380 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4381 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4382 | mResponse = response; |
| 4383 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4384 | mExpectActivityLaunch = expectActivityLaunch; |
| 4385 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4386 | mAccountName = accountName; |
| 4387 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4388 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4389 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4390 | synchronized (mSessions) { |
| 4391 | mSessions.put(toString(), this); |
| 4392 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4393 | if (response != null) { |
| 4394 | try { |
| 4395 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4396 | } catch (RemoteException e) { |
| 4397 | mResponse = null; |
| 4398 | binderDied(); |
| 4399 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4400 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4401 | } |
| 4402 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4403 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4404 | if (mResponse == null) { |
| 4405 | // this session has already been closed |
| 4406 | return null; |
| 4407 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4408 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4409 | 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] | 4410 | return response; |
| 4411 | } |
| 4412 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4413 | /** |
| 4414 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4415 | * security policy. |
| 4416 | * |
| 4417 | * 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] | 4418 | * 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] | 4419 | * supplied entries in the system Settings app. |
| 4420 | */ |
| 4421 | protected void checkKeyIntent( |
| 4422 | int authUid, |
| 4423 | Intent intent) throws SecurityException { |
| 4424 | long bid = Binder.clearCallingIdentity(); |
| 4425 | try { |
| 4426 | PackageManager pm = mContext.getPackageManager(); |
| 4427 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 4428 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4429 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4430 | if (!isExportedSystemActivity(targetActivityInfo) |
| 4431 | && (PackageManager.SIGNATURE_MATCH != pm.checkSignatures(authUid, |
| 4432 | targetUid))) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4433 | String pkgName = targetActivityInfo.packageName; |
| 4434 | String activityName = targetActivityInfo.name; |
| 4435 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4436 | + "does not share a signature with the supplying authenticator (%s)."; |
| 4437 | throw new SecurityException( |
| 4438 | String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4439 | } |
| 4440 | } finally { |
| 4441 | Binder.restoreCallingIdentity(bid); |
| 4442 | } |
| 4443 | } |
| 4444 | |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4445 | private boolean isExportedSystemActivity(ActivityInfo activityInfo) { |
| 4446 | String className = activityInfo.name; |
| 4447 | return "android".equals(activityInfo.packageName) && |
| 4448 | (GrantCredentialsPermissionActivity.class.getName().equals(className) |
| 4449 | || CantAddAccountActivity.class.getName().equals(className)); |
| 4450 | } |
| 4451 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4452 | private void close() { |
| 4453 | synchronized (mSessions) { |
| 4454 | if (mSessions.remove(toString()) == null) { |
| 4455 | // the session was already closed, so bail out now |
| 4456 | return; |
| 4457 | } |
| 4458 | } |
| 4459 | if (mResponse != null) { |
| 4460 | // stop listening for response deaths |
| 4461 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4462 | |
| 4463 | // clear this so that we don't accidentally send any further results |
| 4464 | mResponse = null; |
| 4465 | } |
| 4466 | cancelTimeout(); |
| 4467 | unbind(); |
| 4468 | } |
| 4469 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4470 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4471 | public void binderDied() { |
| 4472 | mResponse = null; |
| 4473 | close(); |
| 4474 | } |
| 4475 | |
| 4476 | protected String toDebugString() { |
| 4477 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4478 | } |
| 4479 | |
| 4480 | protected String toDebugString(long now) { |
| 4481 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4482 | + ", connected " + (mAuthenticator != null) |
| 4483 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4484 | + "/" + mNumErrors + ")" |
| 4485 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4486 | } |
| 4487 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4488 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4489 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4490 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4491 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4492 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4493 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4494 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | private void unbind() { |
| 4499 | if (mAuthenticator != null) { |
| 4500 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4501 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4502 | } |
| 4503 | } |
| 4504 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4505 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4506 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4507 | } |
| 4508 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4509 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4510 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4511 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4512 | try { |
| 4513 | run(); |
| 4514 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4515 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4516 | "remote exception"); |
| 4517 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4518 | } |
| 4519 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4520 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4521 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4522 | mAuthenticator = null; |
| 4523 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4524 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4525 | try { |
| 4526 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4527 | "disconnected"); |
| 4528 | } catch (RemoteException e) { |
| 4529 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4530 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4531 | + "caught RemoteException while responding", e); |
| 4532 | } |
| 4533 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4537 | public abstract void run() throws RemoteException; |
| 4538 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4539 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4540 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4541 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4542 | try { |
| 4543 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4544 | "timeout"); |
| 4545 | } catch (RemoteException e) { |
| 4546 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4547 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4548 | e); |
| 4549 | } |
| 4550 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4551 | } |
| 4552 | } |
| 4553 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4554 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4555 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4556 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4557 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4558 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4559 | if (result != null) { |
| 4560 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4561 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4562 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4563 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4564 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4565 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4566 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4567 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4568 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4569 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4570 | if (needUpdate || mAuthDetailsRequired) { |
| 4571 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4572 | if (needUpdate && accountPresent) { |
| 4573 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4574 | } |
| 4575 | if (mAuthDetailsRequired) { |
| 4576 | long lastAuthenticatedTime = -1; |
| 4577 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4578 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4579 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4580 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4581 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4582 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4583 | lastAuthenticatedTime); |
| 4584 | } |
| 4585 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4586 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4587 | if (result != null |
| 4588 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4589 | checkKeyIntent( |
| 4590 | Binder.getCallingUid(), |
| 4591 | intent); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4592 | } |
| 4593 | if (result != null |
| 4594 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4595 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4596 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4597 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4598 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4599 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4600 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4601 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4602 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4603 | IAccountManagerResponse response; |
| 4604 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4605 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4606 | response = mResponse; |
| 4607 | } else { |
| 4608 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4609 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4610 | if (response != null) { |
| 4611 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4612 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4613 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4614 | Log.v(TAG, getClass().getSimpleName() |
| 4615 | + " calling onError() on response " + response); |
| 4616 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4617 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4618 | "null bundle returned"); |
| 4619 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4620 | if (mStripAuthTokenFromResult) { |
| 4621 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4622 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4623 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4624 | Log.v(TAG, getClass().getSimpleName() |
| 4625 | + " calling onResult() on response " + response); |
| 4626 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4627 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4628 | (intent == null)) { |
| 4629 | // All AccountManager error codes are greater than 0 |
| 4630 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4631 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4632 | } else { |
| 4633 | response.onResult(result); |
| 4634 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4635 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4636 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4637 | // if the caller is dead then there is no one to care about remote exceptions |
| 4638 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4639 | Log.v(TAG, "failure while notifying response", e); |
| 4640 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4641 | } |
| 4642 | } |
| 4643 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4644 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4645 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4646 | public void onRequestContinued() { |
| 4647 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4648 | } |
| 4649 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4650 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4651 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4652 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4653 | IAccountManagerResponse response = getResponseAndClose(); |
| 4654 | if (response != null) { |
| 4655 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4656 | Log.v(TAG, getClass().getSimpleName() |
| 4657 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4658 | } |
| 4659 | try { |
| 4660 | response.onError(errorCode, errorMessage); |
| 4661 | } catch (RemoteException e) { |
| 4662 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4663 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4664 | } |
| 4665 | } |
| 4666 | } else { |
| 4667 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4668 | Log.v(TAG, "Session.onError: already closed"); |
| 4669 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4670 | } |
| 4671 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4672 | |
| 4673 | /** |
| 4674 | * find the component name for the authenticator and initiate a bind |
| 4675 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4676 | */ |
| 4677 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4678 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4679 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4680 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4681 | if (authenticatorInfo == null) { |
| 4682 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4683 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 4684 | + ", bailing out"); |
| 4685 | } |
| 4686 | return false; |
| 4687 | } |
| 4688 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 4689 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 4690 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 4691 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 4692 | + " which isn't encryption aware"); |
| 4693 | return false; |
| 4694 | } |
| 4695 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4696 | Intent intent = new Intent(); |
| 4697 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 4698 | intent.setComponent(authenticatorInfo.componentName); |
| 4699 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4700 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 4701 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 4702 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4703 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4704 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4705 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 4706 | } |
| 4707 | return false; |
| 4708 | } |
| 4709 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4710 | return true; |
| 4711 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4712 | } |
| 4713 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 4714 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4715 | MessageHandler(Looper looper) { |
| 4716 | super(looper); |
| 4717 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4718 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4719 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4720 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4721 | switch (msg.what) { |
| 4722 | case MESSAGE_TIMED_OUT: |
| 4723 | Session session = (Session)msg.obj; |
| 4724 | session.onTimedOut(); |
| 4725 | break; |
| 4726 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4727 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 4728 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4729 | break; |
| 4730 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4731 | default: |
| 4732 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 4733 | } |
| 4734 | } |
| 4735 | } |
| 4736 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4737 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4738 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4739 | } |
| 4740 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4741 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4742 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4743 | } |
| 4744 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4745 | /* |
| 4746 | * This function receives an opened writable database. |
| 4747 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4748 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4749 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4750 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4754 | * 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] | 4755 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4756 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4757 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4758 | |
| 4759 | class LogRecordTask implements Runnable { |
| 4760 | private final String action; |
| 4761 | private final String tableName; |
| 4762 | private final long accountId; |
| 4763 | private final UserAccounts userAccount; |
| 4764 | private final int callingUid; |
| 4765 | private final long userDebugDbInsertionPoint; |
| 4766 | |
| 4767 | LogRecordTask(final String action, |
| 4768 | final String tableName, |
| 4769 | final long accountId, |
| 4770 | final UserAccounts userAccount, |
| 4771 | final int callingUid, |
| 4772 | final long userDebugDbInsertionPoint) { |
| 4773 | this.action = action; |
| 4774 | this.tableName = tableName; |
| 4775 | this.accountId = accountId; |
| 4776 | this.userAccount = userAccount; |
| 4777 | this.callingUid = callingUid; |
| 4778 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 4779 | } |
| 4780 | |
| 4781 | public void run() { |
| 4782 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 4783 | logStatement.bindLong(1, accountId); |
| 4784 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4785 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4786 | logStatement.bindLong(4, callingUid); |
| 4787 | logStatement.bindString(5, tableName); |
| 4788 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 4789 | logStatement.execute(); |
| 4790 | logStatement.clearBindings(); |
| 4791 | } |
| 4792 | } |
| 4793 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4794 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 4795 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4796 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4797 | % AccountsDb.MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4798 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | /* |
| 4802 | * This should only be called once to compile the sql statement for logging |
| 4803 | * and to find the insertion point. |
| 4804 | */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4805 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(UserAccounts userAccount) { |
| 4806 | userAccount.debugDbInsertionPoint = userAccount.accountsDb |
| 4807 | .calculateDebugTableInsertionPoint(); |
| 4808 | userAccount.statementForLogging = userAccount.accountsDb.compileSqlStatementForLogging(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4809 | } |
| 4810 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4811 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4812 | return asBinder(); |
| 4813 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4814 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4815 | /** |
| 4816 | * Searches array of arguments for the specified string |
| 4817 | * @param args array of argument strings |
| 4818 | * @param value value to search for |
| 4819 | * @return true if the value is contained in the array |
| 4820 | */ |
| 4821 | private static boolean scanArgs(String[] args, String value) { |
| 4822 | if (args != null) { |
| 4823 | for (String arg : args) { |
| 4824 | if (value.equals(arg)) { |
| 4825 | return true; |
| 4826 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4827 | } |
| 4828 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4829 | return false; |
| 4830 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4831 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4832 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4833 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4834 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 4835 | != PackageManager.PERMISSION_GRANTED) { |
| 4836 | fout.println("Permission Denial: can't dump AccountsManager from from pid=" |
| 4837 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 4838 | + " without permission " + android.Manifest.permission.DUMP); |
| 4839 | return; |
| 4840 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4841 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4842 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4843 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4844 | final List<UserInfo> users = getUserManager().getUsers(); |
| 4845 | for (UserInfo user : users) { |
| 4846 | ipw.println("User " + user + ":"); |
| 4847 | ipw.increaseIndent(); |
| 4848 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 4849 | ipw.println(); |
| 4850 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4851 | } |
| 4852 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4853 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4854 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 4855 | String[] args, boolean isCheckinRequest) { |
| 4856 | synchronized (userAccounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4857 | if (isCheckinRequest) { |
| 4858 | // This is a checkin request. *Only* upload the account types and the count of each. |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4859 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4860 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4861 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4862 | Process.SYSTEM_UID, null /* packageName */, false); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4863 | fout.println("Accounts: " + accounts.length); |
| 4864 | for (Account account : accounts) { |
| 4865 | fout.println(" " + account); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4866 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 4867 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4868 | // Add debug information. |
| 4869 | fout.println(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4870 | userAccounts.accountsDb.dumpDebugTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4871 | fout.println(); |
| 4872 | synchronized (mSessions) { |
| 4873 | final long now = SystemClock.elapsedRealtime(); |
| 4874 | fout.println("Active Sessions: " + mSessions.size()); |
| 4875 | for (Session session : mSessions.values()) { |
| 4876 | fout.println(" " + session.toDebugString(now)); |
| 4877 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4878 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4879 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4880 | fout.println(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4881 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4882 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4883 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4884 | } |
| 4885 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4886 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4887 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4888 | long identityToken = clearCallingIdentity(); |
| 4889 | try { |
| 4890 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4891 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 4892 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4893 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4894 | if (intent.getComponent() != null && |
| 4895 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 4896 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4897 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4898 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4899 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4900 | final Integer notificationId = getSigninRequiredNotificationId(accounts, account); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4901 | intent.addCategory(String.valueOf(notificationId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4902 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4903 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4904 | contextForUser.getText(R.string.notification_title).toString(); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 4905 | Notification n = |
| 4906 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4907 | .setWhen(0) |
| 4908 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 4909 | .setColor(contextForUser.getColor( |
| 4910 | com.android.internal.R.color.system_notification_accent_color)) |
| 4911 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 4912 | .setContentText(message) |
| 4913 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 4914 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4915 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4916 | .build(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4917 | installNotification(notificationId, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4918 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4919 | } finally { |
| 4920 | restoreCallingIdentity(identityToken); |
| 4921 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4922 | } |
| 4923 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4924 | private void installNotification(int notificationId, final Notification notification, |
| 4925 | String packageName, int userId) { |
| 4926 | final long token = clearCallingIdentity(); |
| 4927 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4928 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4929 | try { |
| 4930 | notificationManager.enqueueNotificationWithTag(packageName, packageName, null, |
| 4931 | notificationId, notification, new int[1], userId); |
| 4932 | } catch (RemoteException e) { |
| 4933 | /* ignore - local call */ |
| 4934 | } |
| 4935 | } finally { |
| 4936 | Binder.restoreCallingIdentity(token); |
| 4937 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4938 | } |
| 4939 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4940 | private void cancelNotification(int id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4941 | cancelNotification(id, mContext.getPackageName(), user); |
| 4942 | } |
| 4943 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4944 | private void cancelNotification(int id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4945 | long identityToken = clearCallingIdentity(); |
| 4946 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4947 | INotificationManager service = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4948 | service.cancelNotificationWithTag(packageName, null, id, user.getIdentifier()); |
| 4949 | } catch (RemoteException e) { |
| 4950 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4951 | } finally { |
| 4952 | restoreCallingIdentity(identityToken); |
| 4953 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4954 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4955 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4956 | private boolean isPermittedForPackage(String packageName, int userId, String... permissions) { |
| 4957 | final long identity = Binder.clearCallingIdentity(); |
| 4958 | try { |
| 4959 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 4960 | for (String perm : permissions) { |
| 4961 | if (pm.checkPermission(perm, packageName, userId) |
| 4962 | == PackageManager.PERMISSION_GRANTED) { |
| 4963 | return true; |
| 4964 | } |
| 4965 | } |
| 4966 | } catch (RemoteException e) { |
| 4967 | /* ignore - local call */ |
| 4968 | } finally { |
| 4969 | Binder.restoreCallingIdentity(identity); |
| 4970 | } |
| 4971 | return false; |
| 4972 | } |
| 4973 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 4974 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 4975 | for (String perm : permissions) { |
| 4976 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 4977 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4978 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 4979 | } |
| 4980 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 4981 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 4982 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 4983 | return true; |
| 4984 | } |
| 4985 | } |
| 4986 | } |
| 4987 | return false; |
| 4988 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4989 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4990 | private int handleIncomingUser(int userId) { |
| 4991 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4992 | return ActivityManager.getService().handleIncomingUser( |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4993 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 4994 | } catch (RemoteException re) { |
| 4995 | // Shouldn't happen, local. |
| 4996 | } |
| 4997 | return userId; |
| 4998 | } |
| 4999 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 5000 | private boolean isPrivileged(int callingUid) { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5001 | String[] packages; |
| 5002 | long identityToken = Binder.clearCallingIdentity(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5003 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5004 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5005 | } finally { |
| 5006 | Binder.restoreCallingIdentity(identityToken); |
| 5007 | } |
| 5008 | if (packages == null) { |
| 5009 | Log.d(TAG, "No packages for callingUid " + callingUid); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5010 | return false; |
| 5011 | } |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5012 | for (String name : packages) { |
| 5013 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5014 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5015 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 5016 | && (packageInfo.applicationInfo.privateFlags |
| 5017 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5018 | return true; |
| 5019 | } |
| 5020 | } catch (PackageManager.NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5021 | Log.d(TAG, "Package not found " + e.getMessage()); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5022 | return false; |
| 5023 | } |
| 5024 | } |
| 5025 | return false; |
| 5026 | } |
| 5027 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5028 | private boolean permissionIsGranted( |
| 5029 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5030 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 5031 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5032 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 5033 | } |
| 5034 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5035 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5036 | |
| 5037 | if (isPrivileged(callerUid)) { |
| 5038 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5039 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5040 | + callerUid + " privileged"); |
| 5041 | } |
| 5042 | return true; |
| 5043 | } |
| 5044 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 5045 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5046 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5047 | + callerUid + " manages the account"); |
| 5048 | } |
| 5049 | return true; |
| 5050 | } |
| 5051 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5052 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5053 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5054 | + callerUid + " user granted access"); |
| 5055 | } |
| 5056 | return true; |
| 5057 | } |
| 5058 | |
| 5059 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5060 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5061 | } |
| 5062 | |
| 5063 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5064 | } |
| 5065 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5066 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5067 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5068 | if (accountType == null) { |
| 5069 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5070 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5071 | return getTypesVisibleToCaller(callingUid, userId, |
| 5072 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5073 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5076 | // Method checks visibility for applications targeing API level below {@link |
| 5077 | // android.os.Build.VERSION_CODES#O}, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5078 | // 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] | 5079 | private boolean checkGetAccountsPermission(String packageName, int userId) { |
| 5080 | return isPermittedForPackage(packageName, userId, Manifest.permission.GET_ACCOUNTS, |
| 5081 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 5082 | } |
| 5083 | |
| 5084 | private boolean checkReadContactsPermission(String packageName, int userId) { |
| 5085 | return isPermittedForPackage(packageName, userId, Manifest.permission.READ_CONTACTS); |
| 5086 | } |
| 5087 | |
| 5088 | /** |
| 5089 | * Method checks package uid and signature with Authenticator which manages accountType. |
| 5090 | * |
| 5091 | * @return SIGNATURE_CHECK_UID_MATCH for uid match, SIGNATURE_CHECK_MATCH for signature match, |
| 5092 | * SIGNATURE_CHECK_MISMATCH otherwise. |
| 5093 | */ |
| 5094 | private int checkPackageSignature(String accountType, int callingUid, int userId) { |
| 5095 | if (accountType == null) { |
| 5096 | return SIGNATURE_CHECK_MISMATCH; |
| 5097 | } |
| 5098 | |
| 5099 | long identityToken = Binder.clearCallingIdentity(); |
| 5100 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5101 | try { |
| 5102 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5103 | } finally { |
| 5104 | Binder.restoreCallingIdentity(identityToken); |
| 5105 | } |
| 5106 | // Check for signature match with Authenticator. |
| 5107 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5108 | : serviceInfos) { |
| 5109 | if (accountType.equals(serviceInfo.type.type)) { |
| 5110 | if (serviceInfo.uid == callingUid) { |
| 5111 | return SIGNATURE_CHECK_UID_MATCH; |
| 5112 | } |
| 5113 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 5114 | if (sigChk == PackageManager.SIGNATURE_MATCH) { |
| 5115 | return SIGNATURE_CHECK_MATCH; |
| 5116 | } |
| 5117 | } |
| 5118 | } |
| 5119 | return SIGNATURE_CHECK_MISMATCH; |
| 5120 | } |
| 5121 | |
| 5122 | // returns true for applications with the same signature as authenticator. |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5123 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5124 | if (accountType == null) { |
| 5125 | return false; |
| 5126 | } else { |
| 5127 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5128 | } |
| 5129 | } |
| 5130 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5131 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5132 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5133 | return getTypesForCaller(callingUid, userId, true /* isOtherwisePermitted*/); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
Dmitry Dementyev | 2e22cfb | 2017-01-09 18:42:14 +0000 | [diff] [blame] | 5137 | return getTypesForCaller(callingUid, userId, false); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5138 | } |
| 5139 | |
| 5140 | private List<String> getTypesForCaller( |
| 5141 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5142 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5143 | long identityToken = Binder.clearCallingIdentity(); |
| 5144 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5145 | try { |
| 5146 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5147 | } finally { |
| 5148 | Binder.restoreCallingIdentity(identityToken); |
| 5149 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5150 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5151 | serviceInfos) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5152 | if (isOtherwisePermitted || (mPackageManager.checkSignatures(serviceInfo.uid, |
| 5153 | callingUid) == PackageManager.SIGNATURE_MATCH)) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5154 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5155 | } |
| 5156 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5157 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5158 | } |
| 5159 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5160 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5161 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5162 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5163 | if (account.name.equals(accountName)) { |
| 5164 | return true; |
| 5165 | } |
| 5166 | } |
| 5167 | } |
| 5168 | return false; |
| 5169 | } |
| 5170 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5171 | private static void checkManageUsersPermission(String message) { |
| 5172 | if (ActivityManager.checkComponentPermission( |
| 5173 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5174 | != PackageManager.PERMISSION_GRANTED) { |
| 5175 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5176 | } |
| 5177 | } |
| 5178 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5179 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5180 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5181 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5182 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5183 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5184 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5185 | + message); |
| 5186 | } |
| 5187 | } |
| 5188 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5189 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5190 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5191 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5192 | return true; |
| 5193 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5194 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5195 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5196 | long grantsCount; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5197 | if (authTokenType != null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5198 | grantsCount = accounts.accountsDb.findMatchingGrantsCount(callerUid, authTokenType, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5199 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5200 | } else { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5201 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5202 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5203 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5204 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5205 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5206 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5207 | // TODO: Skip this check when running automated tests. Replace this |
| 5208 | // with a more general solution. |
| 5209 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5210 | + authTokenType + " by uid " + callerUid |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5211 | + " but ignoring since device is in test harness."); |
| 5212 | return true; |
| 5213 | } |
| 5214 | return permissionGranted; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5215 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5216 | } |
| 5217 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5218 | private boolean isSystemUid(int callingUid) { |
| 5219 | String[] packages = null; |
| 5220 | long ident = Binder.clearCallingIdentity(); |
| 5221 | try { |
| 5222 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5223 | } finally { |
| 5224 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5225 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5226 | if (packages != null) { |
| 5227 | for (String name : packages) { |
| 5228 | try { |
| 5229 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5230 | if (packageInfo != null |
| 5231 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5232 | != 0) { |
| 5233 | return true; |
| 5234 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5235 | } catch (NameNotFoundException e) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5236 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5237 | } |
| 5238 | } |
| 5239 | } else { |
| 5240 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5241 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5242 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5245 | /** Succeeds if any of the specified permissions are granted. */ |
| 5246 | private void checkReadAccountsPermitted( |
| 5247 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5248 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5249 | int userId, |
| 5250 | String opPackageName) { |
| 5251 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5252 | String msg = String.format( |
| 5253 | "caller uid %s cannot access %s accounts", |
| 5254 | callingUid, |
| 5255 | accountType); |
| 5256 | Log.w(TAG, " " + msg); |
| 5257 | throw new SecurityException(msg); |
| 5258 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5259 | } |
| 5260 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5261 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5262 | // the managing app can always modify accounts |
| 5263 | if (isProfileOwner(callingUid)) { |
| 5264 | return true; |
| 5265 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5266 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5267 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5268 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5269 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5270 | return true; |
| 5271 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5272 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5273 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5274 | // the managing app can always modify accounts |
| 5275 | if (isProfileOwner(callingUid)) { |
| 5276 | return true; |
| 5277 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5278 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5279 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5280 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5281 | if (typesArray == null) { |
| 5282 | return true; |
| 5283 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5284 | for (String forbiddenType : typesArray) { |
| 5285 | if (forbiddenType.equals(accountType)) { |
| 5286 | return false; |
| 5287 | } |
| 5288 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5289 | return true; |
| 5290 | } |
| 5291 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5292 | private boolean isProfileOwner(int uid) { |
| 5293 | final DevicePolicyManagerInternal dpmi = |
| 5294 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5295 | return (dpmi != null) |
| 5296 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5297 | } |
| 5298 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5299 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5300 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5301 | throws RemoteException { |
| 5302 | final int callingUid = getCallingUid(); |
| 5303 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5304 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5305 | throw new SecurityException(); |
| 5306 | } |
| 5307 | |
| 5308 | if (value) { |
| 5309 | grantAppPermission(account, authTokenType, uid); |
| 5310 | } else { |
| 5311 | revokeAppPermission(account, authTokenType, uid); |
| 5312 | } |
| 5313 | } |
| 5314 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5315 | /** |
| 5316 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5317 | * <p> |
| 5318 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5319 | * which is in the system. This means we don't need to protect it with permissions. |
| 5320 | * @hide |
| 5321 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5322 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5323 | if (account == null || authTokenType == null) { |
| 5324 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5325 | return; |
| 5326 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5327 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5328 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5329 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5330 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5331 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5332 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5333 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5334 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5335 | |
| 5336 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5337 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5338 | |
| 5339 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5340 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5341 | : mAppPermissionChangeListeners) { |
| 5342 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5343 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | /** |
| 5347 | * Don't allow callers with the given uid permission to get credentials for |
| 5348 | * account/authTokenType. |
| 5349 | * <p> |
| 5350 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5351 | * which is in the system. This means we don't need to protect it with permissions. |
| 5352 | * @hide |
| 5353 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5354 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5355 | if (account == null || authTokenType == null) { |
| 5356 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5357 | return; |
| 5358 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5359 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5360 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5361 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5362 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5363 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5364 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5365 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5366 | accountId, authTokenType, uid); |
| 5367 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5368 | } |
| 5369 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5370 | accounts.accountsDb.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5371 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5372 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5373 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5374 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5375 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5376 | |
| 5377 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5378 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5379 | : mAppPermissionChangeListeners) { |
| 5380 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5381 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5382 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5383 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5384 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5385 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5386 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5387 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5388 | for (Account curAccount : oldAccountsForType) { |
| 5389 | if (!curAccount.equals(account)) { |
| 5390 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5391 | } |
| 5392 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5393 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5394 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5395 | } else { |
| 5396 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5397 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5398 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5399 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5400 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5401 | accounts.userDataCache.remove(account); |
| 5402 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5403 | accounts.previousNameCache.remove(account); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame^] | 5404 | accounts.visibilityCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | /** |
| 5408 | * 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] | 5409 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5410 | * processes and if you will return this account to apps you should return the result. |
| 5411 | * @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] | 5412 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5413 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5414 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5415 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5416 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5417 | if (accountsForType != null) { |
| 5418 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5419 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5420 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5421 | : UUID.randomUUID().toString(); |
| 5422 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5423 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5424 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5425 | } |
| 5426 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5427 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5428 | private Account[] filterAccounts(UserAccounts accounts, Account[] unfiltered, int callingUid, |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5429 | @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5430 | String visibilityFilterPackage = callingPackage; |
| 5431 | if (visibilityFilterPackage == null) { |
| 5432 | visibilityFilterPackage = getPackageNameForUid(callingUid); |
| 5433 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5434 | Map<Account, Integer> firstPass = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5435 | for (Account account : unfiltered) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5436 | int visibility = resolveAccountVisibility(account, visibilityFilterPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5437 | if ((visibility == AccountManager.VISIBILITY_VISIBLE |
| 5438 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) |
| 5439 | || (includeManagedNotVisible |
| 5440 | && (visibility |
| 5441 | == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE))) { |
| 5442 | firstPass.put(account, visibility); |
| 5443 | } |
| 5444 | } |
| 5445 | Map<Account, Integer> secondPass = |
| 5446 | filterSharedAccounts(accounts, firstPass, callingUid, callingPackage); |
| 5447 | |
| 5448 | Account[] filtered = new Account[secondPass.size()]; |
| 5449 | filtered = secondPass.keySet().toArray(filtered); |
| 5450 | return filtered; |
| 5451 | } |
| 5452 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5453 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5454 | private Map<Account, Integer> filterSharedAccounts(UserAccounts userAccounts, |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5455 | @NonNull Map<Account, Integer> unfiltered, int callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5456 | @Nullable String callingPackage) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5457 | // first part is to filter shared accounts. |
| 5458 | // unfiltered type check is not necessary. |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5459 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5460 | || callingUid == Process.SYSTEM_UID) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5461 | return unfiltered; |
| 5462 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5463 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5464 | if (user != null && user.isRestricted()) { |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5465 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5466 | if (packages == null) { |
| 5467 | packages = new String[] {}; |
| 5468 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5469 | // 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] | 5470 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5471 | // This might be a temporary way to specify a visible list |
| 5472 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5473 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5474 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5475 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5476 | return unfiltered; |
| 5477 | } |
| 5478 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5479 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5480 | if (ArrayUtils.isEmpty(sharedAccounts)) { |
| 5481 | return unfiltered; |
| 5482 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5483 | String requiredAccountType = ""; |
| 5484 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5485 | // If there's an explicit callingPackage specified, check if that package |
| 5486 | // opted in to see restricted accounts. |
| 5487 | if (callingPackage != null) { |
| 5488 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5489 | if (pi != null && pi.restrictedAccountType != null) { |
| 5490 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5491 | } |
| 5492 | } else { |
| 5493 | // Otherwise check if the callingUid has a package that has opted in |
| 5494 | for (String packageName : packages) { |
| 5495 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5496 | if (pi != null && pi.restrictedAccountType != null) { |
| 5497 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5498 | break; |
| 5499 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5500 | } |
| 5501 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5502 | } catch (NameNotFoundException e) { |
| 5503 | Log.d(TAG, "Package not found " + e.getMessage()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5504 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5505 | Map<Account, Integer> filtered = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5506 | for (Map.Entry<Account, Integer> entry : unfiltered.entrySet()) { |
| 5507 | Account account = entry.getKey(); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5508 | if (account.type.equals(requiredAccountType)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5509 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5510 | } else { |
| 5511 | boolean found = false; |
| 5512 | for (Account shared : sharedAccounts) { |
| 5513 | if (shared.equals(account)) { |
| 5514 | found = true; |
| 5515 | break; |
| 5516 | } |
| 5517 | } |
| 5518 | if (!found) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5519 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5520 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5521 | } |
| 5522 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5523 | return filtered; |
| 5524 | } else { |
| 5525 | return unfiltered; |
| 5526 | } |
| 5527 | } |
| 5528 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5529 | /* |
| 5530 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5531 | * that the package is not allowed to access. |
| 5532 | */ |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5533 | @NonNull |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5534 | protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5535 | int callingUid, @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5536 | if (accountType != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5537 | final Account[] accounts = userAccounts.accountCache.get(accountType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5538 | if (accounts == null) { |
| 5539 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5540 | } else { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5541 | return filterAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
| 5542 | callingUid, callingPackage, includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5543 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5544 | } else { |
| 5545 | int totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5546 | for (Account[] accounts : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5547 | totalLength += accounts.length; |
| 5548 | } |
| 5549 | if (totalLength == 0) { |
| 5550 | return EMPTY_ACCOUNT_ARRAY; |
| 5551 | } |
| 5552 | Account[] accounts = new Account[totalLength]; |
| 5553 | totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5554 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5555 | System.arraycopy(accountsOfType, 0, accounts, totalLength, |
| 5556 | accountsOfType.length); |
| 5557 | totalLength += accountsOfType.length; |
| 5558 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5559 | return filterAccounts(userAccounts, accounts, callingUid, callingPackage, |
| 5560 | includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5561 | } |
| 5562 | } |
| 5563 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5564 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5565 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5566 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5567 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5568 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5569 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5570 | } |
| 5571 | if (value == null) { |
| 5572 | userDataForAccount.remove(key); |
| 5573 | } else { |
| 5574 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5575 | } |
| 5576 | } |
| 5577 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5578 | protected String readCachedTokenInternal( |
| 5579 | UserAccounts accounts, |
| 5580 | Account account, |
| 5581 | String tokenType, |
| 5582 | String callingPackage, |
| 5583 | byte[] pkgSigDigest) { |
| 5584 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5585 | return accounts.accountTokenCaches.get( |
| 5586 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5587 | } |
| 5588 | } |
| 5589 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5590 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5591 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5592 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5593 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5594 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5595 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5596 | } |
| 5597 | if (value == null) { |
| 5598 | authTokensForAccount.remove(key); |
| 5599 | } else { |
| 5600 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5601 | } |
| 5602 | } |
| 5603 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5604 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 5605 | String authTokenType) { |
| 5606 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5607 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5608 | if (authTokensForAccount == null) { |
| 5609 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5610 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5611 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5612 | } |
| 5613 | return authTokensForAccount.get(authTokenType); |
| 5614 | } |
| 5615 | } |
| 5616 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5617 | protected String readUserDataInternalLocked( |
| 5618 | UserAccounts accounts, Account account, String key) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5619 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5620 | if (userDataForAccount == null) { |
| 5621 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5622 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5623 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5624 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5625 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5626 | } |
| 5627 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5628 | private Context getContextForUser(UserHandle user) { |
| 5629 | try { |
| 5630 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 5631 | } catch (NameNotFoundException e) { |
| 5632 | // Default to mContext, not finding the package system is running as is unlikely. |
| 5633 | return mContext; |
| 5634 | } |
| 5635 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 5636 | |
| 5637 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 5638 | try { |
| 5639 | response.onResult(result); |
| 5640 | } catch (RemoteException e) { |
| 5641 | // if the caller is dead then there is no one to care about remote |
| 5642 | // exceptions |
| 5643 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5644 | Log.v(TAG, "failure while notifying response", e); |
| 5645 | } |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 5650 | String errorMessage) { |
| 5651 | try { |
| 5652 | response.onError(errorCode, errorMessage); |
| 5653 | } catch (RemoteException e) { |
| 5654 | // if the caller is dead then there is no one to care about remote |
| 5655 | // exceptions |
| 5656 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5657 | Log.v(TAG, "failure while notifying response", e); |
| 5658 | } |
| 5659 | } |
| 5660 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5661 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5662 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5663 | private final Object mLock = new Object(); |
| 5664 | |
| 5665 | @GuardedBy("mLock") |
| 5666 | private AccountManagerBackupHelper mBackupHelper; |
| 5667 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5668 | @Override |
| 5669 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 5670 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 5671 | if (account == null) { |
| 5672 | Slog.w(TAG, "account cannot be null"); |
| 5673 | return; |
| 5674 | } |
| 5675 | if (packageName == null) { |
| 5676 | Slog.w(TAG, "packageName cannot be null"); |
| 5677 | return; |
| 5678 | } |
| 5679 | if (userId < UserHandle.USER_SYSTEM) { |
| 5680 | Slog.w(TAG, "user id must be concrete"); |
| 5681 | return; |
| 5682 | } |
| 5683 | if (callback == null) { |
| 5684 | Slog.w(TAG, "callback cannot be null"); |
| 5685 | return; |
| 5686 | } |
| 5687 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5688 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 5689 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5690 | Bundle result = new Bundle(); |
| 5691 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 5692 | callback.sendResult(result); |
| 5693 | return; |
| 5694 | } |
| 5695 | |
| 5696 | final int uid; |
| 5697 | try { |
| 5698 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 5699 | } catch (NameNotFoundException e) { |
| 5700 | Slog.e(TAG, "Unknown package " + packageName); |
| 5701 | return; |
| 5702 | } |
| 5703 | |
| 5704 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5705 | final UserAccounts userAccounts; |
| 5706 | synchronized (mUsers) { |
| 5707 | userAccounts = mUsers.get(userId); |
| 5708 | } |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 5709 | SystemNotificationChannels.createAccountChannelForPackage(packageName, uid, mContext); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5710 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 5711 | } |
| 5712 | |
| 5713 | @Override |
| 5714 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 5715 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5716 | mAppPermissionChangeListeners.add(listener); |
| 5717 | } |
| 5718 | |
| 5719 | @Override |
| 5720 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 5721 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5722 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5723 | |
| 5724 | @Override |
| 5725 | public byte[] backupAccountAccessPermissions(int userId) { |
| 5726 | synchronized (mLock) { |
| 5727 | if (mBackupHelper == null) { |
| 5728 | mBackupHelper = new AccountManagerBackupHelper( |
| 5729 | AccountManagerService.this, this); |
| 5730 | } |
| 5731 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 5732 | } |
| 5733 | } |
| 5734 | |
| 5735 | @Override |
| 5736 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 5737 | synchronized (mLock) { |
| 5738 | if (mBackupHelper == null) { |
| 5739 | mBackupHelper = new AccountManagerBackupHelper( |
| 5740 | AccountManagerService.this, this); |
| 5741 | } |
| 5742 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 5743 | } |
| 5744 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5745 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5746 | |
| 5747 | @VisibleForTesting |
| 5748 | static class Injector { |
| 5749 | private final Context mContext; |
| 5750 | |
| 5751 | public Injector(Context context) { |
| 5752 | mContext = context; |
| 5753 | } |
| 5754 | |
| 5755 | Looper getMessageHandlerLooper() { |
| 5756 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 5757 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 5758 | serviceThread.start(); |
| 5759 | return serviceThread.getLooper(); |
| 5760 | } |
| 5761 | |
| 5762 | Context getContext() { |
| 5763 | return mContext; |
| 5764 | } |
| 5765 | |
| 5766 | void addLocalService(AccountManagerInternal service) { |
| 5767 | LocalServices.addService(AccountManagerInternal.class, service); |
| 5768 | } |
| 5769 | |
| 5770 | String getDeDatabaseName(int userId) { |
| 5771 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 5772 | AccountsDb.DE_DATABASE_NAME); |
| 5773 | return databaseFile.getPath(); |
| 5774 | } |
| 5775 | |
| 5776 | String getCeDatabaseName(int userId) { |
| 5777 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 5778 | AccountsDb.CE_DATABASE_NAME); |
| 5779 | return databaseFile.getPath(); |
| 5780 | } |
| 5781 | |
| 5782 | String getPreNDatabaseName(int userId) { |
| 5783 | File systemDir = Environment.getDataSystemDirectory(); |
| 5784 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 5785 | PRE_N_DATABASE_NAME); |
| 5786 | if (userId == 0) { |
| 5787 | // Migrate old file, if it exists, to the new location. |
| 5788 | // 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] | 5789 | // accidentally created in the old location, |
| 5790 | // causing the new one to become corrupted as well. |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5791 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 5792 | if (oldFile.exists() && !databaseFile.exists()) { |
| 5793 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 5794 | File userDir = Environment.getUserSystemDirectory(userId); |
| 5795 | if (!userDir.exists()) { |
| 5796 | if (!userDir.mkdirs()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5797 | throw new IllegalStateException( |
| 5798 | "User dir cannot be created: " + userDir); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5799 | } |
| 5800 | } |
| 5801 | if (!oldFile.renameTo(databaseFile)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5802 | throw new IllegalStateException( |
| 5803 | "User dir cannot be migrated: " + databaseFile); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5804 | } |
| 5805 | } |
| 5806 | } |
| 5807 | return databaseFile.getPath(); |
| 5808 | } |
| 5809 | |
| 5810 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 5811 | return new AccountAuthenticatorCache(mContext); |
| 5812 | } |
| 5813 | |
| 5814 | INotificationManager getNotificationManager() { |
| 5815 | return NotificationManager.getService(); |
| 5816 | } |
| 5817 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5818 | } |