Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.accounts; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 18 | |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 20 | import android.accounts.AbstractAccountAuthenticator; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 21 | import android.accounts.Account; |
| 22 | import android.accounts.AccountAndUser; |
| 23 | import android.accounts.AccountAuthenticatorResponse; |
| 24 | import android.accounts.AccountManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 25 | import android.accounts.AccountManagerInternal; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 26 | import android.accounts.AuthenticatorDescription; |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 27 | import android.accounts.CantAddAccountActivity; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 28 | import android.accounts.GrantCredentialsPermissionActivity; |
| 29 | import android.accounts.IAccountAuthenticator; |
| 30 | import android.accounts.IAccountAuthenticatorResponse; |
| 31 | import android.accounts.IAccountManager; |
| 32 | import android.accounts.IAccountManagerResponse; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 33 | import android.annotation.IntRange; |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 34 | import android.annotation.NonNull; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 35 | import android.annotation.Nullable; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 36 | import android.app.ActivityManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 37 | import android.app.ActivityThread; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 38 | import android.app.AppOpsManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 39 | import android.app.INotificationManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 40 | import android.app.Notification; |
| 41 | import android.app.NotificationManager; |
| 42 | import android.app.PendingIntent; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 43 | import android.app.admin.DeviceAdminInfo; |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 44 | import android.app.admin.DevicePolicyManager; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 45 | import android.app.admin.DevicePolicyManagerInternal; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 46 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 47 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 48 | import android.content.Context; |
| 49 | import android.content.Intent; |
| 50 | import android.content.IntentFilter; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 51 | import android.content.IntentSender; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 52 | import android.content.ServiceConnection; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 53 | import android.content.pm.ActivityInfo; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 54 | import android.content.pm.ApplicationInfo; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 55 | import android.content.pm.IPackageManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 56 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 57 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 58 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 59 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 60 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 61 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 62 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 63 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 64 | import android.database.Cursor; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 65 | import android.database.sqlite.SQLiteStatement; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 66 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 67 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 68 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 69 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 70 | import android.os.IBinder; |
| 71 | import android.os.Looper; |
| 72 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 73 | import android.os.Parcel; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 74 | import android.os.Process; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 75 | import android.os.RemoteCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 76 | import android.os.RemoteException; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 77 | import android.os.StrictMode; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 78 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 79 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 80 | import android.os.UserManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 81 | import android.text.TextUtils; |
| 82 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 83 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 84 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 85 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 86 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 87 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 88 | import com.android.internal.R; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 89 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 90 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 91 | import com.android.internal.content.PackageMonitor; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 92 | import com.android.internal.notification.SystemNotificationChannels; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 93 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 94 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 95 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 96 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 97 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 98 | import com.android.server.SystemService; |
| 99 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 100 | import com.google.android.collect.Lists; |
| 101 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 102 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 103 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 104 | import java.io.FileDescriptor; |
| 105 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 106 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 107 | import java.security.MessageDigest; |
| 108 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 109 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 110 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 111 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 112 | import java.util.Collection; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 113 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 114 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 115 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 116 | import java.util.LinkedHashMap; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 117 | import java.util.LinkedHashSet; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 118 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 119 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 120 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 121 | import java.util.Objects; |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 122 | import java.util.Set; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 123 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 124 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 125 | import java.util.concurrent.atomic.AtomicInteger; |
| 126 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 127 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 128 | /** |
| 129 | * A system service that provides account, password, and authtoken management for all |
| 130 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 131 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 132 | * instead one uses an instance of {@link AccountManager}, which can be accessed as follows: |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 133 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 134 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 135 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 136 | public class AccountManagerService |
| 137 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 138 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 139 | private static final String TAG = "AccountManagerService"; |
| 140 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 141 | public static class Lifecycle extends SystemService { |
| 142 | private AccountManagerService mService; |
| 143 | |
| 144 | public Lifecycle(Context context) { |
| 145 | super(context); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onStart() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 150 | mService = new AccountManagerService(new Injector(getContext())); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 151 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 152 | } |
| 153 | |
| 154 | @Override |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 155 | public void onUnlockUser(int userHandle) { |
| 156 | mService.onUnlockUser(userHandle); |
| 157 | } |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 158 | |
| 159 | @Override |
| 160 | public void onCleanupUser(int userHandle) { |
| 161 | mService.onCleanupUser(userHandle); |
| 162 | } |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 163 | } |
| 164 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 165 | final Context mContext; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 166 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 167 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 168 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 169 | private UserManager mUserManager; |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 170 | private final Injector mInjector; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 171 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 172 | final MessageHandler mHandler; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 173 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 174 | // Messages that can be sent on mHandler |
| 175 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 176 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 177 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 178 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 179 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 180 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 181 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 182 | private static final int SIGNATURE_CHECK_MISMATCH = 0; |
| 183 | private static final int SIGNATURE_CHECK_MATCH = 1; |
| 184 | private static final int SIGNATURE_CHECK_UID_MATCH = 2; |
| 185 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 186 | static { |
| 187 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
Christopher Tate | bded68f | 2017-02-21 11:41:55 -0800 | [diff] [blame] | 188 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
| 189 | | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 190 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 191 | |
| 192 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 193 | private final AtomicInteger mNotificationIds = new AtomicInteger(1); |
| 194 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 195 | static class UserAccounts { |
| 196 | private final int userId; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 197 | final AccountsDb accountsDb; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 198 | private final HashMap<Pair<Pair<Account, String>, Integer>, Integer> |
| 199 | credentialsPermissionNotificationIds = |
| 200 | new HashMap<Pair<Pair<Account, String>, Integer>, Integer>(); |
| 201 | private final HashMap<Account, Integer> signinRequiredNotificationIds = |
| 202 | new HashMap<Account, Integer>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 203 | final Object cacheLock = new Object(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 204 | /** protected by the {@link #cacheLock} */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 205 | final HashMap<String, Account[]> accountCache = |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 206 | new LinkedHashMap<>(); |
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>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 209 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 210 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 211 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 212 | private final TokenCache accountTokenCaches = new TokenCache(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 213 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 214 | /** protected by the {@link #mReceiversForType} |
| 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; |
| 240 | synchronized (cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 241 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 246 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 247 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 248 | // Not thread-safe. Only use in synchronized context |
| 249 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 250 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 251 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 252 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 253 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 254 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 255 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 256 | /** |
| 257 | * This should only be called by system code. One should only call this after the service |
| 258 | * has started. |
| 259 | * @return a reference to the AccountManagerService instance |
| 260 | * @hide |
| 261 | */ |
| 262 | public static AccountManagerService getSingleton() { |
| 263 | return sThis.get(); |
| 264 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 265 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 266 | public AccountManagerService(Injector injector) { |
| 267 | mInjector = injector; |
| 268 | mContext = injector.getContext(); |
| 269 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 270 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 271 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 272 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 273 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 274 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 275 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 276 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 277 | IntentFilter intentFilter = new IntentFilter(); |
| 278 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 279 | intentFilter.addDataScheme("package"); |
| 280 | mContext.registerReceiver(new BroadcastReceiver() { |
| 281 | @Override |
| 282 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 283 | // Don't delete accounts when updating a authenticator's |
| 284 | // package. |
| 285 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 286 | /* Purging data requires file io, don't block the main thread. This is probably |
| 287 | * less than ideal because we are introducing a race condition where old grants |
| 288 | * could be exercised until they are purged. But that race condition existed |
| 289 | * anyway with the broadcast receiver. |
| 290 | * |
| 291 | * Ideally, we would completely clear the cache, purge data from the database, |
| 292 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 293 | * large at this point. |
| 294 | */ |
Dmitry Dementyev | 0b67642 | 2017-03-09 11:51:26 -0800 | [diff] [blame] | 295 | final String removedPackageName = intent.getData().getSchemeSpecificPart(); |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 296 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 297 | @Override |
| 298 | public void run() { |
| 299 | purgeOldGrantsAll(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 300 | // Notify authenticator about removed app? |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 301 | removeVisibilityValuesForPackage(removedPackageName); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 302 | } |
| 303 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 304 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 305 | } |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 306 | } |
| 307 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 308 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 309 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 310 | |
| 311 | // Need to cancel account request notifications if the update/install can access the account |
| 312 | new PackageMonitor() { |
| 313 | @Override |
| 314 | public void onPackageAdded(String packageName, int uid) { |
| 315 | // Called on a handler, and running as the system |
| 316 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 321 | // Called on a handler, and running as the system |
| 322 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 323 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 324 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 325 | |
| 326 | // Cancel account request notification if an app op was preventing the account access |
| 327 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 328 | new AppOpsManager.OnOpChangedInternalListener() { |
| 329 | @Override |
| 330 | public void onOpChanged(int op, String packageName) { |
| 331 | try { |
| 332 | final int userId = ActivityManager.getCurrentUser(); |
| 333 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 334 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 335 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 336 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 337 | final long identity = Binder.clearCallingIdentity(); |
| 338 | try { |
| 339 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 340 | } finally { |
| 341 | Binder.restoreCallingIdentity(identity); |
| 342 | } |
| 343 | } |
| 344 | } catch (NameNotFoundException e) { |
| 345 | /* ignore */ |
| 346 | } |
| 347 | } |
| 348 | }); |
| 349 | |
| 350 | // Cancel account request notification if a permission was preventing the account access |
| 351 | mPackageManager.addOnPermissionsChangeListener( |
| 352 | (int uid) -> { |
| 353 | Account[] accounts = null; |
| 354 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 355 | if (packageNames != null) { |
| 356 | final int userId = UserHandle.getUserId(uid); |
| 357 | final long identity = Binder.clearCallingIdentity(); |
| 358 | try { |
| 359 | for (String packageName : packageNames) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 360 | // if app asked for permission we need to cancel notification even |
| 361 | // for O+ applications. |
| 362 | if (mPackageManager.checkPermission( |
| 363 | Manifest.permission.GET_ACCOUNTS, |
| 364 | packageName) != PackageManager.PERMISSION_GRANTED) { |
| 365 | continue; |
| 366 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 367 | |
| 368 | if (accounts == null) { |
| 369 | accounts = getAccountsAsUser(null, userId, "android"); |
| 370 | if (ArrayUtils.isEmpty(accounts)) { |
| 371 | return; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | for (Account account : accounts) { |
| 376 | cancelAccountAccessRequestNotificationIfNeeded( |
| 377 | account, uid, packageName, true); |
| 378 | } |
| 379 | } |
| 380 | } finally { |
| 381 | Binder.restoreCallingIdentity(identity); |
| 382 | } |
| 383 | } |
| 384 | }); |
| 385 | } |
| 386 | |
| 387 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 388 | boolean checkAccess) { |
| 389 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 390 | for (Account account : accounts) { |
| 391 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 396 | boolean checkAccess) { |
| 397 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 398 | for (Account account : accounts) { |
| 399 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 404 | boolean checkAccess) { |
| 405 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 406 | if (packageNames != null) { |
| 407 | for (String packageName : packageNames) { |
| 408 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 409 | packageName, checkAccess); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 415 | int uid, String packageName, boolean checkAccess) { |
| 416 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 417 | UserHandle.getUserHandleForUid(uid))) { |
| 418 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 419 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 420 | UserHandle.getUserHandleForUid(uid)); |
| 421 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 424 | @Override |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 425 | public boolean addAccountExplicitlyWithVisibility(Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 426 | Bundle extras, Map packageToVisibility) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 427 | Bundle.setDefusable(extras, true); |
| 428 | |
| 429 | final int callingUid = Binder.getCallingUid(); |
| 430 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 431 | Log.v(TAG, "addAccountExplicitly: " + account + ", caller's uid " + callingUid |
| 432 | + ", pid " + Binder.getCallingPid()); |
| 433 | } |
| 434 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 435 | int userId = UserHandle.getCallingUserId(); |
| 436 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
| 437 | String msg = String.format("uid %s cannot explicitly add accounts of type: %s", |
| 438 | callingUid, account.type); |
| 439 | throw new SecurityException(msg); |
| 440 | } |
| 441 | /* |
| 442 | * Child users are not allowed to add accounts. Only the accounts that are shared by the |
| 443 | * parent profile can be added to child profile. |
| 444 | * |
| 445 | * TODO: Only allow accounts that were shared to be added by a limited user. |
| 446 | */ |
| 447 | // fails if the account already exists |
| 448 | long identityToken = clearCallingIdentity(); |
| 449 | try { |
| 450 | UserAccounts accounts = getUserAccounts(userId); |
| 451 | return addAccountInternal(accounts, account, password, extras, callingUid, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 452 | (Map<String, Integer>) packageToVisibility); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 453 | } finally { |
| 454 | restoreCallingIdentity(identityToken); |
| 455 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | @Override |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 459 | public Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 460 | String accountType) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 461 | int callingUid = Binder.getCallingUid(); |
| 462 | boolean isSystemUid = UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); |
| 463 | List<String> managedTypes = |
| 464 | getTypesForCaller(callingUid, UserHandle.getUserId(callingUid), isSystemUid); |
| 465 | |
| 466 | if ((accountType != null && !managedTypes.contains(accountType)) |
| 467 | || (accountType == null && !isSystemUid)) { |
| 468 | throw new SecurityException( |
| 469 | "getAccountsAndVisibilityForPackage() called from unauthorized uid " |
| 470 | + callingUid + " with packageName=" + packageName); |
| 471 | } |
| 472 | if (accountType != null) { |
| 473 | managedTypes = new ArrayList<String>(); |
| 474 | managedTypes.add(accountType); |
| 475 | } |
| 476 | |
Dmitry Dementyev | 06f32e0 | 2017-02-16 17:47:48 -0800 | [diff] [blame] | 477 | long identityToken = clearCallingIdentity(); |
| 478 | try { |
| 479 | return getAccountsAndVisibilityForPackage(packageName, managedTypes, callingUid, |
| 480 | getUserAccounts(UserHandle.getUserId(callingUid))); |
| 481 | } finally { |
| 482 | restoreCallingIdentity(identityToken); |
| 483 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* |
| 487 | * accountTypes may not be null |
| 488 | */ |
| 489 | private Map<Account, Integer> getAccountsAndVisibilityForPackage(String packageName, |
| 490 | List<String> accountTypes, Integer callingUid, UserAccounts accounts) { |
| 491 | int uid = 0; |
| 492 | try { |
| 493 | uid = mPackageManager.getPackageUidAsUser(packageName, |
| 494 | UserHandle.getUserId(callingUid)); |
| 495 | } catch (NameNotFoundException e) { |
| 496 | Log.d(TAG, "Package not found " + e.getMessage()); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 497 | return new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 500 | Map<Account, Integer> result = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 501 | for (String accountType : accountTypes) { |
| 502 | synchronized (accounts.cacheLock) { |
| 503 | final Account[] accountsOfType = accounts.accountCache.get(accountType); |
| 504 | if (accountsOfType != null) { |
| 505 | for (Account account : accountsOfType) { |
| 506 | result.put(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 507 | resolveAccountVisibility(account, packageName, accounts)); |
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 | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 519 | int userId = UserHandle.getUserId(callingUid); |
| 520 | UserAccounts accounts = getUserAccounts(userId); |
| 521 | if (!isAccountManagedByCaller(account.type, callingUid, userId) |
| 522 | && !isSystemUid(callingUid)) { |
| 523 | String msg = |
| 524 | String.format("uid %s cannot get secrets for account %s", callingUid, account); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 525 | throw new SecurityException(msg); |
| 526 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 527 | return getPackagesAndVisibilityForAccount(account, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 531 | * Returns all package names and visibility values, which were set for given account. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 532 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 533 | * @param account Account to get visibility values. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 534 | * @param accounts UserAccount that currently hosts the account and application |
| 535 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 536 | * @return Map from package names to visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 537 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 538 | private Map<String, Integer> getPackagesAndVisibilityForAccount(Account account, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 539 | UserAccounts accounts) { |
| 540 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 541 | try { |
| 542 | return accounts.accountsDb.findAllVisibilityValuesForAccount(account); |
| 543 | } finally { |
| 544 | StrictMode.setThreadPolicy(oldPolicy); |
| 545 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 549 | public int getAccountVisibility(Account account, String packageName) { |
| 550 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 551 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 552 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 553 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
| 554 | if (!isAccountManagedByCaller(account.type, callingUid, accounts.userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 555 | && !isSystemUid(callingUid)) { |
| 556 | String msg = String.format( |
| 557 | "uid %s cannot get secrets for accounts of type: %s", |
| 558 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 559 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 560 | throw new SecurityException(msg); |
| 561 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 562 | return resolveAccountVisibility(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 566 | * Method returns visibility for given account and package name. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 567 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 568 | * @param account The account to check visibility. |
| 569 | * @param packageName Package name to check visibility. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 570 | * @param accounts UserAccount that currently hosts the account and application |
| 571 | * |
| 572 | * @return Visibility value, AccountManager.VISIBILITY_UNDEFINED if no value was stored. |
| 573 | * |
| 574 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 575 | private int getAccountVisibility(Account account, String packageName, UserAccounts accounts) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 576 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 577 | try { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 578 | Integer visibility = |
| 579 | accounts.accountsDb.findAccountVisibility(account, packageName); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 580 | return visibility != null ? visibility : AccountManager.VISIBILITY_UNDEFINED; |
| 581 | } finally { |
| 582 | StrictMode.setThreadPolicy(oldPolicy); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Method which handles default values for Account visibility. |
| 588 | * |
| 589 | * @param account The account to check visibility. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 590 | * @param packageName Package name to check visibility |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 591 | * @param accounts UserAccount that currently hosts the account and application |
| 592 | * |
| 593 | * @return Visibility value, the method never returns AccountManager.VISIBILITY_UNDEFINED |
| 594 | * |
| 595 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 596 | private Integer resolveAccountVisibility(Account account, @NonNull String packageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 597 | UserAccounts accounts) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 598 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 599 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 600 | |
| 601 | int uid = -1; |
| 602 | try { |
| 603 | long identityToken = clearCallingIdentity(); |
| 604 | try { |
| 605 | uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 606 | } finally { |
| 607 | restoreCallingIdentity(identityToken); |
| 608 | } |
| 609 | } catch (NameNotFoundException e) { |
| 610 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 611 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 612 | } |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 613 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 614 | // System visibility can not be restricted. |
| 615 | if (UserHandle.isSameApp(uid, Process.SYSTEM_UID)) { |
| 616 | return AccountManager.VISIBILITY_VISIBLE; |
| 617 | } |
| 618 | |
| 619 | int signatureCheckResult = |
| 620 | checkPackageSignature(account.type, uid, accounts.userId); |
| 621 | |
| 622 | // Authenticator can not restrict visibility to itself. |
| 623 | if (signatureCheckResult == SIGNATURE_CHECK_UID_MATCH) { |
| 624 | return AccountManager.VISIBILITY_VISIBLE; // Authenticator can always see the account |
| 625 | } |
| 626 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 627 | if (isSpecialPackageKey(packageName)) { |
| 628 | Log.d(TAG, "Package name is forbidden: " + packageName); |
| 629 | return AccountManager.VISIBILITY_NOT_VISIBLE; |
| 630 | } |
| 631 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 632 | // Return stored value if it was set. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 633 | int visibility = getAccountVisibility(account, packageName, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 634 | |
| 635 | if (AccountManager.VISIBILITY_UNDEFINED != visibility) { |
| 636 | return visibility; |
| 637 | } |
| 638 | |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 639 | boolean isPrivileged = isPermittedForPackage(packageName, accounts.userId, |
| 640 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 641 | |
| 642 | // Device/Profile owner gets visibility by default. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 643 | if (isProfileOwner(uid)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 644 | return AccountManager.VISIBILITY_VISIBLE; |
| 645 | } |
| 646 | // Apps with READ_CONTACTS permission get visibility by default even post O. |
| 647 | boolean canReadContacts = checkReadContactsPermission(packageName, accounts.userId); |
| 648 | |
| 649 | boolean preO = isPreOApplication(packageName); |
| 650 | if ((signatureCheckResult != SIGNATURE_CHECK_MISMATCH) |
| 651 | || (preO && checkGetAccountsPermission(packageName, accounts.userId)) |
Dmitry Dementyev | f794c8d | 2017-02-03 18:17:59 -0800 | [diff] [blame] | 652 | || canReadContacts || isPrivileged) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 653 | // Use legacy for preO apps with GET_ACCOUNTS permission or pre/postO with signature |
| 654 | // match. |
| 655 | visibility = getAccountVisibility(account, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 656 | AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 657 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 658 | visibility = AccountManager.VISIBILITY_USER_MANAGED_VISIBLE; |
| 659 | } |
| 660 | } else { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 661 | visibility = getAccountVisibility(account, |
| 662 | AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 663 | if (AccountManager.VISIBILITY_UNDEFINED == visibility) { |
| 664 | visibility = AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE; |
| 665 | } |
| 666 | } |
| 667 | return visibility; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Checks targetSdk for a package; |
| 672 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 673 | * @param packageName Package name |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 674 | * |
| 675 | * @return True if package's target SDK is below {@link android.os.Build.VERSION_CODES#O}, or |
| 676 | * undefined |
| 677 | */ |
| 678 | private boolean isPreOApplication(String packageName) { |
| 679 | try { |
| 680 | long identityToken = clearCallingIdentity(); |
| 681 | ApplicationInfo applicationInfo; |
| 682 | try { |
| 683 | applicationInfo = mPackageManager.getApplicationInfo(packageName, 0); |
| 684 | } finally { |
| 685 | restoreCallingIdentity(identityToken); |
| 686 | } |
| 687 | |
| 688 | if (applicationInfo != null) { |
| 689 | int version = applicationInfo.targetSdkVersion; |
| 690 | return version < android.os.Build.VERSION_CODES.O; |
| 691 | } |
| 692 | return true; |
| 693 | } catch (NameNotFoundException e) { |
| 694 | Log.d(TAG, "Package not found " + e.getMessage()); |
| 695 | return true; |
| 696 | } |
Dmitry Dementyev | 58fa8362 | 2016-12-20 18:08:51 -0800 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | @Override |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 700 | public boolean setAccountVisibility(Account account, String packageName, int newVisibility) { |
| 701 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 702 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 703 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 704 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
| 705 | if (!isAccountManagedByCaller(account.type, callingUid, accounts.userId) |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 706 | && !isSystemUid(callingUid)) { |
| 707 | String msg = String.format( |
| 708 | "uid %s cannot get secrets for accounts of type: %s", |
| 709 | callingUid, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 710 | account.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 711 | throw new SecurityException(msg); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 712 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 713 | return setAccountVisibility(account, packageName, newVisibility, true /* notify */, |
| 714 | accounts); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 718 | * Updates visibility for given account name and package. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 719 | * |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 720 | * @param account Account to update visibility. |
| 721 | * @param packageName Package name for which visibility is updated. |
| 722 | * @param newVisibility New visibility calue |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 723 | * @param notify if the flag is set applications will get notification about visibility change |
| 724 | * @param accounts UserAccount that currently hosts the account and application |
| 725 | * |
| 726 | * @return True if account visibility was changed. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 727 | */ |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 728 | private boolean setAccountVisibility(Account account, String packageName, int newVisibility, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 729 | boolean notify, UserAccounts accounts) { |
| 730 | synchronized (accounts.cacheLock) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 731 | Map<String, Integer> packagesToVisibility; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 732 | if (notify) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 733 | if (isSpecialPackageKey(packageName)) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 734 | packagesToVisibility = |
| 735 | getRequestingPackages(account, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 736 | } else { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 737 | if (!packageExistsForUser(packageName, accounts.userId)) { |
| 738 | return false; // package is not installed. |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 739 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 740 | packagesToVisibility = new HashMap<>(); |
| 741 | packagesToVisibility.put(packageName, |
| 742 | resolveAccountVisibility(account, packageName, accounts)); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 743 | } |
| 744 | } else { |
| 745 | // Notifications will not be send. |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 746 | if (!isSpecialPackageKey(packageName) && |
| 747 | !packageExistsForUser(packageName, accounts.userId)) { |
| 748 | // package is not installed and not meta value. |
| 749 | return false; |
| 750 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 751 | packagesToVisibility = new HashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 752 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 753 | |
| 754 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
| 755 | if (accountId < 0) { |
| 756 | return false; |
| 757 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 758 | |
| 759 | final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
| 760 | try { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 761 | if (!accounts.accountsDb.setAccountVisibility(accountId, packageName, |
| 762 | newVisibility)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 763 | return false; |
| 764 | } |
| 765 | } finally { |
| 766 | StrictMode.setThreadPolicy(oldPolicy); |
| 767 | } |
| 768 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 769 | if (notify) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 770 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
| 771 | if (packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 772 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 773 | } |
| 774 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 775 | sendAccountsChangedBroadcast(accounts.userId); |
| 776 | } |
| 777 | return true; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 778 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 781 | @Override |
| 782 | public void registerAccountListener(String[] accountTypes, String opPackageName) { |
| 783 | int callingUid = Binder.getCallingUid(); |
| 784 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 785 | registerAccountListener(accountTypes, opPackageName, |
| 786 | getUserAccounts(UserHandle.getUserId(callingUid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 787 | } |
| 788 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 789 | private void registerAccountListener(String[] accountTypes, String opPackageName, |
| 790 | UserAccounts accounts) { |
| 791 | synchronized (accounts.mReceiversForType) { |
| 792 | if (accountTypes == null) { |
| 793 | // null for any type |
| 794 | accountTypes = new String[] {null}; |
| 795 | } |
| 796 | for (String type : accountTypes) { |
| 797 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 798 | if (receivers == null) { |
| 799 | receivers = new HashMap<>(); |
| 800 | accounts.mReceiversForType.put(type, receivers); |
| 801 | } |
| 802 | Integer cnt = receivers.get(opPackageName); |
| 803 | receivers.put(opPackageName, cnt != null ? cnt + 1 : 1); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 808 | @Override |
| 809 | public void unregisterAccountListener(String[] accountTypes, String opPackageName) { |
| 810 | int callingUid = Binder.getCallingUid(); |
| 811 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
| 812 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callingUid)); |
| 813 | synchronized (accounts.mReceiversForType) { |
| 814 | if (accountTypes == null) { |
| 815 | // null for any type |
| 816 | accountTypes = new String[] {null}; |
| 817 | } |
| 818 | for (String type : accountTypes) { |
| 819 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 820 | if (receivers == null || receivers.get(opPackageName) == null) { |
| 821 | throw new IllegalArgumentException("attempt to unregister wrong receiver"); |
| 822 | } |
| 823 | Integer cnt = receivers.get(opPackageName); |
| 824 | if (cnt == 1) { |
| 825 | receivers.remove(opPackageName); |
| 826 | } else { |
| 827 | receivers.put(opPackageName, cnt - 1); |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | // Send notification to all packages which can potentially see the account |
| 834 | private void sendNotificationAccountUpdated(Account account, UserAccounts accounts) { |
| 835 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, accounts); |
| 836 | // packages with VISIBILITY_USER_MANAGED_NOT_VISIBL still get notification. |
| 837 | // Should we notify VISIBILITY_NOT_VISIBLE packages when account is added? |
| 838 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
| 839 | if (packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 840 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Sends a direct intent to a package, notifying it of account visibility change. |
| 847 | * |
| 848 | * @param packageName to send Account to |
| 849 | * @param accounts UserAccount that currently hosts the account |
| 850 | */ |
| 851 | private void notifyPackage(String packageName, UserAccounts accounts) { |
| 852 | Intent intent = new Intent(AccountManager.ACTION_VISIBLE_ACCOUNTS_CHANGED); |
| 853 | intent.setPackage(packageName); |
| 854 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 855 | mContext.sendBroadcastAsUser(intent, new UserHandle(accounts.userId)); |
| 856 | } |
| 857 | |
| 858 | // Returns a map from package name to visibility, for packages subscribed |
| 859 | // to notifications about any account type, or type of provided account |
| 860 | // account type or all types. |
| 861 | private Map<String, Integer> getRequestingPackages(Account account, UserAccounts accounts) { |
| 862 | Set<String> packages = new HashSet<>(); |
| 863 | synchronized (accounts.mReceiversForType) { |
| 864 | for (String type : new String[] {account.type, null}) { |
| 865 | Map<String, Integer> receivers = accounts.mReceiversForType.get(type); |
| 866 | if (receivers != null) { |
| 867 | packages.addAll(receivers.keySet()); |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | Map<String, Integer> result = new HashMap<>(); |
| 872 | for (String packageName : packages) { |
| 873 | result.put(packageName, resolveAccountVisibility(account, packageName, accounts)); |
| 874 | } |
| 875 | return result; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 876 | } |
| 877 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 878 | private boolean packageExistsForUser(String packageName, int userId) { |
| 879 | try { |
| 880 | long identityToken = clearCallingIdentity(); |
| 881 | try { |
| 882 | mPackageManager.getPackageUidAsUser(packageName, userId); |
| 883 | return true; // package exist |
| 884 | } finally { |
| 885 | restoreCallingIdentity(identityToken); |
| 886 | } |
| 887 | } catch (NameNotFoundException e) { |
| 888 | return false; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * Returns true if packageName is one of special values. |
| 894 | */ |
| 895 | private boolean isSpecialPackageKey(String packageName) { |
| 896 | return (AccountManager.PACKAGE_NAME_KEY_LEGACY_VISIBLE.equals(packageName) |
| 897 | || AccountManager.PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE.equals(packageName)); |
| 898 | } |
| 899 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 900 | private void sendAccountsChangedBroadcast(int userId) { |
| 901 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 902 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
| 903 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 907 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 908 | throws RemoteException { |
| 909 | try { |
| 910 | return super.onTransact(code, data, reply, flags); |
| 911 | } catch (RuntimeException e) { |
| 912 | // The account manager only throws security exceptions, so let's |
| 913 | // log all others. |
| 914 | if (!(e instanceof SecurityException)) { |
| 915 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 916 | } |
| 917 | throw e; |
| 918 | } |
| 919 | } |
| 920 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 921 | private UserManager getUserManager() { |
| 922 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 923 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 924 | } |
| 925 | return mUserManager; |
| 926 | } |
| 927 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 928 | /** |
| 929 | * Validate internal set of accounts against installed authenticators for |
| 930 | * given user. Clears cached authenticators before validating. |
| 931 | */ |
| 932 | public void validateAccounts(int userId) { |
| 933 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 934 | // Invalidate user-specific cache to make sure we catch any |
| 935 | // removed authenticators. |
| 936 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Validate internal set of accounts against installed authenticators for |
| 941 | * given user. Clear cached authenticators before validating when requested. |
| 942 | */ |
| 943 | private void validateAccountsInternal( |
| 944 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 945 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 946 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 947 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 948 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 949 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 950 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 951 | if (invalidateAuthenticatorCache) { |
| 952 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 953 | } |
| 954 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 955 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 956 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 957 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 958 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 959 | synchronized (accounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 960 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 961 | |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 962 | // Get a map of stored authenticator types to UID |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 963 | final AccountsDb accountsDb = accounts.accountsDb; |
| 964 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 965 | // Create a list of authenticator type whose previous uid no longer exists |
| 966 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 967 | SparseBooleanArray knownUids = null; |
| 968 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 969 | String type = authToUidEntry.getKey(); |
| 970 | int uid = authToUidEntry.getValue(); |
| 971 | Integer knownUid = knownAuth.get(type); |
| 972 | if (knownUid != null && uid == knownUid) { |
| 973 | // Remove it from the knownAuth list if it's unchanged. |
| 974 | knownAuth.remove(type); |
| 975 | } else { |
| 976 | /* |
| 977 | * The authenticator is presently not cached and should only be triggered |
| 978 | * when we think an authenticator has been removed (or is being updated). |
| 979 | * But we still want to check if any data with the associated uid is |
| 980 | * around. This is an (imperfect) signal that the package may be updating. |
| 981 | * |
| 982 | * A side effect of this is that an authenticator sharing a uid with |
| 983 | * multiple apps won't get its credentials wiped as long as some app with |
| 984 | * that uid is still on the device. But I suspect that this is a rare case. |
| 985 | * And it isn't clear to me how an attacker could really exploit that |
| 986 | * feature. |
| 987 | * |
| 988 | * The upshot is that we don't have to worry about accounts getting |
| 989 | * uninstalled while the authenticator's package is being updated. |
| 990 | * |
| 991 | */ |
| 992 | if (knownUids == null) { |
| 993 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 994 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 995 | if (!knownUids.get(uid)) { |
| 996 | // The authenticator is not presently available to the cache. And the |
| 997 | // package no longer has a data directory (so we surmise it isn't updating). |
| 998 | // So purge its data from the account databases. |
| 999 | obsoleteAuthType.add(type); |
| 1000 | // And delete it from the TABLE_META |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1001 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1002 | } |
| 1003 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1006 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1007 | // been re-enabled (after being updated for example), then we just overwrite the old |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1008 | // values. |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1009 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1010 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1011 | } |
| 1012 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1013 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1014 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1015 | accounts.accountCache.clear(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1016 | final HashMap<String, ArrayList<String>> accountNamesByType = new LinkedHashMap<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1017 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1018 | final long accountId = accountEntry.getKey(); |
| 1019 | final Account account = accountEntry.getValue(); |
| 1020 | if (obsoleteAuthType.contains(account.type)) { |
| 1021 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 1022 | + account.type + "'s registered authenticator no longer exist."); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1023 | Map<String, Integer> packagesToVisibility = |
| 1024 | getRequestingPackages(account, accounts); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1025 | accountsDb.beginTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1026 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1027 | accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1028 | // Also delete from CE table if user is unlocked; if user is currently |
| 1029 | // locked the account will be removed later by syncDeCeAccountsLocked |
| 1030 | if (userUnlocked) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1031 | accountsDb.deleteCeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1032 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1033 | accountsDb.setTransactionSuccessful(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1034 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1035 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1036 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1037 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1038 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1039 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1040 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1041 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1042 | accounts.userDataCache.remove(account); |
| 1043 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1044 | accounts.accountTokenCaches.remove(account); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1045 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1046 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
| 1047 | if (packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 1048 | notifyPackage(packageToVisibility.getKey(), accounts); |
| 1049 | } |
| 1050 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1051 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1052 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1053 | if (accountNames == null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1054 | accountNames = new ArrayList<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1055 | accountNamesByType.put(account.type, accountNames); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1056 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1057 | accountNames.add(account.name); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1058 | } |
| 1059 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1060 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1061 | final String accountType = cur.getKey(); |
| 1062 | final ArrayList<String> accountNames = cur.getValue(); |
| 1063 | final Account[] accountsForType = new Account[accountNames.size()]; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1064 | for (int i = 0; i < accountsForType.length; i++) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1065 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1066 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1067 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1068 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1069 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1070 | } finally { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1071 | if (accountDeleted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1072 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1073 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1074 | } |
| 1075 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1078 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1079 | // Get the UIDs of all apps that might have data on the device. We want |
| 1080 | // to preserve user data if the app might otherwise be storing data. |
| 1081 | List<PackageInfo> pkgsWithData = |
| 1082 | mPackageManager.getInstalledPackagesAsUser( |
| 1083 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1084 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1085 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1086 | if (pkgInfo.applicationInfo != null |
| 1087 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1088 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1089 | } |
| 1090 | } |
| 1091 | return knownUids; |
| 1092 | } |
| 1093 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1094 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1095 | Context context, |
| 1096 | int userId) { |
| 1097 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1098 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1099 | } |
| 1100 | |
| 1101 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1102 | IAccountAuthenticatorCache authCache, |
| 1103 | int userId) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 1104 | HashMap<String, Integer> knownAuth = new LinkedHashMap<>(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1105 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1106 | .getAllServices(userId)) { |
| 1107 | knownAuth.put(service.type.type, service.uid); |
| 1108 | } |
| 1109 | return knownAuth; |
| 1110 | } |
| 1111 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1112 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1113 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | protected UserAccounts getUserAccounts(int userId) { |
| 1117 | synchronized (mUsers) { |
| 1118 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1119 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1120 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1121 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1122 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1123 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1124 | initializeDebugDbSizeAndCompileSqlStatementForLogging(accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1125 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1126 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1127 | validateAccounts = true; |
| 1128 | } |
| 1129 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1130 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1131 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
| 1132 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1133 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1134 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1135 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1136 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1137 | } |
| 1138 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1139 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1140 | } |
| 1141 | return accounts; |
| 1142 | } |
| 1143 | } |
| 1144 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1145 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1146 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1147 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1148 | if (!accountsToRemove.isEmpty()) { |
| 1149 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1150 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1151 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1152 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1153 | |
| 1154 | for (Account account : accountsToRemove) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1155 | removeAccountInternal(accounts, account, Process.SYSTEM_UID); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1160 | private void purgeOldGrantsAll() { |
| 1161 | synchronized (mUsers) { |
| 1162 | for (int i = 0; i < mUsers.size(); i++) { |
| 1163 | purgeOldGrants(mUsers.valueAt(i)); |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | private void purgeOldGrants(UserAccounts accounts) { |
| 1169 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1170 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1171 | for (int uid : uids) { |
| 1172 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1173 | if (packageExists) { |
| 1174 | continue; |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1175 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1176 | Log.d(TAG, "deleting grants for UID " + uid |
| 1177 | + " because its package is no longer installed"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1178 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1183 | private void removeVisibilityValuesForPackage(String packageName) { |
| 1184 | synchronized (mUsers) { |
| 1185 | for (int i = 0; i < mUsers.size(); i++) { |
| 1186 | UserAccounts accounts = mUsers.valueAt(i); |
| 1187 | try { |
| 1188 | int uid = mPackageManager.getPackageUidAsUser(packageName, accounts.userId); |
| 1189 | } catch (NameNotFoundException e) { |
| 1190 | // package does not exist - remove visibility values |
| 1191 | accounts.accountsDb.deleteAccountVisibilityForPackage(packageName); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
Fyodor Kupolov | b9da4e4 | 2017-03-16 13:01:12 -0700 | [diff] [blame] | 1197 | private void onCleanupUser(int userId) { |
| 1198 | Log.i(TAG, "onCleanupUser " + userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1199 | UserAccounts accounts; |
| 1200 | synchronized (mUsers) { |
| 1201 | accounts = mUsers.get(userId); |
| 1202 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1203 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1204 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1205 | if (accounts != null) { |
| 1206 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1207 | accounts.accountsDb.close(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1208 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1209 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1212 | @VisibleForTesting |
| 1213 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1214 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1215 | } |
| 1216 | |
| 1217 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1218 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1219 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1220 | } |
| 1221 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1222 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1223 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1224 | if (userId < 1) return; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1225 | syncSharedAccounts(userId); |
| 1226 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1227 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1228 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1229 | // Check if there's a shared account that needs to be created as an account |
| 1230 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1231 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1232 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1233 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1234 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1235 | : UserHandle.USER_SYSTEM; |
| 1236 | if (parentUserId < 0) { |
| 1237 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1238 | return; |
| 1239 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1240 | for (Account sa : sharedAccounts) { |
| 1241 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1242 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1243 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1247 | @Override |
| 1248 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1249 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1252 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1253 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1254 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1255 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1256 | Log.v(TAG, "getPassword: " + account |
| 1257 | + ", caller's uid " + Binder.getCallingUid() |
| 1258 | + ", pid " + Binder.getCallingPid()); |
| 1259 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1260 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1261 | int userId = UserHandle.getCallingUserId(); |
| 1262 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1263 | String msg = String.format( |
| 1264 | "uid %s cannot get secrets for accounts of type: %s", |
| 1265 | callingUid, |
| 1266 | account.type); |
| 1267 | throw new SecurityException(msg); |
| 1268 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1269 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1270 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1271 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1272 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1273 | } finally { |
| 1274 | restoreCallingIdentity(identityToken); |
| 1275 | } |
| 1276 | } |
| 1277 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1278 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1279 | if (account == null) { |
| 1280 | return null; |
| 1281 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1282 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1283 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1284 | return null; |
| 1285 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1286 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1287 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1288 | return accounts.accountsDb.findAccountPasswordByNameAndType(account.name, account.type); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1292 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1293 | public String getPreviousName(Account account) { |
| 1294 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1295 | Log.v(TAG, "getPreviousName: " + account |
| 1296 | + ", caller's uid " + Binder.getCallingUid() |
| 1297 | + ", pid " + Binder.getCallingPid()); |
| 1298 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1299 | Preconditions.checkNotNull(account, "account cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1300 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1301 | long identityToken = clearCallingIdentity(); |
| 1302 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1303 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1304 | return readPreviousNameInternal(accounts, account); |
| 1305 | } finally { |
| 1306 | restoreCallingIdentity(identityToken); |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1311 | if (account == null) { |
| 1312 | return null; |
| 1313 | } |
| 1314 | synchronized (accounts.cacheLock) { |
| 1315 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1316 | if (previousNameRef == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1317 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1318 | previousNameRef = new AtomicReference<>(previousName); |
| 1319 | accounts.previousNameCache.put(account, previousNameRef); |
| 1320 | return previousName; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1321 | } else { |
| 1322 | return previousNameRef.get(); |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1328 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1329 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1330 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1331 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1332 | account, key, callingUid, Binder.getCallingPid()); |
| 1333 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1334 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1335 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 1336 | Preconditions.checkNotNull(key, "key cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1337 | int userId = UserHandle.getCallingUserId(); |
| 1338 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1339 | String msg = String.format( |
| 1340 | "uid %s cannot get user data for accounts of type: %s", |
| 1341 | callingUid, |
| 1342 | account.type); |
| 1343 | throw new SecurityException(msg); |
| 1344 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1345 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1346 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1347 | return null; |
| 1348 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1349 | long identityToken = clearCallingIdentity(); |
| 1350 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1351 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1352 | synchronized (accounts.cacheLock) { |
| 1353 | if (!accountExistsCacheLocked(accounts, account)) { |
| 1354 | return null; |
| 1355 | } |
| 1356 | return readUserDataInternalLocked(accounts, account, key); |
| 1357 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1358 | } finally { |
| 1359 | restoreCallingIdentity(identityToken); |
| 1360 | } |
| 1361 | } |
| 1362 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1363 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1364 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1365 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1366 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1367 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1368 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1369 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1370 | + ", pid " + Binder.getCallingPid()); |
| 1371 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1372 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1373 | if (isCrossUser(callingUid, userId)) { |
| 1374 | throw new SecurityException( |
| 1375 | String.format( |
| 1376 | "User %s tying to get authenticator types for %s" , |
| 1377 | UserHandle.getCallingUserId(), |
| 1378 | userId)); |
| 1379 | } |
| 1380 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1381 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1382 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1383 | return getAuthenticatorTypesInternal(userId); |
| 1384 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1385 | } finally { |
| 1386 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1387 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1390 | /** |
| 1391 | * Should only be called inside of a clearCallingIdentity block. |
| 1392 | */ |
| 1393 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1394 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1395 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1396 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1397 | AuthenticatorDescription[] types = |
| 1398 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1399 | int i = 0; |
| 1400 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1401 | : authenticatorCollection) { |
| 1402 | types[i] = authenticator.type; |
| 1403 | i++; |
| 1404 | } |
| 1405 | return types; |
| 1406 | } |
| 1407 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1408 | private boolean isCrossUser(int callingUid, int userId) { |
| 1409 | return (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1410 | && callingUid != Process.SYSTEM_UID |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1411 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1412 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1413 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1416 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1417 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1418 | return addAccountExplicitlyWithVisibility(account, password, extras, null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1421 | @Override |
| 1422 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1423 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1424 | int callingUid = Binder.getCallingUid(); |
| 1425 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1426 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1427 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1428 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1429 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1430 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1431 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1432 | if (response != null) { |
| 1433 | Bundle result = new Bundle(); |
| 1434 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1435 | try { |
| 1436 | response.onResult(result); |
| 1437 | } catch (RemoteException e) { |
| 1438 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1439 | } |
| 1440 | } |
| 1441 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1444 | Slog.d(TAG, "Copying account " + account.name |
| 1445 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1446 | long identityToken = clearCallingIdentity(); |
| 1447 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1448 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1449 | false /* stripAuthTokenFromResult */, account.name, |
| 1450 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1451 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1452 | protected String toDebugString(long now) { |
| 1453 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1454 | + ", " + account.type; |
| 1455 | } |
| 1456 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1457 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1458 | public void run() throws RemoteException { |
| 1459 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1460 | } |
| 1461 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1462 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1463 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1464 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1465 | if (result != null |
| 1466 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1467 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1468 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1469 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1470 | super.onResult(result); |
| 1471 | } |
| 1472 | } |
| 1473 | }.bind(); |
| 1474 | } finally { |
| 1475 | restoreCallingIdentity(identityToken); |
| 1476 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1477 | } |
| 1478 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1479 | @Override |
| 1480 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1481 | final int callingUid = Binder.getCallingUid(); |
| 1482 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1483 | String msg = String.format( |
| 1484 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1485 | account, |
| 1486 | callingUid); |
| 1487 | Log.v(TAG, msg); |
| 1488 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1489 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1490 | int userId = UserHandle.getCallingUserId(); |
| 1491 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1492 | String msg = String.format( |
| 1493 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1494 | callingUid, |
| 1495 | account.type); |
| 1496 | throw new SecurityException(msg); |
| 1497 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1498 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1499 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1500 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1501 | return false; |
| 1502 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1503 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1504 | long identityToken = clearCallingIdentity(); |
| 1505 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1506 | UserAccounts accounts = getUserAccounts(userId); |
| 1507 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1508 | } finally { |
| 1509 | restoreCallingIdentity(identityToken); |
| 1510 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1514 | final UserAccounts accounts = getUserAccountsForCaller(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1515 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1516 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1517 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1520 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1521 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1522 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1523 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1524 | long id = clearCallingIdentity(); |
| 1525 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1526 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1527 | false /* stripAuthTokenFromResult */, account.name, |
| 1528 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1529 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1530 | protected String toDebugString(long now) { |
| 1531 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1532 | + ", " + account.type; |
| 1533 | } |
| 1534 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1535 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1536 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1537 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1538 | UserAccounts owner = getUserAccounts(parentUserId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1539 | synchronized (owner.cacheLock) { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1540 | for (Account acc : getAccounts(parentUserId, |
| 1541 | mContext.getOpPackageName())) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1542 | if (acc.equals(account)) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1543 | mAuthenticator.addAccountFromCredentials( |
| 1544 | this, account, accountCredentials); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1545 | break; |
| 1546 | } |
| 1547 | } |
| 1548 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1551 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1552 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1553 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1554 | // TODO: Anything to do if if succedded? |
| 1555 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1556 | // account to avoid retries? |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1557 | // TODO: what we do with the visibility? |
| 1558 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1559 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1562 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1563 | public void onError(int errorCode, String errorMessage) { |
| 1564 | super.onError(errorCode, errorMessage); |
| 1565 | // TODO: Show error notification to user |
| 1566 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1567 | } |
| 1568 | |
| 1569 | }.bind(); |
| 1570 | } finally { |
| 1571 | restoreCallingIdentity(id); |
| 1572 | } |
| 1573 | } |
| 1574 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1575 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1576 | Bundle extras, int callingUid, Map<String, Integer> packageToVisibility) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1577 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1578 | if (account == null) { |
| 1579 | return false; |
| 1580 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1581 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1582 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1583 | + " is locked. callingUid=" + callingUid); |
| 1584 | return false; |
| 1585 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1586 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1587 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1588 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1589 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1590 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1591 | + ", skipping since the account already exists"); |
| 1592 | return false; |
| 1593 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1594 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1595 | if (accountId < 0) { |
| 1596 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1597 | + ", skipping the DB insert failed"); |
| 1598 | return false; |
| 1599 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1600 | // Insert into DE table |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1601 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1602 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1603 | + ", skipping the DB insert failed"); |
| 1604 | return false; |
| 1605 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1606 | if (extras != null) { |
| 1607 | for (String key : extras.keySet()) { |
| 1608 | final String value = extras.getString(key); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1609 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1610 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1611 | + ", skipping since insertExtra failed for key " + key); |
| 1612 | return false; |
| 1613 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1614 | } |
| 1615 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1616 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1617 | if (packageToVisibility != null) { |
| 1618 | for (Entry<String, Integer> entry : packageToVisibility.entrySet()) { |
| 1619 | setAccountVisibility(account, entry.getKey() /* package */, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1620 | entry.getValue() /* visibility */, false /* notify */, accounts); |
| 1621 | } |
| 1622 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1623 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1624 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1625 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, accountId, |
| 1626 | accounts, callingUid); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1627 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1628 | insertAccountIntoCacheLocked(accounts, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1629 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1630 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1631 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1632 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1633 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1634 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1635 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1636 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1637 | sendNotificationAccountUpdated(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1638 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1639 | sendAccountsChangedBroadcast(accounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1640 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1641 | return true; |
| 1642 | } |
| 1643 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1644 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1645 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1646 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1650 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1651 | * 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] | 1652 | * running, then clone the account too. |
| 1653 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1654 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1655 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1656 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1657 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1658 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1659 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1660 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1661 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1662 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1663 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1664 | } |
| 1665 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1669 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1670 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1671 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1672 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 1673 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1674 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1675 | Log.v(TAG, "hasFeatures: " + account |
| 1676 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1677 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1678 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1679 | + ", pid " + Binder.getCallingPid()); |
| 1680 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1681 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1682 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1683 | Preconditions.checkArgument(features != null, "features cannot be null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1684 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1685 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1686 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1687 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1688 | long identityToken = clearCallingIdentity(); |
| 1689 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1690 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1691 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1692 | } finally { |
| 1693 | restoreCallingIdentity(identityToken); |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | private class TestFeaturesSession extends Session { |
| 1698 | private final String[] mFeatures; |
| 1699 | private final Account mAccount; |
| 1700 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1701 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1702 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1703 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1704 | true /* stripAuthTokenFromResult */, account.name, |
| 1705 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1706 | mFeatures = features; |
| 1707 | mAccount = account; |
| 1708 | } |
| 1709 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1710 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1711 | public void run() throws RemoteException { |
| 1712 | try { |
| 1713 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1714 | } catch (RemoteException e) { |
| 1715 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1716 | } |
| 1717 | } |
| 1718 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1719 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1720 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1721 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1722 | IAccountManagerResponse response = getResponseAndClose(); |
| 1723 | if (response != null) { |
| 1724 | try { |
| 1725 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1726 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1727 | return; |
| 1728 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1729 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1730 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1731 | + response); |
| 1732 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1733 | final Bundle newResult = new Bundle(); |
| 1734 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1735 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1736 | response.onResult(newResult); |
| 1737 | } catch (RemoteException e) { |
| 1738 | // if the caller is dead then there is no one to care about remote exceptions |
| 1739 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1740 | Log.v(TAG, "failure while notifying response", e); |
| 1741 | } |
| 1742 | } |
| 1743 | } |
| 1744 | } |
| 1745 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1746 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1747 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1748 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1749 | + ", " + mAccount |
| 1750 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1751 | } |
| 1752 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1753 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1754 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1755 | public void renameAccount( |
| 1756 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1757 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1758 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1759 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1760 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1761 | + ", pid " + Binder.getCallingPid()); |
| 1762 | } |
| 1763 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1764 | int userId = UserHandle.getCallingUserId(); |
| 1765 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1766 | String msg = String.format( |
| 1767 | "uid %s cannot rename accounts of type: %s", |
| 1768 | callingUid, |
| 1769 | accountToRename.type); |
| 1770 | throw new SecurityException(msg); |
| 1771 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1772 | long identityToken = clearCallingIdentity(); |
| 1773 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1774 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1775 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1776 | Bundle result = new Bundle(); |
| 1777 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1778 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1779 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1780 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1781 | try { |
| 1782 | response.onResult(result); |
| 1783 | } catch (RemoteException e) { |
| 1784 | Log.w(TAG, e.getMessage()); |
| 1785 | } |
| 1786 | } finally { |
| 1787 | restoreCallingIdentity(identityToken); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1792 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1793 | Account resultAccount = null; |
| 1794 | /* |
| 1795 | * Cancel existing notifications. Let authenticators |
| 1796 | * re-post notifications as required. But we don't know if |
| 1797 | * the authenticators have bound their notifications to |
| 1798 | * now stale account name data. |
| 1799 | * |
| 1800 | * With a rename api, we might not need to do this anymore but it |
| 1801 | * shouldn't hurt. |
| 1802 | */ |
| 1803 | cancelNotification( |
| 1804 | getSigninRequiredNotificationId(accounts, accountToRename), |
| 1805 | new UserHandle(accounts.userId)); |
| 1806 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1807 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1808 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1809 | if (accountToRename.equals(pair.first.first)) { |
| 1810 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
| 1811 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1816 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1817 | Account renamedAccount = new Account(newName, accountToRename.type); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1818 | if ((accounts.accountsDb.findCeAccountId(renamedAccount) >= 0)) { |
| 1819 | Log.e(TAG, "renameAccount failed - account with new name already exists"); |
| 1820 | return null; |
| 1821 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1822 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1823 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1824 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1825 | accounts.accountsDb.renameCeAccount(accountId, newName); |
Dmitry Dementyev | cf50dcf | 2016-11-17 14:14:11 -0800 | [diff] [blame] | 1826 | if (accounts.accountsDb.renameDeAccount( |
| 1827 | accountId, newName, accountToRename.name)) { |
| 1828 | accounts.accountsDb.setTransactionSuccessful(); |
| 1829 | } else { |
| 1830 | Log.e(TAG, "renameAccount failed"); |
| 1831 | return null; |
| 1832 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1833 | } else { |
| 1834 | Log.e(TAG, "renameAccount failed - old account does not exist"); |
| 1835 | return null; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1836 | } |
| 1837 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1838 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1839 | } |
| 1840 | /* |
| 1841 | * Database transaction was successful. Clean up cached |
| 1842 | * data associated with the account in the user profile. |
| 1843 | */ |
Svet Ganov | 5c4a512 | 2016-09-23 19:29:11 -0700 | [diff] [blame] | 1844 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1845 | /* |
| 1846 | * Extract the data and token caches before removing the |
| 1847 | * old account to preserve the user data associated with |
| 1848 | * the account. |
| 1849 | */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1850 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1851 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1852 | removeAccountFromCacheLocked(accounts, accountToRename); |
| 1853 | /* |
| 1854 | * Update the cached data associated with the renamed |
| 1855 | * account. |
| 1856 | */ |
| 1857 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1858 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 1859 | accounts.previousNameCache.put( |
| 1860 | renamedAccount, |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1861 | new AtomicReference<>(accountToRename.name)); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1862 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1863 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1864 | int parentUserId = accounts.userId; |
| 1865 | if (canHaveProfile(parentUserId)) { |
| 1866 | /* |
| 1867 | * Owner or system user account was renamed, rename the account for |
| 1868 | * those users with which the account was shared. |
| 1869 | */ |
| 1870 | List<UserInfo> users = getUserManager().getUsers(true); |
| 1871 | for (UserInfo user : users) { |
| 1872 | if (user.isRestricted() |
| 1873 | && (user.restrictedProfileParentId == parentUserId)) { |
| 1874 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1875 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1876 | } |
| 1877 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 1878 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1879 | sendNotificationAccountUpdated(resultAccount, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1880 | sendAccountsChangedBroadcast(accounts.userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1881 | } |
| 1882 | return resultAccount; |
| 1883 | } |
| 1884 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1885 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1886 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1887 | return userInfo != null && userInfo.canHaveProfile(); |
| 1888 | } |
| 1889 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1890 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1891 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1892 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1893 | removeAccountAsUser( |
| 1894 | response, |
| 1895 | account, |
| 1896 | expectActivityLaunch, |
| 1897 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | @Override |
| 1901 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1902 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1903 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1904 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1905 | Log.v(TAG, "removeAccount: " + account |
| 1906 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1907 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1908 | + ", pid " + Binder.getCallingPid() |
| 1909 | + ", for user id " + userId); |
| 1910 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 1911 | Preconditions.checkArgument(account != null, "account cannot be null"); |
| 1912 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 1913 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1914 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1915 | if (isCrossUser(callingUid, userId)) { |
| 1916 | throw new SecurityException( |
| 1917 | String.format( |
| 1918 | "User %s tying remove account for %s" , |
| 1919 | UserHandle.getCallingUserId(), |
| 1920 | userId)); |
| 1921 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1922 | /* |
| 1923 | * Only the system or authenticator should be allowed to remove accounts for that |
| 1924 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 1925 | * arbitrary applications (like competing authenticators). |
| 1926 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1927 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 1928 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 1929 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1930 | String msg = String.format( |
| 1931 | "uid %s cannot remove accounts of type: %s", |
| 1932 | callingUid, |
| 1933 | account.type); |
| 1934 | throw new SecurityException(msg); |
| 1935 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1936 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1937 | try { |
| 1938 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 1939 | "User cannot modify accounts"); |
| 1940 | } catch (RemoteException re) { |
| 1941 | } |
| 1942 | return; |
| 1943 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1944 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1945 | try { |
| 1946 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 1947 | "User cannot modify accounts of this type (policy)."); |
| 1948 | } catch (RemoteException re) { |
| 1949 | } |
| 1950 | return; |
| 1951 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1952 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1953 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1954 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1955 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1956 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1957 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1958 | if (account.equals(pair.first.first)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1959 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 1960 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1964 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 1965 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1966 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 1967 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 1968 | accountId, |
| 1969 | accounts, |
| 1970 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1971 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1972 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 1973 | } finally { |
| 1974 | restoreCallingIdentity(identityToken); |
| 1975 | } |
| 1976 | } |
| 1977 | |
| 1978 | @Override |
| 1979 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1980 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1981 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1982 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1983 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1984 | + ", pid " + Binder.getCallingPid()); |
| 1985 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1986 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1987 | if (account == null) { |
| 1988 | /* |
| 1989 | * Null accounts should result in returning false, as per |
| 1990 | * AccountManage.addAccountExplicitly(...) java doc. |
| 1991 | */ |
| 1992 | Log.e(TAG, "account is null"); |
| 1993 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1994 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1995 | String msg = String.format( |
| 1996 | "uid %s cannot explicitly add accounts of type: %s", |
| 1997 | callingUid, |
| 1998 | account.type); |
| 1999 | throw new SecurityException(msg); |
| 2000 | } |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2001 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2002 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2003 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2004 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2005 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2006 | accountId, |
| 2007 | accounts, |
| 2008 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2009 | long identityToken = clearCallingIdentity(); |
| 2010 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2011 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2012 | } finally { |
| 2013 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2014 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2015 | } |
| 2016 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2017 | private class RemoveAccountSession extends Session { |
| 2018 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2019 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2020 | Account account, boolean expectActivityLaunch) { |
| 2021 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2022 | true /* stripAuthTokenFromResult */, account.name, |
| 2023 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2024 | mAccount = account; |
| 2025 | } |
| 2026 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2027 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2028 | protected String toDebugString(long now) { |
| 2029 | return super.toDebugString(now) + ", removeAccount" |
| 2030 | + ", account " + mAccount; |
| 2031 | } |
| 2032 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2033 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2034 | public void run() throws RemoteException { |
| 2035 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2036 | } |
| 2037 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2038 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2039 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2040 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2041 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2042 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2043 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2044 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2045 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2046 | } |
| 2047 | IAccountManagerResponse response = getResponseAndClose(); |
| 2048 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2049 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2050 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2051 | + response); |
| 2052 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2053 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2054 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2055 | try { |
| 2056 | response.onResult(result2); |
| 2057 | } catch (RemoteException e) { |
| 2058 | // ignore |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | super.onResult(result); |
| 2063 | } |
| 2064 | } |
| 2065 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2066 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2067 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2068 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2071 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2072 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2073 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2074 | if (!userUnlocked) { |
| 2075 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2076 | + " is still locked. CE data will be removed later"); |
| 2077 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2078 | synchronized (accounts.cacheLock) { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2079 | Map<String, Integer> packagesToVisibility = getRequestingPackages(account, accounts); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2080 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2081 | // Set to a dummy value, this will only be used if the database |
| 2082 | // transaction succeeds. |
| 2083 | long accountId = -1; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2084 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2085 | accountId = accounts.accountsDb.findDeAccountId(account); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2086 | if (accountId >= 0) { |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2087 | isChanged = accounts.accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2088 | } |
Fyodor Kupolov | 98e9e85 | 2016-12-09 14:58:05 -0800 | [diff] [blame] | 2089 | // always delete from CE table if CE storage is available |
| 2090 | // DE account could be removed while CE was locked |
| 2091 | if (userUnlocked) { |
| 2092 | long ceAccountId = accounts.accountsDb.findCeAccountId(account); |
| 2093 | if (ceAccountId >= 0) { |
| 2094 | accounts.accountsDb.deleteCeAccount(ceAccountId); |
| 2095 | } |
| 2096 | } |
| 2097 | accounts.accountsDb.setTransactionSuccessful(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2098 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2099 | accounts.accountsDb.endTransaction(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2100 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2101 | if (isChanged) { |
| 2102 | removeAccountFromCacheLocked(accounts, account); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2103 | for (Entry<String, Integer> packageToVisibility : packagesToVisibility.entrySet()) { |
| 2104 | if (packageToVisibility.getValue() != AccountManager.VISIBILITY_NOT_VISIBLE) { |
| 2105 | notifyPackage(packageToVisibility.getKey(), accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2106 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2107 | } |
| 2108 | |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2109 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occurred. |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2110 | sendAccountsChangedBroadcast(accounts.userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2111 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2112 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2113 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2114 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2115 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2116 | long id = Binder.clearCallingIdentity(); |
| 2117 | try { |
| 2118 | int parentUserId = accounts.userId; |
| 2119 | if (canHaveProfile(parentUserId)) { |
| 2120 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2121 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2122 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2123 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2124 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2125 | } |
| 2126 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2127 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2128 | } finally { |
| 2129 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2130 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2131 | |
| 2132 | if (isChanged) { |
| 2133 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2134 | for (Pair<Pair<Account, String>, Integer> key |
| 2135 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2136 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2137 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2138 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2139 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2140 | account, uid, false)); |
| 2141 | } |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2146 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2149 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2150 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2151 | int callerUid = Binder.getCallingUid(); |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2152 | Preconditions.checkNotNull(accountType, "accountType cannot be null"); |
| 2153 | Preconditions.checkNotNull(authToken, "authToken cannot be null"); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2154 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2155 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2156 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2157 | + ", pid " + Binder.getCallingPid()); |
| 2158 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2159 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2160 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2161 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2162 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2163 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2164 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2165 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2166 | invalidateAuthTokenLocked(accounts, accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2167 | invalidateCustomTokenLocked(accounts, accountType, authToken); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2168 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2169 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2170 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2171 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2172 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2173 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2174 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2175 | } |
| 2176 | } |
| 2177 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2178 | private void invalidateCustomTokenLocked( |
| 2179 | UserAccounts accounts, |
| 2180 | String accountType, |
| 2181 | String authToken) { |
| 2182 | if (authToken == null || accountType == null) { |
| 2183 | return; |
| 2184 | } |
| 2185 | // Also wipe out cached token in memory. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2186 | accounts.accountTokenCaches.remove(accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2187 | } |
| 2188 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2189 | private void invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
| 2190 | String authToken) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2191 | if (authToken == null || accountType == null) { |
| 2192 | return; |
| 2193 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2194 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2195 | try { |
| 2196 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2197 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2198 | String accountName = cursor.getString(1); |
| 2199 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2200 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2201 | writeAuthTokenIntoCacheLocked( |
| 2202 | accounts, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2203 | new Account(accountName, accountType), |
| 2204 | authTokenType, |
| 2205 | null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2206 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2207 | } finally { |
| 2208 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2209 | } |
| 2210 | } |
| 2211 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2212 | private void saveCachedToken( |
| 2213 | UserAccounts accounts, |
| 2214 | Account account, |
| 2215 | String callerPkg, |
| 2216 | byte[] callerSigDigest, |
| 2217 | String tokenType, |
| 2218 | String token, |
| 2219 | long expiryMillis) { |
| 2220 | |
| 2221 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2222 | return; |
| 2223 | } |
| 2224 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2225 | UserHandle.of(accounts.userId)); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2226 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2227 | accounts.accountTokenCaches.put( |
| 2228 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2232 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2233 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2234 | if (account == null || type == null) { |
| 2235 | return false; |
| 2236 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2237 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2238 | UserHandle.of(accounts.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2239 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2240 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2241 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2242 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2243 | if (accountId < 0) { |
| 2244 | return false; |
| 2245 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2246 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2247 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2248 | accounts.accountsDb.setTransactionSuccessful(); |
| 2249 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2250 | return true; |
| 2251 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2252 | return false; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2253 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2254 | accounts.accountsDb.endTransaction(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2255 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2256 | } |
| 2257 | } |
| 2258 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2259 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2260 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2261 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2262 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2263 | Log.v(TAG, "peekAuthToken: " + account |
| 2264 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2265 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2266 | + ", pid " + Binder.getCallingPid()); |
| 2267 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2268 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2269 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2270 | int userId = UserHandle.getCallingUserId(); |
| 2271 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2272 | String msg = String.format( |
| 2273 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2274 | callingUid, |
| 2275 | account.type); |
| 2276 | throw new SecurityException(msg); |
| 2277 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2278 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2279 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2280 | + callingUid); |
| 2281 | return null; |
| 2282 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2283 | long identityToken = clearCallingIdentity(); |
| 2284 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2285 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2286 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2287 | } finally { |
| 2288 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2289 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2292 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2293 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2294 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2295 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2296 | Log.v(TAG, "setAuthToken: " + account |
| 2297 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2298 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2299 | + ", pid " + Binder.getCallingPid()); |
| 2300 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2301 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 2302 | Preconditions.checkNotNull(authTokenType, "authTokenType cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2303 | int userId = UserHandle.getCallingUserId(); |
| 2304 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2305 | String msg = String.format( |
| 2306 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2307 | callingUid, |
| 2308 | account.type); |
| 2309 | throw new SecurityException(msg); |
| 2310 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2311 | long identityToken = clearCallingIdentity(); |
| 2312 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2313 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2314 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2315 | } finally { |
| 2316 | restoreCallingIdentity(identityToken); |
| 2317 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2320 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2321 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2322 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2323 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2324 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2325 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2326 | + ", pid " + Binder.getCallingPid()); |
| 2327 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2328 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2329 | int userId = UserHandle.getCallingUserId(); |
| 2330 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2331 | String msg = String.format( |
| 2332 | "uid %s cannot set secrets for accounts of type: %s", |
| 2333 | callingUid, |
| 2334 | account.type); |
| 2335 | throw new SecurityException(msg); |
| 2336 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2337 | long identityToken = clearCallingIdentity(); |
| 2338 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2339 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2340 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2341 | } finally { |
| 2342 | restoreCallingIdentity(identityToken); |
| 2343 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2344 | } |
| 2345 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2346 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2347 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2348 | if (account == null) { |
| 2349 | return; |
| 2350 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2351 | boolean isChanged = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2352 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2353 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2354 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2355 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2356 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2357 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2358 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2359 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2360 | accounts.accountTokenCaches.remove(account); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2361 | accounts.accountsDb.setTransactionSuccessful(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2362 | // If there is an account whose password will be updated and the database |
| 2363 | // transactions succeed, then we say that a change has occured. Even if the |
| 2364 | // new password is the same as the old and there were no authtokens to delete. |
| 2365 | isChanged = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2366 | String action = (password == null || password.length() == 0) ? |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2367 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2368 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2369 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, callingUid); |
Costin Manolache | f5ffe89 | 2011-01-19 09:35:32 -0800 | [diff] [blame] | 2370 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2371 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2372 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2373 | if (isChanged) { |
| 2374 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2375 | sendNotificationAccountUpdated(account, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2376 | sendAccountsChangedBroadcast(accounts.userId); |
| 2377 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2378 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2379 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2380 | } |
| 2381 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2382 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2383 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2384 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2385 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2386 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2387 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2388 | + ", pid " + Binder.getCallingPid()); |
| 2389 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2390 | Preconditions.checkNotNull(account, "account cannot be null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2391 | int userId = UserHandle.getCallingUserId(); |
| 2392 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2393 | String msg = String.format( |
| 2394 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2395 | callingUid, |
| 2396 | account.type); |
| 2397 | throw new SecurityException(msg); |
| 2398 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2399 | long identityToken = clearCallingIdentity(); |
| 2400 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2401 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2402 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2403 | } finally { |
| 2404 | restoreCallingIdentity(identityToken); |
| 2405 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2406 | } |
| 2407 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2408 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2409 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2410 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2411 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2412 | Log.v(TAG, "setUserData: " + account |
| 2413 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2414 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2415 | + ", pid " + Binder.getCallingPid()); |
| 2416 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2417 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2418 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2419 | int userId = UserHandle.getCallingUserId(); |
| 2420 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2421 | String msg = String.format( |
| 2422 | "uid %s cannot set user data for accounts of type: %s", |
| 2423 | callingUid, |
| 2424 | account.type); |
| 2425 | throw new SecurityException(msg); |
| 2426 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2427 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2428 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2429 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2430 | synchronized (accounts.cacheLock) { |
| 2431 | if (!accountExistsCacheLocked(accounts, account)) { |
| 2432 | return; |
| 2433 | } |
| 2434 | setUserdataInternalLocked(accounts, account, key, value); |
| 2435 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2436 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2437 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2441 | private boolean accountExistsCacheLocked(UserAccounts accounts, Account account) { |
| 2442 | if (accounts.accountCache.containsKey(account.type)) { |
| 2443 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2444 | if (acc.name.equals(account.name)) { |
| 2445 | return true; |
| 2446 | } |
| 2447 | } |
| 2448 | } |
| 2449 | return false; |
| 2450 | } |
| 2451 | |
| 2452 | private void setUserdataInternalLocked(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2453 | String value) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2454 | if (account == null || key == null) { |
| 2455 | return; |
| 2456 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2457 | accounts.accountsDb.beginTransaction(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2458 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2459 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2460 | if (accountId < 0) { |
| 2461 | return; |
| 2462 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2463 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2464 | if (extrasId < 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2465 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2466 | if (extrasId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2467 | return; |
| 2468 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2469 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2470 | return; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2471 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2472 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2473 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2474 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2475 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2479 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2480 | if (result == null) { |
| 2481 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2482 | } |
| 2483 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2484 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2485 | + response); |
| 2486 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2487 | try { |
| 2488 | response.onResult(result); |
| 2489 | } catch (RemoteException e) { |
| 2490 | // if the caller is dead then there is no one to care about remote |
| 2491 | // exceptions |
| 2492 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2493 | Log.v(TAG, "failure while notifying response", e); |
| 2494 | } |
| 2495 | } |
| 2496 | } |
| 2497 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2498 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2499 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2500 | final String authTokenType) |
| 2501 | throws RemoteException { |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2502 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
| 2503 | Preconditions.checkArgument(authTokenType != null, "authTokenType cannot be null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2504 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2505 | final int callingUid = getCallingUid(); |
| 2506 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2507 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2508 | throw new SecurityException("can only call from system"); |
| 2509 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2510 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2511 | long identityToken = clearCallingIdentity(); |
| 2512 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2513 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2514 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2515 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2516 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2517 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2518 | protected String toDebugString(long now) { |
| 2519 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2520 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2521 | + ", authTokenType " + authTokenType; |
| 2522 | } |
| 2523 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2524 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2525 | public void run() throws RemoteException { |
| 2526 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2527 | } |
| 2528 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2529 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2530 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2531 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2532 | if (result != null) { |
| 2533 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2534 | Bundle bundle = new Bundle(); |
| 2535 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2536 | super.onResult(bundle); |
| 2537 | return; |
| 2538 | } else { |
| 2539 | super.onResult(result); |
| 2540 | } |
| 2541 | } |
| 2542 | }.bind(); |
| 2543 | } finally { |
| 2544 | restoreCallingIdentity(identityToken); |
| 2545 | } |
| 2546 | } |
| 2547 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2548 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2549 | public void getAuthToken( |
| 2550 | IAccountManagerResponse response, |
| 2551 | final Account account, |
| 2552 | final String authTokenType, |
| 2553 | final boolean notifyOnAuthFailure, |
| 2554 | final boolean expectActivityLaunch, |
| 2555 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2556 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2557 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2558 | Log.v(TAG, "getAuthToken: " + account |
| 2559 | + ", response " + response |
| 2560 | + ", authTokenType " + authTokenType |
| 2561 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2562 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2563 | + ", caller's uid " + Binder.getCallingUid() |
| 2564 | + ", pid " + Binder.getCallingPid()); |
| 2565 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2566 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2567 | try { |
| 2568 | if (account == null) { |
| 2569 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2570 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2571 | return; |
| 2572 | } |
| 2573 | if (authTokenType == null) { |
| 2574 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2575 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2576 | return; |
| 2577 | } |
| 2578 | } catch (RemoteException e) { |
| 2579 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2580 | return; |
| 2581 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2582 | int userId = UserHandle.getCallingUserId(); |
| 2583 | long ident = Binder.clearCallingIdentity(); |
| 2584 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2585 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2586 | try { |
| 2587 | accounts = getUserAccounts(userId); |
| 2588 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2589 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2590 | } finally { |
| 2591 | Binder.restoreCallingIdentity(ident); |
| 2592 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2593 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2594 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2595 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2596 | |
| 2597 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2598 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2599 | final boolean permissionGranted = |
| 2600 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2601 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2602 | // Get the calling package. We will use it for the purpose of caching. |
| 2603 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2604 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2605 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2606 | try { |
| 2607 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2608 | } finally { |
| 2609 | Binder.restoreCallingIdentity(ident); |
| 2610 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2611 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2612 | String msg = String.format( |
| 2613 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2614 | callerUid, |
| 2615 | callerPkg); |
| 2616 | throw new SecurityException(msg); |
| 2617 | } |
| 2618 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2619 | // let authenticator know the identity of the caller |
| 2620 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2621 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2622 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2623 | if (notifyOnAuthFailure) { |
| 2624 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2625 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2626 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2627 | long identityToken = clearCallingIdentity(); |
| 2628 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2629 | // Distill the caller's package signatures into a single digest. |
| 2630 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2631 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2632 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2633 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2634 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2635 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2636 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2637 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2638 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2639 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2640 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2641 | onResult(response, result); |
| 2642 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2643 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2644 | } |
| 2645 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2646 | if (customTokens) { |
| 2647 | /* |
| 2648 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2649 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2650 | * ANDORID_PACKAGE_NAME. |
| 2651 | */ |
| 2652 | String token = readCachedTokenInternal( |
| 2653 | accounts, |
| 2654 | account, |
| 2655 | authTokenType, |
| 2656 | callerPkg, |
| 2657 | callerPkgSigDigest); |
| 2658 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2659 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2660 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2661 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2662 | Bundle result = new Bundle(); |
| 2663 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2664 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2665 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2666 | onResult(response, result); |
| 2667 | return; |
| 2668 | } |
| 2669 | } |
| 2670 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2671 | new Session( |
| 2672 | accounts, |
| 2673 | response, |
| 2674 | account.type, |
| 2675 | expectActivityLaunch, |
| 2676 | false /* stripAuthTokenFromResult */, |
| 2677 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2678 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2679 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2680 | protected String toDebugString(long now) { |
| 2681 | if (loginOptions != null) loginOptions.keySet(); |
| 2682 | return super.toDebugString(now) + ", getAuthToken" |
| 2683 | + ", " + account |
| 2684 | + ", authTokenType " + authTokenType |
| 2685 | + ", loginOptions " + loginOptions |
| 2686 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2687 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2688 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2689 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2690 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2691 | // If the caller doesn't have permission then create and return the |
| 2692 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2693 | if (!permissionGranted) { |
| 2694 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2695 | } else { |
| 2696 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2697 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2698 | } |
| 2699 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2700 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2701 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2702 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2703 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2704 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2705 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2706 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2707 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2708 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2709 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2710 | authTokenType, |
| 2711 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2712 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2713 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2714 | onResult(bundle); |
| 2715 | return; |
| 2716 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2717 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2718 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2719 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2720 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2721 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2722 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2723 | "the type and name should not be empty"); |
| 2724 | return; |
| 2725 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2726 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2727 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2728 | saveAuthTokenToDatabase( |
| 2729 | mAccounts, |
| 2730 | resultAccount, |
| 2731 | authTokenType, |
| 2732 | authToken); |
| 2733 | } |
| 2734 | long expiryMillis = result.getLong( |
| 2735 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2736 | if (customTokens |
| 2737 | && expiryMillis > System.currentTimeMillis()) { |
| 2738 | saveCachedToken( |
| 2739 | mAccounts, |
| 2740 | account, |
| 2741 | callerPkg, |
| 2742 | callerPkgSigDigest, |
| 2743 | authTokenType, |
| 2744 | authToken, |
| 2745 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2746 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2747 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2748 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2749 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2750 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2751 | /* |
| 2752 | * Make sure that the supplied intent is owned by the authenticator |
| 2753 | * giving it to the system. Otherwise a malicious authenticator could |
| 2754 | * have users launching arbitrary activities by tricking users to |
| 2755 | * interact with malicious notifications. |
| 2756 | */ |
| 2757 | checkKeyIntent( |
| 2758 | Binder.getCallingUid(), |
| 2759 | intent); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 2760 | doNotification( |
| 2761 | mAccounts, |
| 2762 | account, |
| 2763 | result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2764 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2765 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2766 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2767 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2768 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2769 | }.bind(); |
| 2770 | } finally { |
| 2771 | restoreCallingIdentity(identityToken); |
| 2772 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2775 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2776 | MessageDigest digester; |
| 2777 | try { |
| 2778 | digester = MessageDigest.getInstance("SHA-256"); |
| 2779 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2780 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2781 | for (Signature sig : pkgInfo.signatures) { |
| 2782 | digester.update(sig.toByteArray()); |
| 2783 | } |
| 2784 | } catch (NoSuchAlgorithmException x) { |
| 2785 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2786 | digester = null; |
| 2787 | } catch (NameNotFoundException e) { |
| 2788 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2789 | digester = null; |
| 2790 | } |
| 2791 | return (digester == null) ? null : digester.digest(); |
| 2792 | } |
| 2793 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2794 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2795 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2796 | int uid = intent.getIntExtra( |
| 2797 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2798 | String authTokenType = intent.getStringExtra( |
| 2799 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2800 | final String titleAndSubtitle = |
| 2801 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2802 | account.name); |
| 2803 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2804 | String title = titleAndSubtitle; |
| 2805 | String subtitle = ""; |
| 2806 | if (index > 0) { |
| 2807 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2808 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2809 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2810 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2811 | Context contextForUser = getContextForUser(user); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 2812 | Notification n = |
| 2813 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
| 2814 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2815 | .setWhen(0) |
| 2816 | .setColor(contextForUser.getColor( |
| 2817 | com.android.internal.R.color.system_notification_accent_color)) |
| 2818 | .setContentTitle(title) |
| 2819 | .setContentText(subtitle) |
| 2820 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2821 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2822 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2823 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2824 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2825 | } |
| 2826 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2827 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 2828 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 2829 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2830 | |
| 2831 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2832 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2833 | if (startInNewTask) { |
| 2834 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 2835 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2836 | // the intent from a non-Activity context. This is the default behavior. |
| 2837 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2838 | } |
| 2839 | intent.addCategory(String.valueOf(getCredentialPermissionNotificationId(account, |
| 2840 | authTokenType, uid) + (packageName != null ? packageName : ""))); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2841 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2842 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2843 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2844 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2845 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2846 | return intent; |
| 2847 | } |
| 2848 | |
| 2849 | private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, |
| 2850 | int uid) { |
| 2851 | Integer id; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2852 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2853 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2854 | final Pair<Pair<Account, String>, Integer> key = |
| 2855 | new Pair<Pair<Account, String>, Integer>( |
| 2856 | new Pair<Account, String>(account, authTokenType), uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2857 | id = accounts.credentialsPermissionNotificationIds.get(key); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2858 | if (id == null) { |
| 2859 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2860 | accounts.credentialsPermissionNotificationIds.put(key, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2861 | } |
| 2862 | } |
| 2863 | return id; |
| 2864 | } |
| 2865 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2866 | private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2867 | Integer id; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2868 | synchronized (accounts.signinRequiredNotificationIds) { |
| 2869 | id = accounts.signinRequiredNotificationIds.get(account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2870 | if (id == null) { |
| 2871 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2872 | accounts.signinRequiredNotificationIds.put(account, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2873 | } |
| 2874 | } |
| 2875 | return id; |
| 2876 | } |
| 2877 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2878 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2879 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2880 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2881 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2882 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2883 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2884 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2885 | + ", response " + response |
| 2886 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2887 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2888 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2889 | + ", caller's uid " + Binder.getCallingUid() |
| 2890 | + ", pid " + Binder.getCallingPid()); |
| 2891 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2892 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2893 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2894 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2895 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2896 | final int uid = Binder.getCallingUid(); |
| 2897 | final int userId = UserHandle.getUserId(uid); |
| 2898 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2899 | try { |
| 2900 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2901 | "User is not allowed to add an account!"); |
| 2902 | } catch (RemoteException re) { |
| 2903 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2904 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2905 | return; |
| 2906 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2907 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2908 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2909 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2910 | "User cannot modify accounts of this type (policy)."); |
| 2911 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2912 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2913 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2914 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2915 | return; |
| 2916 | } |
| 2917 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2918 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2919 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2920 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2921 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2922 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2923 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2924 | long identityToken = clearCallingIdentity(); |
| 2925 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2926 | UserAccounts accounts = getUserAccounts(usrId); |
| 2927 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2928 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 2929 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2930 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2931 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2932 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2933 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2934 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2935 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2936 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2937 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2938 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2939 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2940 | protected String toDebugString(long now) { |
| 2941 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2942 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2943 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2944 | } |
| 2945 | }.bind(); |
| 2946 | } finally { |
| 2947 | restoreCallingIdentity(identityToken); |
| 2948 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2951 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2952 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 2953 | final String authTokenType, final String[] requiredFeatures, |
| 2954 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2955 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2956 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2957 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2958 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2959 | + ", response " + response |
| 2960 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2961 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2962 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2963 | + ", caller's uid " + Binder.getCallingUid() |
| 2964 | + ", pid " + Binder.getCallingPid() |
| 2965 | + ", for user id " + userId); |
| 2966 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 2967 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 2968 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2969 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2970 | if (isCrossUser(callingUid, userId)) { |
| 2971 | throw new SecurityException( |
| 2972 | String.format( |
| 2973 | "User %s trying to add account for %s" , |
| 2974 | UserHandle.getCallingUserId(), |
| 2975 | userId)); |
| 2976 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2977 | |
| 2978 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2979 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2980 | try { |
| 2981 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2982 | "User is not allowed to add an account!"); |
| 2983 | } catch (RemoteException re) { |
| 2984 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2985 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2986 | return; |
| 2987 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2988 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2989 | try { |
| 2990 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2991 | "User cannot modify accounts of this type (policy)."); |
| 2992 | } catch (RemoteException re) { |
| 2993 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2994 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2995 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2996 | return; |
| 2997 | } |
| 2998 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2999 | final int pid = Binder.getCallingPid(); |
| 3000 | final int uid = Binder.getCallingUid(); |
| 3001 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3002 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3003 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3004 | |
| 3005 | long identityToken = clearCallingIdentity(); |
| 3006 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3007 | UserAccounts accounts = getUserAccounts(userId); |
| 3008 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3009 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3010 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3011 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3012 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3013 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3014 | @Override |
| 3015 | public void run() throws RemoteException { |
| 3016 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3017 | options); |
| 3018 | } |
| 3019 | |
| 3020 | @Override |
| 3021 | protected String toDebugString(long now) { |
| 3022 | return super.toDebugString(now) + ", addAccount" |
| 3023 | + ", accountType " + accountType |
| 3024 | + ", requiredFeatures " |
| 3025 | + (requiredFeatures != null |
| 3026 | ? TextUtils.join(",", requiredFeatures) |
| 3027 | : null); |
| 3028 | } |
| 3029 | }.bind(); |
| 3030 | } finally { |
| 3031 | restoreCallingIdentity(identityToken); |
| 3032 | } |
| 3033 | } |
| 3034 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3035 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3036 | public void startAddAccountSession( |
| 3037 | final IAccountManagerResponse response, |
| 3038 | final String accountType, |
| 3039 | final String authTokenType, |
| 3040 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3041 | final boolean expectActivityLaunch, |
| 3042 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3043 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3044 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3045 | Log.v(TAG, |
| 3046 | "startAddAccountSession: accountType " + accountType |
| 3047 | + ", response " + response |
| 3048 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3049 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3050 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3051 | + ", caller's uid " + Binder.getCallingUid() |
| 3052 | + ", pid " + Binder.getCallingPid()); |
| 3053 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3054 | Preconditions.checkArgument(response != null, "response cannot be null"); |
| 3055 | Preconditions.checkArgument(accountType != null, "accountType cannot be null"); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3056 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3057 | final int uid = Binder.getCallingUid(); |
| 3058 | final int userId = UserHandle.getUserId(uid); |
| 3059 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3060 | try { |
| 3061 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3062 | "User is not allowed to add an account!"); |
| 3063 | } catch (RemoteException re) { |
| 3064 | } |
| 3065 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3066 | return; |
| 3067 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3068 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3069 | try { |
| 3070 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3071 | "User cannot modify accounts of this type (policy)."); |
| 3072 | } catch (RemoteException re) { |
| 3073 | } |
| 3074 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3075 | userId); |
| 3076 | return; |
| 3077 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3078 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3079 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3080 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3081 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3082 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3083 | // Check to see if the Password should be included to the caller. |
| 3084 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3085 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3086 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3087 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3088 | long identityToken = clearCallingIdentity(); |
| 3089 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3090 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3091 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3092 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3093 | new StartAccountSession( |
| 3094 | accounts, |
| 3095 | response, |
| 3096 | accountType, |
| 3097 | expectActivityLaunch, |
| 3098 | null /* accountName */, |
| 3099 | false /* authDetailsRequired */, |
| 3100 | true /* updateLastAuthenticationTime */, |
| 3101 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3102 | @Override |
| 3103 | public void run() throws RemoteException { |
| 3104 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3105 | requiredFeatures, options); |
| 3106 | } |
| 3107 | |
| 3108 | @Override |
| 3109 | protected String toDebugString(long now) { |
| 3110 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3111 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3112 | + accountType + ", requiredFeatures " |
| 3113 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3114 | } |
| 3115 | }.bind(); |
| 3116 | } finally { |
| 3117 | restoreCallingIdentity(identityToken); |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3122 | private abstract class StartAccountSession extends Session { |
| 3123 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3124 | private final boolean mIsPasswordForwardingAllowed; |
| 3125 | |
| 3126 | public StartAccountSession( |
| 3127 | UserAccounts accounts, |
| 3128 | IAccountManagerResponse response, |
| 3129 | String accountType, |
| 3130 | boolean expectActivityLaunch, |
| 3131 | String accountName, |
| 3132 | boolean authDetailsRequired, |
| 3133 | boolean updateLastAuthenticationTime, |
| 3134 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3135 | super(accounts, response, accountType, expectActivityLaunch, |
| 3136 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3137 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3138 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | @Override |
| 3142 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3143 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3144 | mNumResults++; |
| 3145 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3146 | if (result != null |
| 3147 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3148 | checkKeyIntent( |
| 3149 | Binder.getCallingUid(), |
| 3150 | intent); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3151 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3152 | IAccountManagerResponse response; |
| 3153 | if (mExpectActivityLaunch && result != null |
| 3154 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3155 | response = mResponse; |
| 3156 | } else { |
| 3157 | response = getResponseAndClose(); |
| 3158 | } |
| 3159 | if (response == null) { |
| 3160 | return; |
| 3161 | } |
| 3162 | if (result == null) { |
| 3163 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3164 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3165 | + response); |
| 3166 | } |
| 3167 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3168 | "null bundle returned"); |
| 3169 | return; |
| 3170 | } |
| 3171 | |
| 3172 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3173 | // All AccountManager error codes are greater |
| 3174 | // than 0 |
| 3175 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3176 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3177 | return; |
| 3178 | } |
| 3179 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3180 | // Omit passwords if the caller isn't permitted to see them. |
| 3181 | if (!mIsPasswordForwardingAllowed) { |
| 3182 | result.remove(AccountManager.KEY_PASSWORD); |
| 3183 | } |
| 3184 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3185 | // Strip auth token from result. |
| 3186 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3187 | |
| 3188 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3189 | Log.v(TAG, |
| 3190 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3191 | } |
| 3192 | |
| 3193 | // Get the session bundle created by authenticator. The |
| 3194 | // bundle contains data necessary for finishing the session |
| 3195 | // later. The session bundle will be encrypted here and |
| 3196 | // decrypted later when trying to finish the session. |
| 3197 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3198 | if (sessionBundle != null) { |
| 3199 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3200 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3201 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3202 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3203 | } |
| 3204 | // Add accountType info to session bundle. This will |
| 3205 | // override any value set by authenticator. |
| 3206 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3207 | |
| 3208 | // Encrypt session bundle before returning to caller. |
| 3209 | try { |
| 3210 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3211 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3212 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3213 | } catch (GeneralSecurityException e) { |
| 3214 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3215 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3216 | } |
| 3217 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3218 | "failed to encrypt session bundle"); |
| 3219 | return; |
| 3220 | } |
| 3221 | } |
| 3222 | |
| 3223 | sendResponse(response, result); |
| 3224 | } |
| 3225 | } |
| 3226 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3227 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3228 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3229 | @NonNull Bundle sessionBundle, |
| 3230 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3231 | Bundle appInfo, |
| 3232 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3233 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3234 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3235 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3236 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3237 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3238 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3239 | + ", caller's uid " + callingUid |
| 3240 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3241 | + ", pid " + Binder.getCallingPid() |
| 3242 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3243 | } |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3244 | Preconditions.checkArgument(response != null, "response cannot be null"); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3245 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3246 | // Account type is added to it before encryption. |
| 3247 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3248 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3249 | } |
| 3250 | |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 3251 | // Only allow the system process to finish session for other users. |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3252 | if (isCrossUser(callingUid, userId)) { |
| 3253 | throw new SecurityException( |
| 3254 | String.format( |
| 3255 | "User %s trying to finish session for %s without cross user permission", |
| 3256 | UserHandle.getCallingUserId(), |
| 3257 | userId)); |
| 3258 | } |
| 3259 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3260 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3261 | sendErrorResponse(response, |
| 3262 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3263 | "User is not allowed to add an account!"); |
| 3264 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3265 | return; |
| 3266 | } |
| 3267 | |
| 3268 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3269 | final Bundle decryptedBundle; |
| 3270 | final String accountType; |
| 3271 | // First decrypt session bundle to get account type for checking permission. |
| 3272 | try { |
| 3273 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3274 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3275 | if (decryptedBundle == null) { |
| 3276 | sendErrorResponse( |
| 3277 | response, |
| 3278 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3279 | "failed to decrypt session bundle"); |
| 3280 | return; |
| 3281 | } |
| 3282 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3283 | // Account type cannot be null. This should not happen if session bundle was created |
| 3284 | // properly by #StartAccountSession. |
| 3285 | if (TextUtils.isEmpty(accountType)) { |
| 3286 | sendErrorResponse( |
| 3287 | response, |
| 3288 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3289 | "accountType is empty"); |
| 3290 | return; |
| 3291 | } |
| 3292 | |
| 3293 | // If by any chances, decryptedBundle contains colliding keys with |
| 3294 | // system info |
| 3295 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3296 | // update credentials flow, we should replace with the new values of the current call. |
| 3297 | if (appInfo != null) { |
| 3298 | decryptedBundle.putAll(appInfo); |
| 3299 | } |
| 3300 | |
| 3301 | // 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] | 3302 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3303 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3304 | } catch (GeneralSecurityException e) { |
| 3305 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3306 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3307 | } |
| 3308 | sendErrorResponse( |
| 3309 | response, |
| 3310 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3311 | "failed to decrypt session bundle"); |
| 3312 | return; |
| 3313 | } |
| 3314 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3315 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3316 | sendErrorResponse( |
| 3317 | response, |
| 3318 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3319 | "User cannot modify accounts of this type (policy)."); |
| 3320 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3321 | userId); |
| 3322 | return; |
| 3323 | } |
| 3324 | |
| 3325 | long identityToken = clearCallingIdentity(); |
| 3326 | try { |
| 3327 | UserAccounts accounts = getUserAccounts(userId); |
| 3328 | logRecordWithUid( |
| 3329 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3330 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3331 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3332 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3333 | new Session( |
| 3334 | accounts, |
| 3335 | response, |
| 3336 | accountType, |
| 3337 | expectActivityLaunch, |
| 3338 | true /* stripAuthTokenFromResult */, |
| 3339 | null /* accountName */, |
| 3340 | false /* authDetailsRequired */, |
| 3341 | true /* updateLastAuthenticationTime */) { |
| 3342 | @Override |
| 3343 | public void run() throws RemoteException { |
| 3344 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3345 | } |
| 3346 | |
| 3347 | @Override |
| 3348 | protected String toDebugString(long now) { |
| 3349 | return super.toDebugString(now) |
| 3350 | + ", finishSession" |
| 3351 | + ", accountType " + accountType; |
| 3352 | } |
| 3353 | }.bind(); |
| 3354 | } finally { |
| 3355 | restoreCallingIdentity(identityToken); |
| 3356 | } |
| 3357 | } |
| 3358 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3359 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3360 | final DevicePolicyManagerInternal dpmi = |
| 3361 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3362 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3363 | if (dpmi == null) { |
| 3364 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3365 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3366 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3367 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3368 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3369 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3370 | } |
| 3371 | if (intent == null) { |
| 3372 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3373 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3374 | long identityToken = clearCallingIdentity(); |
| 3375 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3376 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3377 | } finally { |
| 3378 | restoreCallingIdentity(identityToken); |
| 3379 | } |
| 3380 | } |
| 3381 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3382 | /** |
| 3383 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3384 | */ |
| 3385 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3386 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3387 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3388 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3389 | return cantAddAccount; |
| 3390 | } |
| 3391 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3392 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3393 | public void confirmCredentialsAsUser( |
| 3394 | IAccountManagerResponse response, |
| 3395 | final Account account, |
| 3396 | final Bundle options, |
| 3397 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3398 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3399 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3400 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3401 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3402 | Log.v(TAG, "confirmCredentials: " + account |
| 3403 | + ", response " + response |
| 3404 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3405 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3406 | + ", pid " + Binder.getCallingPid()); |
| 3407 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3408 | // Only allow the system process to read accounts of other users |
| 3409 | if (isCrossUser(callingUid, userId)) { |
| 3410 | throw new SecurityException( |
| 3411 | String.format( |
| 3412 | "User %s trying to confirm account credentials for %s" , |
| 3413 | UserHandle.getCallingUserId(), |
| 3414 | userId)); |
| 3415 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3416 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3417 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3418 | long identityToken = clearCallingIdentity(); |
| 3419 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3420 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3421 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3422 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3423 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3424 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3425 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3426 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3427 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3428 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3429 | protected String toDebugString(long now) { |
| 3430 | return super.toDebugString(now) + ", confirmCredentials" |
| 3431 | + ", " + account; |
| 3432 | } |
| 3433 | }.bind(); |
| 3434 | } finally { |
| 3435 | restoreCallingIdentity(identityToken); |
| 3436 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3437 | } |
| 3438 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3439 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3440 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3441 | final String authTokenType, final boolean expectActivityLaunch, |
| 3442 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3443 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3444 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3445 | Log.v(TAG, "updateCredentials: " + account |
| 3446 | + ", response " + response |
| 3447 | + ", authTokenType " + authTokenType |
| 3448 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3449 | + ", caller's uid " + Binder.getCallingUid() |
| 3450 | + ", pid " + Binder.getCallingPid()); |
| 3451 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3452 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3453 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3454 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3455 | long identityToken = clearCallingIdentity(); |
| 3456 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3457 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3458 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3459 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3460 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3461 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3462 | public void run() throws RemoteException { |
| 3463 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3464 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3465 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3466 | protected String toDebugString(long now) { |
| 3467 | if (loginOptions != null) loginOptions.keySet(); |
| 3468 | return super.toDebugString(now) + ", updateCredentials" |
| 3469 | + ", " + account |
| 3470 | + ", authTokenType " + authTokenType |
| 3471 | + ", loginOptions " + loginOptions; |
| 3472 | } |
| 3473 | }.bind(); |
| 3474 | } finally { |
| 3475 | restoreCallingIdentity(identityToken); |
| 3476 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3479 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3480 | public void startUpdateCredentialsSession( |
| 3481 | IAccountManagerResponse response, |
| 3482 | final Account account, |
| 3483 | final String authTokenType, |
| 3484 | final boolean expectActivityLaunch, |
| 3485 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3486 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3487 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3488 | Log.v(TAG, |
| 3489 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3490 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3491 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3492 | + ", pid " + Binder.getCallingPid()); |
| 3493 | } |
| 3494 | if (response == null) { |
| 3495 | throw new IllegalArgumentException("response is null"); |
| 3496 | } |
| 3497 | if (account == null) { |
| 3498 | throw new IllegalArgumentException("account is null"); |
| 3499 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3500 | |
| 3501 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3502 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3503 | |
| 3504 | // Check to see if the Password should be included to the caller. |
| 3505 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3506 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3507 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3508 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3509 | long identityToken = clearCallingIdentity(); |
| 3510 | try { |
| 3511 | UserAccounts accounts = getUserAccounts(userId); |
| 3512 | new StartAccountSession( |
| 3513 | accounts, |
| 3514 | response, |
| 3515 | account.type, |
| 3516 | expectActivityLaunch, |
| 3517 | account.name, |
| 3518 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3519 | true /* updateLastCredentialTime */, |
| 3520 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3521 | @Override |
| 3522 | public void run() throws RemoteException { |
| 3523 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3524 | loginOptions); |
| 3525 | } |
| 3526 | |
| 3527 | @Override |
| 3528 | protected String toDebugString(long now) { |
| 3529 | if (loginOptions != null) |
| 3530 | loginOptions.keySet(); |
| 3531 | return super.toDebugString(now) |
| 3532 | + ", startUpdateCredentialsSession" |
| 3533 | + ", " + account |
| 3534 | + ", authTokenType " + authTokenType |
| 3535 | + ", loginOptions " + loginOptions; |
| 3536 | } |
| 3537 | }.bind(); |
| 3538 | } finally { |
| 3539 | restoreCallingIdentity(identityToken); |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3544 | public void isCredentialsUpdateSuggested( |
| 3545 | IAccountManagerResponse response, |
| 3546 | final Account account, |
| 3547 | final String statusToken) { |
| 3548 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3549 | Log.v(TAG, |
| 3550 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3551 | + ", caller's uid " + Binder.getCallingUid() |
| 3552 | + ", pid " + Binder.getCallingPid()); |
| 3553 | } |
| 3554 | if (response == null) { |
| 3555 | throw new IllegalArgumentException("response is null"); |
| 3556 | } |
| 3557 | if (account == null) { |
| 3558 | throw new IllegalArgumentException("account is null"); |
| 3559 | } |
| 3560 | if (TextUtils.isEmpty(statusToken)) { |
| 3561 | throw new IllegalArgumentException("status token is empty"); |
| 3562 | } |
| 3563 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3564 | int usrId = UserHandle.getCallingUserId(); |
| 3565 | long identityToken = clearCallingIdentity(); |
| 3566 | try { |
| 3567 | UserAccounts accounts = getUserAccounts(usrId); |
| 3568 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3569 | false /* stripAuthTokenFromResult */, account.name, |
| 3570 | false /* authDetailsRequired */) { |
| 3571 | @Override |
| 3572 | protected String toDebugString(long now) { |
| 3573 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3574 | + ", " + account; |
| 3575 | } |
| 3576 | |
| 3577 | @Override |
| 3578 | public void run() throws RemoteException { |
| 3579 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3580 | } |
| 3581 | |
| 3582 | @Override |
| 3583 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3584 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3585 | IAccountManagerResponse response = getResponseAndClose(); |
| 3586 | if (response == null) { |
| 3587 | return; |
| 3588 | } |
| 3589 | |
| 3590 | if (result == null) { |
| 3591 | sendErrorResponse( |
| 3592 | response, |
| 3593 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3594 | "null bundle"); |
| 3595 | return; |
| 3596 | } |
| 3597 | |
| 3598 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3599 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3600 | + response); |
| 3601 | } |
| 3602 | // Check to see if an error occurred. We know if an error occurred because all |
| 3603 | // error codes are greater than 0. |
| 3604 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3605 | sendErrorResponse(response, |
| 3606 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3607 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3608 | return; |
| 3609 | } |
| 3610 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3611 | sendErrorResponse( |
| 3612 | response, |
| 3613 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3614 | "no result in response"); |
| 3615 | return; |
| 3616 | } |
| 3617 | final Bundle newResult = new Bundle(); |
| 3618 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3619 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3620 | sendResponse(response, newResult); |
| 3621 | } |
| 3622 | }.bind(); |
| 3623 | } finally { |
| 3624 | restoreCallingIdentity(identityToken); |
| 3625 | } |
| 3626 | } |
| 3627 | |
| 3628 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3629 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3630 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3631 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3632 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3633 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3634 | + ", response " + response |
| 3635 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3636 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3637 | + ", pid " + Binder.getCallingPid()); |
| 3638 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3639 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3640 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3641 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3642 | if (!isAccountManagedByCaller(accountType, callingUid, userId) |
| 3643 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3644 | String msg = String.format( |
| 3645 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3646 | callingUid, |
| 3647 | accountType); |
| 3648 | throw new SecurityException(msg); |
| 3649 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3650 | long identityToken = clearCallingIdentity(); |
| 3651 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3652 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3653 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3654 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3655 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3656 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3657 | public void run() throws RemoteException { |
| 3658 | mAuthenticator.editProperties(this, mAccountType); |
| 3659 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3660 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3661 | protected String toDebugString(long now) { |
| 3662 | return super.toDebugString(now) + ", editProperties" |
| 3663 | + ", accountType " + accountType; |
| 3664 | } |
| 3665 | }.bind(); |
| 3666 | } finally { |
| 3667 | restoreCallingIdentity(identityToken); |
| 3668 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3669 | } |
| 3670 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3671 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3672 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3673 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3674 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3675 | throw new SecurityException("Can be called only by system UID"); |
| 3676 | } |
| 3677 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3678 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3679 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3680 | |
| 3681 | final int userId = userHandle.getIdentifier(); |
| 3682 | |
| 3683 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3684 | |
| 3685 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3686 | int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3687 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3688 | } catch (NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3689 | Log.d(TAG, "Package not found " + e.getMessage()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3690 | return false; |
| 3691 | } |
| 3692 | } |
| 3693 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3694 | // Returns package with oldest target SDK for given UID. |
| 3695 | private String getPackageNameForUid(int uid) { |
| 3696 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3697 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3698 | return null; |
| 3699 | } |
| 3700 | // For app op checks related to permissions all packages in the UID |
| 3701 | // have the same app op state, so doesn't matter which one we pick. |
| 3702 | // Update: due to visibility changes we want to use package with oldest target SDK, |
| 3703 | |
| 3704 | String packageName = packageNames[0]; |
| 3705 | int oldestVersion = Integer.MAX_VALUE; |
| 3706 | for (String name : packageNames) { |
| 3707 | try { |
| 3708 | ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(name, 0); |
| 3709 | if (applicationInfo != null) { |
| 3710 | int version = applicationInfo.targetSdkVersion; |
| 3711 | if (version < oldestVersion) { |
| 3712 | oldestVersion = version; |
| 3713 | packageName = name; |
| 3714 | } |
| 3715 | } |
| 3716 | } catch (NameNotFoundException e) { |
| 3717 | // skip |
| 3718 | } |
| 3719 | } |
| 3720 | return packageName; |
| 3721 | } |
| 3722 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3723 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3724 | int uid) { |
| 3725 | if (packageName == null) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3726 | packageName = getPackageNameForUid(uid); |
| 3727 | if (packageName == null) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3728 | return false; |
| 3729 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | // Use null token which means any token. Having a token means the package |
| 3733 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3734 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3735 | return true; |
| 3736 | } |
| 3737 | // 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] | 3738 | // 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] | 3739 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3740 | int visibility = resolveAccountVisibility(account, packageName, |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3741 | getUserAccounts(UserHandle.getUserId(uid))); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3742 | return (visibility == AccountManager.VISIBILITY_VISIBLE |
Dmitry Dementyev | 8882d88 | 2017-03-14 17:25:46 -0700 | [diff] [blame] | 3743 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | @Override |
| 3747 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3748 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3749 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3750 | throw new SecurityException("Can be called only by system UID"); |
| 3751 | } |
| 3752 | |
| 3753 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3754 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3755 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3756 | |
| 3757 | final int userId = userHandle.getIdentifier(); |
| 3758 | |
| 3759 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3760 | |
| 3761 | final int uid; |
| 3762 | try { |
| 3763 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 3764 | } catch (NameNotFoundException e) { |
| 3765 | Slog.e(TAG, "Unknown package " + packageName); |
| 3766 | return null; |
| 3767 | } |
| 3768 | |
| 3769 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 3770 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3771 | final long identity = Binder.clearCallingIdentity(); |
| 3772 | try { |
| 3773 | return PendingIntent.getActivityAsUser( |
| 3774 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 3775 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 3776 | null, new UserHandle(userId)).getIntentSender(); |
| 3777 | } finally { |
| 3778 | Binder.restoreCallingIdentity(identity); |
| 3779 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 3783 | int uid, RemoteCallback callback) { |
| 3784 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 3785 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 3786 | @Override |
| 3787 | public void onResult(Bundle value) throws RemoteException { |
| 3788 | handleAuthenticatorResponse(true); |
| 3789 | } |
| 3790 | |
| 3791 | @Override |
| 3792 | public void onRequestContinued() { |
| 3793 | /* ignore */ |
| 3794 | } |
| 3795 | |
| 3796 | @Override |
| 3797 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 3798 | handleAuthenticatorResponse(false); |
| 3799 | } |
| 3800 | |
| 3801 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 3802 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3803 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3804 | UserHandle.getUserHandleForUid(uid)); |
| 3805 | if (callback != null) { |
| 3806 | Bundle result = new Bundle(); |
| 3807 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 3808 | callback.sendResult(result); |
| 3809 | } |
| 3810 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3811 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3815 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 3816 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 3817 | throw new SecurityException("Only system can check for accounts across users"); |
| 3818 | } |
| 3819 | final long token = Binder.clearCallingIdentity(); |
| 3820 | try { |
| 3821 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 3822 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 3823 | if (allAccounts[i].account.equals(account)) { |
| 3824 | return true; |
| 3825 | } |
| 3826 | } |
| 3827 | return false; |
| 3828 | } finally { |
| 3829 | Binder.restoreCallingIdentity(token); |
| 3830 | } |
| 3831 | } |
| 3832 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3833 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 3834 | private final String[] mFeatures; |
| 3835 | private volatile Account[] mAccountsOfType = null; |
| 3836 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 3837 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3838 | private final int mCallingUid; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3839 | private final String mPackageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3840 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3841 | public GetAccountsByTypeAndFeatureSession( |
| 3842 | UserAccounts accounts, |
| 3843 | IAccountManagerResponse response, |
| 3844 | String type, |
| 3845 | String[] features, |
| 3846 | int callingUid, |
| 3847 | String packageName) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3848 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3849 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3850 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3851 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3852 | mFeatures = features; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3853 | mPackageName = packageName; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3854 | } |
| 3855 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3856 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3857 | public void run() throws RemoteException { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3858 | synchronized (mAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3859 | mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType, mCallingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3860 | mPackageName, false /* include managed not visible*/); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3861 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3862 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 3863 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3864 | mCurrentAccount = 0; |
| 3865 | |
| 3866 | checkAccount(); |
| 3867 | } |
| 3868 | |
| 3869 | public void checkAccount() { |
| 3870 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 3871 | sendResult(); |
| 3872 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3873 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3874 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3875 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 3876 | if (accountAuthenticator == null) { |
| 3877 | // It is possible that the authenticator has died, which is indicated by |
| 3878 | // mAuthenticator being set to null. If this happens then just abort. |
| 3879 | // There is no need to send back a result or error in this case since |
| 3880 | // that already happened when mAuthenticator was cleared. |
| 3881 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3882 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 3883 | + " connected to the authenticator, " + toDebugString()); |
| 3884 | } |
| 3885 | return; |
| 3886 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3887 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3888 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3889 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3890 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3891 | } |
| 3892 | } |
| 3893 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3894 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3895 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3896 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3897 | mNumResults++; |
| 3898 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3899 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3900 | return; |
| 3901 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3902 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3903 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 3904 | } |
| 3905 | mCurrentAccount++; |
| 3906 | checkAccount(); |
| 3907 | } |
| 3908 | |
| 3909 | public void sendResult() { |
| 3910 | IAccountManagerResponse response = getResponseAndClose(); |
| 3911 | if (response != null) { |
| 3912 | try { |
| 3913 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 3914 | for (int i = 0; i < accounts.length; i++) { |
| 3915 | accounts[i] = mAccountsWithFeatures.get(i); |
| 3916 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3917 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3918 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3919 | + response); |
| 3920 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3921 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3922 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3923 | response.onResult(result); |
| 3924 | } catch (RemoteException e) { |
| 3925 | // if the caller is dead then there is no one to care about remote exceptions |
| 3926 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3927 | Log.v(TAG, "failure while notifying response", e); |
| 3928 | } |
| 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3933 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3934 | protected String toDebugString(long now) { |
| 3935 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 3936 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 3937 | } |
| 3938 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 3939 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3940 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3941 | * 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] | 3942 | * @hide |
| 3943 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3944 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3945 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3946 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3947 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3948 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 3949 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3950 | if (visibleAccountTypes.isEmpty()) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 3951 | return EMPTY_ACCOUNT_ARRAY; |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3952 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3953 | long identityToken = clearCallingIdentity(); |
| 3954 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3955 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3956 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3957 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3958 | callingUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 3959 | opPackageName, |
| 3960 | visibleAccountTypes, |
| 3961 | false /* includeUserManagedNotVisible */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3962 | } finally { |
| 3963 | restoreCallingIdentity(identityToken); |
| 3964 | } |
| 3965 | } |
| 3966 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3967 | /** |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3968 | * Returns accounts for all running users, ignores visibility values. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3969 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3970 | * @hide |
| 3971 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3972 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3973 | public AccountAndUser[] getRunningAccounts() { |
| 3974 | final int[] runningUserIds; |
| 3975 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 3976 | runningUserIds = ActivityManager.getService().getRunningUserIds(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3977 | } catch (RemoteException e) { |
| 3978 | // Running in system_server; should never happen |
| 3979 | throw new RuntimeException(e); |
| 3980 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3981 | return getAccounts(runningUserIds); |
| 3982 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3983 | |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 3984 | /** |
| 3985 | * Returns accounts for all users, ignores visibility values. |
| 3986 | * |
| 3987 | * @hide |
| 3988 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3989 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3990 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 3991 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 3992 | final int[] userIds = new int[users.size()]; |
| 3993 | for (int i = 0; i < userIds.length; i++) { |
| 3994 | userIds[i] = users.get(i).id; |
| 3995 | } |
| 3996 | return getAccounts(userIds); |
| 3997 | } |
| 3998 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3999 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4000 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4001 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4002 | for (int userId : userIds) { |
| 4003 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4004 | if (userAccounts == null) continue; |
| 4005 | synchronized (userAccounts.cacheLock) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4006 | Account[] accounts = getAccountsFromCacheLocked( |
| 4007 | userAccounts, |
| 4008 | null /* type */, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4009 | Binder.getCallingUid(), |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4010 | null /* packageName */, |
| 4011 | false /* include managed not visible*/); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4012 | for (int a = 0; a < accounts.length; a++) { |
| 4013 | runningAccounts.add(new AccountAndUser(accounts[a], userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4017 | |
| 4018 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4019 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4020 | } |
| 4021 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4022 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4023 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4024 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4025 | int callingUid = Binder.getCallingUid(); |
| 4026 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4027 | return getAccountsAsUserForPackage(type, userId, opPackageName /* callingPackage */, -1, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4028 | opPackageName, false /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4029 | } |
| 4030 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4031 | @NonNull |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4032 | private Account[] getAccountsAsUserForPackage( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4033 | String type, |
| 4034 | int userId, |
| 4035 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4036 | int packageUid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4037 | String opPackageName, |
| 4038 | boolean includeUserManagedNotVisible) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4039 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4040 | // Only allow the system process to read accounts of other users |
| 4041 | if (userId != UserHandle.getCallingUserId() |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4042 | && callingUid != Process.SYSTEM_UID |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4043 | && mContext.checkCallingOrSelfPermission( |
| 4044 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4045 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4046 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4047 | + " trying to get account for " + userId); |
| 4048 | } |
| 4049 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4050 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4051 | Log.v(TAG, "getAccounts: accountType " + type |
| 4052 | + ", caller's uid " + Binder.getCallingUid() |
| 4053 | + ", pid " + Binder.getCallingPid()); |
| 4054 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4055 | |
| 4056 | // If the original calling app was using account choosing activity |
| 4057 | // provided by the framework or authenticator we'll passing in |
| 4058 | // the original caller's uid here, which is what should be used for filtering. |
| 4059 | List<String> managedTypes = |
| 4060 | getTypesManagedByCaller(callingUid, UserHandle.getUserId(callingUid)); |
| 4061 | if (packageUid != -1 && |
| 4062 | ((UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
| 4063 | || (type != null && managedTypes.contains(type))))) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4064 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4065 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4066 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4067 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4068 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4069 | if (visibleAccountTypes.isEmpty() |
| 4070 | || (type != null && !visibleAccountTypes.contains(type))) { |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4071 | return EMPTY_ACCOUNT_ARRAY; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4072 | } else if (visibleAccountTypes.contains(type)) { |
| 4073 | // Prune the list down to just the requested type. |
| 4074 | visibleAccountTypes = new ArrayList<>(); |
| 4075 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4076 | } // else aggregate all the visible accounts (it won't matter if the |
| 4077 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4078 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4079 | long identityToken = clearCallingIdentity(); |
| 4080 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4081 | UserAccounts accounts = getUserAccounts(userId); |
Dmitry Dementyev | 5274547 | 2016-12-02 10:27:45 -0800 | [diff] [blame] | 4082 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4083 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4084 | callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4085 | opPackageName, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4086 | visibleAccountTypes, |
| 4087 | includeUserManagedNotVisible); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4088 | } finally { |
| 4089 | restoreCallingIdentity(identityToken); |
| 4090 | } |
| 4091 | } |
| 4092 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4093 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4094 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4095 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4096 | int callingUid, |
| 4097 | String callingPackage, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4098 | List<String> visibleAccountTypes, |
| 4099 | boolean includeUserManagedNotVisible) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4100 | synchronized (userAccounts.cacheLock) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4101 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4102 | for (String visibleType : visibleAccountTypes) { |
| 4103 | Account[] accountsForType = getAccountsFromCacheLocked( |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4104 | userAccounts, visibleType, callingUid, callingPackage, |
| 4105 | includeUserManagedNotVisible); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4106 | if (accountsForType != null) { |
| 4107 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
| 4108 | } |
| 4109 | } |
| 4110 | Account[] result = new Account[visibleAccounts.size()]; |
| 4111 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4112 | result[i] = visibleAccounts.get(i); |
| 4113 | } |
| 4114 | return result; |
| 4115 | } |
| 4116 | } |
| 4117 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4118 | @Override |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4119 | public void addSharedAccountsFromParentUser(int parentUserId, int userId, |
| 4120 | String opPackageName) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4121 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Sudheer Shanka | f88ebeb | 2017-02-14 18:30:40 -0800 | [diff] [blame] | 4122 | Account[] accounts = getAccountsAsUser(null, parentUserId, opPackageName); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4123 | for (Account account : accounts) { |
| 4124 | addSharedAccountAsUser(account, userId); |
| 4125 | } |
| 4126 | } |
| 4127 | |
| 4128 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4129 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4130 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4131 | accounts.accountsDb.deleteSharedAccount(account); |
| 4132 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4133 | if (accountId < 0) { |
| 4134 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4135 | + ", skipping the DB insert failed"); |
| 4136 | return false; |
| 4137 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4138 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4139 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4140 | return true; |
| 4141 | } |
| 4142 | |
| 4143 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4144 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4145 | userId = handleIncomingUser(userId); |
| 4146 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4147 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4148 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4149 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4150 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4151 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4152 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4153 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4154 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4155 | } |
| 4156 | return r > 0; |
| 4157 | } |
| 4158 | |
| 4159 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4160 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4161 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4162 | } |
| 4163 | |
| 4164 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4165 | userId = handleIncomingUser(userId); |
| 4166 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4167 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4168 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4169 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4170 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4171 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4172 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4173 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4174 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | @Override |
| 4178 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4179 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4180 | UserAccounts accounts = getUserAccounts(userId); |
| 4181 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4182 | Account[] accountArray = new Account[accountList.size()]; |
| 4183 | accountList.toArray(accountArray); |
| 4184 | return accountArray; |
| 4185 | } |
| 4186 | |
| 4187 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4188 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4189 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4190 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4191 | } |
| 4192 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4193 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4194 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4195 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4196 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4197 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID)) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4198 | // Don't do opPackageName check - caller is system. |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4199 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4200 | + callingUid + " with uid=" + uid); |
| 4201 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4202 | return getAccountsAsUserForPackage(null, UserHandle.getCallingUserId(), packageName, uid, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4203 | opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4204 | } |
| 4205 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4206 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4207 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4208 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4209 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4210 | int callingUid = Binder.getCallingUid(); |
| 4211 | int userId = UserHandle.getCallingUserId(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4212 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4213 | int packageUid = -1; |
| 4214 | try { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4215 | packageUid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 4216 | } catch (NameNotFoundException re) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4217 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4218 | return EMPTY_ACCOUNT_ARRAY; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4219 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4220 | if (!UserHandle.isSameApp(callingUid, Process.SYSTEM_UID) |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4221 | && (type != null && !isAccountManagedByCaller(type, callingUid, userId))) { |
| 4222 | return EMPTY_ACCOUNT_ARRAY; |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4223 | } |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 4224 | return getAccountsAsUserForPackage(type, userId, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4225 | packageName, packageUid, opPackageName, true /* includeUserManagedNotVisible */); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4226 | } |
| 4227 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4228 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4229 | public void getAccountsByFeatures( |
| 4230 | IAccountManagerResponse response, |
| 4231 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4232 | String[] features, |
| 4233 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4234 | int callingUid = Binder.getCallingUid(); |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4235 | mAppOpsManager.checkPackage(callingUid, opPackageName); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4236 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4237 | Log.v(TAG, "getAccounts: accountType " + type |
| 4238 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4239 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4240 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4241 | + ", pid " + Binder.getCallingPid()); |
| 4242 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4243 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4244 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4245 | int userId = UserHandle.getCallingUserId(); |
| 4246 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4247 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4248 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4249 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4250 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4251 | // Need to return just the accounts that are from matching signatures. |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 4252 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, EMPTY_ACCOUNT_ARRAY); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4253 | try { |
| 4254 | response.onResult(result); |
| 4255 | } catch (RemoteException e) { |
| 4256 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4257 | } |
| 4258 | return; |
| 4259 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4260 | long identityToken = clearCallingIdentity(); |
| 4261 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4262 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4263 | if (features == null || features.length == 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4264 | Account[] accounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4265 | synchronized (userAccounts.cacheLock) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4266 | accounts = getAccountsFromCacheLocked( |
| 4267 | userAccounts, type, callingUid, opPackageName, false); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4268 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4269 | Bundle result = new Bundle(); |
| 4270 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4271 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4272 | return; |
| 4273 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4274 | new GetAccountsByTypeAndFeatureSession( |
| 4275 | userAccounts, |
| 4276 | response, |
| 4277 | type, |
| 4278 | features, |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4279 | callingUid, |
| 4280 | opPackageName).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4281 | } finally { |
| 4282 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4283 | } |
| 4284 | } |
| 4285 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4286 | @Override |
| 4287 | public void onAccountAccessed(String token) throws RemoteException { |
| 4288 | final int uid = Binder.getCallingUid(); |
| 4289 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4290 | return; |
| 4291 | } |
| 4292 | final int userId = UserHandle.getCallingUserId(); |
| 4293 | final long identity = Binder.clearCallingIdentity(); |
| 4294 | try { |
| 4295 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4296 | if (Objects.equals(account.getAccessId(), token)) { |
| 4297 | // An app just accessed the account. At this point it knows about |
| 4298 | // 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] | 4299 | // Do we need to update account visibility here? |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4300 | if (!hasAccountAccess(account, null, uid)) { |
| 4301 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4302 | uid, true); |
| 4303 | } |
| 4304 | } |
| 4305 | } |
| 4306 | } finally { |
| 4307 | Binder.restoreCallingIdentity(identity); |
| 4308 | } |
| 4309 | } |
| 4310 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4311 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4312 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4313 | IAccountManagerResponse mResponse; |
| 4314 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4315 | final boolean mExpectActivityLaunch; |
| 4316 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4317 | final String mAccountName; |
| 4318 | // Indicates if we need to add auth details(like last credential time) |
| 4319 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4320 | // If set, we need to update the last authenticated time. This is |
| 4321 | // currently |
| 4322 | // used on |
| 4323 | // successful confirming credentials. |
| 4324 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4325 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4326 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4327 | private int mNumRequestContinued = 0; |
| 4328 | private int mNumErrors = 0; |
| 4329 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4330 | IAccountAuthenticator mAuthenticator = null; |
| 4331 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4332 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4333 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4334 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4335 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4336 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4337 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4338 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4339 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4340 | } |
| 4341 | |
| 4342 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4343 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4344 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4345 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4346 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4347 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4348 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4349 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4350 | mResponse = response; |
| 4351 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4352 | mExpectActivityLaunch = expectActivityLaunch; |
| 4353 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4354 | mAccountName = accountName; |
| 4355 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4356 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4357 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4358 | synchronized (mSessions) { |
| 4359 | mSessions.put(toString(), this); |
| 4360 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4361 | if (response != null) { |
| 4362 | try { |
| 4363 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4364 | } catch (RemoteException e) { |
| 4365 | mResponse = null; |
| 4366 | binderDied(); |
| 4367 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4368 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4369 | } |
| 4370 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4371 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4372 | if (mResponse == null) { |
| 4373 | // this session has already been closed |
| 4374 | return null; |
| 4375 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4376 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4377 | 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] | 4378 | return response; |
| 4379 | } |
| 4380 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4381 | /** |
| 4382 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4383 | * security policy. |
| 4384 | * |
| 4385 | * 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] | 4386 | * 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] | 4387 | * supplied entries in the system Settings app. |
| 4388 | */ |
| 4389 | protected void checkKeyIntent( |
| 4390 | int authUid, |
| 4391 | Intent intent) throws SecurityException { |
| 4392 | long bid = Binder.clearCallingIdentity(); |
| 4393 | try { |
| 4394 | PackageManager pm = mContext.getPackageManager(); |
| 4395 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 4396 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4397 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4398 | if (!isExportedSystemActivity(targetActivityInfo) |
| 4399 | && (PackageManager.SIGNATURE_MATCH != pm.checkSignatures(authUid, |
| 4400 | targetUid))) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4401 | String pkgName = targetActivityInfo.packageName; |
| 4402 | String activityName = targetActivityInfo.name; |
| 4403 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4404 | + "does not share a signature with the supplying authenticator (%s)."; |
| 4405 | throw new SecurityException( |
| 4406 | String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4407 | } |
| 4408 | } finally { |
| 4409 | Binder.restoreCallingIdentity(bid); |
| 4410 | } |
| 4411 | } |
| 4412 | |
Dmitry Dementyev | d5210ba | 2017-03-14 13:13:35 -0700 | [diff] [blame] | 4413 | private boolean isExportedSystemActivity(ActivityInfo activityInfo) { |
| 4414 | String className = activityInfo.name; |
| 4415 | return "android".equals(activityInfo.packageName) && |
| 4416 | (GrantCredentialsPermissionActivity.class.getName().equals(className) |
| 4417 | || CantAddAccountActivity.class.getName().equals(className)); |
| 4418 | } |
| 4419 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4420 | private void close() { |
| 4421 | synchronized (mSessions) { |
| 4422 | if (mSessions.remove(toString()) == null) { |
| 4423 | // the session was already closed, so bail out now |
| 4424 | return; |
| 4425 | } |
| 4426 | } |
| 4427 | if (mResponse != null) { |
| 4428 | // stop listening for response deaths |
| 4429 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4430 | |
| 4431 | // clear this so that we don't accidentally send any further results |
| 4432 | mResponse = null; |
| 4433 | } |
| 4434 | cancelTimeout(); |
| 4435 | unbind(); |
| 4436 | } |
| 4437 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4438 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4439 | public void binderDied() { |
| 4440 | mResponse = null; |
| 4441 | close(); |
| 4442 | } |
| 4443 | |
| 4444 | protected String toDebugString() { |
| 4445 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4446 | } |
| 4447 | |
| 4448 | protected String toDebugString(long now) { |
| 4449 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4450 | + ", connected " + (mAuthenticator != null) |
| 4451 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4452 | + "/" + mNumErrors + ")" |
| 4453 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4454 | } |
| 4455 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4456 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4457 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4458 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4459 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4460 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4461 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4462 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | private void unbind() { |
| 4467 | if (mAuthenticator != null) { |
| 4468 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4469 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4470 | } |
| 4471 | } |
| 4472 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4473 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4474 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4477 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4478 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4479 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4480 | try { |
| 4481 | run(); |
| 4482 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4483 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4484 | "remote exception"); |
| 4485 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4486 | } |
| 4487 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4488 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4489 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4490 | mAuthenticator = null; |
| 4491 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4492 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4493 | try { |
| 4494 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4495 | "disconnected"); |
| 4496 | } catch (RemoteException e) { |
| 4497 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4498 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4499 | + "caught RemoteException while responding", e); |
| 4500 | } |
| 4501 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4502 | } |
| 4503 | } |
| 4504 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4505 | public abstract void run() throws RemoteException; |
| 4506 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4507 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4508 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4509 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4510 | try { |
| 4511 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4512 | "timeout"); |
| 4513 | } catch (RemoteException e) { |
| 4514 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4515 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4516 | e); |
| 4517 | } |
| 4518 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4519 | } |
| 4520 | } |
| 4521 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4522 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4523 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4524 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4525 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4526 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4527 | if (result != null) { |
| 4528 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4529 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4530 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4531 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4532 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4533 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4534 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4535 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4536 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4537 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4538 | if (needUpdate || mAuthDetailsRequired) { |
| 4539 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4540 | if (needUpdate && accountPresent) { |
| 4541 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4542 | } |
| 4543 | if (mAuthDetailsRequired) { |
| 4544 | long lastAuthenticatedTime = -1; |
| 4545 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4546 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4547 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4548 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4549 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4550 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4551 | lastAuthenticatedTime); |
| 4552 | } |
| 4553 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4554 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4555 | if (result != null |
| 4556 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4557 | checkKeyIntent( |
| 4558 | Binder.getCallingUid(), |
| 4559 | intent); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4560 | } |
| 4561 | if (result != null |
| 4562 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4563 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4564 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4565 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4566 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4567 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4568 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4569 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4570 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4571 | IAccountManagerResponse response; |
| 4572 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4573 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4574 | response = mResponse; |
| 4575 | } else { |
| 4576 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4577 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4578 | if (response != null) { |
| 4579 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4580 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4581 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4582 | Log.v(TAG, getClass().getSimpleName() |
| 4583 | + " calling onError() on response " + response); |
| 4584 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4585 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4586 | "null bundle returned"); |
| 4587 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4588 | if (mStripAuthTokenFromResult) { |
| 4589 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4590 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4591 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4592 | Log.v(TAG, getClass().getSimpleName() |
| 4593 | + " calling onResult() on response " + response); |
| 4594 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4595 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4596 | (intent == null)) { |
| 4597 | // All AccountManager error codes are greater than 0 |
| 4598 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4599 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4600 | } else { |
| 4601 | response.onResult(result); |
| 4602 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4603 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4604 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4605 | // if the caller is dead then there is no one to care about remote exceptions |
| 4606 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4607 | Log.v(TAG, "failure while notifying response", e); |
| 4608 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4609 | } |
| 4610 | } |
| 4611 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4612 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4613 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4614 | public void onRequestContinued() { |
| 4615 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4616 | } |
| 4617 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4618 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4619 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4620 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4621 | IAccountManagerResponse response = getResponseAndClose(); |
| 4622 | if (response != null) { |
| 4623 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4624 | Log.v(TAG, getClass().getSimpleName() |
| 4625 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4626 | } |
| 4627 | try { |
| 4628 | response.onError(errorCode, errorMessage); |
| 4629 | } catch (RemoteException e) { |
| 4630 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4631 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4632 | } |
| 4633 | } |
| 4634 | } else { |
| 4635 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4636 | Log.v(TAG, "Session.onError: already closed"); |
| 4637 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4638 | } |
| 4639 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4640 | |
| 4641 | /** |
| 4642 | * find the component name for the authenticator and initiate a bind |
| 4643 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4644 | */ |
| 4645 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4646 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4647 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4648 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4649 | if (authenticatorInfo == null) { |
| 4650 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4651 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 4652 | + ", bailing out"); |
| 4653 | } |
| 4654 | return false; |
| 4655 | } |
| 4656 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 4657 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 4658 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 4659 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 4660 | + " which isn't encryption aware"); |
| 4661 | return false; |
| 4662 | } |
| 4663 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4664 | Intent intent = new Intent(); |
| 4665 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 4666 | intent.setComponent(authenticatorInfo.componentName); |
| 4667 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4668 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 4669 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 4670 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4671 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4672 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4673 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 4674 | } |
| 4675 | return false; |
| 4676 | } |
| 4677 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4678 | return true; |
| 4679 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4680 | } |
| 4681 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 4682 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4683 | MessageHandler(Looper looper) { |
| 4684 | super(looper); |
| 4685 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4686 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4687 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4688 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4689 | switch (msg.what) { |
| 4690 | case MESSAGE_TIMED_OUT: |
| 4691 | Session session = (Session)msg.obj; |
| 4692 | session.onTimedOut(); |
| 4693 | break; |
| 4694 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4695 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 4696 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4697 | break; |
| 4698 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4699 | default: |
| 4700 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 4701 | } |
| 4702 | } |
| 4703 | } |
| 4704 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4705 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4706 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4707 | } |
| 4708 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4709 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4710 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4711 | } |
| 4712 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4713 | /* |
| 4714 | * This function receives an opened writable database. |
| 4715 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4716 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4717 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4718 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4719 | } |
| 4720 | |
| 4721 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4722 | * 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] | 4723 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4724 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4725 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4726 | |
| 4727 | class LogRecordTask implements Runnable { |
| 4728 | private final String action; |
| 4729 | private final String tableName; |
| 4730 | private final long accountId; |
| 4731 | private final UserAccounts userAccount; |
| 4732 | private final int callingUid; |
| 4733 | private final long userDebugDbInsertionPoint; |
| 4734 | |
| 4735 | LogRecordTask(final String action, |
| 4736 | final String tableName, |
| 4737 | final long accountId, |
| 4738 | final UserAccounts userAccount, |
| 4739 | final int callingUid, |
| 4740 | final long userDebugDbInsertionPoint) { |
| 4741 | this.action = action; |
| 4742 | this.tableName = tableName; |
| 4743 | this.accountId = accountId; |
| 4744 | this.userAccount = userAccount; |
| 4745 | this.callingUid = callingUid; |
| 4746 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 4747 | } |
| 4748 | |
| 4749 | public void run() { |
| 4750 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 4751 | logStatement.bindLong(1, accountId); |
| 4752 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4753 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4754 | logStatement.bindLong(4, callingUid); |
| 4755 | logStatement.bindString(5, tableName); |
| 4756 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 4757 | logStatement.execute(); |
| 4758 | logStatement.clearBindings(); |
| 4759 | } |
| 4760 | } |
| 4761 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4762 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 4763 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4764 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4765 | % AccountsDb.MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4766 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | /* |
| 4770 | * This should only be called once to compile the sql statement for logging |
| 4771 | * and to find the insertion point. |
| 4772 | */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4773 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(UserAccounts userAccount) { |
| 4774 | userAccount.debugDbInsertionPoint = userAccount.accountsDb |
| 4775 | .calculateDebugTableInsertionPoint(); |
| 4776 | userAccount.statementForLogging = userAccount.accountsDb.compileSqlStatementForLogging(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4777 | } |
| 4778 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4779 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4780 | return asBinder(); |
| 4781 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4782 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4783 | /** |
| 4784 | * Searches array of arguments for the specified string |
| 4785 | * @param args array of argument strings |
| 4786 | * @param value value to search for |
| 4787 | * @return true if the value is contained in the array |
| 4788 | */ |
| 4789 | private static boolean scanArgs(String[] args, String value) { |
| 4790 | if (args != null) { |
| 4791 | for (String arg : args) { |
| 4792 | if (value.equals(arg)) { |
| 4793 | return true; |
| 4794 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4795 | } |
| 4796 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4797 | return false; |
| 4798 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4799 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4800 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4801 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4802 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 4803 | != PackageManager.PERMISSION_GRANTED) { |
| 4804 | fout.println("Permission Denial: can't dump AccountsManager from from pid=" |
| 4805 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 4806 | + " without permission " + android.Manifest.permission.DUMP); |
| 4807 | return; |
| 4808 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4809 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4810 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4811 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4812 | final List<UserInfo> users = getUserManager().getUsers(); |
| 4813 | for (UserInfo user : users) { |
| 4814 | ipw.println("User " + user + ":"); |
| 4815 | ipw.increaseIndent(); |
| 4816 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 4817 | ipw.println(); |
| 4818 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4819 | } |
| 4820 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4821 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4822 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 4823 | String[] args, boolean isCheckinRequest) { |
| 4824 | synchronized (userAccounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4825 | if (isCheckinRequest) { |
| 4826 | // This is a checkin request. *Only* upload the account types and the count of each. |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4827 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4828 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4829 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 4830 | Process.SYSTEM_UID, null /* packageName */, false); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4831 | fout.println("Accounts: " + accounts.length); |
| 4832 | for (Account account : accounts) { |
| 4833 | fout.println(" " + account); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4834 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 4835 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4836 | // Add debug information. |
| 4837 | fout.println(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4838 | userAccounts.accountsDb.dumpDebugTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4839 | fout.println(); |
| 4840 | synchronized (mSessions) { |
| 4841 | final long now = SystemClock.elapsedRealtime(); |
| 4842 | fout.println("Active Sessions: " + mSessions.size()); |
| 4843 | for (Session session : mSessions.values()) { |
| 4844 | fout.println(" " + session.toDebugString(now)); |
| 4845 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4846 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4847 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4848 | fout.println(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4849 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4850 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4851 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4852 | } |
| 4853 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4854 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4855 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4856 | long identityToken = clearCallingIdentity(); |
| 4857 | try { |
| 4858 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4859 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 4860 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4861 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4862 | if (intent.getComponent() != null && |
| 4863 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 4864 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4865 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4866 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4867 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4868 | final Integer notificationId = getSigninRequiredNotificationId(accounts, account); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4869 | intent.addCategory(String.valueOf(notificationId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4870 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4871 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4872 | contextForUser.getText(R.string.notification_title).toString(); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 4873 | Notification n = |
| 4874 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4875 | .setWhen(0) |
| 4876 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 4877 | .setColor(contextForUser.getColor( |
| 4878 | com.android.internal.R.color.system_notification_accent_color)) |
| 4879 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 4880 | .setContentText(message) |
| 4881 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 4882 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4883 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4884 | .build(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4885 | installNotification(notificationId, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4886 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4887 | } finally { |
| 4888 | restoreCallingIdentity(identityToken); |
| 4889 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4890 | } |
| 4891 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4892 | private void installNotification(int notificationId, final Notification notification, |
| 4893 | String packageName, int userId) { |
| 4894 | final long token = clearCallingIdentity(); |
| 4895 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4896 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4897 | try { |
| 4898 | notificationManager.enqueueNotificationWithTag(packageName, packageName, null, |
| 4899 | notificationId, notification, new int[1], userId); |
| 4900 | } catch (RemoteException e) { |
| 4901 | /* ignore - local call */ |
| 4902 | } |
| 4903 | } finally { |
| 4904 | Binder.restoreCallingIdentity(token); |
| 4905 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4906 | } |
| 4907 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4908 | private void cancelNotification(int id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4909 | cancelNotification(id, mContext.getPackageName(), user); |
| 4910 | } |
| 4911 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4912 | private void cancelNotification(int id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4913 | long identityToken = clearCallingIdentity(); |
| 4914 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4915 | INotificationManager service = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4916 | service.cancelNotificationWithTag(packageName, null, id, user.getIdentifier()); |
| 4917 | } catch (RemoteException e) { |
| 4918 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4919 | } finally { |
| 4920 | restoreCallingIdentity(identityToken); |
| 4921 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4922 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4923 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4924 | private boolean isPermittedForPackage(String packageName, int userId, String... permissions) { |
| 4925 | final long identity = Binder.clearCallingIdentity(); |
| 4926 | try { |
| 4927 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 4928 | for (String perm : permissions) { |
| 4929 | if (pm.checkPermission(perm, packageName, userId) |
| 4930 | == PackageManager.PERMISSION_GRANTED) { |
| 4931 | return true; |
| 4932 | } |
| 4933 | } |
| 4934 | } catch (RemoteException e) { |
| 4935 | /* ignore - local call */ |
| 4936 | } finally { |
| 4937 | Binder.restoreCallingIdentity(identity); |
| 4938 | } |
| 4939 | return false; |
| 4940 | } |
| 4941 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 4942 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 4943 | for (String perm : permissions) { |
| 4944 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 4945 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4946 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 4947 | } |
| 4948 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 4949 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 4950 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 4951 | return true; |
| 4952 | } |
| 4953 | } |
| 4954 | } |
| 4955 | return false; |
| 4956 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4957 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4958 | private int handleIncomingUser(int userId) { |
| 4959 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4960 | return ActivityManager.getService().handleIncomingUser( |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4961 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 4962 | } catch (RemoteException re) { |
| 4963 | // Shouldn't happen, local. |
| 4964 | } |
| 4965 | return userId; |
| 4966 | } |
| 4967 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 4968 | private boolean isPrivileged(int callingUid) { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 4969 | String[] packages; |
| 4970 | long identityToken = Binder.clearCallingIdentity(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4971 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 4972 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 4973 | } finally { |
| 4974 | Binder.restoreCallingIdentity(identityToken); |
| 4975 | } |
| 4976 | if (packages == null) { |
| 4977 | Log.d(TAG, "No packages for callingUid " + callingUid); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4978 | return false; |
| 4979 | } |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4980 | for (String name : packages) { |
| 4981 | try { |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 4982 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4983 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 4984 | && (packageInfo.applicationInfo.privateFlags |
| 4985 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4986 | return true; |
| 4987 | } |
| 4988 | } catch (PackageManager.NameNotFoundException e) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 4989 | Log.d(TAG, "Package not found " + e.getMessage()); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4990 | return false; |
| 4991 | } |
| 4992 | } |
| 4993 | return false; |
| 4994 | } |
| 4995 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4996 | private boolean permissionIsGranted( |
| 4997 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4998 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 4999 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5000 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 5001 | } |
| 5002 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5003 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5004 | |
| 5005 | if (isPrivileged(callerUid)) { |
| 5006 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5007 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5008 | + callerUid + " privileged"); |
| 5009 | } |
| 5010 | return true; |
| 5011 | } |
| 5012 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 5013 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5014 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5015 | + callerUid + " manages the account"); |
| 5016 | } |
| 5017 | return true; |
| 5018 | } |
| 5019 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5020 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5021 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5022 | + callerUid + " user granted access"); |
| 5023 | } |
| 5024 | return true; |
| 5025 | } |
| 5026 | |
| 5027 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5028 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5029 | } |
| 5030 | |
| 5031 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5032 | } |
| 5033 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5034 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5035 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5036 | if (accountType == null) { |
| 5037 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5038 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5039 | return getTypesVisibleToCaller(callingUid, userId, |
| 5040 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5041 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5042 | } |
| 5043 | |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5044 | // Method checks visibility for applications targeing API level below {@link |
| 5045 | // android.os.Build.VERSION_CODES#O}, |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5046 | // returns true if the the app has GET_ACCOUNTS or GET_ACCOUNTS_PRIVILEGED permission. |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5047 | private boolean checkGetAccountsPermission(String packageName, int userId) { |
| 5048 | return isPermittedForPackage(packageName, userId, Manifest.permission.GET_ACCOUNTS, |
| 5049 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
| 5050 | } |
| 5051 | |
| 5052 | private boolean checkReadContactsPermission(String packageName, int userId) { |
| 5053 | return isPermittedForPackage(packageName, userId, Manifest.permission.READ_CONTACTS); |
| 5054 | } |
| 5055 | |
| 5056 | /** |
| 5057 | * Method checks package uid and signature with Authenticator which manages accountType. |
| 5058 | * |
| 5059 | * @return SIGNATURE_CHECK_UID_MATCH for uid match, SIGNATURE_CHECK_MATCH for signature match, |
| 5060 | * SIGNATURE_CHECK_MISMATCH otherwise. |
| 5061 | */ |
| 5062 | private int checkPackageSignature(String accountType, int callingUid, int userId) { |
| 5063 | if (accountType == null) { |
| 5064 | return SIGNATURE_CHECK_MISMATCH; |
| 5065 | } |
| 5066 | |
| 5067 | long identityToken = Binder.clearCallingIdentity(); |
| 5068 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5069 | try { |
| 5070 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5071 | } finally { |
| 5072 | Binder.restoreCallingIdentity(identityToken); |
| 5073 | } |
| 5074 | // Check for signature match with Authenticator. |
| 5075 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo |
| 5076 | : serviceInfos) { |
| 5077 | if (accountType.equals(serviceInfo.type.type)) { |
| 5078 | if (serviceInfo.uid == callingUid) { |
| 5079 | return SIGNATURE_CHECK_UID_MATCH; |
| 5080 | } |
| 5081 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 5082 | if (sigChk == PackageManager.SIGNATURE_MATCH) { |
| 5083 | return SIGNATURE_CHECK_MATCH; |
| 5084 | } |
| 5085 | } |
| 5086 | } |
| 5087 | return SIGNATURE_CHECK_MISMATCH; |
| 5088 | } |
| 5089 | |
| 5090 | // returns true for applications with the same signature as authenticator. |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5091 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5092 | if (accountType == null) { |
| 5093 | return false; |
| 5094 | } else { |
| 5095 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5096 | } |
| 5097 | } |
| 5098 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5099 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5100 | String opPackageName) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5101 | return getTypesForCaller(callingUid, userId, true /* isOtherwisePermitted*/); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
Dmitry Dementyev | 2e22cfb | 2017-01-09 18:42:14 +0000 | [diff] [blame] | 5105 | return getTypesForCaller(callingUid, userId, false); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | private List<String> getTypesForCaller( |
| 5109 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5110 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5111 | long identityToken = Binder.clearCallingIdentity(); |
| 5112 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5113 | try { |
| 5114 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5115 | } finally { |
| 5116 | Binder.restoreCallingIdentity(identityToken); |
| 5117 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5118 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5119 | serviceInfos) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5120 | if (isOtherwisePermitted || (mPackageManager.checkSignatures(serviceInfo.uid, |
| 5121 | callingUid) == PackageManager.SIGNATURE_MATCH)) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5122 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5123 | } |
| 5124 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5125 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5126 | } |
| 5127 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5128 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5129 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5130 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5131 | if (account.name.equals(accountName)) { |
| 5132 | return true; |
| 5133 | } |
| 5134 | } |
| 5135 | } |
| 5136 | return false; |
| 5137 | } |
| 5138 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5139 | private static void checkManageUsersPermission(String message) { |
| 5140 | if (ActivityManager.checkComponentPermission( |
| 5141 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5142 | != PackageManager.PERMISSION_GRANTED) { |
| 5143 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5144 | } |
| 5145 | } |
| 5146 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5147 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5148 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5149 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5150 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5151 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5152 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5153 | + message); |
| 5154 | } |
| 5155 | } |
| 5156 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5157 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5158 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5159 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5160 | return true; |
| 5161 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5162 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5163 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5164 | long grantsCount; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5165 | if (authTokenType != null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5166 | grantsCount = accounts.accountsDb.findMatchingGrantsCount(callerUid, authTokenType, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5167 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5168 | } else { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5169 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5170 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5171 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5172 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5173 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5174 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5175 | // TODO: Skip this check when running automated tests. Replace this |
| 5176 | // with a more general solution. |
| 5177 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5178 | + authTokenType + " by uid " + callerUid |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5179 | + " but ignoring since device is in test harness."); |
| 5180 | return true; |
| 5181 | } |
| 5182 | return permissionGranted; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5183 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5184 | } |
| 5185 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5186 | private boolean isSystemUid(int callingUid) { |
| 5187 | String[] packages = null; |
| 5188 | long ident = Binder.clearCallingIdentity(); |
| 5189 | try { |
| 5190 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5191 | } finally { |
| 5192 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5193 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5194 | if (packages != null) { |
| 5195 | for (String name : packages) { |
| 5196 | try { |
| 5197 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5198 | if (packageInfo != null |
| 5199 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5200 | != 0) { |
| 5201 | return true; |
| 5202 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5203 | } catch (NameNotFoundException e) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5204 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5205 | } |
| 5206 | } |
| 5207 | } else { |
| 5208 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5209 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5210 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5211 | } |
| 5212 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5213 | /** Succeeds if any of the specified permissions are granted. */ |
| 5214 | private void checkReadAccountsPermitted( |
| 5215 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5216 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5217 | int userId, |
| 5218 | String opPackageName) { |
| 5219 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5220 | String msg = String.format( |
| 5221 | "caller uid %s cannot access %s accounts", |
| 5222 | callingUid, |
| 5223 | accountType); |
| 5224 | Log.w(TAG, " " + msg); |
| 5225 | throw new SecurityException(msg); |
| 5226 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5227 | } |
| 5228 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5229 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5230 | // the managing app can always modify accounts |
| 5231 | if (isProfileOwner(callingUid)) { |
| 5232 | return true; |
| 5233 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5234 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5235 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5236 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5237 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5238 | return true; |
| 5239 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5240 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5241 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5242 | // the managing app can always modify accounts |
| 5243 | if (isProfileOwner(callingUid)) { |
| 5244 | return true; |
| 5245 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5246 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5247 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5248 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5249 | if (typesArray == null) { |
| 5250 | return true; |
| 5251 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5252 | for (String forbiddenType : typesArray) { |
| 5253 | if (forbiddenType.equals(accountType)) { |
| 5254 | return false; |
| 5255 | } |
| 5256 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5257 | return true; |
| 5258 | } |
| 5259 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5260 | private boolean isProfileOwner(int uid) { |
| 5261 | final DevicePolicyManagerInternal dpmi = |
| 5262 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5263 | return (dpmi != null) |
| 5264 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5265 | } |
| 5266 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5267 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5268 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5269 | throws RemoteException { |
| 5270 | final int callingUid = getCallingUid(); |
| 5271 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5272 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5273 | throw new SecurityException(); |
| 5274 | } |
| 5275 | |
| 5276 | if (value) { |
| 5277 | grantAppPermission(account, authTokenType, uid); |
| 5278 | } else { |
| 5279 | revokeAppPermission(account, authTokenType, uid); |
| 5280 | } |
| 5281 | } |
| 5282 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5283 | /** |
| 5284 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5285 | * <p> |
| 5286 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5287 | * which is in the system. This means we don't need to protect it with permissions. |
| 5288 | * @hide |
| 5289 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5290 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5291 | if (account == null || authTokenType == null) { |
| 5292 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5293 | return; |
| 5294 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5295 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5296 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5297 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5298 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5299 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5300 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5301 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5302 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5303 | |
| 5304 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5305 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5306 | |
| 5307 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5308 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5309 | : mAppPermissionChangeListeners) { |
| 5310 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5311 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | /** |
| 5315 | * Don't allow callers with the given uid permission to get credentials for |
| 5316 | * account/authTokenType. |
| 5317 | * <p> |
| 5318 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5319 | * which is in the system. This means we don't need to protect it with permissions. |
| 5320 | * @hide |
| 5321 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5322 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5323 | if (account == null || authTokenType == null) { |
| 5324 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5325 | return; |
| 5326 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5327 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5328 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5329 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5330 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5331 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5332 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5333 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5334 | accountId, authTokenType, uid); |
| 5335 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5336 | } |
| 5337 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5338 | accounts.accountsDb.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5339 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5340 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5341 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5342 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5343 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5344 | |
| 5345 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5346 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5347 | : mAppPermissionChangeListeners) { |
| 5348 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5349 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5350 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5351 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5352 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5353 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5354 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5355 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5356 | for (Account curAccount : oldAccountsForType) { |
| 5357 | if (!curAccount.equals(account)) { |
| 5358 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5359 | } |
| 5360 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5361 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5362 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5363 | } else { |
| 5364 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5365 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5366 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5367 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5368 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5369 | accounts.userDataCache.remove(account); |
| 5370 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5371 | accounts.previousNameCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | /** |
| 5375 | * 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] | 5376 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5377 | * processes and if you will return this account to apps you should return the result. |
| 5378 | * @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] | 5379 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5380 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5381 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5382 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5383 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5384 | if (accountsForType != null) { |
| 5385 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5386 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5387 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5388 | : UUID.randomUUID().toString(); |
| 5389 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5390 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5391 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5392 | } |
| 5393 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5394 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5395 | private Account[] filterAccounts(UserAccounts accounts, Account[] unfiltered, int callingUid, |
| 5396 | String callingPackage, boolean includeManagedNotVisible) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5397 | String visibilityFilterPackage = callingPackage; |
| 5398 | if (visibilityFilterPackage == null) { |
| 5399 | visibilityFilterPackage = getPackageNameForUid(callingUid); |
| 5400 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5401 | Map<Account, Integer> firstPass = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5402 | for (Account account : unfiltered) { |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5403 | int visibility = resolveAccountVisibility(account, visibilityFilterPackage, accounts); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5404 | if ((visibility == AccountManager.VISIBILITY_VISIBLE |
| 5405 | || visibility == AccountManager.VISIBILITY_USER_MANAGED_VISIBLE) |
| 5406 | || (includeManagedNotVisible |
| 5407 | && (visibility |
| 5408 | == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE))) { |
| 5409 | firstPass.put(account, visibility); |
| 5410 | } |
| 5411 | } |
| 5412 | Map<Account, Integer> secondPass = |
| 5413 | filterSharedAccounts(accounts, firstPass, callingUid, callingPackage); |
| 5414 | |
| 5415 | Account[] filtered = new Account[secondPass.size()]; |
| 5416 | filtered = secondPass.keySet().toArray(filtered); |
| 5417 | return filtered; |
| 5418 | } |
| 5419 | |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5420 | @NonNull |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5421 | private Map<Account, Integer> filterSharedAccounts(UserAccounts userAccounts, |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5422 | @NonNull Map<Account, Integer> unfiltered, int callingUid, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5423 | @Nullable String callingPackage) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5424 | // first part is to filter shared accounts. |
| 5425 | // unfiltered type check is not necessary. |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5426 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5427 | || callingUid == Process.SYSTEM_UID) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5428 | return unfiltered; |
| 5429 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5430 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5431 | if (user != null && user.isRestricted()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5432 | String[] packages = |
| 5433 | mPackageManager.getPackagesForUid(callingUid); |
Dmitry Dementyev | 5e46e57 | 2017-02-16 12:25:49 -0800 | [diff] [blame] | 5434 | if (packages == null) { |
| 5435 | packages = new String[] {}; |
| 5436 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5437 | // 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] | 5438 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5439 | // This might be a temporary way to specify a visible list |
| 5440 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5441 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5442 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5443 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5444 | return unfiltered; |
| 5445 | } |
| 5446 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5447 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5448 | if (ArrayUtils.isEmpty(sharedAccounts)) { |
| 5449 | return unfiltered; |
| 5450 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5451 | String requiredAccountType = ""; |
| 5452 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5453 | // If there's an explicit callingPackage specified, check if that package |
| 5454 | // opted in to see restricted accounts. |
| 5455 | if (callingPackage != null) { |
| 5456 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5457 | if (pi != null && pi.restrictedAccountType != null) { |
| 5458 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5459 | } |
| 5460 | } else { |
| 5461 | // Otherwise check if the callingUid has a package that has opted in |
| 5462 | for (String packageName : packages) { |
| 5463 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5464 | if (pi != null && pi.restrictedAccountType != null) { |
| 5465 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5466 | break; |
| 5467 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5468 | } |
| 5469 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5470 | } catch (NameNotFoundException e) { |
| 5471 | Log.d(TAG, "Package not found " + e.getMessage()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5472 | } |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5473 | Map<Account, Integer> filtered = new LinkedHashMap<>(); |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5474 | for (Map.Entry<Account, Integer> entry : unfiltered.entrySet()) { |
| 5475 | Account account = entry.getKey(); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5476 | if (account.type.equals(requiredAccountType)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5477 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5478 | } else { |
| 5479 | boolean found = false; |
| 5480 | for (Account shared : sharedAccounts) { |
| 5481 | if (shared.equals(account)) { |
| 5482 | found = true; |
| 5483 | break; |
| 5484 | } |
| 5485 | } |
| 5486 | if (!found) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5487 | filtered.put(account, entry.getValue()); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5488 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5489 | } |
| 5490 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5491 | return filtered; |
| 5492 | } else { |
| 5493 | return unfiltered; |
| 5494 | } |
| 5495 | } |
| 5496 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5497 | /* |
| 5498 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5499 | * that the package is not allowed to access. |
| 5500 | */ |
Dmitry Dementyev | c34a48d | 2017-03-02 13:53:31 -0800 | [diff] [blame] | 5501 | @NonNull |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5502 | protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType, |
Dmitry Dementyev | 5159f43 | 2017-03-09 12:59:56 -0800 | [diff] [blame] | 5503 | int callingUid, @Nullable String callingPackage, boolean includeManagedNotVisible) { |
Dmitry Dementyev | e366f82 | 2017-01-31 10:25:10 -0800 | [diff] [blame] | 5504 | if (callingPackage == null) { |
| 5505 | callingPackage = getPackageNameForUid(callingUid); |
| 5506 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5507 | if (accountType != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5508 | final Account[] accounts = userAccounts.accountCache.get(accountType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5509 | if (accounts == null) { |
| 5510 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5511 | } else { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5512 | return filterAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
| 5513 | callingUid, callingPackage, includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5514 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5515 | } else { |
| 5516 | int totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5517 | for (Account[] accounts : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5518 | totalLength += accounts.length; |
| 5519 | } |
| 5520 | if (totalLength == 0) { |
| 5521 | return EMPTY_ACCOUNT_ARRAY; |
| 5522 | } |
| 5523 | Account[] accounts = new Account[totalLength]; |
| 5524 | totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5525 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5526 | System.arraycopy(accountsOfType, 0, accounts, totalLength, |
| 5527 | accountsOfType.length); |
| 5528 | totalLength += accountsOfType.length; |
| 5529 | } |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5530 | return filterAccounts(userAccounts, accounts, callingUid, callingPackage, |
| 5531 | includeManagedNotVisible); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5532 | } |
| 5533 | } |
| 5534 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5535 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5536 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5537 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5538 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5539 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5540 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5541 | } |
| 5542 | if (value == null) { |
| 5543 | userDataForAccount.remove(key); |
| 5544 | } else { |
| 5545 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5546 | } |
| 5547 | } |
| 5548 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5549 | protected String readCachedTokenInternal( |
| 5550 | UserAccounts accounts, |
| 5551 | Account account, |
| 5552 | String tokenType, |
| 5553 | String callingPackage, |
| 5554 | byte[] pkgSigDigest) { |
| 5555 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5556 | return accounts.accountTokenCaches.get( |
| 5557 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5558 | } |
| 5559 | } |
| 5560 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5561 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5562 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5563 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5564 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5565 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5566 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5567 | } |
| 5568 | if (value == null) { |
| 5569 | authTokensForAccount.remove(key); |
| 5570 | } else { |
| 5571 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5572 | } |
| 5573 | } |
| 5574 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5575 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 5576 | String authTokenType) { |
| 5577 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5578 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5579 | if (authTokensForAccount == null) { |
| 5580 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5581 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5582 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5583 | } |
| 5584 | return authTokensForAccount.get(authTokenType); |
| 5585 | } |
| 5586 | } |
| 5587 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5588 | protected String readUserDataInternalLocked( |
| 5589 | UserAccounts accounts, Account account, String key) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5590 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5591 | if (userDataForAccount == null) { |
| 5592 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5593 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5594 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5595 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5596 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5597 | } |
| 5598 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5599 | private Context getContextForUser(UserHandle user) { |
| 5600 | try { |
| 5601 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 5602 | } catch (NameNotFoundException e) { |
| 5603 | // Default to mContext, not finding the package system is running as is unlikely. |
| 5604 | return mContext; |
| 5605 | } |
| 5606 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 5607 | |
| 5608 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 5609 | try { |
| 5610 | response.onResult(result); |
| 5611 | } catch (RemoteException e) { |
| 5612 | // if the caller is dead then there is no one to care about remote |
| 5613 | // exceptions |
| 5614 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5615 | Log.v(TAG, "failure while notifying response", e); |
| 5616 | } |
| 5617 | } |
| 5618 | } |
| 5619 | |
| 5620 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 5621 | String errorMessage) { |
| 5622 | try { |
| 5623 | response.onError(errorCode, errorMessage); |
| 5624 | } catch (RemoteException e) { |
| 5625 | // if the caller is dead then there is no one to care about remote |
| 5626 | // exceptions |
| 5627 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5628 | Log.v(TAG, "failure while notifying response", e); |
| 5629 | } |
| 5630 | } |
| 5631 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5632 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5633 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5634 | private final Object mLock = new Object(); |
| 5635 | |
| 5636 | @GuardedBy("mLock") |
| 5637 | private AccountManagerBackupHelper mBackupHelper; |
| 5638 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5639 | @Override |
| 5640 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 5641 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 5642 | if (account == null) { |
| 5643 | Slog.w(TAG, "account cannot be null"); |
| 5644 | return; |
| 5645 | } |
| 5646 | if (packageName == null) { |
| 5647 | Slog.w(TAG, "packageName cannot be null"); |
| 5648 | return; |
| 5649 | } |
| 5650 | if (userId < UserHandle.USER_SYSTEM) { |
| 5651 | Slog.w(TAG, "user id must be concrete"); |
| 5652 | return; |
| 5653 | } |
| 5654 | if (callback == null) { |
| 5655 | Slog.w(TAG, "callback cannot be null"); |
| 5656 | return; |
| 5657 | } |
| 5658 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5659 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 5660 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5661 | Bundle result = new Bundle(); |
| 5662 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 5663 | callback.sendResult(result); |
| 5664 | return; |
| 5665 | } |
| 5666 | |
| 5667 | final int uid; |
| 5668 | try { |
| 5669 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 5670 | } catch (NameNotFoundException e) { |
| 5671 | Slog.e(TAG, "Unknown package " + packageName); |
| 5672 | return; |
| 5673 | } |
| 5674 | |
| 5675 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5676 | final UserAccounts userAccounts; |
| 5677 | synchronized (mUsers) { |
| 5678 | userAccounts = mUsers.get(userId); |
| 5679 | } |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame^] | 5680 | SystemNotificationChannels.createAccountChannelForPackage(packageName, uid, mContext); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5681 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 5682 | } |
| 5683 | |
| 5684 | @Override |
| 5685 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 5686 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5687 | mAppPermissionChangeListeners.add(listener); |
| 5688 | } |
| 5689 | |
| 5690 | @Override |
| 5691 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 5692 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5693 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5694 | |
| 5695 | @Override |
| 5696 | public byte[] backupAccountAccessPermissions(int userId) { |
| 5697 | synchronized (mLock) { |
| 5698 | if (mBackupHelper == null) { |
| 5699 | mBackupHelper = new AccountManagerBackupHelper( |
| 5700 | AccountManagerService.this, this); |
| 5701 | } |
| 5702 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 5703 | } |
| 5704 | } |
| 5705 | |
| 5706 | @Override |
| 5707 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 5708 | synchronized (mLock) { |
| 5709 | if (mBackupHelper == null) { |
| 5710 | mBackupHelper = new AccountManagerBackupHelper( |
| 5711 | AccountManagerService.this, this); |
| 5712 | } |
| 5713 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 5714 | } |
| 5715 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5716 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5717 | |
| 5718 | @VisibleForTesting |
| 5719 | static class Injector { |
| 5720 | private final Context mContext; |
| 5721 | |
| 5722 | public Injector(Context context) { |
| 5723 | mContext = context; |
| 5724 | } |
| 5725 | |
| 5726 | Looper getMessageHandlerLooper() { |
| 5727 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 5728 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 5729 | serviceThread.start(); |
| 5730 | return serviceThread.getLooper(); |
| 5731 | } |
| 5732 | |
| 5733 | Context getContext() { |
| 5734 | return mContext; |
| 5735 | } |
| 5736 | |
| 5737 | void addLocalService(AccountManagerInternal service) { |
| 5738 | LocalServices.addService(AccountManagerInternal.class, service); |
| 5739 | } |
| 5740 | |
| 5741 | String getDeDatabaseName(int userId) { |
| 5742 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 5743 | AccountsDb.DE_DATABASE_NAME); |
| 5744 | return databaseFile.getPath(); |
| 5745 | } |
| 5746 | |
| 5747 | String getCeDatabaseName(int userId) { |
| 5748 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 5749 | AccountsDb.CE_DATABASE_NAME); |
| 5750 | return databaseFile.getPath(); |
| 5751 | } |
| 5752 | |
| 5753 | String getPreNDatabaseName(int userId) { |
| 5754 | File systemDir = Environment.getDataSystemDirectory(); |
| 5755 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 5756 | PRE_N_DATABASE_NAME); |
| 5757 | if (userId == 0) { |
| 5758 | // Migrate old file, if it exists, to the new location. |
| 5759 | // 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] | 5760 | // accidentally created in the old location, |
| 5761 | // causing the new one to become corrupted as well. |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5762 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 5763 | if (oldFile.exists() && !databaseFile.exists()) { |
| 5764 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 5765 | File userDir = Environment.getUserSystemDirectory(userId); |
| 5766 | if (!userDir.exists()) { |
| 5767 | if (!userDir.mkdirs()) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5768 | throw new IllegalStateException( |
| 5769 | "User dir cannot be created: " + userDir); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5770 | } |
| 5771 | } |
| 5772 | if (!oldFile.renameTo(databaseFile)) { |
Dmitry Dementyev | 01985ff | 2017-01-19 16:03:39 -0800 | [diff] [blame] | 5773 | throw new IllegalStateException( |
| 5774 | "User dir cannot be migrated: " + databaseFile); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5775 | } |
| 5776 | } |
| 5777 | } |
| 5778 | return databaseFile.getPath(); |
| 5779 | } |
| 5780 | |
| 5781 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 5782 | return new AccountAuthenticatorCache(mContext); |
| 5783 | } |
| 5784 | |
| 5785 | INotificationManager getNotificationManager() { |
| 5786 | return NotificationManager.getService(); |
| 5787 | } |
| 5788 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5789 | } |