Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.accounts; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 18 | |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 20 | import android.accounts.AbstractAccountAuthenticator; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 21 | import android.accounts.Account; |
| 22 | import android.accounts.AccountAndUser; |
| 23 | import android.accounts.AccountAuthenticatorResponse; |
| 24 | import android.accounts.AccountManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 25 | import android.accounts.AccountManagerInternal; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 26 | import android.accounts.AuthenticatorDescription; |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 27 | import android.accounts.CantAddAccountActivity; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 28 | import android.accounts.GrantCredentialsPermissionActivity; |
| 29 | import android.accounts.IAccountAuthenticator; |
| 30 | import android.accounts.IAccountAuthenticatorResponse; |
| 31 | import android.accounts.IAccountManager; |
| 32 | import android.accounts.IAccountManagerResponse; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 33 | import android.annotation.IntRange; |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 34 | import android.annotation.NonNull; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 35 | import android.annotation.Nullable; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 36 | import android.app.ActivityManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 37 | import android.app.ActivityThread; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 38 | import android.app.AppOpsManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 39 | import android.app.INotificationManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 40 | import android.app.Notification; |
| 41 | import android.app.NotificationManager; |
| 42 | import android.app.PendingIntent; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 43 | import android.app.admin.DeviceAdminInfo; |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 44 | import android.app.admin.DevicePolicyManager; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 45 | import android.app.admin.DevicePolicyManagerInternal; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 46 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 47 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 48 | import android.content.Context; |
| 49 | import android.content.Intent; |
| 50 | import android.content.IntentFilter; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 51 | import android.content.IntentSender; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 52 | import android.content.ServiceConnection; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 53 | import android.content.pm.ActivityInfo; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 54 | import android.content.pm.ApplicationInfo; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 55 | import android.content.pm.IPackageManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 56 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 57 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 58 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 59 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 60 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 61 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 62 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 63 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 64 | import android.database.Cursor; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 65 | import android.database.sqlite.SQLiteStatement; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 66 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 67 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 68 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 69 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 70 | import android.os.IBinder; |
| 71 | import android.os.Looper; |
| 72 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 73 | import android.os.Parcel; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 74 | import android.os.Process; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 75 | import android.os.RemoteCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 76 | import android.os.RemoteException; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 77 | import android.os.StrictMode; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 78 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 79 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 80 | import android.os.UserManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 81 | import android.text.TextUtils; |
| 82 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 83 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 84 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 85 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 86 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 87 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 88 | import com.android.internal.R; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 89 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 90 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 91 | import com.android.internal.content.PackageMonitor; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 92 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 93 | import com.android.internal.notification.SystemNotificationChannels; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 94 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 95 | import com.android.internal.util.DumpUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 96 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 97 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 98 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 99 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 100 | import com.android.server.SystemService; |
| 101 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 102 | import com.google.android.collect.Lists; |
| 103 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 104 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 105 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 106 | import java.io.FileDescriptor; |
| 107 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 108 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 109 | import java.security.MessageDigest; |
| 110 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 111 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 112 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 113 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 114 | import java.util.Collection; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 115 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 116 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 117 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 118 | import java.util.LinkedHashMap; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 119 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 120 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 121 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 122 | import java.util.Objects; |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 123 | import java.util.Set; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 124 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 125 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 126 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 127 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 128 | /** |
| 129 | * A system service that provides account, password, and authtoken management for all |
| 130 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 131 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 132 | * instead one uses an instance of {@link AccountManager}, which can be accessed as follows: |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 133 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 134 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 135 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 136 | public class AccountManagerService |
| 137 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 138 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 139 | private static final String TAG = "AccountManagerService"; |
| 140 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 141 | public static class Lifecycle extends SystemService { |
| 142 | private AccountManagerService mService; |
| 143 | |
| 144 | public Lifecycle(Context context) { |
| 145 | super(context); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onStart() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 150 | mService = new AccountManagerService(new Injector(getContext())); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 151 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 152 | } |
| 153 | |
| 154 | @Override |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 155 | public void onUnlockUser(int userHandle) { |
| 156 | mService.onUnlockUser(userHandle); |
| 157 | } |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 158 | |
| 159 | @Override |
| 160 | public void onCleanupUser(int userHandle) { |
| 161 | mService.onCleanupUser(userHandle); |
| 162 | } |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 163 | } |
| 164 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 165 | final Context mContext; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 166 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 167 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 168 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 169 | private UserManager mUserManager; |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 170 | private final Injector mInjector; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 171 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 172 | final MessageHandler mHandler; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 173 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 174 | // Messages that can be sent on mHandler |
| 175 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 176 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 177 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 178 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 179 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 180 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 181 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 182 | private static final int SIGNATURE_CHECK_MISMATCH = 0; |
| 183 | private static final int SIGNATURE_CHECK_MATCH = 1; |
| 184 | private static final int SIGNATURE_CHECK_UID_MATCH = 2; |
| 185 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 186 | static { |
| 187 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
Christopher Tate | bded68f | 2017-02-21 11:41:55 -0800 | [diff] [blame] | 188 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 189 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 190 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 191 | |
| 192 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 193 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 194 | static class UserAccounts { |
| 195 | private final int userId; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 196 | final AccountsDb accountsDb; |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 197 | private final HashMap<Pair<Pair<Account, String>, Integer>, NotificationId> |
| 198 | credentialsPermissionNotificationIds = new HashMap<>(); |
| 199 | private final HashMap<Account, NotificationId> signinRequiredNotificationIds |
| 200 | = new HashMap<>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 201 | final Object cacheLock = new Object(); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 202 | final Object dbLock = new Object(); // if needed, dbLock must be obtained before cacheLock |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 203 | /** protected by the {@link #cacheLock} */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 204 | final HashMap<String, Account[]> accountCache = new LinkedHashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 205 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 206 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 207 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 208 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 209 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 210 | private final TokenCache accountTokenCaches = new TokenCache(); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 211 | /** protected by the {@link #cacheLock} */ |
| 212 | private final Map<Account, Map<String, Integer>> visibilityCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 213 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 214 | /** protected by the {@link #mReceiversForType}, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 215 | * type -> (packageName -> number of active receivers) |
| 216 | * type == null is used to get notifications about all account types |
| 217 | */ |
| 218 | private final Map<String, Map<String, Integer>> mReceiversForType = new HashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 219 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 220 | /** |
| 221 | * protected by the {@link #cacheLock} |
| 222 | * |
| 223 | * Caches the previous names associated with an account. Previous names |
| 224 | * should be cached because we expect that when an Account is renamed, |
| 225 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 226 | * want to know if the accounts they care about have been renamed. |
| 227 | * |
| 228 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 229 | * we can distinguish between those accounts with no previous names and |
| 230 | * those whose previous names haven't been cached (yet). |
| 231 | */ |
| 232 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 233 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 234 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 235 | private int debugDbInsertionPoint = -1; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 236 | private SQLiteStatement statementForLogging; // TODO Move to AccountsDb |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 237 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 238 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 239 | this.userId = userId; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 240 | synchronized (dbLock) { |
| 241 | synchronized (cacheLock) { |
| 242 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
| 243 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 248 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 249 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 250 | // Not thread-safe. Only use in synchronized context |
| 251 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 252 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 253 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 254 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 255 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 256 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 257 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 258 | /** |
| 259 | * This should only be called by system code. One should only call this after the service |
| 260 | * has started. |
| 261 | * @return a reference to the AccountManagerService instance |
| 262 | * @hide |
| 263 | */ |
| 264 | public static AccountManagerService getSingleton() { |
| 265 | return sThis.get(); |
| 266 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 267 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 268 | public AccountManagerService(Injector injector) { |
| 269 | mInjector = injector; |
| 270 | mContext = injector.getContext(); |
| 271 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 272 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 273 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 274 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 275 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 276 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 277 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 278 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 279 | IntentFilter intentFilter = new IntentFilter(); |
| 280 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 281 | intentFilter.addDataScheme("package"); |
| 282 | mContext.registerReceiver(new BroadcastReceiver() { |
| 283 | @Override |
| 284 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 285 | // Don't delete accounts when updating a authenticator's |
| 286 | // package. |
| 287 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 288 | /* Purging data requires file io, don't block the main thread. This is probably |
| 289 | * less than ideal because we are introducing a race condition where old grants |
| 290 | * could be exercised until they are purged. But that race condition existed |
| 291 | * anyway with the broadcast receiver. |
| 292 | * |
| 293 | * Ideally, we would completely clear the cache, purge data from the database, |
| 294 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 295 | * large at this point. |
| 296 | */ |
Dmitry Dementyev | 0b67642 | 2017-03-09 11:51:26 -0800 | [diff] [blame] | 297 | final String removedPackageName = intent.getData().getSchemeSpecificPart(); |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 298 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 299 | @Override |
| 300 | public void run() { |
| 301 | purgeOldGrantsAll(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 302 | // Notify authenticator about removed app? |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 303 | removeVisibilityValuesForPackage(removedPackageName); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 304 | } |
| 305 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 306 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 307 | } |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 308 | } |
| 309 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 310 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 311 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 312 | |
| 313 | // Need to cancel account request notifications if the update/install can access the account |
| 314 | new PackageMonitor() { |
| 315 | @Override |
| 316 | public void onPackageAdded(String packageName, int uid) { |
| 317 | // Called on a handler, and running as the system |
| 318 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 323 | // Called on a handler, and running as the system |
| 324 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 325 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 326 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 327 | |
| 328 | // Cancel account request notification if an app op was preventing the account access |
| 329 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 330 | new AppOpsManager.OnOpChangedInternalListener() { |
| 331 | @Override |
| 332 | public void onOpChanged(int op, String packageName) { |
| 333 | try { |
| 334 | final int userId = ActivityManager.getCurrentUser(); |
| 335 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 336 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 337 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 338 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 339 | final long identity = Binder.clearCallingIdentity(); |
| 340 | try { |
| 341 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 342 | } finally { |
| 343 | Binder.restoreCallingIdentity(identity); |
| 344 | } |
| 345 | } |
| 346 | } catch (NameNotFoundException e) { |
| 347 | /* ignore */ |
| 348 | } |
| 349 | } |
| 350 | }); |
| 351 | |
| 352 | // Cancel account request notification if a permission was preventing the account access |
| 353 | mPackageManager.addOnPermissionsChangeListener( |
| 354 | (int uid) -> { |
| 355 | Account[] accounts = null; |
| 356 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 357 | if (packageNames != null) { |
| 358 | final int userId = UserHandle.getUserId(uid); |
| 359 | final long identity = Binder.clearCallingIdentity(); |
| 360 | try { |
| 361 | for (String packageName : packageNames) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 362 | // if app asked for permission we need to cancel notification even |
| 363 | // for O+ applications. |
| 364 | if (mPackageManager.checkPermission( |
| 365 | Manifest.permission.GET_ACCOUNTS, |
| 366 | packageName) != PackageManager.PERMISSION_GRANTED) { |
| 367 | continue; |
| 368 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 369 | |
| 370 | if (accounts == null) { |
| 371 | accounts = getAccountsAsUser(null, userId, "android"); |
| 372 | if (ArrayUtils.isEmpty(accounts)) { |
| 373 | return; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | for (Account account : accounts) { |
| 378 | cancelAccountAccessRequestNotificationIfNeeded( |
| 379 | account, uid, packageName, true); |
| 380 | } |
| 381 | } |
| 382 | } finally { |
| 383 | Binder.restoreCallingIdentity(identity); |
| 384 | } |
| 385 | } |
| 386 | }); |
| 387 | } |
| 388 | |
| 389 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 390 | boolean checkAccess) { |
| 391 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 392 | for (Account account : accounts) { |
| 393 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 398 | boolean checkAccess) { |
| 399 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 400 | for (Account account : accounts) { |
| 401 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 406 | boolean checkAccess) { |
| 407 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 408 | if (packageNames != null) { |
| 409 | for (String packageName : packageNames) { |
| 410 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 411 | packageName, checkAccess); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 417 | int uid, String packageName, boolean checkAccess) { |
| 418 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 419 | UserHandle.getUserHandleForUid(uid))) { |
| 420 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 421 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 422 | UserHandle.getUserHandleForUid(uid)); |
| 423 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 426 | @Override |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 427 | public boolean addAccountExplicitlyWithVisibility(Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 428 | Bundle extras, Map packageToVisibility) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 429 | Bundle.setDefusable(extras, true); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 430 | int callingUid = Binder.getCallingUid(); |
| 431 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 432 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 433 | Log.v(TAG, "addAccountExplicitly: " + account + ", caller's uid " + callingUid |
| 434 | + ", pid " + Binder.getCallingPid()); |
| 435 | } |
| 436 | Preconditions.checkNotNull(account, "account cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 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(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 463 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 464 | boolean isSystemUid = UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 465 | List<String> managedTypes = getTypesForCaller(callingUid, userId, isSystemUid); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 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 { |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 480 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 481 | return getAccountsAndVisibilityForPackage(packageName, managedTypes, callingUid, |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 482 | accounts); |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 483 | } finally { |
| 484 | restoreCallingIdentity(identityToken); |
| 485 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* |
| 489 | * accountTypes may not be null |
| 490 | */ |
| 491 | private Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 492 | List<String> accountTypes, Integer callingUid, UserAccounts accounts) { |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 493 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 494 | Log.d(TAG, "Package not found " + packageName); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 495 | return new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 498 | Map<Account, Integer> result = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 499 | for (String accountType : accountTypes) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 500 | synchronized (accounts.dbLock) { |
| 501 | synchronized (accounts.cacheLock) { |
| 502 | final Account[] accountsOfType = accounts.accountCache.get(accountType); |
| 503 | if (accountsOfType != null) { |
| 504 | for (Account account : accountsOfType) { |
| 505 | result.put(account, |
| 506 | resolveAccountVisibility(account, packageName, accounts)); |
| 507 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | return filterSharedAccounts(accounts, result, callingUid, packageName); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | @Override |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 516 | public Map<String, Integer> getPackagesAndVisibilityForAccount(Account account) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 517 | Preconditions.checkNotNull(account, "account cannot be null"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 518 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 519 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 520 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
| 521 | && !isSystemUid(callingUid)) { |
| 522 | String msg = |
| 523 | String.format("uid %s cannot get secrets for account %s", callingUid, account); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 524 | throw new SecurityException(msg); |
| 525 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 526 | |
| 527 | long identityToken = clearCallingIdentity(); |
| 528 | try { |
| 529 | UserAccounts accounts = getUserAccounts(userId); |
| 530 | synchronized (accounts.dbLock) { |
| 531 | synchronized (accounts.cacheLock) { |
| 532 | return getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 533 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 534 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 535 | } finally { |
| 536 | restoreCallingIdentity(identityToken); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 537 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 538 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 542 | * Returns Map with all package names and visibility values for given account. |
| 543 | * The method and returned map must be guarded by accounts.cacheLock |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 544 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 545 | * @param account Account to get visibility values. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 546 | * @param accounts UserAccount that currently hosts the account and application |
| 547 | * |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 548 | * @return Map with cache for package names to visibility. |
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 | private @NonNull Map<String, Integer> getPackagesAndVisibilityForAccountLocked(Account account, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 551 | UserAccounts accounts) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 552 | Map<String, Integer> accountVisibility = accounts.visibilityCache.get(account); |
| 553 | if (accountVisibility == null) { |
| 554 | Log.d(TAG, "Visibility was not initialized"); |
| 555 | accountVisibility = new HashMap<>(); |
| 556 | accounts.visibilityCache.put(account, accountVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 557 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 558 | return accountVisibility; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 562 | public int getAccountVisibility(Account account, String packageName) { |
| 563 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 564 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 565 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 566 | int userId = UserHandle.getCallingUserId(); |
| 567 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 568 | && !isSystemUid(callingUid)) { |
| 569 | String msg = String.format( |
| 570 | "uid %s cannot get secrets for accounts of type: %s", |
| 571 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 572 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 573 | throw new SecurityException(msg); |
| 574 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 575 | long identityToken = clearCallingIdentity(); |
| 576 | try { |
| 577 | UserAccounts accounts = getUserAccounts(userId); |
| 578 | return resolveAccountVisibility(account, packageName, accounts); |
| 579 | } finally { |
| 580 | restoreCallingIdentity(identityToken); |
| 581 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 585 | * Method returns visibility for given account and package name. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 586 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 587 | * @param account The account to check visibility. |
| 588 | * @param packageName Package name to check visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 589 | * @param accounts UserAccount that currently hosts the account and application |
| 590 | * |
| 591 | * @return Visibility value, AccountManager.VISIBILITY_UNDEFINED if no value was stored. |
| 592 | * |
| 593 | */ |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 594 | private int getAccountVisibilityFromCache(Account account, String packageName, |
| 595 | UserAccounts accounts) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 596 | synchronized (accounts.cacheLock) { |
| 597 | Map<String, Integer> accountVisibility = |
| 598 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 599 | Integer visibility = accountVisibility.get(packageName); |
| 600 | return visibility != null ? visibility : AccountManager.VISIBILITY_UNDEFINED; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Method which handles default values for Account visibility. |
| 606 | * |
| 607 | * @param account The account to check visibility. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 608 | * @param packageName Package name to check visibility |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 609 | * @param accounts UserAccount that currently hosts the account and application |
| 610 | * |
| 611 | * @return Visibility value, the method never returns AccountManager.VISIBILITY_UNDEFINED |
| 612 | * |
| 613 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 614 | private Integer resolveAccountVisibility(Account account, @NonNull String packageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 615 | UserAccounts accounts) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 616 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 617 | int uid = -1; |
| 618 | try { |
| 619 | long identityToken = clearCallingIdentity(); |
| 620 | try { |
| 621 | uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 622 | } finally { |
| 623 | restoreCallingIdentity(identityToken); |
| 624 | } |
| 625 | } catch (NameNotFoundException e) { |
| 626 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 627 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 628 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 629 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 630 | // System visibility can not be restricted. |
| 631 | if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) { |
| 632 | return AccountManager.VISIBILITY_VISIBLE; |
| 633 | } |
| 634 | |
| 635 | int signatureCheckResult = |
| 636 | checkPackageSignature(account.type, uid, accounts.userId); |
| 637 | |
| 638 | // Authenticator can not restrict visibility to itself. |
| 639 | if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) { |
| 640 | return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account |
| 641 | } |
| 642 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 643 | if (isSpecialPackageKey(packageName)) { |
| 644 | Log.d(TAG, "Package name is forbidden: " + packageName); |
| 645 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
| 646 | } |
| 647 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 648 | // Return stored value if it was set. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 649 | int visibility = getAccountVisibilityFromCache(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 650 | |
| 651 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 652 | return visibility; |
| 653 | } |
| 654 | |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 655 | boolean isPrivileged = isPermittedForPackage(packageName, uid, accounts.userId, |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 656 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 657 | |
| 658 | // Device/Profile owner gets visibility by default. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 659 | if (isProfileOwner(uid)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 660 | return AccountManager.VISIBILITY_VISIBLE; |
| 661 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 662 | |
| 663 | boolean preO = isPreOApplication(packageName); |
| 664 | if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH) |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 665 | || (preO && checkGetAccountsPermission(packageName, uid, accounts.userId)) |
| 666 | || (checkReadContactsPermission(packageName, uid, accounts.userId) |
| 667 | && accountTypeManagesContacts(account.type, accounts.userId)) |
| 668 | || isPrivileged) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 669 | // Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature |
| 670 | // match. |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 671 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 672 | AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 673 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 674 | visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 675 | } |
| 676 | } else { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 677 | visibility = getAccountVisibilityFromCache(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 678 | AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 679 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 680 | visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 681 | } |
| 682 | } |
| 683 | return visibility; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Checks targetSdk for a package; |
| 688 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 689 | * @param packageName Package name |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 690 | * |
| 691 | * @return True if package's target SDK is below {@link android.os.Build.VERSION_CODES#O}, or |
| 692 | * undefined |
| 693 | */ |
| 694 | private boolean isPreOApplication(String packageName) { |
| 695 | try { |
| 696 | long identityToken = clearCallingIdentity(); |
| 697 | ApplicationInfo applicationInfo; |
| 698 | try { |
| 699 | applicationInfo = mPackageManager.getApplicationInfo(packageName, 0); |
| 700 | } finally { |
| 701 | restoreCallingIdentity(identityToken); |
| 702 | } |
| 703 | |
| 704 | if (applicationInfo != null) { |
| 705 | int version = applicationInfo.targetSdkVersion; |
| 706 | return version < android.os.Build.VERSION_CODES.O; |
| 707 | } |
| 708 | return true; |
| 709 | } catch (NameNotFoundException e) { |
| 710 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 711 | return true; |
| 712 | } |
Dmitry Dementyev | 58fa8362 | 2016-12-20 18:08:51 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 716 | public boolean setAccountVisibility(Account account, String packageName, int newVisibility) { |
| 717 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 718 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 719 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 720 | int userId = UserHandle.getCallingUserId(); |
| 721 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 722 | && !isSystemUid(callingUid)) { |
| 723 | String msg = String.format( |
| 724 | "uid %s cannot get secrets for accounts of type: %s", |
| 725 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 726 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 727 | throw new SecurityException(msg); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 728 | } |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 729 | long identityToken = clearCallingIdentity(); |
| 730 | try { |
| 731 | UserAccounts accounts = getUserAccounts(userId); |
| 732 | return setAccountVisibility(account, packageName, newVisibility, true /* notify */, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 733 | accounts); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 734 | } finally { |
| 735 | restoreCallingIdentity(identityToken); |
| 736 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 740 | * Updates visibility for given account name and package. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 741 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 742 | * @param account Account to update visibility. |
| 743 | * @param packageName Package name for which visibility is updated. |
| 744 | * @param newVisibility New visibility calue |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 745 | * @param notify if the flag is set applications will get notification about visibility change |
| 746 | * @param accounts UserAccount that currently hosts the account and application |
| 747 | * |
| 748 | * @return True if account visibility was changed. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 749 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 750 | private boolean setAccountVisibility(Account account, String packageName, int newVisibility, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 751 | boolean notify, UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 752 | synchronized (accounts.dbLock) { |
| 753 | synchronized (accounts.cacheLock) { |
| 754 | Map<String, Integer> packagesToVisibility; |
| 755 | if (notify) { |
| 756 | if (isSpecialPackageKey(packageName)) { |
| 757 | packagesToVisibility = |
| 758 | getRequestingPackages(account, accounts); |
| 759 | } else { |
| 760 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 761 | return false; // package is not installed. |
| 762 | } |
| 763 | packagesToVisibility = new HashMap<>(); |
| 764 | packagesToVisibility.put(packageName, |
| 765 | resolveAccountVisibility(account, packageName, accounts)); |
| 766 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 767 | } else { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 768 | // Notifications will not be send. |
| 769 | if (!isSpecialPackageKey(packageName) && |
| 770 | !packageExistsForUser(packageName, accounts.userId)) { |
| 771 | // package is not installed and not meta value. |
| 772 | return false; |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 773 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 774 | packagesToVisibility = new HashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 775 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 776 | |
| 777 | if (!updateAccountVisibilityLocked(account, packageName, newVisibility, accounts)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 778 | return false; |
| 779 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 780 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 781 | if (notify) { |
| 782 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 783 | .entrySet()) { |
| 784 | if (packageToVisibility.getValue() |
| 785 | != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 786 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 787 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 788 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 789 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 790 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 791 | return true; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 792 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 793 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 796 | // Update account visibility in cache and database. |
| 797 | private boolean updateAccountVisibilityLocked(Account account, String packageName, |
| 798 | int newVisibility, UserAccounts accounts) { |
| 799 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 800 | if (accountId < 0) { |
| 801 | return false; |
| 802 | } |
| 803 | |
| 804 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
| 805 | try { |
| 806 | if (!accounts.accountsDb.setAccountVisibility(accountId, packageName, |
| 807 | newVisibility)) { |
| 808 | return false; |
| 809 | } |
| 810 | } finally { |
| 811 | StrictMode.setThreadPolicy(oldPolicy); |
| 812 | } |
| 813 | Map<String, Integer> accountVisibility = |
| 814 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 815 | accountVisibility.put(packageName, newVisibility); |
| 816 | return true; |
| 817 | } |
| 818 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 819 | @Override |
| 820 | public void registerAccountListener(String[] accountTypes, String opPackageName) { |
| 821 | int callingUid = Binder.getCallingUid(); |
| 822 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 823 | |
| 824 | int userId = UserHandle.getCallingUserId(); |
| 825 | long identityToken = clearCallingIdentity(); |
| 826 | try { |
| 827 | UserAccounts accounts = getUserAccounts(userId); |
| 828 | registerAccountListener(accountTypes, opPackageName, accounts); |
| 829 | } finally { |
| 830 | restoreCallingIdentity(identityToken); |
| 831 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 832 | } |
| 833 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 834 | private void registerAccountListener(String[] accountTypes, String opPackageName, |
| 835 | UserAccounts accounts) { |
| 836 | synchronized (accounts.mReceiversForType) { |
| 837 | if (accountTypes == null) { |
| 838 | // null for any type |
| 839 | accountTypes = new String[] {null}; |
| 840 | } |
| 841 | for (String type : accountTypes) { |
| 842 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 843 | if (receivers == null) { |
| 844 | receivers = new HashMap<>(); |
| 845 | accounts.mReceiversForType.put(type, receivers); |
| 846 | } |
| 847 | Integer cnt = receivers.get(opPackageName); |
| 848 | receivers.put(opPackageName, cnt != null ? cnt + 1 : 1); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 853 | @Override |
| 854 | public void unregisterAccountListener(String[] accountTypes, String opPackageName) { |
| 855 | int callingUid = Binder.getCallingUid(); |
| 856 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 857 | int userId = UserHandle.getCallingUserId(); |
| 858 | long identityToken = clearCallingIdentity(); |
| 859 | try { |
| 860 | UserAccounts accounts = getUserAccounts(userId); |
| 861 | unregisterAccountListener(accountTypes, opPackageName, accounts); |
| 862 | } finally { |
| 863 | restoreCallingIdentity(identityToken); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | private void unregisterAccountListener(String[] accountTypes, String opPackageName, |
| 868 | UserAccounts accounts) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 869 | synchronized (accounts.mReceiversForType) { |
| 870 | if (accountTypes == null) { |
| 871 | // null for any type |
| 872 | accountTypes = new String[] {null}; |
| 873 | } |
| 874 | for (String type : accountTypes) { |
| 875 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 876 | if (receivers == null || receivers.get(opPackageName) == null) { |
| 877 | throw new IllegalArgumentException("attempt to unregister wrong receiver"); |
| 878 | } |
| 879 | Integer cnt = receivers.get(opPackageName); |
| 880 | if (cnt == 1) { |
| 881 | receivers.remove(opPackageName); |
| 882 | } else { |
| 883 | receivers.put(opPackageName, cnt - 1); |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | // Send notification to all packages which can potentially see the account |
| 890 | private void sendNotificationAccountUpdated(Account account, UserAccounts accounts) { |
| 891 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, accounts); |
| 892 | // packages with VISIBILITY_USER_MANAGED_NOT_VISIBL still get notification. |
| 893 | // Should we notify VISIBILITY_NOT_VISIBLE packages when account is added? |
| 894 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
| 895 | if (packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 896 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Sends a direct intent to a package, notifying it of account visibility change. |
| 903 | * |
| 904 | * @param packageName to send Account to |
| 905 | * @param accounts UserAccount that currently hosts the account |
| 906 | */ |
| 907 | private void notifyPackage(String packageName, UserAccounts accounts) { |
| 908 | Intent intent = new Intent(AccountManager.ACTION_VISIBLE_ACCOUNTS_CHANGED); |
| 909 | intent.setPackage(packageName); |
| 910 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 911 | mContext.sendBroadcastAsUser(intent, new UserHandle(accounts.userId)); |
| 912 | } |
| 913 | |
| 914 | // Returns a map from package name to visibility, for packages subscribed |
| 915 | // to notifications about any account type, or type of provided account |
| 916 | // account type or all types. |
| 917 | private Map<String, Integer> getRequestingPackages(Account account, UserAccounts accounts) { |
| 918 | Set<String> packages = new HashSet<>(); |
| 919 | synchronized (accounts.mReceiversForType) { |
| 920 | for (String type : new String[] {account.type, null}) { |
| 921 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 922 | if (receivers != null) { |
| 923 | packages.addAll(receivers.keySet()); |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | Map<String, Integer> result = new HashMap<>(); |
| 928 | for (String packageName : packages) { |
| 929 | result.put(packageName, resolveAccountVisibility(account, packageName, accounts)); |
| 930 | } |
| 931 | return result; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 934 | private boolean packageExistsForUser(String packageName, int userId) { |
| 935 | try { |
| 936 | long identityToken = clearCallingIdentity(); |
| 937 | try { |
| 938 | mPackageManager.getPackageUidAsUser(packageName, userId); |
Dmitry Dementyev | 5c80deb | 2017-04-04 11:12:42 -0700 | [diff] [blame] | 939 | return true; |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 940 | } finally { |
| 941 | restoreCallingIdentity(identityToken); |
| 942 | } |
| 943 | } catch (NameNotFoundException e) { |
| 944 | return false; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * Returns true if packageName is one of special values. |
| 950 | */ |
| 951 | private boolean isSpecialPackageKey(String packageName) { |
| 952 | return (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName) |
| 953 | || AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)); |
| 954 | } |
| 955 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 956 | private void sendAccountsChangedBroadcast(int userId) { |
| 957 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 958 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
| 959 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame^] | 962 | private void sendAccountRemovedBroadcast(int userId) { |
| 963 | Intent intent = new Intent(AccountManager.ACTION_ACCOUNT_REMOVED); |
| 964 | intent.setFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
| 965 | mContext.sendBroadcastAsUser(intent, new UserHandle(userId)); |
| 966 | } |
| 967 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 968 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 969 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 970 | throws RemoteException { |
| 971 | try { |
| 972 | return super.onTransact(code, data, reply, flags); |
| 973 | } catch (RuntimeException e) { |
| 974 | // The account manager only throws security exceptions, so let's |
| 975 | // log all others. |
| 976 | if (!(e instanceof SecurityException)) { |
| 977 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 978 | } |
| 979 | throw e; |
| 980 | } |
| 981 | } |
| 982 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 983 | private UserManager getUserManager() { |
| 984 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 985 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 986 | } |
| 987 | return mUserManager; |
| 988 | } |
| 989 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 990 | /** |
| 991 | * Validate internal set of accounts against installed authenticators for |
| 992 | * given user. Clears cached authenticators before validating. |
| 993 | */ |
| 994 | public void validateAccounts(int userId) { |
| 995 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 996 | // Invalidate user-specific cache to make sure we catch any |
| 997 | // removed authenticators. |
| 998 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * Validate internal set of accounts against installed authenticators for |
| 1003 | * given user. Clear cached authenticators before validating when requested. |
| 1004 | */ |
| 1005 | private void validateAccountsInternal( |
| 1006 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1007 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 1008 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1009 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1010 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1011 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1012 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1013 | if (invalidateAuthenticatorCache) { |
| 1014 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 1015 | } |
| 1016 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1017 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 1018 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1019 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1020 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1021 | synchronized (accounts.dbLock) { |
| 1022 | synchronized (accounts.cacheLock) { |
| 1023 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1024 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1025 | // Get a map of stored authenticator types to UID |
| 1026 | final AccountsDb accountsDb = accounts.accountsDb; |
| 1027 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
| 1028 | // Create a list of authenticator type whose previous uid no longer exists |
| 1029 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
| 1030 | SparseBooleanArray knownUids = null; |
| 1031 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 1032 | String type = authToUidEntry.getKey(); |
| 1033 | int uid = authToUidEntry.getValue(); |
| 1034 | Integer knownUid = knownAuth.get(type); |
| 1035 | if (knownUid != null && uid == knownUid) { |
| 1036 | // Remove it from the knownAuth list if it's unchanged. |
| 1037 | knownAuth.remove(type); |
| 1038 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1039 | /* |
| 1040 | * The authenticator is presently not cached and should only be triggered |
| 1041 | * when we think an authenticator has been removed (or is being updated). |
| 1042 | * But we still want to check if any data with the associated uid is |
| 1043 | * around. This is an (imperfect) signal that the package may be updating. |
| 1044 | * |
| 1045 | * A side effect of this is that an authenticator sharing a uid with |
| 1046 | * multiple apps won't get its credentials wiped as long as some app with |
| 1047 | * that uid is still on the device. But I suspect that this is a rare case. |
| 1048 | * And it isn't clear to me how an attacker could really exploit that |
| 1049 | * feature. |
| 1050 | * |
| 1051 | * The upshot is that we don't have to worry about accounts getting |
| 1052 | * uninstalled while the authenticator's package is being updated. |
| 1053 | * |
| 1054 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1055 | if (knownUids == null) { |
| 1056 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
| 1057 | } |
| 1058 | if (!knownUids.get(uid)) { |
| 1059 | // The authenticator is not presently available to the cache. And the |
| 1060 | // package no longer has a data directory (so we surmise it isn't |
| 1061 | // updating). So purge its data from the account databases. |
| 1062 | obsoleteAuthType.add(type); |
| 1063 | // And delete it from the TABLE_META |
| 1064 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
| 1065 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1066 | } |
| 1067 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1068 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1069 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
| 1070 | // been re-enabled (after being updated for example), then we just overwrite the old |
| 1071 | // values. |
| 1072 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
| 1073 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
| 1074 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1075 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1076 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
| 1077 | try { |
| 1078 | accounts.accountCache.clear(); |
| 1079 | final HashMap<String, ArrayList<String>> accountNamesByType |
| 1080 | = new LinkedHashMap<>(); |
| 1081 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1082 | final long accountId = accountEntry.getKey(); |
| 1083 | final Account account = accountEntry.getValue(); |
| 1084 | if (obsoleteAuthType.contains(account.type)) { |
| 1085 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 1086 | + account.type |
| 1087 | + "'s registered authenticator no longer exist."); |
| 1088 | Map<String, Integer> packagesToVisibility = |
| 1089 | getRequestingPackages(account, accounts); |
| 1090 | accountsDb.beginTransaction(); |
| 1091 | try { |
| 1092 | accountsDb.deleteDeAccount(accountId); |
| 1093 | // Also delete from CE table if user is unlocked; if user is |
| 1094 | // currently locked the account will be removed later by |
| 1095 | // syncDeCeAccountsLocked |
| 1096 | if (userUnlocked) { |
| 1097 | accountsDb.deleteCeAccount(accountId); |
| 1098 | } |
| 1099 | accountsDb.setTransactionSuccessful(); |
| 1100 | } finally { |
| 1101 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1102 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1103 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1104 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1105 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1106 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1107 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1108 | accounts.userDataCache.remove(account); |
| 1109 | accounts.authTokenCache.remove(account); |
| 1110 | accounts.accountTokenCaches.remove(account); |
| 1111 | accounts.visibilityCache.remove(account); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1112 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1113 | for (Entry<String, Integer> packageToVisibility : |
| 1114 | packagesToVisibility.entrySet()) { |
| 1115 | if (packageToVisibility.getValue() |
| 1116 | != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 1117 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 1118 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1119 | } |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame^] | 1120 | sendAccountRemovedBroadcast(accounts.userId); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1121 | } else { |
| 1122 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
| 1123 | if (accountNames == null) { |
| 1124 | accountNames = new ArrayList<>(); |
| 1125 | accountNamesByType.put(account.type, accountNames); |
| 1126 | } |
| 1127 | accountNames.add(account.name); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1128 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1129 | } |
| 1130 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
| 1131 | final String accountType = cur.getKey(); |
| 1132 | final ArrayList<String> accountNames = cur.getValue(); |
| 1133 | final Account[] accountsForType = new Account[accountNames.size()]; |
| 1134 | for (int i = 0; i < accountsForType.length; i++) { |
| 1135 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
| 1136 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1137 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1138 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1139 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1140 | accounts.visibilityCache.putAll(accountsDb.findAllVisibilityValues()); |
| 1141 | } finally { |
| 1142 | if (accountDeleted) { |
| 1143 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1144 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1145 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1146 | } |
| 1147 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1150 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1151 | // Get the UIDs of all apps that might have data on the device. We want |
| 1152 | // to preserve user data if the app might otherwise be storing data. |
| 1153 | List<PackageInfo> pkgsWithData = |
| 1154 | mPackageManager.getInstalledPackagesAsUser( |
| 1155 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1156 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1157 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1158 | if (pkgInfo.applicationInfo != null |
| 1159 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1160 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1161 | } |
| 1162 | } |
| 1163 | return knownUids; |
| 1164 | } |
| 1165 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1166 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1167 | Context context, |
| 1168 | int userId) { |
| 1169 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1170 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1171 | } |
| 1172 | |
| 1173 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1174 | IAccountAuthenticatorCache authCache, |
| 1175 | int userId) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 1176 | HashMap<String, Integer> knownAuth = new LinkedHashMap<>(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1177 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1178 | .getAllServices(userId)) { |
| 1179 | knownAuth.put(service.type.type, service.uid); |
| 1180 | } |
| 1181 | return knownAuth; |
| 1182 | } |
| 1183 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1184 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1185 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | protected UserAccounts getUserAccounts(int userId) { |
| 1189 | synchronized (mUsers) { |
| 1190 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1191 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1192 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1193 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1194 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1195 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1196 | initializeDebugDbSizeAndCompileSqlStatementForLogging(accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1197 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1198 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1199 | validateAccounts = true; |
| 1200 | } |
| 1201 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1202 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1203 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1204 | synchronized (accounts.dbLock) { |
| 1205 | synchronized (accounts.cacheLock) { |
| 1206 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
| 1207 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
| 1208 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1209 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1210 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1211 | } |
| 1212 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1213 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1214 | } |
| 1215 | return accounts; |
| 1216 | } |
| 1217 | } |
| 1218 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1219 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1220 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1221 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1222 | if (!accountsToRemove.isEmpty()) { |
| 1223 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1224 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1225 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1226 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1227 | |
| 1228 | for (Account account : accountsToRemove) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1229 | removeAccountInternal(accounts, account, Process.SYSTEM_UID); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1234 | private void purgeOldGrantsAll() { |
| 1235 | synchronized (mUsers) { |
| 1236 | for (int i = 0; i < mUsers.size(); i++) { |
| 1237 | purgeOldGrants(mUsers.valueAt(i)); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | private void purgeOldGrants(UserAccounts accounts) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1243 | synchronized (accounts.dbLock) { |
| 1244 | synchronized (accounts.cacheLock) { |
| 1245 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
| 1246 | for (int uid : uids) { |
| 1247 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1248 | if (packageExists) { |
| 1249 | continue; |
| 1250 | } |
| 1251 | Log.d(TAG, "deleting grants for UID " + uid |
| 1252 | + " because its package is no longer installed"); |
| 1253 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1254 | } |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1259 | private void removeVisibilityValuesForPackage(String packageName) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1260 | if (isSpecialPackageKey(packageName)) { |
| 1261 | return; |
| 1262 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1263 | synchronized (mUsers) { |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1264 | int numberOfUsers = mUsers.size(); |
| 1265 | for (int i = 0; i < numberOfUsers; i++) { |
| 1266 | UserAccounts accounts = mUsers.valueAt(i); |
| 1267 | try { |
| 1268 | mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 1269 | } catch (NameNotFoundException e) { |
| 1270 | // package does not exist - remove visibility values |
| 1271 | accounts.accountsDb.deleteAccountVisibilityForPackage(packageName); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1272 | synchronized (accounts.dbLock) { |
| 1273 | synchronized (accounts.cacheLock) { |
| 1274 | for (Account account : accounts.visibilityCache.keySet()) { |
| 1275 | Map<String, Integer> accountVisibility = |
| 1276 | getPackagesAndVisibilityForAccountLocked(account, accounts); |
| 1277 | accountVisibility.remove(packageName); |
| 1278 | } |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1279 | } |
| 1280 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 1286 | |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 1287 | private void onCleanupUser(int userId) { |
| 1288 | Log.i(TAG, "onCleanupUser " + userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1289 | UserAccounts accounts; |
| 1290 | synchronized (mUsers) { |
| 1291 | accounts = mUsers.get(userId); |
| 1292 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1293 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1294 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1295 | if (accounts != null) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1296 | synchronized (accounts.dbLock) { |
| 1297 | synchronized (accounts.cacheLock) { |
| 1298 | accounts.accountsDb.close(); |
| 1299 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1300 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1301 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1304 | @VisibleForTesting |
| 1305 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1306 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1307 | } |
| 1308 | |
| 1309 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1310 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1311 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1312 | } |
| 1313 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1314 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1315 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1316 | if (userId < 1) return; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1317 | syncSharedAccounts(userId); |
| 1318 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1319 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1320 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1321 | // Check if there's a shared account that needs to be created as an account |
| 1322 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1323 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1324 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1325 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1326 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1327 | : UserHandle.USER_SYSTEM; |
| 1328 | if (parentUserId < 0) { |
| 1329 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1330 | return; |
| 1331 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1332 | for (Account sa : sharedAccounts) { |
| 1333 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1334 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1335 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1339 | @Override |
| 1340 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1341 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1344 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1345 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1346 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1347 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1348 | Log.v(TAG, "getPassword: " + account |
| 1349 | + ", caller's uid " + Binder.getCallingUid() |
| 1350 | + ", pid " + Binder.getCallingPid()); |
| 1351 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1352 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1353 | int userId = UserHandle.getCallingUserId(); |
| 1354 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1355 | String msg = String.format( |
| 1356 | "uid %s cannot get secrets for accounts of type: %s", |
| 1357 | callingUid, |
| 1358 | account.type); |
| 1359 | throw new SecurityException(msg); |
| 1360 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1361 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1362 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1363 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1364 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1365 | } finally { |
| 1366 | restoreCallingIdentity(identityToken); |
| 1367 | } |
| 1368 | } |
| 1369 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1370 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1371 | if (account == null) { |
| 1372 | return null; |
| 1373 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1374 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1375 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1376 | return null; |
| 1377 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1378 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1379 | synchronized (accounts.dbLock) { |
| 1380 | synchronized (accounts.cacheLock) { |
| 1381 | return accounts.accountsDb |
| 1382 | .findAccountPasswordByNameAndType(account.name, account.type); |
| 1383 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1387 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1388 | public String getPreviousName(Account account) { |
| 1389 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1390 | Log.v(TAG, "getPreviousName: " + account |
| 1391 | + ", caller's uid " + Binder.getCallingUid() |
| 1392 | + ", pid " + Binder.getCallingPid()); |
| 1393 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1394 | Preconditions.checkNotNull(account, "account cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1395 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1396 | long identityToken = clearCallingIdentity(); |
| 1397 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1398 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1399 | return readPreviousNameInternal(accounts, account); |
| 1400 | } finally { |
| 1401 | restoreCallingIdentity(identityToken); |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1406 | if (account == null) { |
| 1407 | return null; |
| 1408 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1409 | synchronized (accounts.dbLock) { |
| 1410 | synchronized (accounts.cacheLock) { |
| 1411 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1412 | if (previousNameRef == null) { |
| 1413 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
| 1414 | previousNameRef = new AtomicReference<>(previousName); |
| 1415 | accounts.previousNameCache.put(account, previousNameRef); |
| 1416 | return previousName; |
| 1417 | } else { |
| 1418 | return previousNameRef.get(); |
| 1419 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1425 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1426 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1427 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1428 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1429 | account, key, callingUid, Binder.getCallingPid()); |
| 1430 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1431 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1432 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 1433 | Preconditions.checkNotNull(key, "key cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1434 | int userId = UserHandle.getCallingUserId(); |
| 1435 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1436 | String msg = String.format( |
| 1437 | "uid %s cannot get user data for accounts of type: %s", |
| 1438 | callingUid, |
| 1439 | account.type); |
| 1440 | throw new SecurityException(msg); |
| 1441 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1442 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1443 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1444 | return null; |
| 1445 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1446 | long identityToken = clearCallingIdentity(); |
| 1447 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1448 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1449 | if (!accountExistsCache(accounts, account)) { |
| 1450 | return null; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1451 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 1452 | return readUserDataInternal(accounts, account, key); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1453 | } finally { |
| 1454 | restoreCallingIdentity(identityToken); |
| 1455 | } |
| 1456 | } |
| 1457 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1458 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1459 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1460 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1461 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1462 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1463 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1464 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1465 | + ", pid " + Binder.getCallingPid()); |
| 1466 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1467 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1468 | if (isCrossUser(callingUid, userId)) { |
| 1469 | throw new SecurityException( |
| 1470 | String.format( |
| 1471 | "User %s tying to get authenticator types for %s" , |
| 1472 | UserHandle.getCallingUserId(), |
| 1473 | userId)); |
| 1474 | } |
| 1475 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1476 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1477 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1478 | return getAuthenticatorTypesInternal(userId); |
| 1479 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1480 | } finally { |
| 1481 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1482 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1485 | /** |
| 1486 | * Should only be called inside of a clearCallingIdentity block. |
| 1487 | */ |
| 1488 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1489 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1490 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1491 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1492 | AuthenticatorDescription[] types = |
| 1493 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1494 | int i = 0; |
| 1495 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1496 | : authenticatorCollection) { |
| 1497 | types[i] = authenticator.type; |
| 1498 | i++; |
| 1499 | } |
| 1500 | return types; |
| 1501 | } |
| 1502 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1503 | private boolean isCrossUser(int callingUid, int userId) { |
| 1504 | return (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1505 | && callingUid != Process.SYSTEM_UID |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1506 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1507 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1508 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1509 | } |
| 1510 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1511 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1512 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1513 | return addAccountExplicitlyWithVisibility(account, password, extras, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1516 | @Override |
| 1517 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1518 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1519 | int callingUid = Binder.getCallingUid(); |
| 1520 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1521 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1522 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1523 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1524 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1525 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1526 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1527 | if (response != null) { |
| 1528 | Bundle result = new Bundle(); |
| 1529 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1530 | try { |
| 1531 | response.onResult(result); |
| 1532 | } catch (RemoteException e) { |
| 1533 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1534 | } |
| 1535 | } |
| 1536 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1537 | } |
| 1538 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1539 | Slog.d(TAG, "Copying account " + account.name |
| 1540 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1541 | long identityToken = clearCallingIdentity(); |
| 1542 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1543 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1544 | false /* stripAuthTokenFromResult */, account.name, |
| 1545 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1546 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1547 | protected String toDebugString(long now) { |
| 1548 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1549 | + ", " + account.type; |
| 1550 | } |
| 1551 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1552 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1553 | public void run() throws RemoteException { |
| 1554 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1555 | } |
| 1556 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1557 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1558 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1559 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1560 | if (result != null |
| 1561 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1562 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1563 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1564 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1565 | super.onResult(result); |
| 1566 | } |
| 1567 | } |
| 1568 | }.bind(); |
| 1569 | } finally { |
| 1570 | restoreCallingIdentity(identityToken); |
| 1571 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1572 | } |
| 1573 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1574 | @Override |
| 1575 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1576 | final int callingUid = Binder.getCallingUid(); |
| 1577 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1578 | String msg = String.format( |
| 1579 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1580 | account, |
| 1581 | callingUid); |
| 1582 | Log.v(TAG, msg); |
| 1583 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1584 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1585 | int userId = UserHandle.getCallingUserId(); |
| 1586 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1587 | String msg = String.format( |
| 1588 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1589 | callingUid, |
| 1590 | account.type); |
| 1591 | throw new SecurityException(msg); |
| 1592 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1593 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1594 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1595 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1596 | return false; |
| 1597 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1598 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1599 | long identityToken = clearCallingIdentity(); |
| 1600 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1601 | UserAccounts accounts = getUserAccounts(userId); |
| 1602 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1603 | } finally { |
| 1604 | restoreCallingIdentity(identityToken); |
| 1605 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1609 | final UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1610 | synchronized (accounts.dbLock) { |
| 1611 | synchronized (accounts.cacheLock) { |
| 1612 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
| 1613 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1614 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1617 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1618 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1619 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1620 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1621 | long id = clearCallingIdentity(); |
| 1622 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1623 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1624 | false /* stripAuthTokenFromResult */, account.name, |
| 1625 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1626 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1627 | protected String toDebugString(long now) { |
| 1628 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1629 | + ", " + account.type; |
| 1630 | } |
| 1631 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1632 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1633 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1634 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 16bedd4 | 2017-03-30 10:00:49 -0700 | [diff] [blame] | 1635 | for (Account acc : getAccounts(parentUserId, mContext.getOpPackageName())) { |
| 1636 | if (acc.equals(account)) { |
| 1637 | mAuthenticator.addAccountFromCredentials( |
| 1638 | this, account, accountCredentials); |
| 1639 | break; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1640 | } |
| 1641 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1642 | } |
| 1643 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1644 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1645 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1646 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1647 | // TODO: Anything to do if if succedded? |
| 1648 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1649 | // account to avoid retries? |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1650 | // TODO: what we do with the visibility? |
| 1651 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1652 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1655 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1656 | public void onError(int errorCode, String errorMessage) { |
| 1657 | super.onError(errorCode, errorMessage); |
| 1658 | // TODO: Show error notification to user |
| 1659 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1660 | } |
| 1661 | |
| 1662 | }.bind(); |
| 1663 | } finally { |
| 1664 | restoreCallingIdentity(id); |
| 1665 | } |
| 1666 | } |
| 1667 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1668 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1669 | Bundle extras, int callingUid, Map<String, Integer> packageToVisibility) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1670 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1671 | if (account == null) { |
| 1672 | return false; |
| 1673 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1674 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1675 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1676 | + " is locked. callingUid=" + callingUid); |
| 1677 | return false; |
| 1678 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1679 | synchronized (accounts.dbLock) { |
| 1680 | synchronized (accounts.cacheLock) { |
| 1681 | accounts.accountsDb.beginTransaction(); |
| 1682 | try { |
| 1683 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
| 1684 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1685 | + ", skipping since the account already exists"); |
| 1686 | return false; |
| 1687 | } |
| 1688 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
| 1689 | if (accountId < 0) { |
| 1690 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1691 | + ", skipping the DB insert failed"); |
| 1692 | return false; |
| 1693 | } |
| 1694 | // Insert into DE table |
| 1695 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
| 1696 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1697 | + ", skipping the DB insert failed"); |
| 1698 | return false; |
| 1699 | } |
| 1700 | if (extras != null) { |
| 1701 | for (String key : extras.keySet()) { |
| 1702 | final String value = extras.getString(key); |
| 1703 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
| 1704 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1705 | + ", skipping since insertExtra failed for key " + key); |
| 1706 | return false; |
| 1707 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1708 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1709 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1710 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1711 | if (packageToVisibility != null) { |
| 1712 | for (Entry<String, Integer> entry : packageToVisibility.entrySet()) { |
| 1713 | setAccountVisibility(account, entry.getKey() /* package */, |
| 1714 | entry.getValue() /* visibility */, false /* notify */, |
| 1715 | accounts); |
| 1716 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1717 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1718 | accounts.accountsDb.setTransactionSuccessful(); |
| 1719 | |
| 1720 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 1721 | accountId, |
| 1722 | accounts, callingUid); |
| 1723 | |
| 1724 | insertAccountIntoCacheLocked(accounts, account); |
| 1725 | } finally { |
| 1726 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1727 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1728 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1729 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1730 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1731 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1732 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1733 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1734 | sendNotificationAccountUpdated(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1735 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1736 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1737 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1738 | return true; |
| 1739 | } |
| 1740 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1741 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1742 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1743 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1744 | } |
| 1745 | } |
| 1746 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1747 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1748 | * 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] | 1749 | * running, then clone the account too. |
| 1750 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1751 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1752 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1753 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1754 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1755 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1756 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1757 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1758 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1759 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1760 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1761 | } |
| 1762 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1763 | } |
| 1764 | } |
| 1765 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1766 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1767 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1768 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1769 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1770 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1771 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1772 | Log.v(TAG, "hasFeatures: " + account |
| 1773 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1774 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1775 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1776 | + ", pid " + Binder.getCallingPid()); |
| 1777 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1778 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1779 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1780 | Preconditions.checkArgument(features != null, "features cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1781 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1782 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1783 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1784 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1785 | long identityToken = clearCallingIdentity(); |
| 1786 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1787 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1788 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1789 | } finally { |
| 1790 | restoreCallingIdentity(identityToken); |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | private class TestFeaturesSession extends Session { |
| 1795 | private final String[] mFeatures; |
| 1796 | private final Account mAccount; |
| 1797 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1798 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1799 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1800 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1801 | true /* stripAuthTokenFromResult */, account.name, |
| 1802 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1803 | mFeatures = features; |
| 1804 | mAccount = account; |
| 1805 | } |
| 1806 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1807 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1808 | public void run() throws RemoteException { |
| 1809 | try { |
| 1810 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1811 | } catch (RemoteException e) { |
| 1812 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1813 | } |
| 1814 | } |
| 1815 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1816 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1817 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1818 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1819 | IAccountManagerResponse response = getResponseAndClose(); |
| 1820 | if (response != null) { |
| 1821 | try { |
| 1822 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1823 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1824 | return; |
| 1825 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1826 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1827 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1828 | + response); |
| 1829 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1830 | final Bundle newResult = new Bundle(); |
| 1831 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1832 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1833 | response.onResult(newResult); |
| 1834 | } catch (RemoteException e) { |
| 1835 | // if the caller is dead then there is no one to care about remote exceptions |
| 1836 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1837 | Log.v(TAG, "failure while notifying response", e); |
| 1838 | } |
| 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1843 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1844 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1845 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1846 | + ", " + mAccount |
| 1847 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1848 | } |
| 1849 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1850 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1851 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1852 | public void renameAccount( |
| 1853 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1854 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1855 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1856 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1857 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1858 | + ", pid " + Binder.getCallingPid()); |
| 1859 | } |
| 1860 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1861 | int userId = UserHandle.getCallingUserId(); |
| 1862 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1863 | String msg = String.format( |
| 1864 | "uid %s cannot rename accounts of type: %s", |
| 1865 | callingUid, |
| 1866 | accountToRename.type); |
| 1867 | throw new SecurityException(msg); |
| 1868 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1869 | long identityToken = clearCallingIdentity(); |
| 1870 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1871 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1872 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1873 | Bundle result = new Bundle(); |
| 1874 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1875 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1876 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1877 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1878 | try { |
| 1879 | response.onResult(result); |
| 1880 | } catch (RemoteException e) { |
| 1881 | Log.w(TAG, e.getMessage()); |
| 1882 | } |
| 1883 | } finally { |
| 1884 | restoreCallingIdentity(identityToken); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1889 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1890 | Account resultAccount = null; |
| 1891 | /* |
| 1892 | * Cancel existing notifications. Let authenticators |
| 1893 | * re-post notifications as required. But we don't know if |
| 1894 | * the authenticators have bound their notifications to |
| 1895 | * now stale account name data. |
| 1896 | * |
| 1897 | * With a rename api, we might not need to do this anymore but it |
| 1898 | * shouldn't hurt. |
| 1899 | */ |
| 1900 | cancelNotification( |
| 1901 | getSigninRequiredNotificationId(accounts, accountToRename), |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 1902 | new UserHandle(accounts.userId)); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1903 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1904 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1905 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1906 | if (accountToRename.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 1907 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1908 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1909 | } |
| 1910 | } |
| 1911 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1912 | synchronized (accounts.dbLock) { |
| 1913 | synchronized (accounts.cacheLock) { |
| 1914 | accounts.accountsDb.beginTransaction(); |
| 1915 | Account renamedAccount = new Account(newName, accountToRename.type); |
| 1916 | if ((accounts.accountsDb.findCeAccountId(renamedAccount) >= 0)) { |
| 1917 | Log.e(TAG, "renameAccount failed - account with new name already exists"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1918 | return null; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1919 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1920 | try { |
| 1921 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
| 1922 | if (accountId >= 0) { |
| 1923 | accounts.accountsDb.renameCeAccount(accountId, newName); |
| 1924 | if (accounts.accountsDb.renameDeAccount( |
| 1925 | accountId, newName, accountToRename.name)) { |
| 1926 | accounts.accountsDb.setTransactionSuccessful(); |
| 1927 | } else { |
| 1928 | Log.e(TAG, "renameAccount failed"); |
| 1929 | return null; |
| 1930 | } |
| 1931 | } else { |
| 1932 | Log.e(TAG, "renameAccount failed - old account does not exist"); |
| 1933 | return null; |
| 1934 | } |
| 1935 | } finally { |
| 1936 | accounts.accountsDb.endTransaction(); |
| 1937 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1938 | /* |
| 1939 | * Database transaction was successful. Clean up cached |
| 1940 | * data associated with the account in the user profile. |
| 1941 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1942 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1943 | /* |
| 1944 | * Extract the data and token caches before removing the |
| 1945 | * old account to preserve the user data associated with |
| 1946 | * the account. |
| 1947 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1948 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1949 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
| 1950 | Map<String, Integer> tmpVisibility = accounts.visibilityCache.get(accountToRename); |
| 1951 | removeAccountFromCacheLocked(accounts, accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1952 | /* |
| 1953 | * Update the cached data associated with the renamed |
| 1954 | * account. |
| 1955 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1956 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1957 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 1958 | accounts.visibilityCache.put(renamedAccount, tmpVisibility); |
| 1959 | accounts.previousNameCache.put( |
| 1960 | renamedAccount, |
| 1961 | new AtomicReference<>(accountToRename.name)); |
| 1962 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1963 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1964 | int parentUserId = accounts.userId; |
| 1965 | if (canHaveProfile(parentUserId)) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1966 | /* |
| 1967 | * Owner or system user account was renamed, rename the account for |
| 1968 | * those users with which the account was shared. |
| 1969 | */ |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1970 | List<UserInfo> users = getUserManager().getUsers(true); |
| 1971 | for (UserInfo user : users) { |
| 1972 | if (user.isRestricted() |
| 1973 | && (user.restrictedProfileParentId == parentUserId)) { |
| 1974 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
| 1975 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1976 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1977 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1978 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1979 | sendNotificationAccountUpdated(resultAccount, accounts); |
| 1980 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame^] | 1981 | sendAccountRemovedBroadcast(accounts.userId); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 1982 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1983 | } |
| 1984 | return resultAccount; |
| 1985 | } |
| 1986 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1987 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1988 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1989 | return userInfo != null && userInfo.canHaveProfile(); |
| 1990 | } |
| 1991 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1992 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1993 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1994 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1995 | removeAccountAsUser( |
| 1996 | response, |
| 1997 | account, |
| 1998 | expectActivityLaunch, |
| 1999 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | @Override |
| 2003 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2004 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2005 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2006 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2007 | Log.v(TAG, "removeAccount: " + account |
| 2008 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2009 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2010 | + ", pid " + Binder.getCallingPid() |
| 2011 | + ", for user id " + userId); |
| 2012 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2013 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 2014 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 2015 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2016 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2017 | if (isCrossUser(callingUid, userId)) { |
| 2018 | throw new SecurityException( |
| 2019 | String.format( |
| 2020 | "User %s tying remove account for %s" , |
| 2021 | UserHandle.getCallingUserId(), |
| 2022 | userId)); |
| 2023 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2024 | /* |
| 2025 | * Only the system or authenticator should be allowed to remove accounts for that |
| 2026 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 2027 | * arbitrary applications (like competing authenticators). |
| 2028 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2029 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 2030 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 2031 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2032 | String msg = String.format( |
| 2033 | "uid %s cannot remove accounts of type: %s", |
| 2034 | callingUid, |
| 2035 | account.type); |
| 2036 | throw new SecurityException(msg); |
| 2037 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2038 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2039 | try { |
| 2040 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2041 | "User cannot modify accounts"); |
| 2042 | } catch (RemoteException re) { |
| 2043 | } |
| 2044 | return; |
| 2045 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2046 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2047 | try { |
| 2048 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2049 | "User cannot modify accounts of this type (policy)."); |
| 2050 | } catch (RemoteException re) { |
| 2051 | } |
| 2052 | return; |
| 2053 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2054 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2055 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2056 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2057 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2058 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2059 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2060 | if (account.equals(pair.first.first)) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2061 | NotificationId id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2062 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2063 | } |
| 2064 | } |
| 2065 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2066 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2067 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2068 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2069 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2070 | accountId, |
| 2071 | accounts, |
| 2072 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2073 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2074 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2075 | } finally { |
| 2076 | restoreCallingIdentity(identityToken); |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | @Override |
| 2081 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2082 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2083 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2084 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2085 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2086 | + ", pid " + Binder.getCallingPid()); |
| 2087 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2088 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2089 | if (account == null) { |
| 2090 | /* |
| 2091 | * Null accounts should result in returning false, as per |
| 2092 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2093 | */ |
| 2094 | Log.e(TAG, "account is null"); |
| 2095 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2096 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2097 | String msg = String.format( |
| 2098 | "uid %s cannot explicitly add accounts of type: %s", |
| 2099 | callingUid, |
| 2100 | account.type); |
| 2101 | throw new SecurityException(msg); |
| 2102 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2103 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2104 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2105 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2106 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2107 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2108 | accountId, |
| 2109 | accounts, |
| 2110 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2111 | long identityToken = clearCallingIdentity(); |
| 2112 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2113 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2114 | } finally { |
| 2115 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2116 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2119 | private class RemoveAccountSession extends Session { |
| 2120 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2121 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2122 | Account account, boolean expectActivityLaunch) { |
| 2123 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2124 | true /* stripAuthTokenFromResult */, account.name, |
| 2125 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2126 | mAccount = account; |
| 2127 | } |
| 2128 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2129 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2130 | protected String toDebugString(long now) { |
| 2131 | return super.toDebugString(now) + ", removeAccount" |
| 2132 | + ", account " + mAccount; |
| 2133 | } |
| 2134 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2135 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2136 | public void run() throws RemoteException { |
| 2137 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2138 | } |
| 2139 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2140 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2141 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2142 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2143 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2144 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2145 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2146 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2147 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2148 | } |
| 2149 | IAccountManagerResponse response = getResponseAndClose(); |
| 2150 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2151 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2152 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2153 | + response); |
| 2154 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2155 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2156 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2157 | try { |
| 2158 | response.onResult(result2); |
| 2159 | } catch (RemoteException e) { |
| 2160 | // ignore |
| 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | super.onResult(result); |
| 2165 | } |
| 2166 | } |
| 2167 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2168 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2169 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2170 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2171 | } |
| 2172 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2173 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2174 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2175 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2176 | if (!userUnlocked) { |
| 2177 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2178 | + " is still locked. CE data will be removed later"); |
| 2179 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2180 | synchronized (accounts.dbLock) { |
| 2181 | synchronized (accounts.cacheLock) { |
| 2182 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, |
| 2183 | accounts); |
| 2184 | accounts.accountsDb.beginTransaction(); |
| 2185 | // Set to a dummy value, this will only be used if the database |
| 2186 | // transaction succeeds. |
| 2187 | long accountId = -1; |
| 2188 | try { |
| 2189 | accountId = accounts.accountsDb.findDeAccountId(account); |
| 2190 | if (accountId >= 0) { |
| 2191 | isChanged = accounts.accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2192 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2193 | // always delete from CE table if CE storage is available |
| 2194 | // DE account could be removed while CE was locked |
| 2195 | if (userUnlocked) { |
| 2196 | long ceAccountId = accounts.accountsDb.findCeAccountId(account); |
| 2197 | if (ceAccountId >= 0) { |
| 2198 | accounts.accountsDb.deleteCeAccount(ceAccountId); |
| 2199 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2200 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2201 | accounts.accountsDb.setTransactionSuccessful(); |
| 2202 | } finally { |
| 2203 | accounts.accountsDb.endTransaction(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2204 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2205 | if (isChanged) { |
| 2206 | removeAccountFromCacheLocked(accounts, account); |
| 2207 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility |
| 2208 | .entrySet()) { |
| 2209 | if (packageToVisibility.getValue() |
| 2210 | != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 2211 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 2212 | } |
| 2213 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2214 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2215 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occurred. |
| 2216 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | a461e30 | 2017-04-12 11:00:48 -0700 | [diff] [blame^] | 2217 | sendAccountRemovedBroadcast(accounts.userId); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2218 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2219 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2220 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
| 2221 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2222 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2223 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2224 | long id = Binder.clearCallingIdentity(); |
| 2225 | try { |
| 2226 | int parentUserId = accounts.userId; |
| 2227 | if (canHaveProfile(parentUserId)) { |
| 2228 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2229 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2230 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2231 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2232 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2233 | } |
| 2234 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2235 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2236 | } finally { |
| 2237 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2238 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2239 | |
| 2240 | if (isChanged) { |
| 2241 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2242 | for (Pair<Pair<Account, String>, Integer> key |
| 2243 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2244 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2245 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2246 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2247 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2248 | account, uid, false)); |
| 2249 | } |
| 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2254 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2255 | } |
| 2256 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2257 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2258 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2259 | int callerUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2260 | Preconditions.checkNotNull(accountType, "accountType cannot be null"); |
| 2261 | Preconditions.checkNotNull(authToken, "authToken cannot be null"); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2262 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2263 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2264 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2265 | + ", pid " + Binder.getCallingPid()); |
| 2266 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2267 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2268 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2269 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2270 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2271 | List<Pair<Account, String>> deletedTokens; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2272 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2273 | accounts.accountsDb.beginTransaction(); |
| 2274 | try { |
| 2275 | deletedTokens = invalidateAuthTokenLocked(accounts, accountType, authToken); |
| 2276 | accounts.accountsDb.setTransactionSuccessful(); |
| 2277 | } finally { |
| 2278 | accounts.accountsDb.endTransaction(); |
| 2279 | } |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2280 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2281 | for (Pair<Account, String> tokenInfo : deletedTokens) { |
| 2282 | Account act = tokenInfo.first; |
| 2283 | String tokenType = tokenInfo.second; |
| 2284 | writeAuthTokenIntoCacheLocked(accounts, act, tokenType, null); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2285 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2286 | // wipe out cached token in memory. |
| 2287 | accounts.accountTokenCaches.remove(accountType, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2288 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2289 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2290 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2291 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2292 | } |
| 2293 | } |
| 2294 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2295 | private List<Pair<Account, String>> invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2296 | String authToken) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2297 | // TODO Move to AccountsDB |
| 2298 | List<Pair<Account, String>> results = new ArrayList<>(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2299 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2300 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2301 | try { |
| 2302 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2303 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2304 | String accountName = cursor.getString(1); |
| 2305 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2306 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2307 | results.add(Pair.create(new Account(accountName, accountType), authTokenType)); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2308 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2309 | } finally { |
| 2310 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2311 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2312 | return results; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2315 | private void saveCachedToken( |
| 2316 | UserAccounts accounts, |
| 2317 | Account account, |
| 2318 | String callerPkg, |
| 2319 | byte[] callerSigDigest, |
| 2320 | String tokenType, |
| 2321 | String token, |
| 2322 | long expiryMillis) { |
| 2323 | |
| 2324 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2325 | return; |
| 2326 | } |
| 2327 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2328 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2329 | synchronized (accounts.cacheLock) { |
| 2330 | accounts.accountTokenCaches.put( |
| 2331 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2335 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2336 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2337 | if (account == null || type == null) { |
| 2338 | return false; |
| 2339 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2340 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2341 | UserHandle.of(accounts.userId)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2342 | synchronized (accounts.dbLock) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2343 | accounts.accountsDb.beginTransaction(); |
| 2344 | boolean updateCache = false; |
| 2345 | try { |
| 2346 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2347 | if (accountId < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2348 | return false; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 2349 | } |
| 2350 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2351 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2352 | accounts.accountsDb.setTransactionSuccessful(); |
| 2353 | updateCache = true; |
| 2354 | return true; |
| 2355 | } |
| 2356 | return false; |
| 2357 | } finally { |
| 2358 | accounts.accountsDb.endTransaction(); |
| 2359 | if (updateCache) { |
| 2360 | synchronized (accounts.cacheLock) { |
| 2361 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
| 2362 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2363 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2364 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2365 | } |
| 2366 | } |
| 2367 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2368 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2369 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2370 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2371 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2372 | Log.v(TAG, "peekAuthToken: " + account |
| 2373 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2374 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2375 | + ", pid " + Binder.getCallingPid()); |
| 2376 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2377 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2378 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2379 | int userId = UserHandle.getCallingUserId(); |
| 2380 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2381 | String msg = String.format( |
| 2382 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2383 | callingUid, |
| 2384 | account.type); |
| 2385 | throw new SecurityException(msg); |
| 2386 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2387 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2388 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2389 | + callingUid); |
| 2390 | return null; |
| 2391 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2392 | long identityToken = clearCallingIdentity(); |
| 2393 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2394 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2395 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2396 | } finally { |
| 2397 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2398 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2399 | } |
| 2400 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2401 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2402 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2403 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2404 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2405 | Log.v(TAG, "setAuthToken: " + account |
| 2406 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2407 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2408 | + ", pid " + Binder.getCallingPid()); |
| 2409 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2410 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2411 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2412 | int userId = UserHandle.getCallingUserId(); |
| 2413 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2414 | String msg = String.format( |
| 2415 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2416 | callingUid, |
| 2417 | account.type); |
| 2418 | throw new SecurityException(msg); |
| 2419 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2420 | long identityToken = clearCallingIdentity(); |
| 2421 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2422 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2423 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2424 | } finally { |
| 2425 | restoreCallingIdentity(identityToken); |
| 2426 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2427 | } |
| 2428 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2429 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2430 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2431 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2432 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2433 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2434 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2435 | + ", pid " + Binder.getCallingPid()); |
| 2436 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2437 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2438 | int userId = UserHandle.getCallingUserId(); |
| 2439 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2440 | String msg = String.format( |
| 2441 | "uid %s cannot set secrets for accounts of type: %s", |
| 2442 | callingUid, |
| 2443 | account.type); |
| 2444 | throw new SecurityException(msg); |
| 2445 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2446 | long identityToken = clearCallingIdentity(); |
| 2447 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2448 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2449 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2450 | } finally { |
| 2451 | restoreCallingIdentity(identityToken); |
| 2452 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2455 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2456 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2457 | if (account == null) { |
| 2458 | return; |
| 2459 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2460 | boolean isChanged = false; |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 2461 | synchronized (accounts.dbLock) { |
| 2462 | synchronized (accounts.cacheLock) { |
| 2463 | accounts.accountsDb.beginTransaction(); |
| 2464 | try { |
| 2465 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2466 | if (accountId >= 0) { |
| 2467 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2468 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
| 2469 | accounts.authTokenCache.remove(account); |
| 2470 | accounts.accountTokenCaches.remove(account); |
| 2471 | accounts.accountsDb.setTransactionSuccessful(); |
| 2472 | // If there is an account whose password will be updated and the database |
| 2473 | // transactions succeed, then we say that a change has occured. Even if the |
| 2474 | // new password is the same as the old and there were no authtokens to |
| 2475 | // delete. |
| 2476 | isChanged = true; |
| 2477 | String action = (password == null || password.length() == 0) ? |
| 2478 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2479 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2480 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, |
| 2481 | callingUid); |
| 2482 | } |
| 2483 | } finally { |
| 2484 | accounts.accountsDb.endTransaction(); |
| 2485 | if (isChanged) { |
| 2486 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
| 2487 | sendNotificationAccountUpdated(account, accounts); |
| 2488 | sendAccountsChangedBroadcast(accounts.userId); |
| 2489 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2490 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2491 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2492 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2493 | } |
| 2494 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2495 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2496 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2497 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2498 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2499 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2500 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2501 | + ", pid " + Binder.getCallingPid()); |
| 2502 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2503 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2504 | int userId = UserHandle.getCallingUserId(); |
| 2505 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2506 | String msg = String.format( |
| 2507 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2508 | callingUid, |
| 2509 | account.type); |
| 2510 | throw new SecurityException(msg); |
| 2511 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2512 | long identityToken = clearCallingIdentity(); |
| 2513 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2514 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2515 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2516 | } finally { |
| 2517 | restoreCallingIdentity(identityToken); |
| 2518 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2521 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2522 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2523 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2524 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2525 | Log.v(TAG, "setUserData: " + account |
| 2526 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2527 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2528 | + ", pid " + Binder.getCallingPid()); |
| 2529 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2530 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2531 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2532 | int userId = UserHandle.getCallingUserId(); |
| 2533 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2534 | String msg = String.format( |
| 2535 | "uid %s cannot set user data for accounts of type: %s", |
| 2536 | callingUid, |
| 2537 | account.type); |
| 2538 | throw new SecurityException(msg); |
| 2539 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2540 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2541 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2542 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2543 | if (!accountExistsCache(accounts, account)) { |
| 2544 | return; |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2545 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2546 | setUserdataInternal(accounts, account, key, value); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2547 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2548 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2549 | } |
| 2550 | } |
| 2551 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2552 | private boolean accountExistsCache(UserAccounts accounts, Account account) { |
| 2553 | synchronized (accounts.cacheLock) { |
| 2554 | if (accounts.accountCache.containsKey(account.type)) { |
| 2555 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2556 | if (acc.name.equals(account.name)) { |
| 2557 | return true; |
| 2558 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | } |
| 2562 | return false; |
| 2563 | } |
| 2564 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2565 | private void setUserdataInternal(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2566 | String value) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2567 | synchronized (accounts.dbLock) { |
| 2568 | accounts.accountsDb.beginTransaction(); |
| 2569 | try { |
| 2570 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 2571 | if (accountId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2572 | return; |
| 2573 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2574 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
| 2575 | if (extrasId < 0) { |
| 2576 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
| 2577 | if (extrasId < 0) { |
| 2578 | return; |
| 2579 | } |
| 2580 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
| 2581 | return; |
| 2582 | } |
| 2583 | accounts.accountsDb.setTransactionSuccessful(); |
| 2584 | } finally { |
| 2585 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2586 | } |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 2587 | synchronized (accounts.cacheLock) { |
| 2588 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2589 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2590 | } |
| 2591 | } |
| 2592 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2593 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2594 | if (result == null) { |
| 2595 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2596 | } |
| 2597 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2598 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2599 | + response); |
| 2600 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2601 | try { |
| 2602 | response.onResult(result); |
| 2603 | } catch (RemoteException e) { |
| 2604 | // if the caller is dead then there is no one to care about remote |
| 2605 | // exceptions |
| 2606 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2607 | Log.v(TAG, "failure while notifying response", e); |
| 2608 | } |
| 2609 | } |
| 2610 | } |
| 2611 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2612 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2613 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2614 | final String authTokenType) |
| 2615 | throws RemoteException { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2616 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
| 2617 | Preconditions.checkArgument(authTokenType != null, "authTokenType cannot be null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2618 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2619 | final int callingUid = getCallingUid(); |
| 2620 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2621 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2622 | throw new SecurityException("can only call from system"); |
| 2623 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2624 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2625 | long identityToken = clearCallingIdentity(); |
| 2626 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2627 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2628 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2629 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2630 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2631 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2632 | protected String toDebugString(long now) { |
| 2633 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2634 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2635 | + ", authTokenType " + authTokenType; |
| 2636 | } |
| 2637 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2638 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2639 | public void run() throws RemoteException { |
| 2640 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2641 | } |
| 2642 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2643 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2644 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2645 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2646 | if (result != null) { |
| 2647 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2648 | Bundle bundle = new Bundle(); |
| 2649 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2650 | super.onResult(bundle); |
| 2651 | return; |
| 2652 | } else { |
| 2653 | super.onResult(result); |
| 2654 | } |
| 2655 | } |
| 2656 | }.bind(); |
| 2657 | } finally { |
| 2658 | restoreCallingIdentity(identityToken); |
| 2659 | } |
| 2660 | } |
| 2661 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2662 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2663 | public void getAuthToken( |
| 2664 | IAccountManagerResponse response, |
| 2665 | final Account account, |
| 2666 | final String authTokenType, |
| 2667 | final boolean notifyOnAuthFailure, |
| 2668 | final boolean expectActivityLaunch, |
| 2669 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2670 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2671 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2672 | Log.v(TAG, "getAuthToken: " + account |
| 2673 | + ", response " + response |
| 2674 | + ", authTokenType " + authTokenType |
| 2675 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2676 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2677 | + ", caller's uid " + Binder.getCallingUid() |
| 2678 | + ", pid " + Binder.getCallingPid()); |
| 2679 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2680 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2681 | try { |
| 2682 | if (account == null) { |
| 2683 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2684 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2685 | return; |
| 2686 | } |
| 2687 | if (authTokenType == null) { |
| 2688 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2689 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2690 | return; |
| 2691 | } |
| 2692 | } catch (RemoteException e) { |
| 2693 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2694 | return; |
| 2695 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2696 | int userId = UserHandle.getCallingUserId(); |
| 2697 | long ident = Binder.clearCallingIdentity(); |
| 2698 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2699 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2700 | try { |
| 2701 | accounts = getUserAccounts(userId); |
| 2702 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2703 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2704 | } finally { |
| 2705 | Binder.restoreCallingIdentity(ident); |
| 2706 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2707 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2708 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2709 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2710 | |
| 2711 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2712 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2713 | final boolean permissionGranted = |
| 2714 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2715 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2716 | // Get the calling package. We will use it for the purpose of caching. |
| 2717 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2718 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2719 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2720 | try { |
| 2721 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2722 | } finally { |
| 2723 | Binder.restoreCallingIdentity(ident); |
| 2724 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2725 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2726 | String msg = String.format( |
| 2727 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2728 | callerUid, |
| 2729 | callerPkg); |
| 2730 | throw new SecurityException(msg); |
| 2731 | } |
| 2732 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2733 | // let authenticator know the identity of the caller |
| 2734 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2735 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2736 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2737 | if (notifyOnAuthFailure) { |
| 2738 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2739 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2740 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2741 | long identityToken = clearCallingIdentity(); |
| 2742 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2743 | // Distill the caller's package signatures into a single digest. |
| 2744 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2745 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2746 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2747 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2748 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2749 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2750 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2751 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2752 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2753 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2754 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2755 | onResult(response, result); |
| 2756 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2757 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2760 | if (customTokens) { |
| 2761 | /* |
| 2762 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2763 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2764 | * ANDORID_PACKAGE_NAME. |
| 2765 | */ |
| 2766 | String token = readCachedTokenInternal( |
| 2767 | accounts, |
| 2768 | account, |
| 2769 | authTokenType, |
| 2770 | callerPkg, |
| 2771 | callerPkgSigDigest); |
| 2772 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2773 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2774 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2775 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2776 | Bundle result = new Bundle(); |
| 2777 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2778 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2779 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2780 | onResult(response, result); |
| 2781 | return; |
| 2782 | } |
| 2783 | } |
| 2784 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2785 | new Session( |
| 2786 | accounts, |
| 2787 | response, |
| 2788 | account.type, |
| 2789 | expectActivityLaunch, |
| 2790 | false /* stripAuthTokenFromResult */, |
| 2791 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2792 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2793 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2794 | protected String toDebugString(long now) { |
| 2795 | if (loginOptions != null) loginOptions.keySet(); |
| 2796 | return super.toDebugString(now) + ", getAuthToken" |
| 2797 | + ", " + account |
| 2798 | + ", authTokenType " + authTokenType |
| 2799 | + ", loginOptions " + loginOptions |
| 2800 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2801 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2802 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2803 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2804 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2805 | // If the caller doesn't have permission then create and return the |
| 2806 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2807 | if (!permissionGranted) { |
| 2808 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2809 | } else { |
| 2810 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2811 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2814 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2815 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2816 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2817 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2818 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2819 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2820 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2821 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2822 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2823 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2824 | authTokenType, |
| 2825 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2826 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2827 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2828 | onResult(bundle); |
| 2829 | return; |
| 2830 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2831 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2832 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2833 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2834 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2835 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2836 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2837 | "the type and name should not be empty"); |
| 2838 | return; |
| 2839 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2840 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2841 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2842 | saveAuthTokenToDatabase( |
| 2843 | mAccounts, |
| 2844 | resultAccount, |
| 2845 | authTokenType, |
| 2846 | authToken); |
| 2847 | } |
| 2848 | long expiryMillis = result.getLong( |
| 2849 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2850 | if (customTokens |
| 2851 | && expiryMillis > System.currentTimeMillis()) { |
| 2852 | saveCachedToken( |
| 2853 | mAccounts, |
| 2854 | account, |
| 2855 | callerPkg, |
| 2856 | callerPkgSigDigest, |
| 2857 | authTokenType, |
| 2858 | authToken, |
| 2859 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2860 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2861 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2862 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2863 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2864 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2865 | /* |
| 2866 | * Make sure that the supplied intent is owned by the authenticator |
| 2867 | * giving it to the system. Otherwise a malicious authenticator could |
| 2868 | * have users launching arbitrary activities by tricking users to |
| 2869 | * interact with malicious notifications. |
| 2870 | */ |
| 2871 | checkKeyIntent( |
| 2872 | Binder.getCallingUid(), |
| 2873 | intent); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2874 | doNotification( |
| 2875 | mAccounts, |
| 2876 | account, |
| 2877 | result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2878 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2879 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2880 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2881 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2882 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2883 | }.bind(); |
| 2884 | } finally { |
| 2885 | restoreCallingIdentity(identityToken); |
| 2886 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2889 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2890 | MessageDigest digester; |
| 2891 | try { |
| 2892 | digester = MessageDigest.getInstance("SHA-256"); |
| 2893 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2894 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2895 | for (Signature sig : pkgInfo.signatures) { |
| 2896 | digester.update(sig.toByteArray()); |
| 2897 | } |
| 2898 | } catch (NoSuchAlgorithmException x) { |
| 2899 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2900 | digester = null; |
| 2901 | } catch (NameNotFoundException e) { |
| 2902 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2903 | digester = null; |
| 2904 | } |
| 2905 | return (digester == null) ? null : digester.digest(); |
| 2906 | } |
| 2907 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2908 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2909 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2910 | int uid = intent.getIntExtra( |
| 2911 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2912 | String authTokenType = intent.getStringExtra( |
| 2913 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2914 | final String titleAndSubtitle = |
| 2915 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2916 | account.name); |
| 2917 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2918 | String title = titleAndSubtitle; |
| 2919 | String subtitle = ""; |
| 2920 | if (index > 0) { |
| 2921 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2922 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2923 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2924 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2925 | Context contextForUser = getContextForUser(user); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 2926 | Notification n = |
| 2927 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
| 2928 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2929 | .setWhen(0) |
| 2930 | .setColor(contextForUser.getColor( |
| 2931 | com.android.internal.R.color.system_notification_accent_color)) |
| 2932 | .setContentTitle(title) |
| 2933 | .setContentText(subtitle) |
| 2934 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2935 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2936 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2937 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2938 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2941 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 2942 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 2943 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2944 | |
| 2945 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2946 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2947 | if (startInNewTask) { |
| 2948 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 2949 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2950 | // the intent from a non-Activity context. This is the default behavior. |
| 2951 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2952 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2953 | intent.addCategory(getCredentialPermissionNotificationId(account, |
| 2954 | authTokenType, uid).mTag + (packageName != null ? packageName : "")); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2955 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2956 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2957 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2958 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2959 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2960 | return intent; |
| 2961 | } |
| 2962 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2963 | private NotificationId getCredentialPermissionNotificationId(Account account, |
| 2964 | String authTokenType, int uid) { |
| 2965 | NotificationId nId; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2966 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2967 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2968 | final Pair<Pair<Account, String>, Integer> key = |
| 2969 | new Pair<Pair<Account, String>, Integer>( |
| 2970 | new Pair<Account, String>(account, authTokenType), uid); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2971 | nId = accounts.credentialsPermissionNotificationIds.get(key); |
| 2972 | if (nId == null) { |
| 2973 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION |
| 2974 | + ":" + account.hashCode() + ":" + authTokenType.hashCode(); |
| 2975 | int id = SystemMessage.NOTE_ACCOUNT_CREDENTIAL_PERMISSION; |
| 2976 | nId = new NotificationId(tag, id); |
| 2977 | accounts.credentialsPermissionNotificationIds.put(key, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2978 | } |
| 2979 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2980 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2983 | private NotificationId getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
| 2984 | NotificationId nId; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2985 | synchronized (accounts.signinRequiredNotificationIds) { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2986 | nId = accounts.signinRequiredNotificationIds.get(account); |
| 2987 | if (nId == null) { |
| 2988 | String tag = TAG + ":" + SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN |
| 2989 | + ":" + account.hashCode(); |
| 2990 | int id = SystemMessage.NOTE_ACCOUNT_REQUIRE_SIGNIN; |
| 2991 | nId = new NotificationId(tag, id); |
| 2992 | accounts.signinRequiredNotificationIds.put(account, nId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2993 | } |
| 2994 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 2995 | return nId; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2996 | } |
| 2997 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2998 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2999 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3000 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3001 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3002 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3003 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3004 | Log.v(TAG, "addAccount: accountType " + accountType |
| 3005 | + ", response " + response |
| 3006 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3007 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3008 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3009 | + ", caller's uid " + Binder.getCallingUid() |
| 3010 | + ", pid " + Binder.getCallingPid()); |
| 3011 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3012 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3013 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3014 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3015 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3016 | final int uid = Binder.getCallingUid(); |
| 3017 | final int userId = UserHandle.getUserId(uid); |
| 3018 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3019 | try { |
| 3020 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3021 | "User is not allowed to add an account!"); |
| 3022 | } catch (RemoteException re) { |
| 3023 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3024 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3025 | return; |
| 3026 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3027 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 3028 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3029 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3030 | "User cannot modify accounts of this type (policy)."); |
| 3031 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 3032 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3033 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3034 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3035 | return; |
| 3036 | } |
| 3037 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3038 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 3039 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3040 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3041 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3042 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3043 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3044 | long identityToken = clearCallingIdentity(); |
| 3045 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3046 | UserAccounts accounts = getUserAccounts(usrId); |
| 3047 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3048 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3049 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3050 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3051 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3052 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3053 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3054 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 3055 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3056 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3057 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3058 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3059 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3060 | protected String toDebugString(long now) { |
| 3061 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3062 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3063 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3064 | } |
| 3065 | }.bind(); |
| 3066 | } finally { |
| 3067 | restoreCallingIdentity(identityToken); |
| 3068 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3069 | } |
| 3070 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3071 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3072 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 3073 | final String authTokenType, final String[] requiredFeatures, |
| 3074 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3075 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3076 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3077 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3078 | Log.v(TAG, "addAccount: 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) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3082 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3083 | + ", caller's uid " + Binder.getCallingUid() |
| 3084 | + ", pid " + Binder.getCallingPid() |
| 3085 | + ", for user id " + userId); |
| 3086 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3087 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3088 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3089 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3090 | if (isCrossUser(callingUid, userId)) { |
| 3091 | throw new SecurityException( |
| 3092 | String.format( |
| 3093 | "User %s trying to add account for %s" , |
| 3094 | UserHandle.getCallingUserId(), |
| 3095 | userId)); |
| 3096 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3097 | |
| 3098 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3099 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3100 | try { |
| 3101 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3102 | "User is not allowed to add an account!"); |
| 3103 | } catch (RemoteException re) { |
| 3104 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3105 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3106 | return; |
| 3107 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3108 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3109 | try { |
| 3110 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3111 | "User cannot modify accounts of this type (policy)."); |
| 3112 | } catch (RemoteException re) { |
| 3113 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3114 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3115 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3116 | return; |
| 3117 | } |
| 3118 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3119 | final int pid = Binder.getCallingPid(); |
| 3120 | final int uid = Binder.getCallingUid(); |
| 3121 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3122 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3123 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3124 | |
| 3125 | long identityToken = clearCallingIdentity(); |
| 3126 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3127 | UserAccounts accounts = getUserAccounts(userId); |
| 3128 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3129 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3130 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3131 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3132 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3133 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3134 | @Override |
| 3135 | public void run() throws RemoteException { |
| 3136 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3137 | options); |
| 3138 | } |
| 3139 | |
| 3140 | @Override |
| 3141 | protected String toDebugString(long now) { |
| 3142 | return super.toDebugString(now) + ", addAccount" |
| 3143 | + ", accountType " + accountType |
| 3144 | + ", requiredFeatures " |
| 3145 | + (requiredFeatures != null |
| 3146 | ? TextUtils.join(",", requiredFeatures) |
| 3147 | : null); |
| 3148 | } |
| 3149 | }.bind(); |
| 3150 | } finally { |
| 3151 | restoreCallingIdentity(identityToken); |
| 3152 | } |
| 3153 | } |
| 3154 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3155 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3156 | public void startAddAccountSession( |
| 3157 | final IAccountManagerResponse response, |
| 3158 | final String accountType, |
| 3159 | final String authTokenType, |
| 3160 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3161 | final boolean expectActivityLaunch, |
| 3162 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3163 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3164 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3165 | Log.v(TAG, |
| 3166 | "startAddAccountSession: accountType " + accountType |
| 3167 | + ", response " + response |
| 3168 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3169 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3170 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3171 | + ", caller's uid " + Binder.getCallingUid() |
| 3172 | + ", pid " + Binder.getCallingPid()); |
| 3173 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3174 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3175 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3176 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3177 | final int uid = Binder.getCallingUid(); |
| 3178 | final int userId = UserHandle.getUserId(uid); |
| 3179 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3180 | try { |
| 3181 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3182 | "User is not allowed to add an account!"); |
| 3183 | } catch (RemoteException re) { |
| 3184 | } |
| 3185 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3186 | return; |
| 3187 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3188 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3189 | try { |
| 3190 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3191 | "User cannot modify accounts of this type (policy)."); |
| 3192 | } catch (RemoteException re) { |
| 3193 | } |
| 3194 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3195 | userId); |
| 3196 | return; |
| 3197 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3198 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3199 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3200 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3201 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3202 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3203 | // Check to see if the Password should be included to the caller. |
| 3204 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3205 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3206 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3207 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3208 | long identityToken = clearCallingIdentity(); |
| 3209 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3210 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3211 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3212 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3213 | new StartAccountSession( |
| 3214 | accounts, |
| 3215 | response, |
| 3216 | accountType, |
| 3217 | expectActivityLaunch, |
| 3218 | null /* accountName */, |
| 3219 | false /* authDetailsRequired */, |
| 3220 | true /* updateLastAuthenticationTime */, |
| 3221 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3222 | @Override |
| 3223 | public void run() throws RemoteException { |
| 3224 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3225 | requiredFeatures, options); |
| 3226 | } |
| 3227 | |
| 3228 | @Override |
| 3229 | protected String toDebugString(long now) { |
| 3230 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3231 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3232 | + accountType + ", requiredFeatures " |
| 3233 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3234 | } |
| 3235 | }.bind(); |
| 3236 | } finally { |
| 3237 | restoreCallingIdentity(identityToken); |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3242 | private abstract class StartAccountSession extends Session { |
| 3243 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3244 | private final boolean mIsPasswordForwardingAllowed; |
| 3245 | |
| 3246 | public StartAccountSession( |
| 3247 | UserAccounts accounts, |
| 3248 | IAccountManagerResponse response, |
| 3249 | String accountType, |
| 3250 | boolean expectActivityLaunch, |
| 3251 | String accountName, |
| 3252 | boolean authDetailsRequired, |
| 3253 | boolean updateLastAuthenticationTime, |
| 3254 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3255 | super(accounts, response, accountType, expectActivityLaunch, |
| 3256 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3257 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3258 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | @Override |
| 3262 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3263 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3264 | mNumResults++; |
| 3265 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3266 | if (result != null |
| 3267 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3268 | checkKeyIntent( |
| 3269 | Binder.getCallingUid(), |
| 3270 | intent); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3271 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3272 | IAccountManagerResponse response; |
| 3273 | if (mExpectActivityLaunch && result != null |
| 3274 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3275 | response = mResponse; |
| 3276 | } else { |
| 3277 | response = getResponseAndClose(); |
| 3278 | } |
| 3279 | if (response == null) { |
| 3280 | return; |
| 3281 | } |
| 3282 | if (result == null) { |
| 3283 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3284 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3285 | + response); |
| 3286 | } |
| 3287 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3288 | "null bundle returned"); |
| 3289 | return; |
| 3290 | } |
| 3291 | |
| 3292 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3293 | // All AccountManager error codes are greater |
| 3294 | // than 0 |
| 3295 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3296 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3297 | return; |
| 3298 | } |
| 3299 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3300 | // Omit passwords if the caller isn't permitted to see them. |
| 3301 | if (!mIsPasswordForwardingAllowed) { |
| 3302 | result.remove(AccountManager.KEY_PASSWORD); |
| 3303 | } |
| 3304 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3305 | // Strip auth token from result. |
| 3306 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3307 | |
| 3308 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3309 | Log.v(TAG, |
| 3310 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3311 | } |
| 3312 | |
| 3313 | // Get the session bundle created by authenticator. The |
| 3314 | // bundle contains data necessary for finishing the session |
| 3315 | // later. The session bundle will be encrypted here and |
| 3316 | // decrypted later when trying to finish the session. |
| 3317 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3318 | if (sessionBundle != null) { |
| 3319 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3320 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3321 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3322 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3323 | } |
| 3324 | // Add accountType info to session bundle. This will |
| 3325 | // override any value set by authenticator. |
| 3326 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3327 | |
| 3328 | // Encrypt session bundle before returning to caller. |
| 3329 | try { |
| 3330 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3331 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3332 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3333 | } catch (GeneralSecurityException e) { |
| 3334 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3335 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3336 | } |
| 3337 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3338 | "failed to encrypt session bundle"); |
| 3339 | return; |
| 3340 | } |
| 3341 | } |
| 3342 | |
| 3343 | sendResponse(response, result); |
| 3344 | } |
| 3345 | } |
| 3346 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3347 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3348 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3349 | @NonNull Bundle sessionBundle, |
| 3350 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3351 | Bundle appInfo, |
| 3352 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3353 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3354 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3355 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3356 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3357 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3358 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3359 | + ", caller's uid " + callingUid |
| 3360 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3361 | + ", pid " + Binder.getCallingPid() |
| 3362 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3363 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3364 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3365 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3366 | // Account type is added to it before encryption. |
| 3367 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3368 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3369 | } |
| 3370 | |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 3371 | // Only allow the system process to finish session for other users. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3372 | if (isCrossUser(callingUid, userId)) { |
| 3373 | throw new SecurityException( |
| 3374 | String.format( |
| 3375 | "User %s trying to finish session for %s without cross user permission", |
| 3376 | UserHandle.getCallingUserId(), |
| 3377 | userId)); |
| 3378 | } |
| 3379 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3380 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3381 | sendErrorResponse(response, |
| 3382 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3383 | "User is not allowed to add an account!"); |
| 3384 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3385 | return; |
| 3386 | } |
| 3387 | |
| 3388 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3389 | final Bundle decryptedBundle; |
| 3390 | final String accountType; |
| 3391 | // First decrypt session bundle to get account type for checking permission. |
| 3392 | try { |
| 3393 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3394 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3395 | if (decryptedBundle == null) { |
| 3396 | sendErrorResponse( |
| 3397 | response, |
| 3398 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3399 | "failed to decrypt session bundle"); |
| 3400 | return; |
| 3401 | } |
| 3402 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3403 | // Account type cannot be null. This should not happen if session bundle was created |
| 3404 | // properly by #StartAccountSession. |
| 3405 | if (TextUtils.isEmpty(accountType)) { |
| 3406 | sendErrorResponse( |
| 3407 | response, |
| 3408 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3409 | "accountType is empty"); |
| 3410 | return; |
| 3411 | } |
| 3412 | |
| 3413 | // If by any chances, decryptedBundle contains colliding keys with |
| 3414 | // system info |
| 3415 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3416 | // update credentials flow, we should replace with the new values of the current call. |
| 3417 | if (appInfo != null) { |
| 3418 | decryptedBundle.putAll(appInfo); |
| 3419 | } |
| 3420 | |
| 3421 | // 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] | 3422 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3423 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3424 | } catch (GeneralSecurityException e) { |
| 3425 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3426 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3427 | } |
| 3428 | sendErrorResponse( |
| 3429 | response, |
| 3430 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3431 | "failed to decrypt session bundle"); |
| 3432 | return; |
| 3433 | } |
| 3434 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3435 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3436 | sendErrorResponse( |
| 3437 | response, |
| 3438 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3439 | "User cannot modify accounts of this type (policy)."); |
| 3440 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3441 | userId); |
| 3442 | return; |
| 3443 | } |
| 3444 | |
| 3445 | long identityToken = clearCallingIdentity(); |
| 3446 | try { |
| 3447 | UserAccounts accounts = getUserAccounts(userId); |
| 3448 | logRecordWithUid( |
| 3449 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3450 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3451 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3452 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3453 | new Session( |
| 3454 | accounts, |
| 3455 | response, |
| 3456 | accountType, |
| 3457 | expectActivityLaunch, |
| 3458 | true /* stripAuthTokenFromResult */, |
| 3459 | null /* accountName */, |
| 3460 | false /* authDetailsRequired */, |
| 3461 | true /* updateLastAuthenticationTime */) { |
| 3462 | @Override |
| 3463 | public void run() throws RemoteException { |
| 3464 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3465 | } |
| 3466 | |
| 3467 | @Override |
| 3468 | protected String toDebugString(long now) { |
| 3469 | return super.toDebugString(now) |
| 3470 | + ", finishSession" |
| 3471 | + ", accountType " + accountType; |
| 3472 | } |
| 3473 | }.bind(); |
| 3474 | } finally { |
| 3475 | restoreCallingIdentity(identityToken); |
| 3476 | } |
| 3477 | } |
| 3478 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3479 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3480 | final DevicePolicyManagerInternal dpmi = |
| 3481 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3482 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3483 | if (dpmi == null) { |
| 3484 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3485 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3486 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3487 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3488 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3489 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3490 | } |
| 3491 | if (intent == null) { |
| 3492 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3493 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3494 | long identityToken = clearCallingIdentity(); |
| 3495 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3496 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3497 | } finally { |
| 3498 | restoreCallingIdentity(identityToken); |
| 3499 | } |
| 3500 | } |
| 3501 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3502 | /** |
| 3503 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3504 | */ |
| 3505 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3506 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3507 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3508 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3509 | return cantAddAccount; |
| 3510 | } |
| 3511 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3512 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3513 | public void confirmCredentialsAsUser( |
| 3514 | IAccountManagerResponse response, |
| 3515 | final Account account, |
| 3516 | final Bundle options, |
| 3517 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3518 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3519 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3520 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3521 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3522 | Log.v(TAG, "confirmCredentials: " + account |
| 3523 | + ", response " + response |
| 3524 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3525 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3526 | + ", pid " + Binder.getCallingPid()); |
| 3527 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3528 | // Only allow the system process to read accounts of other users |
| 3529 | if (isCrossUser(callingUid, userId)) { |
| 3530 | throw new SecurityException( |
| 3531 | String.format( |
| 3532 | "User %s trying to confirm account credentials for %s" , |
| 3533 | UserHandle.getCallingUserId(), |
| 3534 | userId)); |
| 3535 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3536 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3537 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3538 | long identityToken = clearCallingIdentity(); |
| 3539 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3540 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3541 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3542 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3543 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3544 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3545 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3546 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3547 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3548 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3549 | protected String toDebugString(long now) { |
| 3550 | return super.toDebugString(now) + ", confirmCredentials" |
| 3551 | + ", " + account; |
| 3552 | } |
| 3553 | }.bind(); |
| 3554 | } finally { |
| 3555 | restoreCallingIdentity(identityToken); |
| 3556 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3559 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3560 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3561 | final String authTokenType, final boolean expectActivityLaunch, |
| 3562 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3563 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3564 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3565 | Log.v(TAG, "updateCredentials: " + account |
| 3566 | + ", response " + response |
| 3567 | + ", authTokenType " + authTokenType |
| 3568 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3569 | + ", caller's uid " + Binder.getCallingUid() |
| 3570 | + ", pid " + Binder.getCallingPid()); |
| 3571 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3572 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3573 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3574 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3575 | long identityToken = clearCallingIdentity(); |
| 3576 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3577 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3578 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3579 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3580 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3581 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3582 | public void run() throws RemoteException { |
| 3583 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3584 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3585 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3586 | protected String toDebugString(long now) { |
| 3587 | if (loginOptions != null) loginOptions.keySet(); |
| 3588 | return super.toDebugString(now) + ", updateCredentials" |
| 3589 | + ", " + account |
| 3590 | + ", authTokenType " + authTokenType |
| 3591 | + ", loginOptions " + loginOptions; |
| 3592 | } |
| 3593 | }.bind(); |
| 3594 | } finally { |
| 3595 | restoreCallingIdentity(identityToken); |
| 3596 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3597 | } |
| 3598 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3599 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3600 | public void startUpdateCredentialsSession( |
| 3601 | IAccountManagerResponse response, |
| 3602 | final Account account, |
| 3603 | final String authTokenType, |
| 3604 | final boolean expectActivityLaunch, |
| 3605 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3606 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3607 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3608 | Log.v(TAG, |
| 3609 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3610 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3611 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3612 | + ", pid " + Binder.getCallingPid()); |
| 3613 | } |
| 3614 | if (response == null) { |
| 3615 | throw new IllegalArgumentException("response is null"); |
| 3616 | } |
| 3617 | if (account == null) { |
| 3618 | throw new IllegalArgumentException("account is null"); |
| 3619 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3620 | |
| 3621 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3622 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3623 | |
| 3624 | // Check to see if the Password should be included to the caller. |
| 3625 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3626 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3627 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3628 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3629 | long identityToken = clearCallingIdentity(); |
| 3630 | try { |
| 3631 | UserAccounts accounts = getUserAccounts(userId); |
| 3632 | new StartAccountSession( |
| 3633 | accounts, |
| 3634 | response, |
| 3635 | account.type, |
| 3636 | expectActivityLaunch, |
| 3637 | account.name, |
| 3638 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3639 | true /* updateLastCredentialTime */, |
| 3640 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3641 | @Override |
| 3642 | public void run() throws RemoteException { |
| 3643 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3644 | loginOptions); |
| 3645 | } |
| 3646 | |
| 3647 | @Override |
| 3648 | protected String toDebugString(long now) { |
| 3649 | if (loginOptions != null) |
| 3650 | loginOptions.keySet(); |
| 3651 | return super.toDebugString(now) |
| 3652 | + ", startUpdateCredentialsSession" |
| 3653 | + ", " + account |
| 3654 | + ", authTokenType " + authTokenType |
| 3655 | + ", loginOptions " + loginOptions; |
| 3656 | } |
| 3657 | }.bind(); |
| 3658 | } finally { |
| 3659 | restoreCallingIdentity(identityToken); |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3664 | public void isCredentialsUpdateSuggested( |
| 3665 | IAccountManagerResponse response, |
| 3666 | final Account account, |
| 3667 | final String statusToken) { |
| 3668 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3669 | Log.v(TAG, |
| 3670 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3671 | + ", caller's uid " + Binder.getCallingUid() |
| 3672 | + ", pid " + Binder.getCallingPid()); |
| 3673 | } |
| 3674 | if (response == null) { |
| 3675 | throw new IllegalArgumentException("response is null"); |
| 3676 | } |
| 3677 | if (account == null) { |
| 3678 | throw new IllegalArgumentException("account is null"); |
| 3679 | } |
| 3680 | if (TextUtils.isEmpty(statusToken)) { |
| 3681 | throw new IllegalArgumentException("status token is empty"); |
| 3682 | } |
| 3683 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3684 | int usrId = UserHandle.getCallingUserId(); |
| 3685 | long identityToken = clearCallingIdentity(); |
| 3686 | try { |
| 3687 | UserAccounts accounts = getUserAccounts(usrId); |
| 3688 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3689 | false /* stripAuthTokenFromResult */, account.name, |
| 3690 | false /* authDetailsRequired */) { |
| 3691 | @Override |
| 3692 | protected String toDebugString(long now) { |
| 3693 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3694 | + ", " + account; |
| 3695 | } |
| 3696 | |
| 3697 | @Override |
| 3698 | public void run() throws RemoteException { |
| 3699 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3700 | } |
| 3701 | |
| 3702 | @Override |
| 3703 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3704 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3705 | IAccountManagerResponse response = getResponseAndClose(); |
| 3706 | if (response == null) { |
| 3707 | return; |
| 3708 | } |
| 3709 | |
| 3710 | if (result == null) { |
| 3711 | sendErrorResponse( |
| 3712 | response, |
| 3713 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3714 | "null bundle"); |
| 3715 | return; |
| 3716 | } |
| 3717 | |
| 3718 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3719 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3720 | + response); |
| 3721 | } |
| 3722 | // Check to see if an error occurred. We know if an error occurred because all |
| 3723 | // error codes are greater than 0. |
| 3724 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3725 | sendErrorResponse(response, |
| 3726 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3727 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3728 | return; |
| 3729 | } |
| 3730 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3731 | sendErrorResponse( |
| 3732 | response, |
| 3733 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3734 | "no result in response"); |
| 3735 | return; |
| 3736 | } |
| 3737 | final Bundle newResult = new Bundle(); |
| 3738 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3739 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3740 | sendResponse(response, newResult); |
| 3741 | } |
| 3742 | }.bind(); |
| 3743 | } finally { |
| 3744 | restoreCallingIdentity(identityToken); |
| 3745 | } |
| 3746 | } |
| 3747 | |
| 3748 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3749 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3750 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3751 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3752 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3753 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3754 | + ", response " + response |
| 3755 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3756 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3757 | + ", pid " + Binder.getCallingPid()); |
| 3758 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3759 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3760 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3761 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3762 | if (!isAccountManagedByCaller(accountType, callingUid, userId) |
| 3763 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3764 | String msg = String.format( |
| 3765 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3766 | callingUid, |
| 3767 | accountType); |
| 3768 | throw new SecurityException(msg); |
| 3769 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3770 | long identityToken = clearCallingIdentity(); |
| 3771 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3772 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3773 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3774 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3775 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3776 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3777 | public void run() throws RemoteException { |
| 3778 | mAuthenticator.editProperties(this, mAccountType); |
| 3779 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3780 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3781 | protected String toDebugString(long now) { |
| 3782 | return super.toDebugString(now) + ", editProperties" |
| 3783 | + ", accountType " + accountType; |
| 3784 | } |
| 3785 | }.bind(); |
| 3786 | } finally { |
| 3787 | restoreCallingIdentity(identityToken); |
| 3788 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3789 | } |
| 3790 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3791 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3792 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3793 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3794 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3795 | throw new SecurityException("Can be called only by system UID"); |
| 3796 | } |
| 3797 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3798 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3799 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3800 | |
| 3801 | final int userId = userHandle.getIdentifier(); |
| 3802 | |
| 3803 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3804 | |
| 3805 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3806 | int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3807 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3808 | } catch (NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3809 | Log.d(TAG, "Package not found " + e.getMessage()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3810 | return false; |
| 3811 | } |
| 3812 | } |
| 3813 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3814 | // Returns package with oldest target SDK for given UID. |
| 3815 | private String getPackageNameForUid(int uid) { |
| 3816 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3817 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3818 | return null; |
| 3819 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3820 | String packageName = packageNames[0]; |
Fyodor Kupolov | 892fc8d | 2017-03-22 12:57:04 -0700 | [diff] [blame] | 3821 | if (packageNames.length == 1) { |
| 3822 | return packageName; |
| 3823 | } |
| 3824 | // 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] | 3825 | int oldestVersion = Integer.MAX_VALUE; |
| 3826 | for (String name : packageNames) { |
| 3827 | try { |
| 3828 | ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(name, 0); |
| 3829 | if (applicationInfo != null) { |
| 3830 | int version = applicationInfo.targetSdkVersion; |
| 3831 | if (version < oldestVersion) { |
| 3832 | oldestVersion = version; |
| 3833 | packageName = name; |
| 3834 | } |
| 3835 | } |
| 3836 | } catch (NameNotFoundException e) { |
| 3837 | // skip |
| 3838 | } |
| 3839 | } |
| 3840 | return packageName; |
| 3841 | } |
| 3842 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3843 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3844 | int uid) { |
| 3845 | if (packageName == null) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3846 | packageName = getPackageNameForUid(uid); |
| 3847 | if (packageName == null) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3848 | return false; |
| 3849 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | // Use null token which means any token. Having a token means the package |
| 3853 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3854 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3855 | return true; |
| 3856 | } |
| 3857 | // 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] | 3858 | // 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] | 3859 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3860 | int visibility = resolveAccountVisibility(account, packageName, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3861 | getUserAccounts(UserHandle.getUserId(uid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3862 | return (visibility == AccountManager.VISIBILITY_VISIBLE |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3863 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | @Override |
| 3867 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3868 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3869 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3870 | throw new SecurityException("Can be called only by system UID"); |
| 3871 | } |
| 3872 | |
| 3873 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3874 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3875 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3876 | |
| 3877 | final int userId = userHandle.getIdentifier(); |
| 3878 | |
| 3879 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3880 | |
| 3881 | final int uid; |
| 3882 | try { |
| 3883 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 3884 | } catch (NameNotFoundException e) { |
| 3885 | Slog.e(TAG, "Unknown package " + packageName); |
| 3886 | return null; |
| 3887 | } |
| 3888 | |
| 3889 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 3890 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3891 | final long identity = Binder.clearCallingIdentity(); |
| 3892 | try { |
| 3893 | return PendingIntent.getActivityAsUser( |
| 3894 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 3895 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 3896 | null, new UserHandle(userId)).getIntentSender(); |
| 3897 | } finally { |
| 3898 | Binder.restoreCallingIdentity(identity); |
| 3899 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 3903 | int uid, RemoteCallback callback) { |
| 3904 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 3905 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 3906 | @Override |
| 3907 | public void onResult(Bundle value) throws RemoteException { |
| 3908 | handleAuthenticatorResponse(true); |
| 3909 | } |
| 3910 | |
| 3911 | @Override |
| 3912 | public void onRequestContinued() { |
| 3913 | /* ignore */ |
| 3914 | } |
| 3915 | |
| 3916 | @Override |
| 3917 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 3918 | handleAuthenticatorResponse(false); |
| 3919 | } |
| 3920 | |
| 3921 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 3922 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3923 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3924 | UserHandle.getUserHandleForUid(uid)); |
| 3925 | if (callback != null) { |
| 3926 | Bundle result = new Bundle(); |
| 3927 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 3928 | callback.sendResult(result); |
| 3929 | } |
| 3930 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3931 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3935 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 3936 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 3937 | throw new SecurityException("Only system can check for accounts across users"); |
| 3938 | } |
| 3939 | final long token = Binder.clearCallingIdentity(); |
| 3940 | try { |
| 3941 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 3942 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 3943 | if (allAccounts[i].account.equals(account)) { |
| 3944 | return true; |
| 3945 | } |
| 3946 | } |
| 3947 | return false; |
| 3948 | } finally { |
| 3949 | Binder.restoreCallingIdentity(token); |
| 3950 | } |
| 3951 | } |
| 3952 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3953 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 3954 | private final String[] mFeatures; |
| 3955 | private volatile Account[] mAccountsOfType = null; |
| 3956 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 3957 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3958 | private final int mCallingUid; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3959 | private final String mPackageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3960 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3961 | public GetAccountsByTypeAndFeatureSession( |
| 3962 | UserAccounts accounts, |
| 3963 | IAccountManagerResponse response, |
| 3964 | String type, |
| 3965 | String[] features, |
| 3966 | int callingUid, |
| 3967 | String packageName) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3968 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3969 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3970 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3971 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3972 | mFeatures = features; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3973 | mPackageName = packageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3976 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3977 | public void run() throws RemoteException { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 3978 | mAccountsOfType = getAccountsFromCache(mAccounts, mAccountType, |
| 3979 | mCallingUid, mPackageName, false /* include managed not visible*/); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3980 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 3981 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3982 | mCurrentAccount = 0; |
| 3983 | |
| 3984 | checkAccount(); |
| 3985 | } |
| 3986 | |
| 3987 | public void checkAccount() { |
| 3988 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 3989 | sendResult(); |
| 3990 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3991 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3992 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3993 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 3994 | if (accountAuthenticator == null) { |
| 3995 | // It is possible that the authenticator has died, which is indicated by |
| 3996 | // mAuthenticator being set to null. If this happens then just abort. |
| 3997 | // There is no need to send back a result or error in this case since |
| 3998 | // that already happened when mAuthenticator was cleared. |
| 3999 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4000 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 4001 | + " connected to the authenticator, " + toDebugString()); |
| 4002 | } |
| 4003 | return; |
| 4004 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4005 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 4006 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4007 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4008 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4009 | } |
| 4010 | } |
| 4011 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4012 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4013 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4014 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4015 | mNumResults++; |
| 4016 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4017 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4018 | return; |
| 4019 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4020 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4021 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 4022 | } |
| 4023 | mCurrentAccount++; |
| 4024 | checkAccount(); |
| 4025 | } |
| 4026 | |
| 4027 | public void sendResult() { |
| 4028 | IAccountManagerResponse response = getResponseAndClose(); |
| 4029 | if (response != null) { |
| 4030 | try { |
| 4031 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 4032 | for (int i = 0; i < accounts.length; i++) { |
| 4033 | accounts[i] = mAccountsWithFeatures.get(i); |
| 4034 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4035 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4036 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 4037 | + response); |
| 4038 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4039 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4040 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4041 | response.onResult(result); |
| 4042 | } catch (RemoteException e) { |
| 4043 | // if the caller is dead then there is no one to care about remote exceptions |
| 4044 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4045 | Log.v(TAG, "failure while notifying response", e); |
| 4046 | } |
| 4047 | } |
| 4048 | } |
| 4049 | } |
| 4050 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4051 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4052 | protected String toDebugString(long now) { |
| 4053 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 4054 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 4055 | } |
| 4056 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4057 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4058 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4059 | * 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] | 4060 | * @hide |
| 4061 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4062 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4063 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4064 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4065 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4066 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4067 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4068 | if (visibleAccountTypes.isEmpty()) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4069 | return EMPTY_ACCOUNT_ARRAY; |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4070 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4071 | long identityToken = clearCallingIdentity(); |
| 4072 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4073 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4074 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4075 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4076 | callingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4077 | opPackageName, |
| 4078 | visibleAccountTypes, |
| 4079 | false /* includeUserManagedNotVisible */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4080 | } finally { |
| 4081 | restoreCallingIdentity(identityToken); |
| 4082 | } |
| 4083 | } |
| 4084 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4085 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4086 | * Returns accounts for all running users, ignores visibility values. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4087 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4088 | * @hide |
| 4089 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4090 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4091 | public AccountAndUser[] getRunningAccounts() { |
| 4092 | final int[] runningUserIds; |
| 4093 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4094 | runningUserIds = ActivityManager.getService().getRunningUserIds(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4095 | } catch (RemoteException e) { |
| 4096 | // Running in system_server; should never happen |
| 4097 | throw new RuntimeException(e); |
| 4098 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4099 | return getAccounts(runningUserIds); |
| 4100 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4101 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4102 | /** |
| 4103 | * Returns accounts for all users, ignores visibility values. |
| 4104 | * |
| 4105 | * @hide |
| 4106 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4107 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4108 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 4109 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4110 | final int[] userIds = new int[users.size()]; |
| 4111 | for (int i = 0; i < userIds.length; i++) { |
| 4112 | userIds[i] = users.get(i).id; |
| 4113 | } |
| 4114 | return getAccounts(userIds); |
| 4115 | } |
| 4116 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4117 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4118 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4119 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4120 | for (int userId : userIds) { |
| 4121 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4122 | if (userAccounts == null) continue; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4123 | Account[] accounts = getAccountsFromCache( |
| 4124 | userAccounts, |
| 4125 | null /* type */, |
| 4126 | Binder.getCallingUid(), |
| 4127 | null /* packageName */, |
| 4128 | false /* include managed not visible*/); |
| 4129 | for (Account account : accounts) { |
| 4130 | runningAccounts.add(new AccountAndUser(account, userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4131 | } |
| 4132 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4133 | |
| 4134 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4135 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4136 | } |
| 4137 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4138 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4139 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4140 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4141 | int callingUid = Binder.getCallingUid(); |
| 4142 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4143 | return getAccountsAsUserForPackage(type, userId, opPackageName /* callingPackage */, -1, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4144 | opPackageName, false /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4145 | } |
| 4146 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4147 | @NonNull |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4148 | private Account[] getAccountsAsUserForPackage( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4149 | String type, |
| 4150 | int userId, |
| 4151 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4152 | int packageUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4153 | String opPackageName, |
| 4154 | boolean includeUserManagedNotVisible) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4155 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4156 | // Only allow the system process to read accounts of other users |
| 4157 | if (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4158 | && callingUid != Process.SYSTEM_UID |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4159 | && mContext.checkCallingOrSelfPermission( |
| 4160 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4161 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4162 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4163 | + " trying to get account for " + userId); |
| 4164 | } |
| 4165 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4166 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4167 | Log.v(TAG, "getAccounts: accountType " + type |
| 4168 | + ", caller's uid " + Binder.getCallingUid() |
| 4169 | + ", pid " + Binder.getCallingPid()); |
| 4170 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4171 | |
| 4172 | // If the original calling app was using account choosing activity |
| 4173 | // provided by the framework or authenticator we'll passing in |
| 4174 | // the original caller's uid here, which is what should be used for filtering. |
| 4175 | List<String> managedTypes = |
| 4176 | getTypesManagedByCaller(callingUid, UserHandle.getUserId(callingUid)); |
| 4177 | if (packageUid != -1 && |
| 4178 | ((UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 4179 | || (type != null && managedTypes.contains(type))))) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4180 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4181 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4182 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4183 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4184 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4185 | if (visibleAccountTypes.isEmpty() |
| 4186 | || (type != null && !visibleAccountTypes.contains(type))) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4187 | return EMPTY_ACCOUNT_ARRAY; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4188 | } else if (visibleAccountTypes.contains(type)) { |
| 4189 | // Prune the list down to just the requested type. |
| 4190 | visibleAccountTypes = new ArrayList<>(); |
| 4191 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4192 | } // else aggregate all the visible accounts (it won't matter if the |
| 4193 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4194 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4195 | long identityToken = clearCallingIdentity(); |
| 4196 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4197 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 4198 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4199 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4200 | callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4201 | opPackageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4202 | visibleAccountTypes, |
| 4203 | includeUserManagedNotVisible); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4204 | } finally { |
| 4205 | restoreCallingIdentity(identityToken); |
| 4206 | } |
| 4207 | } |
| 4208 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4209 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4210 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4211 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4212 | int callingUid, |
| 4213 | String callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4214 | List<String> visibleAccountTypes, |
| 4215 | boolean includeUserManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4216 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4217 | for (String visibleType : visibleAccountTypes) { |
| 4218 | Account[] accountsForType = getAccountsFromCache( |
| 4219 | userAccounts, visibleType, callingUid, callingPackage, |
| 4220 | includeUserManagedNotVisible); |
| 4221 | if (accountsForType != null) { |
| 4222 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4223 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4224 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4225 | Account[] result = new Account[visibleAccounts.size()]; |
| 4226 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4227 | result[i] = visibleAccounts.get(i); |
| 4228 | } |
| 4229 | return result; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4232 | @Override |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4233 | public void addSharedAccountsFromParentUser(int parentUserId, int userId, |
| 4234 | String opPackageName) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4235 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4236 | Account[] accounts = getAccountsAsUser(null, parentUserId, opPackageName); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4237 | for (Account account : accounts) { |
| 4238 | addSharedAccountAsUser(account, userId); |
| 4239 | } |
| 4240 | } |
| 4241 | |
| 4242 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4243 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4244 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4245 | accounts.accountsDb.deleteSharedAccount(account); |
| 4246 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4247 | if (accountId < 0) { |
| 4248 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4249 | + ", skipping the DB insert failed"); |
| 4250 | return false; |
| 4251 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4252 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4253 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4254 | return true; |
| 4255 | } |
| 4256 | |
| 4257 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4258 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4259 | userId = handleIncomingUser(userId); |
| 4260 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4261 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4262 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4263 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4264 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4265 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4266 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4267 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4268 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4269 | } |
| 4270 | return r > 0; |
| 4271 | } |
| 4272 | |
| 4273 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4274 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4275 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4276 | } |
| 4277 | |
| 4278 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4279 | userId = handleIncomingUser(userId); |
| 4280 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4281 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4282 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4283 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4284 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4285 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4286 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4287 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4288 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | @Override |
| 4292 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4293 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4294 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4295 | synchronized (accounts.dbLock) { |
| 4296 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
| 4297 | Account[] accountArray = new Account[accountList.size()]; |
| 4298 | accountList.toArray(accountArray); |
| 4299 | return accountArray; |
| 4300 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4304 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4305 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4306 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4307 | } |
| 4308 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4309 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4310 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4311 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4312 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4313 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4314 | // Don't do opPackageName check - caller is system. |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4315 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4316 | + callingUid + " with uid=" + uid); |
| 4317 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4318 | return getAccountsAsUserForPackage(null, UserHandle.getCallingUserId(), packageName, uid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4319 | opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4320 | } |
| 4321 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4322 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4323 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4324 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4325 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4326 | int callingUid = Binder.getCallingUid(); |
| 4327 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4328 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4329 | int packageUid = -1; |
| 4330 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4331 | packageUid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4332 | } catch (NameNotFoundException re) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4333 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4334 | return EMPTY_ACCOUNT_ARRAY; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4335 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4336 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4337 | && (type != null && !isAccountManagedByCaller(type, callingUid, userId))) { |
| 4338 | return EMPTY_ACCOUNT_ARRAY; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4339 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4340 | return getAccountsAsUserForPackage(type, userId, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4341 | packageName, packageUid, opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4342 | } |
| 4343 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4344 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4345 | public void getAccountsByFeatures( |
| 4346 | IAccountManagerResponse response, |
| 4347 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4348 | String[] features, |
| 4349 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4350 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4351 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4352 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4353 | Log.v(TAG, "getAccounts: accountType " + type |
| 4354 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4355 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4356 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4357 | + ", pid " + Binder.getCallingPid()); |
| 4358 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4359 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4360 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4361 | int userId = UserHandle.getCallingUserId(); |
| 4362 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4363 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4364 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4365 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4366 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4367 | // Need to return just the accounts that are from matching signatures. |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4368 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, EMPTY_ACCOUNT_ARRAY); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4369 | try { |
| 4370 | response.onResult(result); |
| 4371 | } catch (RemoteException e) { |
| 4372 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4373 | } |
| 4374 | return; |
| 4375 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4376 | long identityToken = clearCallingIdentity(); |
| 4377 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4378 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4379 | if (features == null || features.length == 0) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4380 | Account[] accounts = getAccountsFromCache(userAccounts, type, callingUid, |
| 4381 | opPackageName, false); |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4382 | Bundle result = new Bundle(); |
| 4383 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4384 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4385 | return; |
| 4386 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4387 | new GetAccountsByTypeAndFeatureSession( |
| 4388 | userAccounts, |
| 4389 | response, |
| 4390 | type, |
| 4391 | features, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4392 | callingUid, |
| 4393 | opPackageName).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4394 | } finally { |
| 4395 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4396 | } |
| 4397 | } |
| 4398 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4399 | @Override |
| 4400 | public void onAccountAccessed(String token) throws RemoteException { |
| 4401 | final int uid = Binder.getCallingUid(); |
| 4402 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4403 | return; |
| 4404 | } |
| 4405 | final int userId = UserHandle.getCallingUserId(); |
| 4406 | final long identity = Binder.clearCallingIdentity(); |
| 4407 | try { |
| 4408 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4409 | if (Objects.equals(account.getAccessId(), token)) { |
| 4410 | // An app just accessed the account. At this point it knows about |
| 4411 | // 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] | 4412 | // Do we need to update account visibility here? |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4413 | if (!hasAccountAccess(account, null, uid)) { |
| 4414 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4415 | uid, true); |
| 4416 | } |
| 4417 | } |
| 4418 | } |
| 4419 | } finally { |
| 4420 | Binder.restoreCallingIdentity(identity); |
| 4421 | } |
| 4422 | } |
| 4423 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4424 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4425 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4426 | IAccountManagerResponse mResponse; |
| 4427 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4428 | final boolean mExpectActivityLaunch; |
| 4429 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4430 | final String mAccountName; |
| 4431 | // Indicates if we need to add auth details(like last credential time) |
| 4432 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4433 | // If set, we need to update the last authenticated time. This is |
| 4434 | // currently |
| 4435 | // used on |
| 4436 | // successful confirming credentials. |
| 4437 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4438 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4439 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4440 | private int mNumRequestContinued = 0; |
| 4441 | private int mNumErrors = 0; |
| 4442 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4443 | IAccountAuthenticator mAuthenticator = null; |
| 4444 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4445 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4446 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4447 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4448 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4449 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4450 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4451 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4452 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4453 | } |
| 4454 | |
| 4455 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4456 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4457 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4458 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4459 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4460 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4461 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4462 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4463 | mResponse = response; |
| 4464 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4465 | mExpectActivityLaunch = expectActivityLaunch; |
| 4466 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4467 | mAccountName = accountName; |
| 4468 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4469 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4470 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4471 | synchronized (mSessions) { |
| 4472 | mSessions.put(toString(), this); |
| 4473 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4474 | if (response != null) { |
| 4475 | try { |
| 4476 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4477 | } catch (RemoteException e) { |
| 4478 | mResponse = null; |
| 4479 | binderDied(); |
| 4480 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4481 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4482 | } |
| 4483 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4484 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4485 | if (mResponse == null) { |
| 4486 | // this session has already been closed |
| 4487 | return null; |
| 4488 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4489 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4490 | 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] | 4491 | return response; |
| 4492 | } |
| 4493 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4494 | /** |
| 4495 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4496 | * security policy. |
| 4497 | * |
| 4498 | * 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] | 4499 | * 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] | 4500 | * supplied entries in the system Settings app. |
| 4501 | */ |
| 4502 | protected void checkKeyIntent( |
| 4503 | int authUid, |
| 4504 | Intent intent) throws SecurityException { |
| 4505 | long bid = Binder.clearCallingIdentity(); |
| 4506 | try { |
| 4507 | PackageManager pm = mContext.getPackageManager(); |
| 4508 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 4509 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4510 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4511 | if (!isExportedSystemActivity(targetActivityInfo) |
| 4512 | && (PackageManager.SIGNATURE_MATCH != pm.checkSignatures(authUid, |
| 4513 | targetUid))) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4514 | String pkgName = targetActivityInfo.packageName; |
| 4515 | String activityName = targetActivityInfo.name; |
| 4516 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4517 | + "does not share a signature with the supplying authenticator (%s)."; |
| 4518 | throw new SecurityException( |
| 4519 | String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4520 | } |
| 4521 | } finally { |
| 4522 | Binder.restoreCallingIdentity(bid); |
| 4523 | } |
| 4524 | } |
| 4525 | |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4526 | private boolean isExportedSystemActivity(ActivityInfo activityInfo) { |
| 4527 | String className = activityInfo.name; |
| 4528 | return "android".equals(activityInfo.packageName) && |
| 4529 | (GrantCredentialsPermissionActivity.class.getName().equals(className) |
| 4530 | || CantAddAccountActivity.class.getName().equals(className)); |
| 4531 | } |
| 4532 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4533 | private void close() { |
| 4534 | synchronized (mSessions) { |
| 4535 | if (mSessions.remove(toString()) == null) { |
| 4536 | // the session was already closed, so bail out now |
| 4537 | return; |
| 4538 | } |
| 4539 | } |
| 4540 | if (mResponse != null) { |
| 4541 | // stop listening for response deaths |
| 4542 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4543 | |
| 4544 | // clear this so that we don't accidentally send any further results |
| 4545 | mResponse = null; |
| 4546 | } |
| 4547 | cancelTimeout(); |
| 4548 | unbind(); |
| 4549 | } |
| 4550 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4551 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4552 | public void binderDied() { |
| 4553 | mResponse = null; |
| 4554 | close(); |
| 4555 | } |
| 4556 | |
| 4557 | protected String toDebugString() { |
| 4558 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4559 | } |
| 4560 | |
| 4561 | protected String toDebugString(long now) { |
| 4562 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4563 | + ", connected " + (mAuthenticator != null) |
| 4564 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4565 | + "/" + mNumErrors + ")" |
| 4566 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4567 | } |
| 4568 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4569 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4570 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4571 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4572 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4573 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4574 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4575 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4576 | } |
| 4577 | } |
| 4578 | |
| 4579 | private void unbind() { |
| 4580 | if (mAuthenticator != null) { |
| 4581 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4582 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4583 | } |
| 4584 | } |
| 4585 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4586 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4587 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4588 | } |
| 4589 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4590 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4591 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4592 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4593 | try { |
| 4594 | run(); |
| 4595 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4596 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4597 | "remote exception"); |
| 4598 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4599 | } |
| 4600 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4601 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4602 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4603 | mAuthenticator = null; |
| 4604 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4605 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4606 | try { |
| 4607 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4608 | "disconnected"); |
| 4609 | } catch (RemoteException e) { |
| 4610 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4611 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4612 | + "caught RemoteException while responding", e); |
| 4613 | } |
| 4614 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4615 | } |
| 4616 | } |
| 4617 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4618 | public abstract void run() throws RemoteException; |
| 4619 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4620 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4621 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4622 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4623 | try { |
| 4624 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4625 | "timeout"); |
| 4626 | } catch (RemoteException e) { |
| 4627 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4628 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4629 | e); |
| 4630 | } |
| 4631 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4632 | } |
| 4633 | } |
| 4634 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4635 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4636 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4637 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4638 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4639 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4640 | if (result != null) { |
| 4641 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4642 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4643 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4644 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4645 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4646 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4647 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4648 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4649 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4650 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4651 | if (needUpdate || mAuthDetailsRequired) { |
| 4652 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4653 | if (needUpdate && accountPresent) { |
| 4654 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4655 | } |
| 4656 | if (mAuthDetailsRequired) { |
| 4657 | long lastAuthenticatedTime = -1; |
| 4658 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4659 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4660 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4661 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4662 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4663 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4664 | lastAuthenticatedTime); |
| 4665 | } |
| 4666 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4667 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4668 | if (result != null |
| 4669 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4670 | checkKeyIntent( |
| 4671 | Binder.getCallingUid(), |
| 4672 | intent); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4673 | } |
| 4674 | if (result != null |
| 4675 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4676 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4677 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4678 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4679 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4680 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4681 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4682 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4683 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4684 | IAccountManagerResponse response; |
| 4685 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4686 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4687 | response = mResponse; |
| 4688 | } else { |
| 4689 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4690 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4691 | if (response != null) { |
| 4692 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4693 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4694 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4695 | Log.v(TAG, getClass().getSimpleName() |
| 4696 | + " calling onError() on response " + response); |
| 4697 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4698 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4699 | "null bundle returned"); |
| 4700 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4701 | if (mStripAuthTokenFromResult) { |
| 4702 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4703 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4704 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4705 | Log.v(TAG, getClass().getSimpleName() |
| 4706 | + " calling onResult() on response " + response); |
| 4707 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4708 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4709 | (intent == null)) { |
| 4710 | // All AccountManager error codes are greater than 0 |
| 4711 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4712 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4713 | } else { |
| 4714 | response.onResult(result); |
| 4715 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4716 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4717 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4718 | // if the caller is dead then there is no one to care about remote exceptions |
| 4719 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4720 | Log.v(TAG, "failure while notifying response", e); |
| 4721 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4725 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4726 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4727 | public void onRequestContinued() { |
| 4728 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4729 | } |
| 4730 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4731 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4732 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4733 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4734 | IAccountManagerResponse response = getResponseAndClose(); |
| 4735 | if (response != null) { |
| 4736 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4737 | Log.v(TAG, getClass().getSimpleName() |
| 4738 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4739 | } |
| 4740 | try { |
| 4741 | response.onError(errorCode, errorMessage); |
| 4742 | } catch (RemoteException e) { |
| 4743 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4744 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4745 | } |
| 4746 | } |
| 4747 | } else { |
| 4748 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4749 | Log.v(TAG, "Session.onError: already closed"); |
| 4750 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4751 | } |
| 4752 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4753 | |
| 4754 | /** |
| 4755 | * find the component name for the authenticator and initiate a bind |
| 4756 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4757 | */ |
| 4758 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4759 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4760 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4761 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4762 | if (authenticatorInfo == null) { |
| 4763 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4764 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 4765 | + ", bailing out"); |
| 4766 | } |
| 4767 | return false; |
| 4768 | } |
| 4769 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 4770 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 4771 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 4772 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 4773 | + " which isn't encryption aware"); |
| 4774 | return false; |
| 4775 | } |
| 4776 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4777 | Intent intent = new Intent(); |
| 4778 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 4779 | intent.setComponent(authenticatorInfo.componentName); |
| 4780 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4781 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 4782 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 4783 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4784 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4785 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4786 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 4787 | } |
| 4788 | return false; |
| 4789 | } |
| 4790 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4791 | return true; |
| 4792 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 4795 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4796 | MessageHandler(Looper looper) { |
| 4797 | super(looper); |
| 4798 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4799 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4800 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4801 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4802 | switch (msg.what) { |
| 4803 | case MESSAGE_TIMED_OUT: |
| 4804 | Session session = (Session)msg.obj; |
| 4805 | session.onTimedOut(); |
| 4806 | break; |
| 4807 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4808 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 4809 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4810 | break; |
| 4811 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4812 | default: |
| 4813 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 4814 | } |
| 4815 | } |
| 4816 | } |
| 4817 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4818 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4819 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4822 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4823 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4824 | } |
| 4825 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4826 | /* |
| 4827 | * This function receives an opened writable database. |
| 4828 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4829 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4830 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4831 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4832 | } |
| 4833 | |
| 4834 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4835 | * 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] | 4836 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4837 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4838 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4839 | |
| 4840 | class LogRecordTask implements Runnable { |
| 4841 | private final String action; |
| 4842 | private final String tableName; |
| 4843 | private final long accountId; |
| 4844 | private final UserAccounts userAccount; |
| 4845 | private final int callingUid; |
| 4846 | private final long userDebugDbInsertionPoint; |
| 4847 | |
| 4848 | LogRecordTask(final String action, |
| 4849 | final String tableName, |
| 4850 | final long accountId, |
| 4851 | final UserAccounts userAccount, |
| 4852 | final int callingUid, |
| 4853 | final long userDebugDbInsertionPoint) { |
| 4854 | this.action = action; |
| 4855 | this.tableName = tableName; |
| 4856 | this.accountId = accountId; |
| 4857 | this.userAccount = userAccount; |
| 4858 | this.callingUid = callingUid; |
| 4859 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 4860 | } |
| 4861 | |
| 4862 | public void run() { |
| 4863 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 4864 | logStatement.bindLong(1, accountId); |
| 4865 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4866 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4867 | logStatement.bindLong(4, callingUid); |
| 4868 | logStatement.bindString(5, tableName); |
| 4869 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 4870 | logStatement.execute(); |
| 4871 | logStatement.clearBindings(); |
| 4872 | } |
| 4873 | } |
| 4874 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4875 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 4876 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4877 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4878 | % AccountsDb.MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4879 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | /* |
| 4883 | * This should only be called once to compile the sql statement for logging |
| 4884 | * and to find the insertion point. |
| 4885 | */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4886 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(UserAccounts userAccount) { |
| 4887 | userAccount.debugDbInsertionPoint = userAccount.accountsDb |
| 4888 | .calculateDebugTableInsertionPoint(); |
| 4889 | userAccount.statementForLogging = userAccount.accountsDb.compileSqlStatementForLogging(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4890 | } |
| 4891 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4892 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4893 | return asBinder(); |
| 4894 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4895 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4896 | /** |
| 4897 | * Searches array of arguments for the specified string |
| 4898 | * @param args array of argument strings |
| 4899 | * @param value value to search for |
| 4900 | * @return true if the value is contained in the array |
| 4901 | */ |
| 4902 | private static boolean scanArgs(String[] args, String value) { |
| 4903 | if (args != null) { |
| 4904 | for (String arg : args) { |
| 4905 | if (value.equals(arg)) { |
| 4906 | return true; |
| 4907 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4908 | } |
| 4909 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4910 | return false; |
| 4911 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4912 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4913 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4914 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 4915 | if (!DumpUtils.checkDumpPermission(mContext, TAG, fout)) return; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4916 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4917 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4918 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4919 | final List<UserInfo> users = getUserManager().getUsers(); |
| 4920 | for (UserInfo user : users) { |
| 4921 | ipw.println("User " + user + ":"); |
| 4922 | ipw.increaseIndent(); |
| 4923 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 4924 | ipw.println(); |
| 4925 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4926 | } |
| 4927 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4928 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4929 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 4930 | String[] args, boolean isCheckinRequest) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4931 | if (isCheckinRequest) { |
| 4932 | // This is a checkin request. *Only* upload the account types and the count of |
| 4933 | // each. |
| 4934 | synchronized (userAccounts.dbLock) { |
| 4935 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
| 4936 | } |
| 4937 | } else { |
| 4938 | Account[] accounts = getAccountsFromCache(userAccounts, null /* type */, |
| 4939 | Process.SYSTEM_UID, null /* packageName */, false); |
| 4940 | fout.println("Accounts: " + accounts.length); |
| 4941 | for (Account account : accounts) { |
| 4942 | fout.println(" " + account); |
| 4943 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4944 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4945 | // Add debug information. |
| 4946 | fout.println(); |
| 4947 | synchronized (userAccounts.dbLock) { |
| 4948 | userAccounts.accountsDb.dumpDebugTable(fout); |
| 4949 | } |
| 4950 | fout.println(); |
| 4951 | synchronized (mSessions) { |
| 4952 | final long now = SystemClock.elapsedRealtime(); |
| 4953 | fout.println("Active Sessions: " + mSessions.size()); |
| 4954 | for (Session session : mSessions.values()) { |
| 4955 | fout.println(" " + session.toDebugString(now)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 4956 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4957 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 4958 | |
| 4959 | fout.println(); |
| 4960 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4961 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4962 | } |
| 4963 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4964 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4965 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4966 | long identityToken = clearCallingIdentity(); |
| 4967 | try { |
| 4968 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4969 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 4970 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4971 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4972 | if (intent.getComponent() != null && |
| 4973 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 4974 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4975 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4976 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4977 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 4978 | final NotificationId id = getSigninRequiredNotificationId(accounts, account); |
| 4979 | intent.addCategory(id.mTag); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4980 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4981 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4982 | contextForUser.getText(R.string.notification_title).toString(); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 4983 | Notification n = |
| 4984 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4985 | .setWhen(0) |
| 4986 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 4987 | .setColor(contextForUser.getColor( |
| 4988 | com.android.internal.R.color.system_notification_accent_color)) |
| 4989 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 4990 | .setContentText(message) |
| 4991 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 4992 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4993 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4994 | .build(); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 4995 | installNotification(id, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4996 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4997 | } finally { |
| 4998 | restoreCallingIdentity(identityToken); |
| 4999 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5000 | } |
| 5001 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5002 | private void installNotification(NotificationId id, final Notification notification, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5003 | String packageName, int userId) { |
| 5004 | final long token = clearCallingIdentity(); |
| 5005 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5006 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5007 | try { |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5008 | notificationManager.enqueueNotificationWithTag(packageName, packageName, |
| 5009 | id.mTag, id.mId, notification, new int[1], userId); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5010 | } catch (RemoteException e) { |
| 5011 | /* ignore - local call */ |
| 5012 | } |
| 5013 | } finally { |
| 5014 | Binder.restoreCallingIdentity(token); |
| 5015 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5016 | } |
| 5017 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5018 | private void cancelNotification(NotificationId id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5019 | cancelNotification(id, mContext.getPackageName(), user); |
| 5020 | } |
| 5021 | |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5022 | private void cancelNotification(NotificationId id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5023 | long identityToken = clearCallingIdentity(); |
| 5024 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5025 | INotificationManager service = mInjector.getNotificationManager(); |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5026 | service.cancelNotificationWithTag(packageName, id.mTag, id.mId, user.getIdentifier()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5027 | } catch (RemoteException e) { |
| 5028 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 5029 | } finally { |
| 5030 | restoreCallingIdentity(identityToken); |
| 5031 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 5032 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5033 | |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5034 | private boolean isPermittedForPackage(String packageName, int uid, int userId, |
| 5035 | String... permissions) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5036 | final long identity = Binder.clearCallingIdentity(); |
| 5037 | try { |
| 5038 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 5039 | for (String perm : permissions) { |
| 5040 | if (pm.checkPermission(perm, packageName, userId) |
| 5041 | == PackageManager.PERMISSION_GRANTED) { |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5042 | // Checks runtime permission revocation. |
| 5043 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 5044 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 5045 | opCode, uid, packageName) == AppOpsManager.MODE_ALLOWED) { |
| 5046 | return true; |
| 5047 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5048 | } |
| 5049 | } |
| 5050 | } catch (RemoteException e) { |
| 5051 | /* ignore - local call */ |
| 5052 | } finally { |
| 5053 | Binder.restoreCallingIdentity(identity); |
| 5054 | } |
| 5055 | return false; |
| 5056 | } |
| 5057 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5058 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 5059 | for (String perm : permissions) { |
| 5060 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 5061 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5062 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 5063 | } |
| 5064 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 5065 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 5066 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 5067 | return true; |
| 5068 | } |
| 5069 | } |
| 5070 | } |
| 5071 | return false; |
| 5072 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5073 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5074 | private int handleIncomingUser(int userId) { |
| 5075 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 5076 | return ActivityManager.getService().handleIncomingUser( |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 5077 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 5078 | } catch (RemoteException re) { |
| 5079 | // Shouldn't happen, local. |
| 5080 | } |
| 5081 | return userId; |
| 5082 | } |
| 5083 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 5084 | private boolean isPrivileged(int callingUid) { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5085 | String[] packages; |
| 5086 | long identityToken = Binder.clearCallingIdentity(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5087 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5088 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5089 | } finally { |
| 5090 | Binder.restoreCallingIdentity(identityToken); |
| 5091 | } |
| 5092 | if (packages == null) { |
| 5093 | Log.d(TAG, "No packages for callingUid " + callingUid); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 5094 | return false; |
| 5095 | } |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5096 | for (String name : packages) { |
| 5097 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5098 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5099 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 5100 | && (packageInfo.applicationInfo.privateFlags |
| 5101 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5102 | return true; |
| 5103 | } |
| 5104 | } catch (PackageManager.NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5105 | Log.d(TAG, "Package not found " + e.getMessage()); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 5106 | return false; |
| 5107 | } |
| 5108 | } |
| 5109 | return false; |
| 5110 | } |
| 5111 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5112 | private boolean permissionIsGranted( |
| 5113 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5114 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 5115 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5116 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 5117 | } |
| 5118 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5119 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5120 | |
| 5121 | if (isPrivileged(callerUid)) { |
| 5122 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5123 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5124 | + callerUid + " privileged"); |
| 5125 | } |
| 5126 | return true; |
| 5127 | } |
| 5128 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 5129 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5130 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5131 | + callerUid + " manages the account"); |
| 5132 | } |
| 5133 | return true; |
| 5134 | } |
| 5135 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5136 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5137 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5138 | + callerUid + " user granted access"); |
| 5139 | } |
| 5140 | return true; |
| 5141 | } |
| 5142 | |
| 5143 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5144 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5145 | } |
| 5146 | |
| 5147 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5148 | } |
| 5149 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5150 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5151 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5152 | if (accountType == null) { |
| 5153 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5154 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5155 | return getTypesVisibleToCaller(callingUid, userId, |
| 5156 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5157 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5158 | } |
| 5159 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5160 | // Method checks visibility for applications targeing API level below {@link |
| 5161 | // android.os.Build.VERSION_CODES#O}, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5162 | // returns true if the the app has GET_ACCOUNTS or GET_ACCOUNTS_PRIVILEGED permission. |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5163 | private boolean checkGetAccountsPermission(String packageName, int uid, int userId) { |
| 5164 | return isPermittedForPackage(packageName, uid, userId, Manifest.permission.GET_ACCOUNTS, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5165 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 5166 | } |
| 5167 | |
Dmitry Dementyev | d6f0672 | 2017-04-05 12:43:26 -0700 | [diff] [blame] | 5168 | private boolean checkReadContactsPermission(String packageName, int uid, int userId) { |
| 5169 | return isPermittedForPackage(packageName, uid, userId, Manifest.permission.READ_CONTACTS); |
| 5170 | } |
| 5171 | |
| 5172 | // Heuristic to check that account type may be associated with some contacts data and |
| 5173 | // therefore READ_CONTACTS permission grants the access to account by default. |
| 5174 | private boolean accountTypeManagesContacts(String accountType, int userId) { |
| 5175 | if (accountType == null) { |
| 5176 | return false; |
| 5177 | } |
| 5178 | long identityToken = Binder.clearCallingIdentity(); |
| 5179 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5180 | try { |
| 5181 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5182 | } finally { |
| 5183 | Binder.restoreCallingIdentity(identityToken); |
| 5184 | } |
| 5185 | // Check contacts related permissions for authenticator. |
| 5186 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5187 | : serviceInfos) { |
| 5188 | if (accountType.equals(serviceInfo.type.type)) { |
| 5189 | return isPermittedForPackage(serviceInfo.type.packageName, serviceInfo.uid, userId, |
| 5190 | Manifest.permission.WRITE_CONTACTS); |
| 5191 | } |
| 5192 | } |
| 5193 | return false; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | /** |
| 5197 | * Method checks package uid and signature with Authenticator which manages accountType. |
| 5198 | * |
| 5199 | * @return SIGNATURE_CHECK_UID_MATCH for uid match, SIGNATURE_CHECK_MATCH for signature match, |
| 5200 | * SIGNATURE_CHECK_MISMATCH otherwise. |
| 5201 | */ |
| 5202 | private int checkPackageSignature(String accountType, int callingUid, int userId) { |
| 5203 | if (accountType == null) { |
| 5204 | return SIGNATURE_CHECK_MISMATCH; |
| 5205 | } |
| 5206 | |
| 5207 | long identityToken = Binder.clearCallingIdentity(); |
| 5208 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5209 | try { |
| 5210 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5211 | } finally { |
| 5212 | Binder.restoreCallingIdentity(identityToken); |
| 5213 | } |
| 5214 | // Check for signature match with Authenticator. |
| 5215 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5216 | : serviceInfos) { |
| 5217 | if (accountType.equals(serviceInfo.type.type)) { |
| 5218 | if (serviceInfo.uid == callingUid) { |
| 5219 | return SIGNATURE_CHECK_UID_MATCH; |
| 5220 | } |
| 5221 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 5222 | if (sigChk == PackageManager.SIGNATURE_MATCH) { |
| 5223 | return SIGNATURE_CHECK_MATCH; |
| 5224 | } |
| 5225 | } |
| 5226 | } |
| 5227 | return SIGNATURE_CHECK_MISMATCH; |
| 5228 | } |
| 5229 | |
| 5230 | // returns true for applications with the same signature as authenticator. |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5231 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5232 | if (accountType == null) { |
| 5233 | return false; |
| 5234 | } else { |
| 5235 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5236 | } |
| 5237 | } |
| 5238 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5239 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5240 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5241 | return getTypesForCaller(callingUid, userId, true /* isOtherwisePermitted*/); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
Dmitry Dementyev | 2e22cfb | 2017-01-09 18:42:14 +0000 | [diff] [blame] | 5245 | return getTypesForCaller(callingUid, userId, false); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
| 5248 | private List<String> getTypesForCaller( |
| 5249 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5250 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5251 | long identityToken = Binder.clearCallingIdentity(); |
| 5252 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5253 | try { |
| 5254 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5255 | } finally { |
| 5256 | Binder.restoreCallingIdentity(identityToken); |
| 5257 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5258 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5259 | serviceInfos) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5260 | if (isOtherwisePermitted || (mPackageManager.checkSignatures(serviceInfo.uid, |
| 5261 | callingUid) == PackageManager.SIGNATURE_MATCH)) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5262 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5263 | } |
| 5264 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5265 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5266 | } |
| 5267 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5268 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5269 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5270 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5271 | if (account.name.equals(accountName)) { |
| 5272 | return true; |
| 5273 | } |
| 5274 | } |
| 5275 | } |
| 5276 | return false; |
| 5277 | } |
| 5278 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5279 | private static void checkManageUsersPermission(String message) { |
| 5280 | if (ActivityManager.checkComponentPermission( |
| 5281 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5282 | != PackageManager.PERMISSION_GRANTED) { |
| 5283 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5284 | } |
| 5285 | } |
| 5286 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5287 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5288 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5289 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5290 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5291 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5292 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5293 | + message); |
| 5294 | } |
| 5295 | } |
| 5296 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5297 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5298 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5299 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5300 | return true; |
| 5301 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5302 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5303 | synchronized (accounts.dbLock) { |
| 5304 | synchronized (accounts.cacheLock) { |
| 5305 | long grantsCount; |
| 5306 | if (authTokenType != null) { |
| 5307 | grantsCount = accounts.accountsDb |
| 5308 | .findMatchingGrantsCount(callerUid, authTokenType, account); |
| 5309 | } else { |
| 5310 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
| 5311 | account); |
| 5312 | } |
| 5313 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5314 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5315 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5316 | // TODO: Skip this check when running automated tests. Replace this |
| 5317 | // with a more general solution. |
| 5318 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
| 5319 | + authTokenType + " by uid " + callerUid |
| 5320 | + " but ignoring since device is in test harness."); |
| 5321 | return true; |
| 5322 | } |
| 5323 | return permissionGranted; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5324 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5325 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5326 | } |
| 5327 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5328 | private boolean isSystemUid(int callingUid) { |
| 5329 | String[] packages = null; |
| 5330 | long ident = Binder.clearCallingIdentity(); |
| 5331 | try { |
| 5332 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5333 | } finally { |
| 5334 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5335 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5336 | if (packages != null) { |
| 5337 | for (String name : packages) { |
| 5338 | try { |
| 5339 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5340 | if (packageInfo != null |
| 5341 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5342 | != 0) { |
| 5343 | return true; |
| 5344 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5345 | } catch (NameNotFoundException e) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5346 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5347 | } |
| 5348 | } |
| 5349 | } else { |
| 5350 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5351 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5352 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5355 | /** Succeeds if any of the specified permissions are granted. */ |
| 5356 | private void checkReadAccountsPermitted( |
| 5357 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5358 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5359 | int userId, |
| 5360 | String opPackageName) { |
| 5361 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5362 | String msg = String.format( |
| 5363 | "caller uid %s cannot access %s accounts", |
| 5364 | callingUid, |
| 5365 | accountType); |
| 5366 | Log.w(TAG, " " + msg); |
| 5367 | throw new SecurityException(msg); |
| 5368 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5369 | } |
| 5370 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5371 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5372 | // the managing app can always modify accounts |
| 5373 | if (isProfileOwner(callingUid)) { |
| 5374 | return true; |
| 5375 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5376 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5377 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5378 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5379 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5380 | return true; |
| 5381 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5382 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5383 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5384 | // the managing app can always modify accounts |
| 5385 | if (isProfileOwner(callingUid)) { |
| 5386 | return true; |
| 5387 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5388 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5389 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5390 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5391 | if (typesArray == null) { |
| 5392 | return true; |
| 5393 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5394 | for (String forbiddenType : typesArray) { |
| 5395 | if (forbiddenType.equals(accountType)) { |
| 5396 | return false; |
| 5397 | } |
| 5398 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5399 | return true; |
| 5400 | } |
| 5401 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5402 | private boolean isProfileOwner(int uid) { |
| 5403 | final DevicePolicyManagerInternal dpmi = |
| 5404 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5405 | return (dpmi != null) |
| 5406 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5407 | } |
| 5408 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5409 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5410 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5411 | throws RemoteException { |
| 5412 | final int callingUid = getCallingUid(); |
| 5413 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5414 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5415 | throw new SecurityException(); |
| 5416 | } |
| 5417 | |
| 5418 | if (value) { |
| 5419 | grantAppPermission(account, authTokenType, uid); |
| 5420 | } else { |
| 5421 | revokeAppPermission(account, authTokenType, uid); |
| 5422 | } |
| 5423 | } |
| 5424 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5425 | /** |
| 5426 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5427 | * <p> |
| 5428 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5429 | * which is in the system. This means we don't need to protect it with permissions. |
| 5430 | * @hide |
| 5431 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5432 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5433 | if (account == null || authTokenType == null) { |
| 5434 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5435 | return; |
| 5436 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5437 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5438 | synchronized (accounts.dbLock) { |
| 5439 | synchronized (accounts.cacheLock) { |
| 5440 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5441 | if (accountId >= 0) { |
| 5442 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
| 5443 | } |
| 5444 | cancelNotification( |
| 5445 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5446 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5447 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5448 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
| 5449 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5450 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5451 | |
| 5452 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5453 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5454 | : mAppPermissionChangeListeners) { |
| 5455 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5456 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | /** |
| 5460 | * Don't allow callers with the given uid permission to get credentials for |
| 5461 | * account/authTokenType. |
| 5462 | * <p> |
| 5463 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5464 | * which is in the system. This means we don't need to protect it with permissions. |
| 5465 | * @hide |
| 5466 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5467 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5468 | if (account == null || authTokenType == null) { |
| 5469 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5470 | return; |
| 5471 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5472 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5473 | synchronized (accounts.dbLock) { |
| 5474 | synchronized (accounts.cacheLock) { |
| 5475 | accounts.accountsDb.beginTransaction(); |
| 5476 | try { |
| 5477 | long accountId = accounts.accountsDb.findDeAccountId(account); |
| 5478 | if (accountId >= 0) { |
| 5479 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5480 | accountId, authTokenType, uid); |
| 5481 | accounts.accountsDb.setTransactionSuccessful(); |
| 5482 | } |
| 5483 | } finally { |
| 5484 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5485 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5486 | |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5487 | cancelNotification( |
| 5488 | getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5489 | UserHandle.of(accounts.userId)); |
| 5490 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5491 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5492 | |
| 5493 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5494 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5495 | : mAppPermissionChangeListeners) { |
| 5496 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5497 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5498 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5499 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5500 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5501 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5502 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5503 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5504 | for (Account curAccount : oldAccountsForType) { |
| 5505 | if (!curAccount.equals(account)) { |
| 5506 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5507 | } |
| 5508 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5509 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5510 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5511 | } else { |
| 5512 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5513 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5514 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5515 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5516 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5517 | accounts.userDataCache.remove(account); |
| 5518 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5519 | accounts.previousNameCache.remove(account); |
Dmitry Dementyev | 71fa526 | 2017-03-23 12:29:17 -0700 | [diff] [blame] | 5520 | accounts.visibilityCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5521 | } |
| 5522 | |
| 5523 | /** |
| 5524 | * 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] | 5525 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5526 | * processes and if you will return this account to apps you should return the result. |
| 5527 | * @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] | 5528 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5529 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5530 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5531 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5532 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5533 | if (accountsForType != null) { |
| 5534 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5535 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5536 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5537 | : UUID.randomUUID().toString(); |
| 5538 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5539 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5540 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5541 | } |
| 5542 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5543 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5544 | private Account[] filterAccounts(UserAccounts accounts, Account[] unfiltered, int callingUid, |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5545 | @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5546 | String visibilityFilterPackage = callingPackage; |
| 5547 | if (visibilityFilterPackage == null) { |
| 5548 | visibilityFilterPackage = getPackageNameForUid(callingUid); |
| 5549 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5550 | Map<Account, Integer> firstPass = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5551 | for (Account account : unfiltered) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5552 | int visibility = resolveAccountVisibility(account, visibilityFilterPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5553 | if ((visibility == AccountManager.VISIBILITY_VISIBLE |
| 5554 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) |
| 5555 | || (includeManagedNotVisible |
| 5556 | && (visibility |
| 5557 | == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE))) { |
| 5558 | firstPass.put(account, visibility); |
| 5559 | } |
| 5560 | } |
| 5561 | Map<Account, Integer> secondPass = |
| 5562 | filterSharedAccounts(accounts, firstPass, callingUid, callingPackage); |
| 5563 | |
| 5564 | Account[] filtered = new Account[secondPass.size()]; |
| 5565 | filtered = secondPass.keySet().toArray(filtered); |
| 5566 | return filtered; |
| 5567 | } |
| 5568 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5569 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5570 | private Map<Account, Integer> filterSharedAccounts(UserAccounts userAccounts, |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5571 | @NonNull Map<Account, Integer> unfiltered, int callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5572 | @Nullable String callingPackage) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5573 | // first part is to filter shared accounts. |
| 5574 | // unfiltered type check is not necessary. |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5575 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5576 | || callingUid == Process.SYSTEM_UID) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5577 | return unfiltered; |
| 5578 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5579 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5580 | if (user != null && user.isRestricted()) { |
Dmitry Dementyev | 16e3789 | 2017-03-22 13:13:40 -0700 | [diff] [blame] | 5581 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5582 | if (packages == null) { |
| 5583 | packages = new String[] {}; |
| 5584 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5585 | // 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] | 5586 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5587 | // This might be a temporary way to specify a visible list |
| 5588 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5589 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5590 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5591 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5592 | return unfiltered; |
| 5593 | } |
| 5594 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5595 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5596 | if (ArrayUtils.isEmpty(sharedAccounts)) { |
| 5597 | return unfiltered; |
| 5598 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5599 | String requiredAccountType = ""; |
| 5600 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5601 | // If there's an explicit callingPackage specified, check if that package |
| 5602 | // opted in to see restricted accounts. |
| 5603 | if (callingPackage != null) { |
| 5604 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5605 | if (pi != null && pi.restrictedAccountType != null) { |
| 5606 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5607 | } |
| 5608 | } else { |
| 5609 | // Otherwise check if the callingUid has a package that has opted in |
| 5610 | for (String packageName : packages) { |
| 5611 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5612 | if (pi != null && pi.restrictedAccountType != null) { |
| 5613 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5614 | break; |
| 5615 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5616 | } |
| 5617 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5618 | } catch (NameNotFoundException e) { |
| 5619 | Log.d(TAG, "Package not found " + e.getMessage()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5620 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5621 | Map<Account, Integer> filtered = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5622 | for (Map.Entry<Account, Integer> entry : unfiltered.entrySet()) { |
| 5623 | Account account = entry.getKey(); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5624 | if (account.type.equals(requiredAccountType)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5625 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5626 | } else { |
| 5627 | boolean found = false; |
| 5628 | for (Account shared : sharedAccounts) { |
| 5629 | if (shared.equals(account)) { |
| 5630 | found = true; |
| 5631 | break; |
| 5632 | } |
| 5633 | } |
| 5634 | if (!found) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5635 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5636 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5637 | } |
| 5638 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5639 | return filtered; |
| 5640 | } else { |
| 5641 | return unfiltered; |
| 5642 | } |
| 5643 | } |
| 5644 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5645 | /* |
| 5646 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5647 | * that the package is not allowed to access. |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5648 | * |
| 5649 | * <p>The method shouldn't be called with UserAccounts#cacheLock held, otherwise it will cause a |
| 5650 | * deadlock |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5651 | */ |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5652 | @NonNull |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5653 | protected Account[] getAccountsFromCache(UserAccounts userAccounts, String accountType, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5654 | int callingUid, @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5655 | Preconditions.checkState(!Thread.holdsLock(userAccounts.cacheLock), |
| 5656 | "Method should not be called with cacheLock"); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5657 | if (accountType != null) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5658 | Account[] accounts; |
| 5659 | synchronized (userAccounts.cacheLock) { |
| 5660 | accounts = userAccounts.accountCache.get(accountType); |
| 5661 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5662 | if (accounts == null) { |
| 5663 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5664 | } else { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5665 | return filterAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
| 5666 | callingUid, callingPackage, includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5667 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5668 | } else { |
| 5669 | int totalLength = 0; |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5670 | Account[] accountsArray; |
| 5671 | synchronized (userAccounts.cacheLock) { |
| 5672 | for (Account[] accounts : userAccounts.accountCache.values()) { |
| 5673 | totalLength += accounts.length; |
| 5674 | } |
| 5675 | if (totalLength == 0) { |
| 5676 | return EMPTY_ACCOUNT_ARRAY; |
| 5677 | } |
| 5678 | accountsArray = new Account[totalLength]; |
| 5679 | totalLength = 0; |
| 5680 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
| 5681 | System.arraycopy(accountsOfType, 0, accountsArray, totalLength, |
| 5682 | accountsOfType.length); |
| 5683 | totalLength += accountsOfType.length; |
| 5684 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5685 | } |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5686 | return filterAccounts(userAccounts, accountsArray, callingUid, callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5687 | includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5688 | } |
| 5689 | } |
| 5690 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 5691 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5692 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5693 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5694 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5695 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5696 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5697 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5698 | } |
| 5699 | if (value == null) { |
| 5700 | userDataForAccount.remove(key); |
| 5701 | } else { |
| 5702 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5703 | } |
| 5704 | } |
| 5705 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5706 | protected String readCachedTokenInternal( |
| 5707 | UserAccounts accounts, |
| 5708 | Account account, |
| 5709 | String tokenType, |
| 5710 | String callingPackage, |
| 5711 | byte[] pkgSigDigest) { |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5712 | synchronized (accounts.dbLock) { |
| 5713 | synchronized (accounts.cacheLock) { |
| 5714 | return accounts.accountTokenCaches.get( |
| 5715 | account, tokenType, callingPackage, pkgSigDigest); |
| 5716 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5717 | } |
| 5718 | } |
| 5719 | |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5720 | /** protected by the {@code dbLock}, {@code cacheLock} */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5721 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5722 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5723 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5724 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5725 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5726 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5727 | } |
| 5728 | if (value == null) { |
| 5729 | authTokensForAccount.remove(key); |
| 5730 | } else { |
| 5731 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5732 | } |
| 5733 | } |
| 5734 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5735 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 5736 | String authTokenType) { |
Fyodor Kupolov | 9ac40f1 | 2017-03-28 19:11:17 -0700 | [diff] [blame] | 5737 | // Fast path - check if account is already cached |
| 5738 | synchronized (accounts.cacheLock) { |
| 5739 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 5740 | if (authTokensForAccount != null) { |
| 5741 | return authTokensForAccount.get(authTokenType); |
| 5742 | } |
| 5743 | } |
| 5744 | // If not cached yet - do slow path and sync with db if necessary |
Fyodor Kupolov | 8cd927d | 2017-03-27 17:02:11 -0700 | [diff] [blame] | 5745 | synchronized (accounts.dbLock) { |
| 5746 | synchronized (accounts.cacheLock) { |
| 5747 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
| 5748 | if (authTokensForAccount == null) { |
| 5749 | // need to populate the cache for this account |
| 5750 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
| 5751 | accounts.authTokenCache.put(account, authTokensForAccount); |
| 5752 | } |
| 5753 | return authTokensForAccount.get(authTokenType); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5754 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5755 | } |
| 5756 | } |
| 5757 | |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 5758 | private String readUserDataInternal(UserAccounts accounts, Account account, String key) { |
| 5759 | Map<String, String> userDataForAccount; |
| 5760 | // Fast path - check if data is already cached |
| 5761 | synchronized (accounts.cacheLock) { |
| 5762 | userDataForAccount = accounts.userDataCache.get(account); |
| 5763 | } |
| 5764 | // If not cached yet - do slow path and sync with db if necessary |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5765 | if (userDataForAccount == null) { |
Fyodor Kupolov | 3d73499 | 2017-03-29 17:28:52 -0700 | [diff] [blame] | 5766 | synchronized (accounts.dbLock) { |
| 5767 | synchronized (accounts.cacheLock) { |
| 5768 | userDataForAccount = accounts.userDataCache.get(account); |
| 5769 | if (userDataForAccount == null) { |
| 5770 | // need to populate the cache for this account |
| 5771 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
| 5772 | accounts.userDataCache.put(account, userDataForAccount); |
| 5773 | } |
| 5774 | } |
| 5775 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5776 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5777 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5778 | } |
| 5779 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5780 | private Context getContextForUser(UserHandle user) { |
| 5781 | try { |
| 5782 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 5783 | } catch (NameNotFoundException e) { |
| 5784 | // Default to mContext, not finding the package system is running as is unlikely. |
| 5785 | return mContext; |
| 5786 | } |
| 5787 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 5788 | |
| 5789 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 5790 | try { |
| 5791 | response.onResult(result); |
| 5792 | } catch (RemoteException e) { |
| 5793 | // if the caller is dead then there is no one to care about remote |
| 5794 | // exceptions |
| 5795 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5796 | Log.v(TAG, "failure while notifying response", e); |
| 5797 | } |
| 5798 | } |
| 5799 | } |
| 5800 | |
| 5801 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 5802 | String errorMessage) { |
| 5803 | try { |
| 5804 | response.onError(errorCode, errorMessage); |
| 5805 | } catch (RemoteException e) { |
| 5806 | // if the caller is dead then there is no one to care about remote |
| 5807 | // exceptions |
| 5808 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5809 | Log.v(TAG, "failure while notifying response", e); |
| 5810 | } |
| 5811 | } |
| 5812 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5813 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5814 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5815 | private final Object mLock = new Object(); |
| 5816 | |
| 5817 | @GuardedBy("mLock") |
| 5818 | private AccountManagerBackupHelper mBackupHelper; |
| 5819 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5820 | @Override |
| 5821 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 5822 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 5823 | if (account == null) { |
| 5824 | Slog.w(TAG, "account cannot be null"); |
| 5825 | return; |
| 5826 | } |
| 5827 | if (packageName == null) { |
| 5828 | Slog.w(TAG, "packageName cannot be null"); |
| 5829 | return; |
| 5830 | } |
| 5831 | if (userId < UserHandle.USER_SYSTEM) { |
| 5832 | Slog.w(TAG, "user id must be concrete"); |
| 5833 | return; |
| 5834 | } |
| 5835 | if (callback == null) { |
| 5836 | Slog.w(TAG, "callback cannot be null"); |
| 5837 | return; |
| 5838 | } |
| 5839 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5840 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 5841 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5842 | Bundle result = new Bundle(); |
| 5843 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 5844 | callback.sendResult(result); |
| 5845 | return; |
| 5846 | } |
| 5847 | |
| 5848 | final int uid; |
| 5849 | try { |
| 5850 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 5851 | } catch (NameNotFoundException e) { |
| 5852 | Slog.e(TAG, "Unknown package " + packageName); |
| 5853 | return; |
| 5854 | } |
| 5855 | |
| 5856 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5857 | final UserAccounts userAccounts; |
| 5858 | synchronized (mUsers) { |
| 5859 | userAccounts = mUsers.get(userId); |
| 5860 | } |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 5861 | SystemNotificationChannels.createAccountChannelForPackage(packageName, uid, mContext); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5862 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 5863 | } |
| 5864 | |
| 5865 | @Override |
| 5866 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 5867 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5868 | mAppPermissionChangeListeners.add(listener); |
| 5869 | } |
| 5870 | |
| 5871 | @Override |
| 5872 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 5873 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5874 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5875 | |
| 5876 | @Override |
| 5877 | public byte[] backupAccountAccessPermissions(int userId) { |
| 5878 | synchronized (mLock) { |
| 5879 | if (mBackupHelper == null) { |
| 5880 | mBackupHelper = new AccountManagerBackupHelper( |
| 5881 | AccountManagerService.this, this); |
| 5882 | } |
| 5883 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 5884 | } |
| 5885 | } |
| 5886 | |
| 5887 | @Override |
| 5888 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 5889 | synchronized (mLock) { |
| 5890 | if (mBackupHelper == null) { |
| 5891 | mBackupHelper = new AccountManagerBackupHelper( |
| 5892 | AccountManagerService.this, this); |
| 5893 | } |
| 5894 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 5895 | } |
| 5896 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5897 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5898 | |
| 5899 | @VisibleForTesting |
| 5900 | static class Injector { |
| 5901 | private final Context mContext; |
| 5902 | |
| 5903 | public Injector(Context context) { |
| 5904 | mContext = context; |
| 5905 | } |
| 5906 | |
| 5907 | Looper getMessageHandlerLooper() { |
| 5908 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 5909 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 5910 | serviceThread.start(); |
| 5911 | return serviceThread.getLooper(); |
| 5912 | } |
| 5913 | |
| 5914 | Context getContext() { |
| 5915 | return mContext; |
| 5916 | } |
| 5917 | |
| 5918 | void addLocalService(AccountManagerInternal service) { |
| 5919 | LocalServices.addService(AccountManagerInternal.class, service); |
| 5920 | } |
| 5921 | |
| 5922 | String getDeDatabaseName(int userId) { |
| 5923 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 5924 | AccountsDb.DE_DATABASE_NAME); |
| 5925 | return databaseFile.getPath(); |
| 5926 | } |
| 5927 | |
| 5928 | String getCeDatabaseName(int userId) { |
| 5929 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 5930 | AccountsDb.CE_DATABASE_NAME); |
| 5931 | return databaseFile.getPath(); |
| 5932 | } |
| 5933 | |
| 5934 | String getPreNDatabaseName(int userId) { |
| 5935 | File systemDir = Environment.getDataSystemDirectory(); |
| 5936 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 5937 | PRE_N_DATABASE_NAME); |
| 5938 | if (userId == 0) { |
| 5939 | // Migrate old file, if it exists, to the new location. |
| 5940 | // 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] | 5941 | // accidentally created in the old location, |
| 5942 | // causing the new one to become corrupted as well. |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5943 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 5944 | if (oldFile.exists() && !databaseFile.exists()) { |
| 5945 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 5946 | File userDir = Environment.getUserSystemDirectory(userId); |
| 5947 | if (!userDir.exists()) { |
| 5948 | if (!userDir.mkdirs()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5949 | throw new IllegalStateException( |
| 5950 | "User dir cannot be created: " + userDir); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5951 | } |
| 5952 | } |
| 5953 | if (!oldFile.renameTo(databaseFile)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5954 | throw new IllegalStateException( |
| 5955 | "User dir cannot be migrated: " + databaseFile); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5956 | } |
| 5957 | } |
| 5958 | } |
| 5959 | return databaseFile.getPath(); |
| 5960 | } |
| 5961 | |
| 5962 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 5963 | return new AccountAuthenticatorCache(mContext); |
| 5964 | } |
| 5965 | |
| 5966 | INotificationManager getNotificationManager() { |
| 5967 | return NotificationManager.getService(); |
| 5968 | } |
| 5969 | } |
Chris Wren | 717a881 | 2017-03-31 15:34:39 -0400 | [diff] [blame] | 5970 | |
| 5971 | private class NotificationId { |
| 5972 | final String mTag; |
| 5973 | private final int mId; |
| 5974 | |
| 5975 | NotificationId(String tag, int type) { |
| 5976 | mTag = tag; |
| 5977 | mId = type; |
| 5978 | } |
| 5979 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5980 | } |