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; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 37 | import android.app.ActivityManagerNative; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 38 | import android.app.ActivityThread; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 39 | import android.app.AppGlobals; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 40 | import android.app.AppOpsManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 41 | import android.app.INotificationManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 42 | import android.app.Notification; |
| 43 | import android.app.NotificationManager; |
| 44 | import android.app.PendingIntent; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 45 | import android.app.admin.DeviceAdminInfo; |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 46 | import android.app.admin.DevicePolicyManager; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 47 | import android.app.admin.DevicePolicyManagerInternal; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 48 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 49 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 50 | import android.content.Context; |
| 51 | import android.content.Intent; |
| 52 | import android.content.IntentFilter; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 53 | import android.content.IntentSender; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 54 | import android.content.ServiceConnection; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 55 | import android.content.pm.ActivityInfo; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 56 | import android.content.pm.ApplicationInfo; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 57 | import android.content.pm.IPackageManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 58 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 59 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 60 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 61 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 62 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 63 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 64 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 65 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 66 | import android.database.Cursor; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 67 | import android.database.sqlite.SQLiteDatabase; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 68 | import android.database.sqlite.SQLiteStatement; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 69 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 70 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 71 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 72 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 73 | import android.os.IBinder; |
| 74 | import android.os.Looper; |
| 75 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 76 | import android.os.Parcel; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 77 | import android.os.Process; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 78 | import android.os.RemoteCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 79 | import android.os.RemoteException; |
| 80 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 81 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 82 | import android.os.UserManager; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 83 | import android.os.storage.StorageManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 84 | import android.text.TextUtils; |
| 85 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 86 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 87 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 88 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 89 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 90 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 91 | import com.android.internal.R; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 92 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 93 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 94 | import com.android.internal.content.PackageMonitor; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 95 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 96 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 97 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 98 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 99 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 100 | import com.android.server.SystemService; |
| 101 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 102 | import com.google.android.collect.Lists; |
| 103 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 104 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 105 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 106 | import java.io.FileDescriptor; |
| 107 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 108 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 109 | import java.security.MessageDigest; |
| 110 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 111 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 112 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 113 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 114 | import java.util.Collection; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 115 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 116 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 117 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 118 | import java.util.LinkedHashMap; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 119 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 120 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 121 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 122 | import java.util.Objects; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 123 | import java.util.Set; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 124 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 125 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 126 | import java.util.concurrent.atomic.AtomicInteger; |
| 127 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 128 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 129 | /** |
| 130 | * A system service that provides account, password, and authtoken management for all |
| 131 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 132 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 133 | * 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] | 134 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 135 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 136 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 137 | public class AccountManagerService |
| 138 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 139 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 140 | private static final String TAG = "AccountManagerService"; |
| 141 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 142 | public static class Lifecycle extends SystemService { |
| 143 | private AccountManagerService mService; |
| 144 | |
| 145 | public Lifecycle(Context context) { |
| 146 | super(context); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void onStart() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 151 | mService = new AccountManagerService(new Injector(getContext())); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 152 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 153 | } |
| 154 | |
| 155 | @Override |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 156 | public void onUnlockUser(int userHandle) { |
| 157 | mService.onUnlockUser(userHandle); |
| 158 | } |
| 159 | } |
| 160 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 161 | final Context mContext; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 162 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 163 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 164 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 165 | private UserManager mUserManager; |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 166 | private final Injector mInjector; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 167 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 168 | final MessageHandler mHandler; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 169 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 170 | // Messages that can be sent on mHandler |
| 171 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 172 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 173 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 174 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 175 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 176 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 177 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 178 | static { |
| 179 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
| 180 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 181 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 182 | |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 183 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 184 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 185 | private final AtomicInteger mNotificationIds = new AtomicInteger(1); |
| 186 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 187 | private static final String NEW_ACCOUNT_VISIBLE = "android.accounts.NEW_ACCOUNT_VISIBLE"; |
| 188 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 189 | static class UserAccounts { |
| 190 | private final int userId; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 191 | final AccountsDb accountsDb; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 192 | private final HashMap<Pair<Pair<Account, String>, Integer>, Integer> |
| 193 | credentialsPermissionNotificationIds = |
| 194 | new HashMap<Pair<Pair<Account, String>, Integer>, Integer>(); |
| 195 | private final HashMap<Account, Integer> signinRequiredNotificationIds = |
| 196 | new HashMap<Account, Integer>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 197 | final Object cacheLock = new Object(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 198 | /** protected by the {@link #cacheLock} */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 199 | final HashMap<String, Account[]> accountCache = |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 200 | new LinkedHashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 201 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 202 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 203 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 204 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 205 | |
| 206 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 207 | private final TokenCache accountTokenCaches = new TokenCache(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 208 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 209 | /** protected by the {@link #cacheLock} */ |
| 210 | private final Map<String, ArrayList<Integer>> mApplicationAccountRequestMappings = |
| 211 | new HashMap<>(); |
| 212 | |
| 213 | /* Together the below two Sparse Arrays serve as visible list. One maps UID to account |
| 214 | number. Another maps Account number to Account.*/ |
| 215 | |
| 216 | /** protected by the {@link #cacheLock} */ |
| 217 | private final SparseArray<ArrayList<Integer>> mVisibleListUidToMockAccountNumbers = |
| 218 | new SparseArray<>(); |
| 219 | |
| 220 | //TODO: Instead of using Mock Account IDs, use the actual account IDs. |
| 221 | /** protected by the {@link #cacheLock} */ |
| 222 | private final SparseArray<Account> mMockAccountIdToAccount = new SparseArray<>(); |
| 223 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 224 | /** |
| 225 | * protected by the {@link #cacheLock} |
| 226 | * |
| 227 | * Caches the previous names associated with an account. Previous names |
| 228 | * should be cached because we expect that when an Account is renamed, |
| 229 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 230 | * want to know if the accounts they care about have been renamed. |
| 231 | * |
| 232 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 233 | * we can distinguish between those accounts with no previous names and |
| 234 | * those whose previous names haven't been cached (yet). |
| 235 | */ |
| 236 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 237 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 238 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 239 | private int debugDbInsertionPoint = -1; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 240 | private SQLiteStatement statementForLogging; // TODO Move to AccountsDb |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 241 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 242 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 243 | this.userId = userId; |
| 244 | synchronized (cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 245 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 250 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 251 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 252 | // Not thread-safe. Only use in synchronized context |
| 253 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 254 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 255 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 256 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 257 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 258 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 259 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 260 | /** |
| 261 | * This should only be called by system code. One should only call this after the service |
| 262 | * has started. |
| 263 | * @return a reference to the AccountManagerService instance |
| 264 | * @hide |
| 265 | */ |
| 266 | public static AccountManagerService getSingleton() { |
| 267 | return sThis.get(); |
| 268 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 269 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 270 | public AccountManagerService(Injector injector) { |
| 271 | mInjector = injector; |
| 272 | mContext = injector.getContext(); |
| 273 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 274 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 275 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 276 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 277 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 278 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 279 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 280 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 281 | addRequestsForPreInstalledApplications(); |
| 282 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 283 | IntentFilter intentFilter = new IntentFilter(); |
| 284 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 285 | intentFilter.addDataScheme("package"); |
| 286 | mContext.registerReceiver(new BroadcastReceiver() { |
| 287 | @Override |
| 288 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 289 | // Don't delete accounts when updating a authenticator's |
| 290 | // package. |
| 291 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 292 | /* Purging data requires file io, don't block the main thread. This is probably |
| 293 | * less than ideal because we are introducing a race condition where old grants |
| 294 | * could be exercised until they are purged. But that race condition existed |
| 295 | * anyway with the broadcast receiver. |
| 296 | * |
| 297 | * Ideally, we would completely clear the cache, purge data from the database, |
| 298 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 299 | * large at this point. |
| 300 | */ |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 301 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 302 | @Override |
| 303 | public void run() { |
| 304 | purgeOldGrantsAll(); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 305 | |
| 306 | /* clears application request's for account types supported */ |
| 307 | int uidOfUninstalledApplication = |
| 308 | intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 309 | if(uidOfUninstalledApplication != -1) { |
| 310 | clearRequestedAccountVisibility(uidOfUninstalledApplication, |
| 311 | getUserAccounts(UserHandle.getUserId( |
| 312 | uidOfUninstalledApplication))); |
| 313 | } |
| 314 | |
| 315 | /* removes visibility of previous UID of this uninstalled application*/ |
| 316 | removeAccountVisibilityAllAccounts(uidOfUninstalledApplication, |
| 317 | getUserAccounts(UserHandle.getUserId( |
| 318 | uidOfUninstalledApplication))); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 319 | } |
| 320 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 321 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 322 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 323 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 324 | } |
| 325 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 326 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 327 | IntentFilter packageAddedOrChangedFilter = new IntentFilter(); |
| 328 | intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 329 | packageAddedOrChangedFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 330 | packageAddedOrChangedFilter.addDataScheme("package"); |
| 331 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 332 | @Override |
| 333 | public void onReceive(Context context1, Intent intent) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 334 | mHandler.post(new Runnable() { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 335 | @Override |
| 336 | public void run() { |
| 337 | int uidOfInstalledApplication = |
| 338 | intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 339 | if(uidOfInstalledApplication != -1) { |
| 340 | registerAccountTypesSupported( |
| 341 | uidOfInstalledApplication, |
| 342 | getUserAccounts( |
| 343 | UserHandle.getUserId(uidOfInstalledApplication))); |
| 344 | } |
| 345 | } |
| 346 | }); |
| 347 | } |
| 348 | }, UserHandle.ALL, packageAddedOrChangedFilter, null, null); |
| 349 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 350 | IntentFilter userFilter = new IntentFilter(); |
| 351 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 352 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 353 | @Override |
| 354 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 355 | String action = intent.getAction(); |
| 356 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 357 | onUserRemoved(intent); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 358 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 359 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 360 | }, UserHandle.ALL, userFilter, null, null); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 361 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 362 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 363 | |
| 364 | // Need to cancel account request notifications if the update/install can access the account |
| 365 | new PackageMonitor() { |
| 366 | @Override |
| 367 | public void onPackageAdded(String packageName, int uid) { |
| 368 | // Called on a handler, and running as the system |
| 369 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 374 | // Called on a handler, and running as the system |
| 375 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 376 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 377 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 378 | |
| 379 | // Cancel account request notification if an app op was preventing the account access |
| 380 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 381 | new AppOpsManager.OnOpChangedInternalListener() { |
| 382 | @Override |
| 383 | public void onOpChanged(int op, String packageName) { |
| 384 | try { |
| 385 | final int userId = ActivityManager.getCurrentUser(); |
| 386 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 387 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 388 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 389 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 390 | final long identity = Binder.clearCallingIdentity(); |
| 391 | try { |
| 392 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 393 | } finally { |
| 394 | Binder.restoreCallingIdentity(identity); |
| 395 | } |
| 396 | } |
| 397 | } catch (NameNotFoundException e) { |
| 398 | /* ignore */ |
| 399 | } |
| 400 | } |
| 401 | }); |
| 402 | |
| 403 | // Cancel account request notification if a permission was preventing the account access |
| 404 | mPackageManager.addOnPermissionsChangeListener( |
| 405 | (int uid) -> { |
| 406 | Account[] accounts = null; |
| 407 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 408 | if (packageNames != null) { |
| 409 | final int userId = UserHandle.getUserId(uid); |
| 410 | final long identity = Binder.clearCallingIdentity(); |
| 411 | try { |
| 412 | for (String packageName : packageNames) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 413 | if (mPackageManager.checkPermission( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 414 | Manifest.permission.GET_ACCOUNTS, packageName) |
| 415 | != PackageManager.PERMISSION_GRANTED) { |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | if (accounts == null) { |
| 420 | accounts = getAccountsAsUser(null, userId, "android"); |
| 421 | if (ArrayUtils.isEmpty(accounts)) { |
| 422 | return; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | for (Account account : accounts) { |
| 427 | cancelAccountAccessRequestNotificationIfNeeded( |
| 428 | account, uid, packageName, true); |
| 429 | } |
| 430 | } |
| 431 | } finally { |
| 432 | Binder.restoreCallingIdentity(identity); |
| 433 | } |
| 434 | } |
| 435 | }); |
| 436 | } |
| 437 | |
| 438 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 439 | boolean checkAccess) { |
| 440 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 441 | for (Account account : accounts) { |
| 442 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 447 | boolean checkAccess) { |
| 448 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 449 | for (Account account : accounts) { |
| 450 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 455 | boolean checkAccess) { |
| 456 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 457 | if (packageNames != null) { |
| 458 | for (String packageName : packageNames) { |
| 459 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 460 | packageName, checkAccess); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 466 | int uid, String packageName, boolean checkAccess) { |
| 467 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 468 | UserHandle.getUserHandleForUid(uid))) { |
| 469 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 470 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 471 | UserHandle.getUserHandleForUid(uid)); |
| 472 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 475 | @Override |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 476 | public boolean addAccountExplicitlyWithUid(Account account, String password, Bundle extras, |
| 477 | int[] selectedUids) { |
| 478 | if(addAccountExplicitly(account,password,extras)) { |
| 479 | for(int thisUid : selectedUids) { |
| 480 | makeAccountVisible(account, thisUid); |
| 481 | } |
| 482 | return true; |
| 483 | } |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public int[] getRequestingUidsForType(String accountType) { |
| 489 | int callingUid = Binder.getCallingUid(); |
| 490 | if (!isAccountManagedByCaller(accountType, callingUid, UserHandle.getUserId(callingUid))) { |
| 491 | String msg = String.format( |
| 492 | "uid %s cannot get secrets for accounts of type: %s", |
| 493 | callingUid, |
| 494 | accountType); |
| 495 | throw new SecurityException(msg); |
| 496 | } |
| 497 | return getRequestingUidsForType(accountType, getUserAccounts( |
| 498 | UserHandle.getUserId(callingUid))); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Returns all UIDs for applications that requested the account type. This method |
| 503 | * is called indirectly by the Authenticator and AccountManager |
| 504 | * |
| 505 | * @param accountType authenticator would like to know the requesting apps of |
| 506 | * @param ua UserAccount that currently hosts the account and application |
| 507 | * |
| 508 | * @return ArrayList of all UIDs that support accounts of this |
| 509 | * account type that seek approval (to be used to know which accounts for |
| 510 | * the authenticator to include in addAccountExplicitly). Null if none. |
| 511 | */ |
| 512 | private int[] getRequestingUidsForType(String accountType, UserAccounts ua) { |
| 513 | synchronized(ua.cacheLock) { |
| 514 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 515 | ua.mApplicationAccountRequestMappings; |
| 516 | ArrayList<Integer> allUidsForAccountType = userApplicationAccountRequestMappings.get( |
| 517 | accountType); |
| 518 | if(allUidsForAccountType == null) { |
| 519 | return null; |
| 520 | } |
| 521 | int[] toReturn = new int[allUidsForAccountType.size()]; |
| 522 | for(int i = 0 ; i < toReturn.length ; i++) { |
| 523 | toReturn[i] = allUidsForAccountType.get(i); |
| 524 | } |
| 525 | return toReturn; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | @Override |
| 530 | public boolean isAccountVisible(Account a, int uid) { |
| 531 | int callingUid = Binder.getCallingUid(); |
| 532 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 533 | String msg = String.format( |
| 534 | "uid %s cannot get secrets for accounts of type: %s", |
| 535 | callingUid, |
| 536 | a.type); |
| 537 | throw new SecurityException(msg); |
| 538 | } |
| 539 | return isAccountVisible(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Checks visibility of certain account of a process identified |
| 544 | * by a given UID. This is called by the Authenticator indirectly. |
| 545 | * |
| 546 | * @param a The account to check visibility of |
| 547 | * @param uid UID to check visibility of |
| 548 | * @param ua UserAccount that currently hosts the account and application |
| 549 | * |
| 550 | * @return True if application has access to the account |
| 551 | * |
| 552 | */ |
| 553 | private boolean isAccountVisible(Account a, int uid, UserAccounts ua) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 554 | if(isAccountManagedByCaller(a.type, uid, UserHandle.getUserId(uid))) { |
| 555 | return true; |
| 556 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 557 | int accountMapping = getMockAccountNumber(a, ua); |
| 558 | if(accountMapping < 0) { |
| 559 | return true; |
| 560 | } |
| 561 | synchronized(ua.cacheLock) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 562 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 563 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 564 | ua.mVisibleListUidToMockAccountNumbers; |
| 565 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 566 | int indexOfAccountMapping = userAcctIdToAcctMap.indexOfValueByValue(a); |
| 567 | return indexOfAccountMapping == -1 || (linkedAccountsToUid != null |
| 568 | && linkedAccountsToUid.contains(accountMapping)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | @Override |
| 573 | public boolean makeAccountVisible(Account a, int uid) { |
| 574 | int callingUid = Binder.getCallingUid(); |
| 575 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 576 | String msg = String.format( |
| 577 | "uid %s cannot get secrets for accounts of type: %s", |
| 578 | callingUid, |
| 579 | a.type); |
| 580 | throw new SecurityException(msg); |
| 581 | } |
| 582 | return makeAccountVisible(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Gives a certain UID, represented a application, access to an account. This method |
| 587 | * is called indirectly by the Authenticator. |
| 588 | * |
| 589 | * @param a Account to make visible |
| 590 | * @param uid to add visibility of the Account from |
| 591 | * @param ua UserAccount that currently hosts the account and application |
| 592 | * |
| 593 | * @return True if account made visible to application and was not previously visible. |
| 594 | */ |
| 595 | private boolean makeAccountVisible(Account a, int uid, UserAccounts ua) { |
| 596 | int accountMapping = getMockAccountNumber(a, ua); |
| 597 | if(accountMapping < 0) { |
| 598 | accountMapping = makeAccountNumber(a, ua); |
| 599 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 600 | synchronized(ua.cacheLock) { |
| 601 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 602 | ua.mVisibleListUidToMockAccountNumbers; |
| 603 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
| 604 | if(linkedAccountsToUid == null) { |
| 605 | linkedAccountsToUid = new ArrayList<>(); |
| 606 | linkedAccountsToUid.add(accountMapping); |
| 607 | userWlUidToMockAccountNums.put(uid, linkedAccountsToUid); |
| 608 | } else if(!linkedAccountsToUid.contains(accountMapping)) { |
| 609 | linkedAccountsToUid.add(accountMapping); |
| 610 | } else { |
| 611 | return false; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | String[] subPackages = mPackageManager.getPackagesForUid(uid); |
| 616 | if(subPackages != null) { |
| 617 | for(String subPackage : subPackages) { |
| 618 | sendNotification(subPackage, a); |
| 619 | } |
| 620 | } |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public boolean removeAccountVisibility(Account a, int uid) { |
| 626 | int callingUid = Binder.getCallingUid(); |
| 627 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 628 | String msg = String.format( |
| 629 | "uid %s cannot get secrets for accounts of type: %s", |
| 630 | callingUid, |
| 631 | a.type); |
| 632 | throw new SecurityException(msg); |
| 633 | } |
| 634 | return removeAccountVisibility(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Removes visibility of certain account of a process identified |
| 639 | * by a given UID to an application. This is called directly by the |
| 640 | * AccountManager and indirectly by the Authenticator. |
| 641 | * |
| 642 | * @param a Account to remove visibility from |
| 643 | * @param uid UID to remove visibility of the Account from |
| 644 | * @param ua UserAccount that hosts the account and application |
| 645 | * |
| 646 | * @return True if application access to account removed and was previously visible. |
| 647 | */ |
| 648 | private boolean removeAccountVisibility(Account a, int uid, UserAccounts ua) { |
| 649 | int accountMapping = getMockAccountNumber(a, ua); |
| 650 | if(accountMapping < 0) { |
| 651 | return false; |
| 652 | } |
| 653 | synchronized(ua.cacheLock) { |
| 654 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 655 | ua.mVisibleListUidToMockAccountNumbers; |
| 656 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
| 657 | if(linkedAccountsToUid != null) { |
| 658 | boolean toReturn = linkedAccountsToUid.remove((Integer) accountMapping); |
| 659 | if(linkedAccountsToUid.size() == 0) { |
| 660 | userWlUidToMockAccountNums.remove(uid); |
| 661 | } |
| 662 | return toReturn; |
| 663 | } |
| 664 | } |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Registers an application's preferences for supported account types for login. This is |
| 670 | * a helper method of requestAccountVisibility and indirectly called by AccountManager. |
| 671 | * |
| 672 | * @param accountTypes account types third party app is willing to support |
| 673 | * @param uid of application requesting account visibility |
| 674 | * @param ua UserAccount that hosts the account and application |
| 675 | */ |
| 676 | private void addRequestedAccountsVisibility(String[] accountTypes, int uid, UserAccounts ua) { |
| 677 | synchronized(ua.cacheLock) { |
| 678 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 679 | ua.mApplicationAccountRequestMappings; |
| 680 | for(String accountType : accountTypes) { |
| 681 | ArrayList<Integer> accountUidAppList = userApplicationAccountRequestMappings |
| 682 | .get(accountType); |
| 683 | if(accountUidAppList == null) { |
| 684 | accountUidAppList = new ArrayList<>(); |
| 685 | accountUidAppList.add(uid); |
| 686 | userApplicationAccountRequestMappings.put(accountType, accountUidAppList); |
| 687 | } else if (!accountUidAppList.contains(uid)) { |
| 688 | accountUidAppList.add(uid); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Registers the requested login account types requested by all the applications already |
| 696 | * installed on the device. |
| 697 | */ |
| 698 | private void addRequestsForPreInstalledApplications() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 699 | List<PackageInfo> allInstalledPackages = mPackageManager.getInstalledPackages(0); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 700 | for(PackageInfo pi : allInstalledPackages) { |
| 701 | int currentUid = pi.applicationInfo.uid; |
| 702 | if(currentUid != -1) { |
| 703 | registerAccountTypesSupported(currentUid, |
| 704 | getUserAccounts(UserHandle.getUserId(currentUid))); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * Clears all preferences an application had for login account types it offered |
| 711 | * support for. This method is used by AccountManager after application is |
| 712 | * uninstalled. |
| 713 | * |
| 714 | * @param uid Uid of the application to clear account type preferences |
| 715 | * @param ua UserAccount that hosted the account and application |
| 716 | * |
| 717 | * @return true if any previous settings were overridden. |
| 718 | */ |
| 719 | private boolean clearRequestedAccountVisibility(int uid, UserAccounts ua) { |
| 720 | boolean accountsDeleted = false; |
| 721 | ArrayList<String> accountTypesToRemove = new ArrayList<>(); |
| 722 | synchronized(ua.cacheLock) { |
| 723 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 724 | ua.mApplicationAccountRequestMappings; |
| 725 | Set<Entry<String, ArrayList<Integer>>> accountTypeAppListEntries = |
| 726 | userApplicationAccountRequestMappings.entrySet(); |
| 727 | |
| 728 | for(Entry<String, ArrayList<Integer>> entry : accountTypeAppListEntries) { |
| 729 | ArrayList<Integer> supportedApps = entry.getValue(); |
| 730 | if(supportedApps.remove((Integer) uid)) { |
| 731 | accountsDeleted = true; |
| 732 | } |
| 733 | |
| 734 | if(supportedApps.isEmpty()) { |
| 735 | accountTypesToRemove.add(entry.getKey()); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | for(String s : accountTypesToRemove) { |
| 740 | userApplicationAccountRequestMappings.remove(s); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | return accountsDeleted; |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Retrieves the mock account number associated with an Account in order to later retrieve |
| 749 | * the account from the Integer-Account Mapping. An account number is not the same as |
| 750 | * accountId in the database. This method can be indirectly called by AccountManager and |
| 751 | * indirectly by the Authenticator. |
| 752 | * |
| 753 | * @param a account to retrieve account number mapping |
| 754 | * @param ua UserAccount that currently hosts the account and application |
| 755 | * |
| 756 | * @return account number affiliated with the Account in question. Negative number if none. |
| 757 | */ |
| 758 | private int getMockAccountNumber(Account a, UserAccounts ua) { |
| 759 | //TODO: Each account is linked to AccountId rather than generated mock account numbers |
| 760 | SparseArray<Account> userAcctIdToAcctMap = |
| 761 | ua.mMockAccountIdToAccount; |
| 762 | synchronized(ua.cacheLock) { |
| 763 | int indexOfAccount = userAcctIdToAcctMap.indexOfValueByValue(a); |
| 764 | if(indexOfAccount < 0) { |
| 765 | return -1; |
| 766 | } |
| 767 | return userAcctIdToAcctMap.keyAt(indexOfAccount); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * Returns a full list of accounts that a certain UID is allowed access |
| 773 | * based on the visible list entries. |
| 774 | * |
| 775 | * @param uid of application to retrieve visible listed accounts for |
| 776 | * @param ua UserAccount that currently hosts the account and application |
| 777 | * |
| 778 | * @return array of Account values that are accessible by the given uids |
| 779 | */ |
| 780 | private Account[] getVisibleListedAccounts(int uid, UserAccounts ua) { |
| 781 | ArrayList<Account> visibleListedAccounts = new ArrayList<>(); |
| 782 | synchronized(ua.cacheLock) { |
| 783 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 784 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 785 | ua.mVisibleListUidToMockAccountNumbers; |
| 786 | ArrayList<Integer> visibleListedUidAccountNumbers = |
| 787 | userWlUidToMockAccountNums.get(uid); |
| 788 | if(visibleListedUidAccountNumbers != null) { |
| 789 | for(Integer accountNumber : visibleListedUidAccountNumbers) { |
| 790 | Account currentAccount = userAcctIdToAcctMap.get(accountNumber); |
| 791 | visibleListedAccounts.add(currentAccount); |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | Account[] arrVisibleListedAccounts = new Account[visibleListedAccounts.size()]; |
| 796 | return visibleListedAccounts.toArray(arrVisibleListedAccounts); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Makes an account number for a given Account to be mapped to. |
| 801 | * This method is called by makeVisible if an Account does not have |
| 802 | * a mapping for the visible list. This method is thus indirectly |
| 803 | * called by the Authenticator. |
| 804 | * |
| 805 | * @param a account to make an account number mapping of |
| 806 | * @param ua UserAccount that currently hosts the account and application |
| 807 | * |
| 808 | * @return account number created to map to the given account |
| 809 | */ |
| 810 | // TODO: Remove this method and use accountId from DB. |
| 811 | private int makeAccountNumber(Account a, UserAccounts ua) { |
| 812 | synchronized(ua.cacheLock) { |
| 813 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 814 | int newAccountMapping = 0; |
| 815 | while(userAcctIdToAcctMap.get(newAccountMapping) != null) { |
| 816 | newAccountMapping++; |
| 817 | } |
| 818 | userAcctIdToAcctMap.put(newAccountMapping, a); |
| 819 | return newAccountMapping; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | |
| 824 | |
| 825 | /** |
| 826 | * Registers an application, represented by a UID, to support account types detailed in |
| 827 | * the applications manifest as well as allowing it to opt for notifications. |
| 828 | * |
| 829 | * @param uid UID of application |
| 830 | * @param ua UserAccount that currently hosts the account and application |
| 831 | */ |
| 832 | private void registerAccountTypesSupported(int uid, UserAccounts ua) { |
| 833 | /* Account types supported are drawn from the Android Manifest of the Application */ |
| 834 | String interestedPackages = null; |
| 835 | try { |
| 836 | String[] allPackages = mPackageManager.getPackagesForUid(uid); |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 837 | if (allPackages != null) { |
| 838 | for(String aPackage : allPackages) { |
| 839 | ApplicationInfo ai = mPackageManager.getApplicationInfo(aPackage, |
| 840 | PackageManager.GET_META_DATA); |
| 841 | Bundle b = ai.metaData; |
| 842 | if(b == null) { |
| 843 | return; |
| 844 | } |
| 845 | interestedPackages = b.getString("android.accounts.SupportedLoginTypes"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 846 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 847 | } |
| 848 | } catch (PackageManager.NameNotFoundException e) { |
| 849 | Log.d("NameNotFoundException", e.getMessage()); |
| 850 | } |
| 851 | if(interestedPackages != null) { |
| 852 | /* request remote account types directly from here. Reads from Android Manifest */ |
| 853 | requestAccountVisibility(interestedPackages.split(";"), uid, ua); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * Allows AccountManager to register account types that an application has login |
| 859 | * support for. This method over-writes all of the application's previous settings |
| 860 | * for accounts it supported. |
| 861 | * |
| 862 | * @param accountTypes array of account types application wishes to support |
| 863 | * @param uid of application registering requested account types |
| 864 | * @param ua UserAccount that hosts the account and application |
| 865 | */ |
| 866 | private void requestAccountVisibility(String[] accountTypes, int uid, UserAccounts ua) { |
| 867 | if(accountTypes.length > 0) { |
| 868 | clearRequestedAccountVisibility(uid, ua); |
| 869 | addRequestedAccountsVisibility(accountTypes, uid, ua); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * Removes visibility of all Accounts to this particular UID. This is called when an |
| 875 | * application is uninstalled so another application that is installed with the same |
| 876 | * UID cannot access Accounts. This is called by AccountManager. |
| 877 | * |
| 878 | * @param uid of application to remove all Account visibility to |
| 879 | * @param ua UserAccount that hosts the current Account |
| 880 | */ |
| 881 | private void removeAccountVisibilityAllAccounts(int uid, UserAccounts ua) { |
| 882 | synchronized(ua.cacheLock) { |
| 883 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 884 | ua.mVisibleListUidToMockAccountNumbers; |
| 885 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 886 | ArrayList<Integer> allAccountNumbersList = userWlUidToMockAccountNums.get(uid); |
| 887 | if(allAccountNumbersList != null) { |
| 888 | Integer[] allAccountNumbers = allAccountNumbersList.toArray( |
| 889 | new Integer[allAccountNumbersList.size()]); |
| 890 | for(int accountNum : allAccountNumbers) { |
| 891 | removeAccountVisibility(userAcctIdToAcctMap.get(accountNum), uid, ua); |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * Removes visible list functionality of a certain Account. |
| 899 | * This method is currently called by (1) addAccountExplicitly (as opposed to |
| 900 | * addAccountExplicitlyWithUid) and (2) removeAccountExplicitly. |
| 901 | * |
| 902 | * @param a the account to clear the visible list functionality for |
| 903 | * @param ua currently UserAccounts profile containing Account |
| 904 | * |
| 905 | * @return true if account previously had visible list functionality |
| 906 | */ |
| 907 | private boolean removeVisibleListFunctionality(Account a, UserAccounts ua) { |
| 908 | int mockAccountNum = getMockAccountNumber(a, ua); |
| 909 | if(mockAccountNum < 0) { |
| 910 | return false; |
| 911 | } |
| 912 | synchronized(ua.cacheLock) { |
| 913 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 914 | ua.mVisibleListUidToMockAccountNumbers; |
| 915 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 916 | |
| 917 | /* Removing mapping from account number to account removes visible list functionality*/ |
| 918 | userAcctIdToAcctMap.remove(mockAccountNum); |
| 919 | |
| 920 | for(int i = userWlUidToMockAccountNums.size() - 1 ; i >= 0 ; i--) { |
| 921 | int uidKey = userWlUidToMockAccountNums.keyAt(i); |
| 922 | ArrayList<Integer> allAccountNumbers = userWlUidToMockAccountNums.get(uidKey); |
| 923 | if(allAccountNumbers != null) { |
| 924 | allAccountNumbers.remove(mockAccountNum); |
| 925 | if(allAccountNumbers.isEmpty()) { |
| 926 | userWlUidToMockAccountNums.remove(uidKey); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | return true; |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Sends a direct intent to a package, notifying it of a visible account. This |
| 936 | * method is a helper method of makeAccountVisible. |
| 937 | * |
| 938 | * @param desiredPackage to send Account to |
| 939 | * @param visibleAccount to send to package |
| 940 | */ |
| 941 | private void sendNotification(String desiredPackage, Account visibleAccount) { |
| 942 | Intent intent = new Intent(); |
| 943 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 944 | intent.setAction(NEW_ACCOUNT_VISIBLE); |
| 945 | intent.setPackage(desiredPackage); |
| 946 | intent.putExtra("android.accounts.KEY_ACCOUNT", (Account) visibleAccount); |
| 947 | mContext.sendBroadcast(intent); |
| 948 | } |
| 949 | |
| 950 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 951 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 952 | throws RemoteException { |
| 953 | try { |
| 954 | return super.onTransact(code, data, reply, flags); |
| 955 | } catch (RuntimeException e) { |
| 956 | // The account manager only throws security exceptions, so let's |
| 957 | // log all others. |
| 958 | if (!(e instanceof SecurityException)) { |
| 959 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 960 | } |
| 961 | throw e; |
| 962 | } |
| 963 | } |
| 964 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 965 | private UserManager getUserManager() { |
| 966 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 967 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 968 | } |
| 969 | return mUserManager; |
| 970 | } |
| 971 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 972 | /** |
| 973 | * Validate internal set of accounts against installed authenticators for |
| 974 | * given user. Clears cached authenticators before validating. |
| 975 | */ |
| 976 | public void validateAccounts(int userId) { |
| 977 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 978 | // Invalidate user-specific cache to make sure we catch any |
| 979 | // removed authenticators. |
| 980 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * Validate internal set of accounts against installed authenticators for |
| 985 | * given user. Clear cached authenticators before validating when requested. |
| 986 | */ |
| 987 | private void validateAccountsInternal( |
| 988 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 989 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 990 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 991 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 992 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 993 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 994 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 995 | if (invalidateAuthenticatorCache) { |
| 996 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 997 | } |
| 998 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 999 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 1000 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1001 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1002 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1003 | synchronized (accounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1004 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1005 | |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1006 | // Get a map of stored authenticator types to UID |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1007 | final AccountsDb accountsDb = accounts.accountsDb; |
| 1008 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1009 | // Create a list of authenticator type whose previous uid no longer exists |
| 1010 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1011 | SparseBooleanArray knownUids = null; |
| 1012 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 1013 | String type = authToUidEntry.getKey(); |
| 1014 | int uid = authToUidEntry.getValue(); |
| 1015 | Integer knownUid = knownAuth.get(type); |
| 1016 | if (knownUid != null && uid == knownUid) { |
| 1017 | // Remove it from the knownAuth list if it's unchanged. |
| 1018 | knownAuth.remove(type); |
| 1019 | } else { |
| 1020 | /* |
| 1021 | * The authenticator is presently not cached and should only be triggered |
| 1022 | * when we think an authenticator has been removed (or is being updated). |
| 1023 | * But we still want to check if any data with the associated uid is |
| 1024 | * around. This is an (imperfect) signal that the package may be updating. |
| 1025 | * |
| 1026 | * A side effect of this is that an authenticator sharing a uid with |
| 1027 | * multiple apps won't get its credentials wiped as long as some app with |
| 1028 | * that uid is still on the device. But I suspect that this is a rare case. |
| 1029 | * And it isn't clear to me how an attacker could really exploit that |
| 1030 | * feature. |
| 1031 | * |
| 1032 | * The upshot is that we don't have to worry about accounts getting |
| 1033 | * uninstalled while the authenticator's package is being updated. |
| 1034 | * |
| 1035 | */ |
| 1036 | if (knownUids == null) { |
| 1037 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1038 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1039 | if (!knownUids.get(uid)) { |
| 1040 | // The authenticator is not presently available to the cache. And the |
| 1041 | // package no longer has a data directory (so we surmise it isn't updating). |
| 1042 | // So purge its data from the account databases. |
| 1043 | obsoleteAuthType.add(type); |
| 1044 | // And delete it from the TABLE_META |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1045 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1046 | } |
| 1047 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1050 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1051 | // 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] | 1052 | // values. |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1053 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1054 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1057 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1058 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1059 | accounts.accountCache.clear(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1060 | final HashMap<String, ArrayList<String>> accountNamesByType = new LinkedHashMap<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1061 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1062 | final long accountId = accountEntry.getKey(); |
| 1063 | final Account account = accountEntry.getValue(); |
| 1064 | if (obsoleteAuthType.contains(account.type)) { |
| 1065 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 1066 | + account.type + "'s registered authenticator no longer exist."); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1067 | accountsDb.beginTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1068 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1069 | accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1070 | // Also delete from CE table if user is unlocked; if user is currently |
| 1071 | // locked the account will be removed later by syncDeCeAccountsLocked |
| 1072 | if (userUnlocked) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1073 | accountsDb.deleteCeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1074 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1075 | accountsDb.setTransactionSuccessful(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1076 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1077 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1078 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1079 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1080 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1081 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1082 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1083 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1084 | accounts.userDataCache.remove(account); |
| 1085 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1086 | accounts.accountTokenCaches.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1087 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1088 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1089 | if (accountNames == null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1090 | accountNames = new ArrayList<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1091 | accountNamesByType.put(account.type, accountNames); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1092 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1093 | accountNames.add(account.name); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1094 | } |
| 1095 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1096 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1097 | final String accountType = cur.getKey(); |
| 1098 | final ArrayList<String> accountNames = cur.getValue(); |
| 1099 | final Account[] accountsForType = new Account[accountNames.size()]; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1100 | for (int i = 0; i < accountsForType.length; i++) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1101 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1102 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1103 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1104 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1105 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1106 | } finally { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1107 | if (accountDeleted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1108 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1109 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1110 | } |
| 1111 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1114 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1115 | // Get the UIDs of all apps that might have data on the device. We want |
| 1116 | // to preserve user data if the app might otherwise be storing data. |
| 1117 | List<PackageInfo> pkgsWithData = |
| 1118 | mPackageManager.getInstalledPackagesAsUser( |
| 1119 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1120 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1121 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1122 | if (pkgInfo.applicationInfo != null |
| 1123 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1124 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1125 | } |
| 1126 | } |
| 1127 | return knownUids; |
| 1128 | } |
| 1129 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1130 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1131 | Context context, |
| 1132 | int userId) { |
| 1133 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1134 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1135 | } |
| 1136 | |
| 1137 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1138 | IAccountAuthenticatorCache authCache, |
| 1139 | int userId) { |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1140 | HashMap<String, Integer> knownAuth = new HashMap<>(); |
| 1141 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1142 | .getAllServices(userId)) { |
| 1143 | knownAuth.put(service.type.type, service.uid); |
| 1144 | } |
| 1145 | return knownAuth; |
| 1146 | } |
| 1147 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1148 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1149 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | protected UserAccounts getUserAccounts(int userId) { |
| 1153 | synchronized (mUsers) { |
| 1154 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1155 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1156 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1157 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1158 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1159 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1160 | initializeDebugDbSizeAndCompileSqlStatementForLogging(accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1161 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1162 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1163 | validateAccounts = true; |
| 1164 | } |
| 1165 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1166 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1167 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
| 1168 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1169 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1170 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1171 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1172 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1173 | } |
| 1174 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1175 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1176 | } |
| 1177 | return accounts; |
| 1178 | } |
| 1179 | } |
| 1180 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1181 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1182 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1183 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1184 | if (!accountsToRemove.isEmpty()) { |
| 1185 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1186 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1187 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1188 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1189 | |
| 1190 | for (Account account : accountsToRemove) { |
| 1191 | removeAccountInternal(accounts, account, Process.myUid()); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1196 | private void purgeOldGrantsAll() { |
| 1197 | synchronized (mUsers) { |
| 1198 | for (int i = 0; i < mUsers.size(); i++) { |
| 1199 | purgeOldGrants(mUsers.valueAt(i)); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | private void purgeOldGrants(UserAccounts accounts) { |
| 1205 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1206 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1207 | for (int uid : uids) { |
| 1208 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1209 | if (packageExists) { |
| 1210 | continue; |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1211 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1212 | Log.d(TAG, "deleting grants for UID " + uid |
| 1213 | + " because its package is no longer installed"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1214 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1219 | private void onUserRemoved(Intent intent) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1220 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1221 | if (userId < 1) return; |
| 1222 | |
| 1223 | UserAccounts accounts; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1224 | boolean userUnlocked; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1225 | synchronized (mUsers) { |
| 1226 | accounts = mUsers.get(userId); |
| 1227 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1228 | userUnlocked = mLocalUnlockedUsers.get(userId); |
| 1229 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1230 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1231 | if (accounts != null) { |
| 1232 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1233 | accounts.accountsDb.close(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1234 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1235 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1236 | Log.i(TAG, "Removing database files for user " + userId); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1237 | File dbFile = new File(mInjector.getDeDatabaseName(userId)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1238 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1239 | AccountsDb.deleteDbFileWarnIfFailed(dbFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1240 | // Remove CE file if user is unlocked, or FBE is not enabled |
| 1241 | boolean fbeEnabled = StorageManager.isFileEncryptedNativeOrEmulated(); |
| 1242 | if (!fbeEnabled || userUnlocked) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1243 | File ceDb = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1244 | if (ceDb.exists()) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1245 | AccountsDb.deleteDbFileWarnIfFailed(ceDb); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1250 | @VisibleForTesting |
| 1251 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1252 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1253 | } |
| 1254 | |
| 1255 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1256 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1257 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1258 | } |
| 1259 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1260 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1261 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1262 | if (userId < 1) return; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1263 | syncSharedAccounts(userId); |
| 1264 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1265 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1266 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1267 | // Check if there's a shared account that needs to be created as an account |
| 1268 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1269 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1270 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1271 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1272 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1273 | : UserHandle.USER_SYSTEM; |
| 1274 | if (parentUserId < 0) { |
| 1275 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1276 | return; |
| 1277 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1278 | for (Account sa : sharedAccounts) { |
| 1279 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1280 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1281 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1285 | @Override |
| 1286 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1287 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1290 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1291 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1292 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1293 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1294 | Log.v(TAG, "getPassword: " + account |
| 1295 | + ", caller's uid " + Binder.getCallingUid() |
| 1296 | + ", pid " + Binder.getCallingPid()); |
| 1297 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1298 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1299 | int userId = UserHandle.getCallingUserId(); |
| 1300 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1301 | String msg = String.format( |
| 1302 | "uid %s cannot get secrets for accounts of type: %s", |
| 1303 | callingUid, |
| 1304 | account.type); |
| 1305 | throw new SecurityException(msg); |
| 1306 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1307 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1308 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1309 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1310 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1311 | } finally { |
| 1312 | restoreCallingIdentity(identityToken); |
| 1313 | } |
| 1314 | } |
| 1315 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1316 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1317 | if (account == null) { |
| 1318 | return null; |
| 1319 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1320 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1321 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1322 | return null; |
| 1323 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1324 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1325 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1326 | return accounts.accountsDb.findAccountPasswordByNameAndType(account.name, account.type); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1330 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1331 | public String getPreviousName(Account account) { |
| 1332 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1333 | Log.v(TAG, "getPreviousName: " + account |
| 1334 | + ", caller's uid " + Binder.getCallingUid() |
| 1335 | + ", pid " + Binder.getCallingPid()); |
| 1336 | } |
| 1337 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1338 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1339 | long identityToken = clearCallingIdentity(); |
| 1340 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1341 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1342 | return readPreviousNameInternal(accounts, account); |
| 1343 | } finally { |
| 1344 | restoreCallingIdentity(identityToken); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1349 | if (account == null) { |
| 1350 | return null; |
| 1351 | } |
| 1352 | synchronized (accounts.cacheLock) { |
| 1353 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1354 | if (previousNameRef == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1355 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1356 | previousNameRef = new AtomicReference<>(previousName); |
| 1357 | accounts.previousNameCache.put(account, previousNameRef); |
| 1358 | return previousName; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1359 | } else { |
| 1360 | return previousNameRef.get(); |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1366 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1367 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1368 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1369 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1370 | account, key, callingUid, Binder.getCallingPid()); |
| 1371 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1372 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1373 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1374 | if (key == null) throw new IllegalArgumentException("key is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1375 | int userId = UserHandle.getCallingUserId(); |
| 1376 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1377 | String msg = String.format( |
| 1378 | "uid %s cannot get user data for accounts of type: %s", |
| 1379 | callingUid, |
| 1380 | account.type); |
| 1381 | throw new SecurityException(msg); |
| 1382 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1383 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1384 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1385 | return null; |
| 1386 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1387 | long identityToken = clearCallingIdentity(); |
| 1388 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1389 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1390 | synchronized (accounts.cacheLock) { |
| 1391 | if (!accountExistsCacheLocked(accounts, account)) { |
| 1392 | return null; |
| 1393 | } |
| 1394 | return readUserDataInternalLocked(accounts, account, key); |
| 1395 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1396 | } finally { |
| 1397 | restoreCallingIdentity(identityToken); |
| 1398 | } |
| 1399 | } |
| 1400 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1401 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1402 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1403 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1404 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1405 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1406 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1407 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1408 | + ", pid " + Binder.getCallingPid()); |
| 1409 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1410 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1411 | if (isCrossUser(callingUid, userId)) { |
| 1412 | throw new SecurityException( |
| 1413 | String.format( |
| 1414 | "User %s tying to get authenticator types for %s" , |
| 1415 | UserHandle.getCallingUserId(), |
| 1416 | userId)); |
| 1417 | } |
| 1418 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1419 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1420 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1421 | return getAuthenticatorTypesInternal(userId); |
| 1422 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1423 | } finally { |
| 1424 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1425 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1428 | /** |
| 1429 | * Should only be called inside of a clearCallingIdentity block. |
| 1430 | */ |
| 1431 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1432 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1433 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1434 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1435 | AuthenticatorDescription[] types = |
| 1436 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1437 | int i = 0; |
| 1438 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1439 | : authenticatorCollection) { |
| 1440 | types[i] = authenticator.type; |
| 1441 | i++; |
| 1442 | } |
| 1443 | return types; |
| 1444 | } |
| 1445 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1446 | private boolean isCrossUser(int callingUid, int userId) { |
| 1447 | return (userId != UserHandle.getCallingUserId() |
| 1448 | && callingUid != Process.myUid() |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1449 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1450 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1451 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1452 | } |
| 1453 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1454 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1455 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1456 | Bundle.setDefusable(extras, true); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1457 | // clears the visible list functionality for this account because this method allows |
| 1458 | // default account access to all applications for account. |
| 1459 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1460 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1461 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1462 | Log.v(TAG, "addAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1463 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1464 | + ", pid " + Binder.getCallingPid()); |
| 1465 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1466 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1467 | int userId = UserHandle.getCallingUserId(); |
| 1468 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1469 | String msg = String.format( |
| 1470 | "uid %s cannot explicitly add accounts of type: %s", |
| 1471 | callingUid, |
| 1472 | account.type); |
| 1473 | throw new SecurityException(msg); |
| 1474 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1475 | removeVisibleListFunctionality(account, getUserAccounts(UserHandle.getUserId(callingUid))); |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1476 | /* |
| 1477 | * Child users are not allowed to add accounts. Only the accounts that are |
| 1478 | * shared by the parent profile can be added to child profile. |
| 1479 | * |
| 1480 | * TODO: Only allow accounts that were shared to be added by |
| 1481 | * a limited user. |
| 1482 | */ |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1483 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1484 | // fails if the account already exists |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1485 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1486 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1487 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1488 | return addAccountInternal(accounts, account, password, extras, callingUid); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1489 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1490 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1494 | @Override |
| 1495 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1496 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1497 | int callingUid = Binder.getCallingUid(); |
| 1498 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1499 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1500 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1501 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1502 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1503 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1504 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1505 | if (response != null) { |
| 1506 | Bundle result = new Bundle(); |
| 1507 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1508 | try { |
| 1509 | response.onResult(result); |
| 1510 | } catch (RemoteException e) { |
| 1511 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1512 | } |
| 1513 | } |
| 1514 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1517 | Slog.d(TAG, "Copying account " + account.name |
| 1518 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1519 | long identityToken = clearCallingIdentity(); |
| 1520 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1521 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1522 | false /* stripAuthTokenFromResult */, account.name, |
| 1523 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1524 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1525 | protected String toDebugString(long now) { |
| 1526 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1527 | + ", " + account.type; |
| 1528 | } |
| 1529 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1530 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1531 | public void run() throws RemoteException { |
| 1532 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 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 onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1537 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1538 | if (result != null |
| 1539 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1540 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1541 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1542 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1543 | super.onResult(result); |
| 1544 | } |
| 1545 | } |
| 1546 | }.bind(); |
| 1547 | } finally { |
| 1548 | restoreCallingIdentity(identityToken); |
| 1549 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1550 | } |
| 1551 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1552 | @Override |
| 1553 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1554 | final int callingUid = Binder.getCallingUid(); |
| 1555 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1556 | String msg = String.format( |
| 1557 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1558 | account, |
| 1559 | callingUid); |
| 1560 | Log.v(TAG, msg); |
| 1561 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1562 | if (account == null) { |
| 1563 | throw new IllegalArgumentException("account is null"); |
| 1564 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1565 | int userId = UserHandle.getCallingUserId(); |
| 1566 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1567 | String msg = String.format( |
| 1568 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1569 | callingUid, |
| 1570 | account.type); |
| 1571 | throw new SecurityException(msg); |
| 1572 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1573 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1574 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1575 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1576 | return false; |
| 1577 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1578 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1579 | long identityToken = clearCallingIdentity(); |
| 1580 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1581 | UserAccounts accounts = getUserAccounts(userId); |
| 1582 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1583 | } finally { |
| 1584 | restoreCallingIdentity(identityToken); |
| 1585 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1589 | final UserAccounts accounts = getUserAccountsForCaller(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1590 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1591 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1592 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1593 | } |
| 1594 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1595 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1596 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1597 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1598 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1599 | long id = clearCallingIdentity(); |
| 1600 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1601 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1602 | false /* stripAuthTokenFromResult */, account.name, |
| 1603 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1604 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1605 | protected String toDebugString(long now) { |
| 1606 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1607 | + ", " + account.type; |
| 1608 | } |
| 1609 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1610 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1611 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1612 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1613 | UserAccounts owner = getUserAccounts(parentUserId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1614 | synchronized (owner.cacheLock) { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1615 | for (Account acc : getAccounts(parentUserId, |
| 1616 | mContext.getOpPackageName())) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1617 | if (acc.equals(account)) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1618 | mAuthenticator.addAccountFromCredentials( |
| 1619 | this, account, accountCredentials); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1620 | break; |
| 1621 | } |
| 1622 | } |
| 1623 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1626 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1627 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1628 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1629 | // TODO: Anything to do if if succedded? |
| 1630 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1631 | // account to avoid retries? |
| 1632 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1633 | } |
| 1634 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1635 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1636 | public void onError(int errorCode, String errorMessage) { |
| 1637 | super.onError(errorCode, errorMessage); |
| 1638 | // TODO: Show error notification to user |
| 1639 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1640 | } |
| 1641 | |
| 1642 | }.bind(); |
| 1643 | } finally { |
| 1644 | restoreCallingIdentity(id); |
| 1645 | } |
| 1646 | } |
| 1647 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1648 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1649 | Bundle extras, int callingUid) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1650 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1651 | if (account == null) { |
| 1652 | return false; |
| 1653 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1654 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1655 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1656 | + " is locked. callingUid=" + callingUid); |
| 1657 | return false; |
| 1658 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1659 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1660 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1661 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1662 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1663 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1664 | + ", skipping since the account already exists"); |
| 1665 | return false; |
| 1666 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1667 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1668 | if (accountId < 0) { |
| 1669 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1670 | + ", skipping the DB insert failed"); |
| 1671 | return false; |
| 1672 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1673 | // Insert into DE table |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1674 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1675 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1676 | + ", skipping the DB insert failed"); |
| 1677 | return false; |
| 1678 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1679 | if (extras != null) { |
| 1680 | for (String key : extras.keySet()) { |
| 1681 | final String value = extras.getString(key); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1682 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1683 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1684 | + ", skipping since insertExtra failed for key " + key); |
| 1685 | return false; |
| 1686 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1687 | } |
| 1688 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1689 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1690 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1691 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 1692 | accountId, accounts, callingUid); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1693 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1694 | insertAccountIntoCacheLocked(accounts, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1695 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1696 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1697 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1698 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1699 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1700 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1701 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1702 | |
| 1703 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1704 | sendAccountsChangedBroadcast(accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1705 | return true; |
| 1706 | } |
| 1707 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1708 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1709 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1710 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1714 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1715 | * 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] | 1716 | * running, then clone the account too. |
| 1717 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1718 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1719 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1720 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1721 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1722 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1723 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1724 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1725 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1726 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1727 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1728 | } |
| 1729 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1730 | } |
| 1731 | } |
| 1732 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1733 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1734 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1735 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1736 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1737 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1738 | Log.v(TAG, "hasFeatures: " + account |
| 1739 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1740 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1741 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1742 | + ", pid " + Binder.getCallingPid()); |
| 1743 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1744 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1745 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1746 | if (features == null) throw new IllegalArgumentException("features is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1747 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1748 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1749 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1750 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1751 | long identityToken = clearCallingIdentity(); |
| 1752 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1753 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1754 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1755 | } finally { |
| 1756 | restoreCallingIdentity(identityToken); |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | private class TestFeaturesSession extends Session { |
| 1761 | private final String[] mFeatures; |
| 1762 | private final Account mAccount; |
| 1763 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1764 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1765 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1766 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1767 | true /* stripAuthTokenFromResult */, account.name, |
| 1768 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1769 | mFeatures = features; |
| 1770 | mAccount = account; |
| 1771 | } |
| 1772 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1773 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1774 | public void run() throws RemoteException { |
| 1775 | try { |
| 1776 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1777 | } catch (RemoteException e) { |
| 1778 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1779 | } |
| 1780 | } |
| 1781 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1782 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1783 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1784 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1785 | IAccountManagerResponse response = getResponseAndClose(); |
| 1786 | if (response != null) { |
| 1787 | try { |
| 1788 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1789 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1790 | return; |
| 1791 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1792 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1793 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1794 | + response); |
| 1795 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1796 | final Bundle newResult = new Bundle(); |
| 1797 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1798 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1799 | response.onResult(newResult); |
| 1800 | } catch (RemoteException e) { |
| 1801 | // if the caller is dead then there is no one to care about remote exceptions |
| 1802 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1803 | Log.v(TAG, "failure while notifying response", e); |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1809 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1810 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1811 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1812 | + ", " + mAccount |
| 1813 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1814 | } |
| 1815 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1816 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1817 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1818 | public void renameAccount( |
| 1819 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1820 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1821 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1822 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1823 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1824 | + ", pid " + Binder.getCallingPid()); |
| 1825 | } |
| 1826 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1827 | int userId = UserHandle.getCallingUserId(); |
| 1828 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1829 | String msg = String.format( |
| 1830 | "uid %s cannot rename accounts of type: %s", |
| 1831 | callingUid, |
| 1832 | accountToRename.type); |
| 1833 | throw new SecurityException(msg); |
| 1834 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1835 | long identityToken = clearCallingIdentity(); |
| 1836 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1837 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1838 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1839 | Bundle result = new Bundle(); |
| 1840 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1841 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1842 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1843 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1844 | try { |
| 1845 | response.onResult(result); |
| 1846 | } catch (RemoteException e) { |
| 1847 | Log.w(TAG, e.getMessage()); |
| 1848 | } |
| 1849 | } finally { |
| 1850 | restoreCallingIdentity(identityToken); |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1855 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1856 | Account resultAccount = null; |
| 1857 | /* |
| 1858 | * Cancel existing notifications. Let authenticators |
| 1859 | * re-post notifications as required. But we don't know if |
| 1860 | * the authenticators have bound their notifications to |
| 1861 | * now stale account name data. |
| 1862 | * |
| 1863 | * With a rename api, we might not need to do this anymore but it |
| 1864 | * shouldn't hurt. |
| 1865 | */ |
| 1866 | cancelNotification( |
| 1867 | getSigninRequiredNotificationId(accounts, accountToRename), |
| 1868 | new UserHandle(accounts.userId)); |
| 1869 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1870 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1871 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1872 | if (accountToRename.equals(pair.first.first)) { |
| 1873 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
| 1874 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1875 | } |
| 1876 | } |
| 1877 | } |
| 1878 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1879 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1880 | Account renamedAccount = new Account(newName, accountToRename.type); |
| 1881 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1882 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1883 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1884 | accounts.accountsDb.renameCeAccount(accountId, newName); |
| 1885 | accounts.accountsDb.renameDeAccount(accountId, newName, accountToRename.name); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1886 | } |
| 1887 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 1888 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1889 | } |
| 1890 | /* |
| 1891 | * Database transaction was successful. Clean up cached |
| 1892 | * data associated with the account in the user profile. |
| 1893 | */ |
Svet Ganov | 5c4a512 | 2016-09-23 19:29:11 -0700 | [diff] [blame] | 1894 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1895 | /* |
| 1896 | * Extract the data and token caches before removing the |
| 1897 | * old account to preserve the user data associated with |
| 1898 | * the account. |
| 1899 | */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1900 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1901 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1902 | removeAccountFromCacheLocked(accounts, accountToRename); |
| 1903 | /* |
| 1904 | * Update the cached data associated with the renamed |
| 1905 | * account. |
| 1906 | */ |
| 1907 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1908 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 1909 | accounts.previousNameCache.put( |
| 1910 | renamedAccount, |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1911 | new AtomicReference<>(accountToRename.name)); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1912 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1913 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1914 | int parentUserId = accounts.userId; |
| 1915 | if (canHaveProfile(parentUserId)) { |
| 1916 | /* |
| 1917 | * Owner or system user account was renamed, rename the account for |
| 1918 | * those users with which the account was shared. |
| 1919 | */ |
| 1920 | List<UserInfo> users = getUserManager().getUsers(true); |
| 1921 | for (UserInfo user : users) { |
| 1922 | if (user.isRestricted() |
| 1923 | && (user.restrictedProfileParentId == parentUserId)) { |
| 1924 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1925 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1926 | } |
| 1927 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1928 | sendAccountsChangedBroadcast(accounts.userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1929 | } |
| 1930 | return resultAccount; |
| 1931 | } |
| 1932 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1933 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1934 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1935 | return userInfo != null && userInfo.canHaveProfile(); |
| 1936 | } |
| 1937 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1938 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1939 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1940 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1941 | removeAccountAsUser( |
| 1942 | response, |
| 1943 | account, |
| 1944 | expectActivityLaunch, |
| 1945 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | @Override |
| 1949 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1950 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1951 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1952 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1953 | Log.v(TAG, "removeAccount: " + account |
| 1954 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1955 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1956 | + ", pid " + Binder.getCallingPid() |
| 1957 | + ", for user id " + userId); |
| 1958 | } |
| 1959 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1960 | if (account == null) throw new IllegalArgumentException("account is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1961 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1962 | if (isCrossUser(callingUid, userId)) { |
| 1963 | throw new SecurityException( |
| 1964 | String.format( |
| 1965 | "User %s tying remove account for %s" , |
| 1966 | UserHandle.getCallingUserId(), |
| 1967 | userId)); |
| 1968 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1969 | /* |
| 1970 | * Only the system or authenticator should be allowed to remove accounts for that |
| 1971 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 1972 | * arbitrary applications (like competing authenticators). |
| 1973 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1974 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 1975 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 1976 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1977 | String msg = String.format( |
| 1978 | "uid %s cannot remove accounts of type: %s", |
| 1979 | callingUid, |
| 1980 | account.type); |
| 1981 | throw new SecurityException(msg); |
| 1982 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1983 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1984 | try { |
| 1985 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 1986 | "User cannot modify accounts"); |
| 1987 | } catch (RemoteException re) { |
| 1988 | } |
| 1989 | return; |
| 1990 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1991 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1992 | try { |
| 1993 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 1994 | "User cannot modify accounts of this type (policy)."); |
| 1995 | } catch (RemoteException re) { |
| 1996 | } |
| 1997 | return; |
| 1998 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1999 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2000 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2001 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2002 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2003 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2004 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2005 | if (account.equals(pair.first.first)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2006 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2007 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2011 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2012 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2013 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2014 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2015 | accountId, |
| 2016 | accounts, |
| 2017 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2018 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2019 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2020 | } finally { |
| 2021 | restoreCallingIdentity(identityToken); |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | @Override |
| 2026 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2027 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2028 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2029 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2030 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2031 | + ", pid " + Binder.getCallingPid()); |
| 2032 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2033 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2034 | if (account == null) { |
| 2035 | /* |
| 2036 | * Null accounts should result in returning false, as per |
| 2037 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2038 | */ |
| 2039 | Log.e(TAG, "account is null"); |
| 2040 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2041 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2042 | String msg = String.format( |
| 2043 | "uid %s cannot explicitly add accounts of type: %s", |
| 2044 | callingUid, |
| 2045 | account.type); |
| 2046 | throw new SecurityException(msg); |
| 2047 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 2048 | removeVisibleListFunctionality(account, getUserAccounts(UserHandle.getUserId(callingUid))); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2049 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2050 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2051 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2052 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2053 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2054 | accountId, |
| 2055 | accounts, |
| 2056 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2057 | long identityToken = clearCallingIdentity(); |
| 2058 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2059 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2060 | } finally { |
| 2061 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2062 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2065 | private class RemoveAccountSession extends Session { |
| 2066 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2067 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2068 | Account account, boolean expectActivityLaunch) { |
| 2069 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2070 | true /* stripAuthTokenFromResult */, account.name, |
| 2071 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2072 | mAccount = account; |
| 2073 | } |
| 2074 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2075 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2076 | protected String toDebugString(long now) { |
| 2077 | return super.toDebugString(now) + ", removeAccount" |
| 2078 | + ", account " + mAccount; |
| 2079 | } |
| 2080 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2081 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2082 | public void run() throws RemoteException { |
| 2083 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2084 | } |
| 2085 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2086 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2087 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2088 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2089 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2090 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2091 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2092 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2093 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2094 | } |
| 2095 | IAccountManagerResponse response = getResponseAndClose(); |
| 2096 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2097 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2098 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2099 | + response); |
| 2100 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2101 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2102 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2103 | try { |
| 2104 | response.onResult(result2); |
| 2105 | } catch (RemoteException e) { |
| 2106 | // ignore |
| 2107 | } |
| 2108 | } |
| 2109 | } |
| 2110 | super.onResult(result); |
| 2111 | } |
| 2112 | } |
| 2113 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2114 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2115 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2116 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2117 | } |
| 2118 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2119 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2120 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2121 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2122 | if (!userUnlocked) { |
| 2123 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2124 | + " is still locked. CE data will be removed later"); |
| 2125 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2126 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2127 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2128 | // Set to a dummy value, this will only be used if the database |
| 2129 | // transaction succeeds. |
| 2130 | long accountId = -1; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2131 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2132 | accountId = accounts.accountsDb.findDeAccountId(account); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2133 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2134 | accounts.accountsDb.deleteDeAccount(accountId); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2135 | if (userUnlocked) { |
| 2136 | // Delete from CE table |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2137 | accounts.accountsDb.deleteCeAccount(accountId); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2138 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2139 | accounts.accountsDb.setTransactionSuccessful(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2140 | isChanged = true; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2141 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2142 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2143 | accounts.accountsDb.endTransaction(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2144 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2145 | if (isChanged) { |
| 2146 | removeAccountFromCacheLocked(accounts, account); |
| 2147 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occured. |
| 2148 | sendAccountsChangedBroadcast(accounts.userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2149 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2150 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2151 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2152 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2153 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2154 | long id = Binder.clearCallingIdentity(); |
| 2155 | try { |
| 2156 | int parentUserId = accounts.userId; |
| 2157 | if (canHaveProfile(parentUserId)) { |
| 2158 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2159 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2160 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2161 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2162 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2163 | } |
| 2164 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2165 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2166 | } finally { |
| 2167 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2168 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2169 | |
| 2170 | if (isChanged) { |
| 2171 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2172 | for (Pair<Pair<Account, String>, Integer> key |
| 2173 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2174 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2175 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2176 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2177 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2178 | account, uid, false)); |
| 2179 | } |
| 2180 | } |
| 2181 | } |
| 2182 | } |
| 2183 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2184 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2187 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2188 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2189 | int callerUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2190 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2191 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2192 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2193 | + ", pid " + Binder.getCallingPid()); |
| 2194 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2195 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 2196 | if (authToken == null) throw new IllegalArgumentException("authToken is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2197 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2198 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2199 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2200 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2201 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2202 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2203 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2204 | invalidateAuthTokenLocked(accounts, accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2205 | invalidateCustomTokenLocked(accounts, accountType, authToken); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2206 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2207 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2208 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2209 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2210 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2211 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2212 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2216 | private void invalidateCustomTokenLocked( |
| 2217 | UserAccounts accounts, |
| 2218 | String accountType, |
| 2219 | String authToken) { |
| 2220 | if (authToken == null || accountType == null) { |
| 2221 | return; |
| 2222 | } |
| 2223 | // Also wipe out cached token in memory. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2224 | accounts.accountTokenCaches.remove(accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2227 | private void invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
| 2228 | String authToken) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2229 | if (authToken == null || accountType == null) { |
| 2230 | return; |
| 2231 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2232 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2233 | try { |
| 2234 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2235 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2236 | String accountName = cursor.getString(1); |
| 2237 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2238 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2239 | writeAuthTokenIntoCacheLocked( |
| 2240 | accounts, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2241 | new Account(accountName, accountType), |
| 2242 | authTokenType, |
| 2243 | null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2244 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2245 | } finally { |
| 2246 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2250 | private void saveCachedToken( |
| 2251 | UserAccounts accounts, |
| 2252 | Account account, |
| 2253 | String callerPkg, |
| 2254 | byte[] callerSigDigest, |
| 2255 | String tokenType, |
| 2256 | String token, |
| 2257 | long expiryMillis) { |
| 2258 | |
| 2259 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2260 | return; |
| 2261 | } |
| 2262 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2263 | UserHandle.of(accounts.userId)); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2264 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2265 | accounts.accountTokenCaches.put( |
| 2266 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2270 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2271 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2272 | if (account == null || type == null) { |
| 2273 | return false; |
| 2274 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2275 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2276 | UserHandle.of(accounts.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2277 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2278 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2279 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2280 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2281 | if (accountId < 0) { |
| 2282 | return false; |
| 2283 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2284 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2285 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2286 | accounts.accountsDb.setTransactionSuccessful(); |
| 2287 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2288 | return true; |
| 2289 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2290 | return false; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2291 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2292 | accounts.accountsDb.endTransaction(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2293 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2297 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2298 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2299 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2300 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2301 | Log.v(TAG, "peekAuthToken: " + account |
| 2302 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2303 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2304 | + ", pid " + Binder.getCallingPid()); |
| 2305 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2306 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2307 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2308 | int userId = UserHandle.getCallingUserId(); |
| 2309 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2310 | String msg = String.format( |
| 2311 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2312 | callingUid, |
| 2313 | account.type); |
| 2314 | throw new SecurityException(msg); |
| 2315 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2316 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2317 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2318 | + callingUid); |
| 2319 | return null; |
| 2320 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2321 | long identityToken = clearCallingIdentity(); |
| 2322 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2323 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2324 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2325 | } finally { |
| 2326 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2327 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2328 | } |
| 2329 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2330 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2331 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2332 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2333 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2334 | Log.v(TAG, "setAuthToken: " + account |
| 2335 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2336 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2337 | + ", pid " + Binder.getCallingPid()); |
| 2338 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2339 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2340 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2341 | int userId = UserHandle.getCallingUserId(); |
| 2342 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2343 | String msg = String.format( |
| 2344 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2345 | callingUid, |
| 2346 | account.type); |
| 2347 | throw new SecurityException(msg); |
| 2348 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2349 | long identityToken = clearCallingIdentity(); |
| 2350 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2351 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2352 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2353 | } finally { |
| 2354 | restoreCallingIdentity(identityToken); |
| 2355 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2358 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2359 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2360 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2361 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2362 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2363 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2364 | + ", pid " + Binder.getCallingPid()); |
| 2365 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2366 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2367 | int userId = UserHandle.getCallingUserId(); |
| 2368 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2369 | String msg = String.format( |
| 2370 | "uid %s cannot set secrets for accounts of type: %s", |
| 2371 | callingUid, |
| 2372 | account.type); |
| 2373 | throw new SecurityException(msg); |
| 2374 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2375 | long identityToken = clearCallingIdentity(); |
| 2376 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2377 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2378 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2379 | } finally { |
| 2380 | restoreCallingIdentity(identityToken); |
| 2381 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2384 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2385 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2386 | if (account == null) { |
| 2387 | return; |
| 2388 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2389 | boolean isChanged = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2390 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2391 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2392 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2393 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2394 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2395 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2396 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2397 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2398 | accounts.accountTokenCaches.remove(account); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2399 | accounts.accountsDb.setTransactionSuccessful(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2400 | // If there is an account whose password will be updated and the database |
| 2401 | // transactions succeed, then we say that a change has occured. Even if the |
| 2402 | // new password is the same as the old and there were no authtokens to delete. |
| 2403 | isChanged = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2404 | String action = (password == null || password.length() == 0) ? |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2405 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2406 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2407 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, callingUid); |
Costin Manolache | f5ffe89 | 2011-01-19 09:35:32 -0800 | [diff] [blame] | 2408 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2409 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2410 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2411 | if (isChanged) { |
| 2412 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
| 2413 | sendAccountsChangedBroadcast(accounts.userId); |
| 2414 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2415 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2416 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2419 | private void sendAccountsChangedBroadcast(int userId) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2420 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 2421 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 2422 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2425 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2426 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2427 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2428 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2429 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2430 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2431 | + ", pid " + Binder.getCallingPid()); |
| 2432 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2433 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2434 | int userId = UserHandle.getCallingUserId(); |
| 2435 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2436 | String msg = String.format( |
| 2437 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2438 | callingUid, |
| 2439 | account.type); |
| 2440 | throw new SecurityException(msg); |
| 2441 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2442 | long identityToken = clearCallingIdentity(); |
| 2443 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2444 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2445 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2446 | } finally { |
| 2447 | restoreCallingIdentity(identityToken); |
| 2448 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2451 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2452 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2453 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2454 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2455 | Log.v(TAG, "setUserData: " + account |
| 2456 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2457 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2458 | + ", pid " + Binder.getCallingPid()); |
| 2459 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2460 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2461 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2462 | int userId = UserHandle.getCallingUserId(); |
| 2463 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2464 | String msg = String.format( |
| 2465 | "uid %s cannot set user data for accounts of type: %s", |
| 2466 | callingUid, |
| 2467 | account.type); |
| 2468 | throw new SecurityException(msg); |
| 2469 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2470 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2471 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2472 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2473 | synchronized (accounts.cacheLock) { |
| 2474 | if (!accountExistsCacheLocked(accounts, account)) { |
| 2475 | return; |
| 2476 | } |
| 2477 | setUserdataInternalLocked(accounts, account, key, value); |
| 2478 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2479 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2480 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2484 | private boolean accountExistsCacheLocked(UserAccounts accounts, Account account) { |
| 2485 | if (accounts.accountCache.containsKey(account.type)) { |
| 2486 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2487 | if (acc.name.equals(account.name)) { |
| 2488 | return true; |
| 2489 | } |
| 2490 | } |
| 2491 | } |
| 2492 | return false; |
| 2493 | } |
| 2494 | |
| 2495 | private void setUserdataInternalLocked(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2496 | String value) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2497 | if (account == null || key == null) { |
| 2498 | return; |
| 2499 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2500 | accounts.accountsDb.beginTransaction(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2501 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2502 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2503 | if (accountId < 0) { |
| 2504 | return; |
| 2505 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2506 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2507 | if (extrasId < 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2508 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2509 | if (extrasId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2510 | return; |
| 2511 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2512 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2513 | return; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2514 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2515 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2516 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2517 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 2518 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2519 | } |
| 2520 | } |
| 2521 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2522 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2523 | if (result == null) { |
| 2524 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2525 | } |
| 2526 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2527 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2528 | + response); |
| 2529 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2530 | try { |
| 2531 | response.onResult(result); |
| 2532 | } catch (RemoteException e) { |
| 2533 | // if the caller is dead then there is no one to care about remote |
| 2534 | // exceptions |
| 2535 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2536 | Log.v(TAG, "failure while notifying response", e); |
| 2537 | } |
| 2538 | } |
| 2539 | } |
| 2540 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2541 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2542 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2543 | final String authTokenType) |
| 2544 | throws RemoteException { |
| 2545 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2546 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 2547 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2548 | final int callingUid = getCallingUid(); |
| 2549 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2550 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2551 | throw new SecurityException("can only call from system"); |
| 2552 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2553 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2554 | long identityToken = clearCallingIdentity(); |
| 2555 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2556 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2557 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2558 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2559 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2560 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2561 | protected String toDebugString(long now) { |
| 2562 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2563 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2564 | + ", authTokenType " + authTokenType; |
| 2565 | } |
| 2566 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2567 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2568 | public void run() throws RemoteException { |
| 2569 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2570 | } |
| 2571 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2572 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2573 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2574 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2575 | if (result != null) { |
| 2576 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2577 | Bundle bundle = new Bundle(); |
| 2578 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2579 | super.onResult(bundle); |
| 2580 | return; |
| 2581 | } else { |
| 2582 | super.onResult(result); |
| 2583 | } |
| 2584 | } |
| 2585 | }.bind(); |
| 2586 | } finally { |
| 2587 | restoreCallingIdentity(identityToken); |
| 2588 | } |
| 2589 | } |
| 2590 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2591 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2592 | public void getAuthToken( |
| 2593 | IAccountManagerResponse response, |
| 2594 | final Account account, |
| 2595 | final String authTokenType, |
| 2596 | final boolean notifyOnAuthFailure, |
| 2597 | final boolean expectActivityLaunch, |
| 2598 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2599 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2600 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2601 | Log.v(TAG, "getAuthToken: " + account |
| 2602 | + ", response " + response |
| 2603 | + ", authTokenType " + authTokenType |
| 2604 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2605 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2606 | + ", caller's uid " + Binder.getCallingUid() |
| 2607 | + ", pid " + Binder.getCallingPid()); |
| 2608 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2609 | if (response == null) throw new IllegalArgumentException("response is null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2610 | try { |
| 2611 | if (account == null) { |
| 2612 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2613 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2614 | return; |
| 2615 | } |
| 2616 | if (authTokenType == null) { |
| 2617 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2618 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2619 | return; |
| 2620 | } |
| 2621 | } catch (RemoteException e) { |
| 2622 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2623 | return; |
| 2624 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2625 | int userId = UserHandle.getCallingUserId(); |
| 2626 | long ident = Binder.clearCallingIdentity(); |
| 2627 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2628 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2629 | try { |
| 2630 | accounts = getUserAccounts(userId); |
| 2631 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2632 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2633 | } finally { |
| 2634 | Binder.restoreCallingIdentity(ident); |
| 2635 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2636 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2637 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2638 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2639 | |
| 2640 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2641 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2642 | final boolean permissionGranted = |
| 2643 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2644 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2645 | // Get the calling package. We will use it for the purpose of caching. |
| 2646 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2647 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2648 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2649 | try { |
| 2650 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2651 | } finally { |
| 2652 | Binder.restoreCallingIdentity(ident); |
| 2653 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2654 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2655 | String msg = String.format( |
| 2656 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2657 | callerUid, |
| 2658 | callerPkg); |
| 2659 | throw new SecurityException(msg); |
| 2660 | } |
| 2661 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2662 | // let authenticator know the identity of the caller |
| 2663 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2664 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2665 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2666 | if (notifyOnAuthFailure) { |
| 2667 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2668 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2669 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2670 | long identityToken = clearCallingIdentity(); |
| 2671 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2672 | // Distill the caller's package signatures into a single digest. |
| 2673 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2674 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2675 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2676 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2677 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2678 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2679 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2680 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2681 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2682 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2683 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2684 | onResult(response, result); |
| 2685 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2686 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2687 | } |
| 2688 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2689 | if (customTokens) { |
| 2690 | /* |
| 2691 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2692 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2693 | * ANDORID_PACKAGE_NAME. |
| 2694 | */ |
| 2695 | String token = readCachedTokenInternal( |
| 2696 | accounts, |
| 2697 | account, |
| 2698 | authTokenType, |
| 2699 | callerPkg, |
| 2700 | callerPkgSigDigest); |
| 2701 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2702 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2703 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2704 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2705 | Bundle result = new Bundle(); |
| 2706 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2707 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2708 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2709 | onResult(response, result); |
| 2710 | return; |
| 2711 | } |
| 2712 | } |
| 2713 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2714 | new Session( |
| 2715 | accounts, |
| 2716 | response, |
| 2717 | account.type, |
| 2718 | expectActivityLaunch, |
| 2719 | false /* stripAuthTokenFromResult */, |
| 2720 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2721 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2722 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2723 | protected String toDebugString(long now) { |
| 2724 | if (loginOptions != null) loginOptions.keySet(); |
| 2725 | return super.toDebugString(now) + ", getAuthToken" |
| 2726 | + ", " + account |
| 2727 | + ", authTokenType " + authTokenType |
| 2728 | + ", loginOptions " + loginOptions |
| 2729 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2730 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2731 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2732 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2733 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2734 | // If the caller doesn't have permission then create and return the |
| 2735 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2736 | if (!permissionGranted) { |
| 2737 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2738 | } else { |
| 2739 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2740 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2741 | } |
| 2742 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2743 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2744 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2745 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2746 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2747 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2748 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2749 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2750 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2751 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2752 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2753 | authTokenType, |
| 2754 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2755 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2756 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2757 | onResult(bundle); |
| 2758 | return; |
| 2759 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2760 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2761 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2762 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2763 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2764 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2765 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2766 | "the type and name should not be empty"); |
| 2767 | return; |
| 2768 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2769 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2770 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2771 | saveAuthTokenToDatabase( |
| 2772 | mAccounts, |
| 2773 | resultAccount, |
| 2774 | authTokenType, |
| 2775 | authToken); |
| 2776 | } |
| 2777 | long expiryMillis = result.getLong( |
| 2778 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2779 | if (customTokens |
| 2780 | && expiryMillis > System.currentTimeMillis()) { |
| 2781 | saveCachedToken( |
| 2782 | mAccounts, |
| 2783 | account, |
| 2784 | callerPkg, |
| 2785 | callerPkgSigDigest, |
| 2786 | authTokenType, |
| 2787 | authToken, |
| 2788 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2789 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2790 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2791 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2792 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2793 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2794 | /* |
| 2795 | * Make sure that the supplied intent is owned by the authenticator |
| 2796 | * giving it to the system. Otherwise a malicious authenticator could |
| 2797 | * have users launching arbitrary activities by tricking users to |
| 2798 | * interact with malicious notifications. |
| 2799 | */ |
| 2800 | checkKeyIntent( |
| 2801 | Binder.getCallingUid(), |
| 2802 | intent); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2803 | doNotification(mAccounts, |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2804 | account, result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2805 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2806 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2807 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2808 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2809 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2810 | }.bind(); |
| 2811 | } finally { |
| 2812 | restoreCallingIdentity(identityToken); |
| 2813 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2814 | } |
| 2815 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2816 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2817 | MessageDigest digester; |
| 2818 | try { |
| 2819 | digester = MessageDigest.getInstance("SHA-256"); |
| 2820 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2821 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2822 | for (Signature sig : pkgInfo.signatures) { |
| 2823 | digester.update(sig.toByteArray()); |
| 2824 | } |
| 2825 | } catch (NoSuchAlgorithmException x) { |
| 2826 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2827 | digester = null; |
| 2828 | } catch (NameNotFoundException e) { |
| 2829 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2830 | digester = null; |
| 2831 | } |
| 2832 | return (digester == null) ? null : digester.digest(); |
| 2833 | } |
| 2834 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2835 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2836 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2837 | int uid = intent.getIntExtra( |
| 2838 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2839 | String authTokenType = intent.getStringExtra( |
| 2840 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2841 | final String titleAndSubtitle = |
| 2842 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2843 | account.name); |
| 2844 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2845 | String title = titleAndSubtitle; |
| 2846 | String subtitle = ""; |
| 2847 | if (index > 0) { |
| 2848 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2849 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2850 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2851 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2852 | Context contextForUser = getContextForUser(user); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 2853 | Notification n = new Notification.Builder(contextForUser) |
| 2854 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2855 | .setWhen(0) |
| 2856 | .setColor(contextForUser.getColor( |
| 2857 | com.android.internal.R.color.system_notification_accent_color)) |
| 2858 | .setContentTitle(title) |
| 2859 | .setContentText(subtitle) |
| 2860 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2861 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2862 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2863 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2864 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2865 | } |
| 2866 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2867 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 2868 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 2869 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2870 | |
| 2871 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2872 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2873 | if (startInNewTask) { |
| 2874 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 2875 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2876 | // the intent from a non-Activity context. This is the default behavior. |
| 2877 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2878 | } |
| 2879 | intent.addCategory(String.valueOf(getCredentialPermissionNotificationId(account, |
| 2880 | authTokenType, uid) + (packageName != null ? packageName : ""))); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2881 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2882 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2883 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2884 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2885 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2886 | return intent; |
| 2887 | } |
| 2888 | |
| 2889 | private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, |
| 2890 | int uid) { |
| 2891 | Integer id; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2892 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2893 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2894 | final Pair<Pair<Account, String>, Integer> key = |
| 2895 | new Pair<Pair<Account, String>, Integer>( |
| 2896 | new Pair<Account, String>(account, authTokenType), uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2897 | id = accounts.credentialsPermissionNotificationIds.get(key); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2898 | if (id == null) { |
| 2899 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2900 | accounts.credentialsPermissionNotificationIds.put(key, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2901 | } |
| 2902 | } |
| 2903 | return id; |
| 2904 | } |
| 2905 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2906 | private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2907 | Integer id; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2908 | synchronized (accounts.signinRequiredNotificationIds) { |
| 2909 | id = accounts.signinRequiredNotificationIds.get(account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2910 | if (id == null) { |
| 2911 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2912 | accounts.signinRequiredNotificationIds.put(account, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2913 | } |
| 2914 | } |
| 2915 | return id; |
| 2916 | } |
| 2917 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2918 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2919 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2920 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2921 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2922 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2923 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2924 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2925 | + ", response " + response |
| 2926 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2927 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2928 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2929 | + ", caller's uid " + Binder.getCallingUid() |
| 2930 | + ", pid " + Binder.getCallingPid()); |
| 2931 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2932 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2933 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2934 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2935 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2936 | final int uid = Binder.getCallingUid(); |
| 2937 | final int userId = UserHandle.getUserId(uid); |
| 2938 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2939 | try { |
| 2940 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2941 | "User is not allowed to add an account!"); |
| 2942 | } catch (RemoteException re) { |
| 2943 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2944 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2945 | return; |
| 2946 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2947 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2948 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2949 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2950 | "User cannot modify accounts of this type (policy)."); |
| 2951 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2952 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2953 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2954 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2955 | return; |
| 2956 | } |
| 2957 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2958 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2959 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2960 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2961 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2962 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2963 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2964 | long identityToken = clearCallingIdentity(); |
| 2965 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2966 | UserAccounts accounts = getUserAccounts(usrId); |
| 2967 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2968 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 2969 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2970 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2971 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2972 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2973 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2974 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2975 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2976 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2977 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2978 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2979 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2980 | protected String toDebugString(long now) { |
| 2981 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2982 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2983 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2984 | } |
| 2985 | }.bind(); |
| 2986 | } finally { |
| 2987 | restoreCallingIdentity(identityToken); |
| 2988 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2989 | } |
| 2990 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2991 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2992 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 2993 | final String authTokenType, final String[] requiredFeatures, |
| 2994 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2995 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2996 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2997 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2998 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2999 | + ", response " + response |
| 3000 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3001 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3002 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3003 | + ", caller's uid " + Binder.getCallingUid() |
| 3004 | + ", pid " + Binder.getCallingPid() |
| 3005 | + ", for user id " + userId); |
| 3006 | } |
| 3007 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3008 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3009 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3010 | if (isCrossUser(callingUid, userId)) { |
| 3011 | throw new SecurityException( |
| 3012 | String.format( |
| 3013 | "User %s trying to add account for %s" , |
| 3014 | UserHandle.getCallingUserId(), |
| 3015 | userId)); |
| 3016 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3017 | |
| 3018 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3019 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3020 | try { |
| 3021 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3022 | "User is not allowed to add an account!"); |
| 3023 | } catch (RemoteException re) { |
| 3024 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3025 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3026 | return; |
| 3027 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3028 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3029 | try { |
| 3030 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3031 | "User cannot modify accounts of this type (policy)."); |
| 3032 | } catch (RemoteException re) { |
| 3033 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3034 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3035 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3036 | return; |
| 3037 | } |
| 3038 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3039 | final int pid = Binder.getCallingPid(); |
| 3040 | final int uid = Binder.getCallingUid(); |
| 3041 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3042 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3043 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3044 | |
| 3045 | long identityToken = clearCallingIdentity(); |
| 3046 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3047 | UserAccounts accounts = getUserAccounts(userId); |
| 3048 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3049 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3050 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3051 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3052 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3053 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3054 | @Override |
| 3055 | public void run() throws RemoteException { |
| 3056 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3057 | options); |
| 3058 | } |
| 3059 | |
| 3060 | @Override |
| 3061 | protected String toDebugString(long now) { |
| 3062 | return super.toDebugString(now) + ", addAccount" |
| 3063 | + ", accountType " + accountType |
| 3064 | + ", requiredFeatures " |
| 3065 | + (requiredFeatures != null |
| 3066 | ? TextUtils.join(",", requiredFeatures) |
| 3067 | : null); |
| 3068 | } |
| 3069 | }.bind(); |
| 3070 | } finally { |
| 3071 | restoreCallingIdentity(identityToken); |
| 3072 | } |
| 3073 | } |
| 3074 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3075 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3076 | public void startAddAccountSession( |
| 3077 | final IAccountManagerResponse response, |
| 3078 | final String accountType, |
| 3079 | final String authTokenType, |
| 3080 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3081 | final boolean expectActivityLaunch, |
| 3082 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3083 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3084 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3085 | Log.v(TAG, |
| 3086 | "startAddAccountSession: accountType " + accountType |
| 3087 | + ", response " + response |
| 3088 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3089 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3090 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3091 | + ", caller's uid " + Binder.getCallingUid() |
| 3092 | + ", pid " + Binder.getCallingPid()); |
| 3093 | } |
| 3094 | if (response == null) { |
| 3095 | throw new IllegalArgumentException("response is null"); |
| 3096 | } |
| 3097 | if (accountType == null) { |
| 3098 | throw new IllegalArgumentException("accountType is null"); |
| 3099 | } |
| 3100 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3101 | final int uid = Binder.getCallingUid(); |
| 3102 | final int userId = UserHandle.getUserId(uid); |
| 3103 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3104 | try { |
| 3105 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3106 | "User is not allowed to add an account!"); |
| 3107 | } catch (RemoteException re) { |
| 3108 | } |
| 3109 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3110 | return; |
| 3111 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3112 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3113 | try { |
| 3114 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3115 | "User cannot modify accounts of this type (policy)."); |
| 3116 | } catch (RemoteException re) { |
| 3117 | } |
| 3118 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3119 | userId); |
| 3120 | return; |
| 3121 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3122 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3123 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3124 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3125 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3126 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3127 | // Check to see if the Password should be included to the caller. |
| 3128 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3129 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3130 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3131 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3132 | long identityToken = clearCallingIdentity(); |
| 3133 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3134 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3135 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3136 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3137 | new StartAccountSession( |
| 3138 | accounts, |
| 3139 | response, |
| 3140 | accountType, |
| 3141 | expectActivityLaunch, |
| 3142 | null /* accountName */, |
| 3143 | false /* authDetailsRequired */, |
| 3144 | true /* updateLastAuthenticationTime */, |
| 3145 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3146 | @Override |
| 3147 | public void run() throws RemoteException { |
| 3148 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3149 | requiredFeatures, options); |
| 3150 | } |
| 3151 | |
| 3152 | @Override |
| 3153 | protected String toDebugString(long now) { |
| 3154 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3155 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3156 | + accountType + ", requiredFeatures " |
| 3157 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3158 | } |
| 3159 | }.bind(); |
| 3160 | } finally { |
| 3161 | restoreCallingIdentity(identityToken); |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3166 | private abstract class StartAccountSession extends Session { |
| 3167 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3168 | private final boolean mIsPasswordForwardingAllowed; |
| 3169 | |
| 3170 | public StartAccountSession( |
| 3171 | UserAccounts accounts, |
| 3172 | IAccountManagerResponse response, |
| 3173 | String accountType, |
| 3174 | boolean expectActivityLaunch, |
| 3175 | String accountName, |
| 3176 | boolean authDetailsRequired, |
| 3177 | boolean updateLastAuthenticationTime, |
| 3178 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3179 | super(accounts, response, accountType, expectActivityLaunch, |
| 3180 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3181 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3182 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | @Override |
| 3186 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3187 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3188 | mNumResults++; |
| 3189 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3190 | if (result != null |
| 3191 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3192 | checkKeyIntent( |
| 3193 | Binder.getCallingUid(), |
| 3194 | intent); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3195 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3196 | IAccountManagerResponse response; |
| 3197 | if (mExpectActivityLaunch && result != null |
| 3198 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3199 | response = mResponse; |
| 3200 | } else { |
| 3201 | response = getResponseAndClose(); |
| 3202 | } |
| 3203 | if (response == null) { |
| 3204 | return; |
| 3205 | } |
| 3206 | if (result == null) { |
| 3207 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3208 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3209 | + response); |
| 3210 | } |
| 3211 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3212 | "null bundle returned"); |
| 3213 | return; |
| 3214 | } |
| 3215 | |
| 3216 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3217 | // All AccountManager error codes are greater |
| 3218 | // than 0 |
| 3219 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3220 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3221 | return; |
| 3222 | } |
| 3223 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3224 | // Omit passwords if the caller isn't permitted to see them. |
| 3225 | if (!mIsPasswordForwardingAllowed) { |
| 3226 | result.remove(AccountManager.KEY_PASSWORD); |
| 3227 | } |
| 3228 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3229 | // Strip auth token from result. |
| 3230 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3231 | |
| 3232 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3233 | Log.v(TAG, |
| 3234 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3235 | } |
| 3236 | |
| 3237 | // Get the session bundle created by authenticator. The |
| 3238 | // bundle contains data necessary for finishing the session |
| 3239 | // later. The session bundle will be encrypted here and |
| 3240 | // decrypted later when trying to finish the session. |
| 3241 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3242 | if (sessionBundle != null) { |
| 3243 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3244 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3245 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3246 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3247 | } |
| 3248 | // Add accountType info to session bundle. This will |
| 3249 | // override any value set by authenticator. |
| 3250 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3251 | |
| 3252 | // Encrypt session bundle before returning to caller. |
| 3253 | try { |
| 3254 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3255 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3256 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3257 | } catch (GeneralSecurityException e) { |
| 3258 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3259 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3260 | } |
| 3261 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3262 | "failed to encrypt session bundle"); |
| 3263 | return; |
| 3264 | } |
| 3265 | } |
| 3266 | |
| 3267 | sendResponse(response, result); |
| 3268 | } |
| 3269 | } |
| 3270 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3271 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3272 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3273 | @NonNull Bundle sessionBundle, |
| 3274 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3275 | Bundle appInfo, |
| 3276 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3277 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3278 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3279 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3280 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3281 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3282 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3283 | + ", caller's uid " + callingUid |
| 3284 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3285 | + ", pid " + Binder.getCallingPid() |
| 3286 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3287 | } |
| 3288 | if (response == null) { |
| 3289 | throw new IllegalArgumentException("response is null"); |
| 3290 | } |
| 3291 | |
| 3292 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3293 | // Account type is added to it before encryption. |
| 3294 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3295 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3296 | } |
| 3297 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3298 | // Only allow the system process to finish session for other users |
| 3299 | if (isCrossUser(callingUid, userId)) { |
| 3300 | throw new SecurityException( |
| 3301 | String.format( |
| 3302 | "User %s trying to finish session for %s without cross user permission", |
| 3303 | UserHandle.getCallingUserId(), |
| 3304 | userId)); |
| 3305 | } |
| 3306 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3307 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3308 | sendErrorResponse(response, |
| 3309 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3310 | "User is not allowed to add an account!"); |
| 3311 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3312 | return; |
| 3313 | } |
| 3314 | |
| 3315 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3316 | final Bundle decryptedBundle; |
| 3317 | final String accountType; |
| 3318 | // First decrypt session bundle to get account type for checking permission. |
| 3319 | try { |
| 3320 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3321 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3322 | if (decryptedBundle == null) { |
| 3323 | sendErrorResponse( |
| 3324 | response, |
| 3325 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3326 | "failed to decrypt session bundle"); |
| 3327 | return; |
| 3328 | } |
| 3329 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3330 | // Account type cannot be null. This should not happen if session bundle was created |
| 3331 | // properly by #StartAccountSession. |
| 3332 | if (TextUtils.isEmpty(accountType)) { |
| 3333 | sendErrorResponse( |
| 3334 | response, |
| 3335 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3336 | "accountType is empty"); |
| 3337 | return; |
| 3338 | } |
| 3339 | |
| 3340 | // If by any chances, decryptedBundle contains colliding keys with |
| 3341 | // system info |
| 3342 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3343 | // update credentials flow, we should replace with the new values of the current call. |
| 3344 | if (appInfo != null) { |
| 3345 | decryptedBundle.putAll(appInfo); |
| 3346 | } |
| 3347 | |
| 3348 | // 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] | 3349 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3350 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3351 | } catch (GeneralSecurityException e) { |
| 3352 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3353 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3354 | } |
| 3355 | sendErrorResponse( |
| 3356 | response, |
| 3357 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3358 | "failed to decrypt session bundle"); |
| 3359 | return; |
| 3360 | } |
| 3361 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3362 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3363 | sendErrorResponse( |
| 3364 | response, |
| 3365 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3366 | "User cannot modify accounts of this type (policy)."); |
| 3367 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3368 | userId); |
| 3369 | return; |
| 3370 | } |
| 3371 | |
| 3372 | long identityToken = clearCallingIdentity(); |
| 3373 | try { |
| 3374 | UserAccounts accounts = getUserAccounts(userId); |
| 3375 | logRecordWithUid( |
| 3376 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3377 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3378 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3379 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3380 | new Session( |
| 3381 | accounts, |
| 3382 | response, |
| 3383 | accountType, |
| 3384 | expectActivityLaunch, |
| 3385 | true /* stripAuthTokenFromResult */, |
| 3386 | null /* accountName */, |
| 3387 | false /* authDetailsRequired */, |
| 3388 | true /* updateLastAuthenticationTime */) { |
| 3389 | @Override |
| 3390 | public void run() throws RemoteException { |
| 3391 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3392 | } |
| 3393 | |
| 3394 | @Override |
| 3395 | protected String toDebugString(long now) { |
| 3396 | return super.toDebugString(now) |
| 3397 | + ", finishSession" |
| 3398 | + ", accountType " + accountType; |
| 3399 | } |
| 3400 | }.bind(); |
| 3401 | } finally { |
| 3402 | restoreCallingIdentity(identityToken); |
| 3403 | } |
| 3404 | } |
| 3405 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3406 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3407 | final DevicePolicyManagerInternal dpmi = |
| 3408 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3409 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3410 | if (dpmi == null) { |
| 3411 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3412 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3413 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3414 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3415 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3416 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3417 | } |
| 3418 | if (intent == null) { |
| 3419 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3420 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3421 | long identityToken = clearCallingIdentity(); |
| 3422 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3423 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3424 | } finally { |
| 3425 | restoreCallingIdentity(identityToken); |
| 3426 | } |
| 3427 | } |
| 3428 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3429 | /** |
| 3430 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3431 | */ |
| 3432 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3433 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3434 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3435 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3436 | return cantAddAccount; |
| 3437 | } |
| 3438 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3439 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3440 | public void confirmCredentialsAsUser( |
| 3441 | IAccountManagerResponse response, |
| 3442 | final Account account, |
| 3443 | final Bundle options, |
| 3444 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3445 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3446 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3447 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3448 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3449 | Log.v(TAG, "confirmCredentials: " + account |
| 3450 | + ", response " + response |
| 3451 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3452 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3453 | + ", pid " + Binder.getCallingPid()); |
| 3454 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3455 | // Only allow the system process to read accounts of other users |
| 3456 | if (isCrossUser(callingUid, userId)) { |
| 3457 | throw new SecurityException( |
| 3458 | String.format( |
| 3459 | "User %s trying to confirm account credentials for %s" , |
| 3460 | UserHandle.getCallingUserId(), |
| 3461 | userId)); |
| 3462 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3463 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3464 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3465 | long identityToken = clearCallingIdentity(); |
| 3466 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3467 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3468 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3469 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3470 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3471 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3472 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3473 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3474 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3475 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3476 | protected String toDebugString(long now) { |
| 3477 | return super.toDebugString(now) + ", confirmCredentials" |
| 3478 | + ", " + account; |
| 3479 | } |
| 3480 | }.bind(); |
| 3481 | } finally { |
| 3482 | restoreCallingIdentity(identityToken); |
| 3483 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3484 | } |
| 3485 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3486 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3487 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3488 | final String authTokenType, final boolean expectActivityLaunch, |
| 3489 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3490 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3491 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3492 | Log.v(TAG, "updateCredentials: " + account |
| 3493 | + ", response " + response |
| 3494 | + ", authTokenType " + authTokenType |
| 3495 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3496 | + ", caller's uid " + Binder.getCallingUid() |
| 3497 | + ", pid " + Binder.getCallingPid()); |
| 3498 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3499 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3500 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3501 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3502 | long identityToken = clearCallingIdentity(); |
| 3503 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3504 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3505 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3506 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3507 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3508 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3509 | public void run() throws RemoteException { |
| 3510 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3511 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3512 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3513 | protected String toDebugString(long now) { |
| 3514 | if (loginOptions != null) loginOptions.keySet(); |
| 3515 | return super.toDebugString(now) + ", updateCredentials" |
| 3516 | + ", " + account |
| 3517 | + ", authTokenType " + authTokenType |
| 3518 | + ", loginOptions " + loginOptions; |
| 3519 | } |
| 3520 | }.bind(); |
| 3521 | } finally { |
| 3522 | restoreCallingIdentity(identityToken); |
| 3523 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3524 | } |
| 3525 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3526 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3527 | public void startUpdateCredentialsSession( |
| 3528 | IAccountManagerResponse response, |
| 3529 | final Account account, |
| 3530 | final String authTokenType, |
| 3531 | final boolean expectActivityLaunch, |
| 3532 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3533 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3534 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3535 | Log.v(TAG, |
| 3536 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3537 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3538 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3539 | + ", pid " + Binder.getCallingPid()); |
| 3540 | } |
| 3541 | if (response == null) { |
| 3542 | throw new IllegalArgumentException("response is null"); |
| 3543 | } |
| 3544 | if (account == null) { |
| 3545 | throw new IllegalArgumentException("account is null"); |
| 3546 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3547 | |
| 3548 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3549 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3550 | |
| 3551 | // Check to see if the Password should be included to the caller. |
| 3552 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3553 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3554 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3555 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3556 | long identityToken = clearCallingIdentity(); |
| 3557 | try { |
| 3558 | UserAccounts accounts = getUserAccounts(userId); |
| 3559 | new StartAccountSession( |
| 3560 | accounts, |
| 3561 | response, |
| 3562 | account.type, |
| 3563 | expectActivityLaunch, |
| 3564 | account.name, |
| 3565 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3566 | true /* updateLastCredentialTime */, |
| 3567 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3568 | @Override |
| 3569 | public void run() throws RemoteException { |
| 3570 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3571 | loginOptions); |
| 3572 | } |
| 3573 | |
| 3574 | @Override |
| 3575 | protected String toDebugString(long now) { |
| 3576 | if (loginOptions != null) |
| 3577 | loginOptions.keySet(); |
| 3578 | return super.toDebugString(now) |
| 3579 | + ", startUpdateCredentialsSession" |
| 3580 | + ", " + account |
| 3581 | + ", authTokenType " + authTokenType |
| 3582 | + ", loginOptions " + loginOptions; |
| 3583 | } |
| 3584 | }.bind(); |
| 3585 | } finally { |
| 3586 | restoreCallingIdentity(identityToken); |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3591 | public void isCredentialsUpdateSuggested( |
| 3592 | IAccountManagerResponse response, |
| 3593 | final Account account, |
| 3594 | final String statusToken) { |
| 3595 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3596 | Log.v(TAG, |
| 3597 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3598 | + ", caller's uid " + Binder.getCallingUid() |
| 3599 | + ", pid " + Binder.getCallingPid()); |
| 3600 | } |
| 3601 | if (response == null) { |
| 3602 | throw new IllegalArgumentException("response is null"); |
| 3603 | } |
| 3604 | if (account == null) { |
| 3605 | throw new IllegalArgumentException("account is null"); |
| 3606 | } |
| 3607 | if (TextUtils.isEmpty(statusToken)) { |
| 3608 | throw new IllegalArgumentException("status token is empty"); |
| 3609 | } |
| 3610 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3611 | int usrId = UserHandle.getCallingUserId(); |
| 3612 | long identityToken = clearCallingIdentity(); |
| 3613 | try { |
| 3614 | UserAccounts accounts = getUserAccounts(usrId); |
| 3615 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3616 | false /* stripAuthTokenFromResult */, account.name, |
| 3617 | false /* authDetailsRequired */) { |
| 3618 | @Override |
| 3619 | protected String toDebugString(long now) { |
| 3620 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3621 | + ", " + account; |
| 3622 | } |
| 3623 | |
| 3624 | @Override |
| 3625 | public void run() throws RemoteException { |
| 3626 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3627 | } |
| 3628 | |
| 3629 | @Override |
| 3630 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3631 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3632 | IAccountManagerResponse response = getResponseAndClose(); |
| 3633 | if (response == null) { |
| 3634 | return; |
| 3635 | } |
| 3636 | |
| 3637 | if (result == null) { |
| 3638 | sendErrorResponse( |
| 3639 | response, |
| 3640 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3641 | "null bundle"); |
| 3642 | return; |
| 3643 | } |
| 3644 | |
| 3645 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3646 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3647 | + response); |
| 3648 | } |
| 3649 | // Check to see if an error occurred. We know if an error occurred because all |
| 3650 | // error codes are greater than 0. |
| 3651 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3652 | sendErrorResponse(response, |
| 3653 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3654 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3655 | return; |
| 3656 | } |
| 3657 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3658 | sendErrorResponse( |
| 3659 | response, |
| 3660 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3661 | "no result in response"); |
| 3662 | return; |
| 3663 | } |
| 3664 | final Bundle newResult = new Bundle(); |
| 3665 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3666 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3667 | sendResponse(response, newResult); |
| 3668 | } |
| 3669 | }.bind(); |
| 3670 | } finally { |
| 3671 | restoreCallingIdentity(identityToken); |
| 3672 | } |
| 3673 | } |
| 3674 | |
| 3675 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3676 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3677 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3678 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3679 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3680 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3681 | + ", response " + response |
| 3682 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3683 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3684 | + ", pid " + Binder.getCallingPid()); |
| 3685 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3686 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3687 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3688 | int userId = UserHandle.getCallingUserId(); |
| 3689 | if (!isAccountManagedByCaller(accountType, callingUid, userId) && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3690 | String msg = String.format( |
| 3691 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3692 | callingUid, |
| 3693 | accountType); |
| 3694 | throw new SecurityException(msg); |
| 3695 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3696 | long identityToken = clearCallingIdentity(); |
| 3697 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3698 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3699 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3700 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3701 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3702 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3703 | public void run() throws RemoteException { |
| 3704 | mAuthenticator.editProperties(this, mAccountType); |
| 3705 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3706 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3707 | protected String toDebugString(long now) { |
| 3708 | return super.toDebugString(now) + ", editProperties" |
| 3709 | + ", accountType " + accountType; |
| 3710 | } |
| 3711 | }.bind(); |
| 3712 | } finally { |
| 3713 | restoreCallingIdentity(identityToken); |
| 3714 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3715 | } |
| 3716 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3717 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3718 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3719 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3720 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3721 | throw new SecurityException("Can be called only by system UID"); |
| 3722 | } |
| 3723 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3724 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3725 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3726 | |
| 3727 | final int userId = userHandle.getIdentifier(); |
| 3728 | |
| 3729 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3730 | |
| 3731 | try { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3732 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3733 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3734 | } catch (NameNotFoundException e) { |
| 3735 | return false; |
| 3736 | } |
| 3737 | } |
| 3738 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3739 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3740 | int uid) { |
| 3741 | if (packageName == null) { |
| 3742 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3743 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3744 | return false; |
| 3745 | } |
| 3746 | // For app op checks related to permissions all packages in the UID |
| 3747 | // have the same app op state, so doesn't matter which one we pick. |
| 3748 | packageName = packageNames[0]; |
| 3749 | } |
| 3750 | |
| 3751 | // Use null token which means any token. Having a token means the package |
| 3752 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3753 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3754 | return true; |
| 3755 | } |
| 3756 | // In addition to the permissions required to get an auth token we also allow |
| 3757 | // the account to be accessed by holders of the get accounts permissions. |
| 3758 | return checkUidPermission(Manifest.permission.GET_ACCOUNTS_PRIVILEGED, uid, packageName) |
| 3759 | || checkUidPermission(Manifest.permission.GET_ACCOUNTS, uid, packageName); |
| 3760 | } |
| 3761 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3762 | private boolean checkUidPermission(String permission, int uid, String opPackageName) { |
| 3763 | final long identity = Binder.clearCallingIdentity(); |
| 3764 | try { |
| 3765 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 3766 | if (pm.checkUidPermission(permission, uid) != PackageManager.PERMISSION_GRANTED) { |
| 3767 | return false; |
| 3768 | } |
| 3769 | final int opCode = AppOpsManager.permissionToOpCode(permission); |
| 3770 | return (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOpNoThrow( |
| 3771 | opCode, uid, opPackageName) == AppOpsManager.MODE_ALLOWED); |
| 3772 | } catch (RemoteException e) { |
| 3773 | /* ignore - local call */ |
| 3774 | } finally { |
| 3775 | Binder.restoreCallingIdentity(identity); |
| 3776 | } |
| 3777 | return false; |
| 3778 | } |
| 3779 | |
| 3780 | @Override |
| 3781 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3782 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3783 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3784 | throw new SecurityException("Can be called only by system UID"); |
| 3785 | } |
| 3786 | |
| 3787 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3788 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3789 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3790 | |
| 3791 | final int userId = userHandle.getIdentifier(); |
| 3792 | |
| 3793 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3794 | |
| 3795 | final int uid; |
| 3796 | try { |
| 3797 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 3798 | } catch (NameNotFoundException e) { |
| 3799 | Slog.e(TAG, "Unknown package " + packageName); |
| 3800 | return null; |
| 3801 | } |
| 3802 | |
| 3803 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 3804 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3805 | final long identity = Binder.clearCallingIdentity(); |
| 3806 | try { |
| 3807 | return PendingIntent.getActivityAsUser( |
| 3808 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 3809 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 3810 | null, new UserHandle(userId)).getIntentSender(); |
| 3811 | } finally { |
| 3812 | Binder.restoreCallingIdentity(identity); |
| 3813 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 3817 | int uid, RemoteCallback callback) { |
| 3818 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 3819 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 3820 | @Override |
| 3821 | public void onResult(Bundle value) throws RemoteException { |
| 3822 | handleAuthenticatorResponse(true); |
| 3823 | } |
| 3824 | |
| 3825 | @Override |
| 3826 | public void onRequestContinued() { |
| 3827 | /* ignore */ |
| 3828 | } |
| 3829 | |
| 3830 | @Override |
| 3831 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 3832 | handleAuthenticatorResponse(false); |
| 3833 | } |
| 3834 | |
| 3835 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 3836 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3837 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3838 | UserHandle.getUserHandleForUid(uid)); |
| 3839 | if (callback != null) { |
| 3840 | Bundle result = new Bundle(); |
| 3841 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 3842 | callback.sendResult(result); |
| 3843 | } |
| 3844 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3845 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3849 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 3850 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 3851 | throw new SecurityException("Only system can check for accounts across users"); |
| 3852 | } |
| 3853 | final long token = Binder.clearCallingIdentity(); |
| 3854 | try { |
| 3855 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 3856 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 3857 | if (allAccounts[i].account.equals(account)) { |
| 3858 | return true; |
| 3859 | } |
| 3860 | } |
| 3861 | return false; |
| 3862 | } finally { |
| 3863 | Binder.restoreCallingIdentity(token); |
| 3864 | } |
| 3865 | } |
| 3866 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3867 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 3868 | private final String[] mFeatures; |
| 3869 | private volatile Account[] mAccountsOfType = null; |
| 3870 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 3871 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3872 | private final int mCallingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3873 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3874 | public GetAccountsByTypeAndFeatureSession(UserAccounts accounts, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3875 | IAccountManagerResponse response, String type, String[] features, int callingUid) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3876 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3877 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3878 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3879 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3880 | mFeatures = features; |
| 3881 | } |
| 3882 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3883 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3884 | public void run() throws RemoteException { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3885 | synchronized (mAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3886 | mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType, mCallingUid, |
| 3887 | null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3888 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3889 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 3890 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3891 | mCurrentAccount = 0; |
| 3892 | |
| 3893 | checkAccount(); |
| 3894 | } |
| 3895 | |
| 3896 | public void checkAccount() { |
| 3897 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 3898 | sendResult(); |
| 3899 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3900 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3901 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3902 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 3903 | if (accountAuthenticator == null) { |
| 3904 | // It is possible that the authenticator has died, which is indicated by |
| 3905 | // mAuthenticator being set to null. If this happens then just abort. |
| 3906 | // There is no need to send back a result or error in this case since |
| 3907 | // that already happened when mAuthenticator was cleared. |
| 3908 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3909 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 3910 | + " connected to the authenticator, " + toDebugString()); |
| 3911 | } |
| 3912 | return; |
| 3913 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3914 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3915 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3916 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3917 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3918 | } |
| 3919 | } |
| 3920 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3921 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3922 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3923 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3924 | mNumResults++; |
| 3925 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3926 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3927 | return; |
| 3928 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3929 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3930 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 3931 | } |
| 3932 | mCurrentAccount++; |
| 3933 | checkAccount(); |
| 3934 | } |
| 3935 | |
| 3936 | public void sendResult() { |
| 3937 | IAccountManagerResponse response = getResponseAndClose(); |
| 3938 | if (response != null) { |
| 3939 | try { |
| 3940 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 3941 | for (int i = 0; i < accounts.length; i++) { |
| 3942 | accounts[i] = mAccountsWithFeatures.get(i); |
| 3943 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3944 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3945 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3946 | + response); |
| 3947 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3948 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3949 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3950 | response.onResult(result); |
| 3951 | } catch (RemoteException e) { |
| 3952 | // if the caller is dead then there is no one to care about remote exceptions |
| 3953 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3954 | Log.v(TAG, "failure while notifying response", e); |
| 3955 | } |
| 3956 | } |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3961 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3962 | protected String toDebugString(long now) { |
| 3963 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 3964 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 3965 | } |
| 3966 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 3967 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3968 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3969 | * 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] | 3970 | * @hide |
| 3971 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3972 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3973 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3974 | int callingUid = Binder.getCallingUid(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3975 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 3976 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3977 | if (visibleAccountTypes.isEmpty()) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3978 | return new Account[0]; |
| 3979 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3980 | long identityToken = clearCallingIdentity(); |
| 3981 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3982 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3983 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3984 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3985 | callingUid, |
| 3986 | null, // packageName |
| 3987 | visibleAccountTypes); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3988 | } finally { |
| 3989 | restoreCallingIdentity(identityToken); |
| 3990 | } |
| 3991 | } |
| 3992 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3993 | /** |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3994 | * Returns accounts for all running users. |
| 3995 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3996 | * @hide |
| 3997 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3998 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3999 | public AccountAndUser[] getRunningAccounts() { |
| 4000 | final int[] runningUserIds; |
| 4001 | try { |
| 4002 | runningUserIds = ActivityManagerNative.getDefault().getRunningUserIds(); |
| 4003 | } catch (RemoteException e) { |
| 4004 | // Running in system_server; should never happen |
| 4005 | throw new RuntimeException(e); |
| 4006 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4007 | return getAccounts(runningUserIds); |
| 4008 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4009 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4010 | /** {@hide} */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4011 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4012 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 4013 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4014 | final int[] userIds = new int[users.size()]; |
| 4015 | for (int i = 0; i < userIds.length; i++) { |
| 4016 | userIds[i] = users.get(i).id; |
| 4017 | } |
| 4018 | return getAccounts(userIds); |
| 4019 | } |
| 4020 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4021 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4022 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4023 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4024 | for (int userId : userIds) { |
| 4025 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4026 | if (userAccounts == null) continue; |
| 4027 | synchronized (userAccounts.cacheLock) { |
| 4028 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null, |
| 4029 | Binder.getCallingUid(), null); |
| 4030 | for (int a = 0; a < accounts.length; a++) { |
| 4031 | runningAccounts.add(new AccountAndUser(accounts[a], userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4032 | } |
| 4033 | } |
| 4034 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4035 | |
| 4036 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4037 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4038 | } |
| 4039 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4040 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4041 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4042 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
| 4043 | return getAccountsAsUser(type, userId, null, -1, opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4044 | } |
| 4045 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4046 | @NonNull |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4047 | private Account[] getAccountsAsUser( |
| 4048 | String type, |
| 4049 | int userId, |
| 4050 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4051 | int packageUid, |
| 4052 | String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4053 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4054 | // Only allow the system process to read accounts of other users |
| 4055 | if (userId != UserHandle.getCallingUserId() |
Amith Yamasani | bb49e85 | 2013-03-30 19:20:18 -0700 | [diff] [blame] | 4056 | && callingUid != Process.myUid() |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4057 | && mContext.checkCallingOrSelfPermission( |
| 4058 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4059 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4060 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4061 | + " trying to get account for " + userId); |
| 4062 | } |
| 4063 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4064 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4065 | Log.v(TAG, "getAccounts: accountType " + type |
| 4066 | + ", caller's uid " + Binder.getCallingUid() |
| 4067 | + ", pid " + Binder.getCallingPid()); |
| 4068 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4069 | // If the original calling app was using the framework account chooser activity, we'll |
| 4070 | // be passed in the original caller's uid here, which is what should be used for filtering. |
| 4071 | if (packageUid != -1 && UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 4072 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4073 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4074 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4075 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4076 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4077 | if (visibleAccountTypes.isEmpty() |
| 4078 | || (type != null && !visibleAccountTypes.contains(type))) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4079 | return new Account[0]; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4080 | } else if (visibleAccountTypes.contains(type)) { |
| 4081 | // Prune the list down to just the requested type. |
| 4082 | visibleAccountTypes = new ArrayList<>(); |
| 4083 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4084 | } // else aggregate all the visible accounts (it won't matter if the |
| 4085 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4086 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4087 | long identityToken = clearCallingIdentity(); |
| 4088 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4089 | UserAccounts accounts = getUserAccounts(userId); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4090 | Account[] accountsToReturn = getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4091 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4092 | callingUid, |
| 4093 | callingPackage, |
| 4094 | visibleAccountTypes); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4095 | ArrayList<Account> accountsToReturnList = new |
| 4096 | ArrayList<Account>(Arrays.asList(accountsToReturn)); |
| 4097 | for(int i = accountsToReturnList.size() - 1; i >= 0 ; i--) { |
| 4098 | // if account not visible to caller or managed by caller, remove from |
| 4099 | // accounts to return. Note that all accounts visible by default unless |
| 4100 | // visible list functionality implemented |
| 4101 | if(!(isAccountVisible(accountsToReturnList.get(i), callingUid, |
| 4102 | getUserAccounts(userId)))) { |
| 4103 | accountsToReturnList.remove(i); |
| 4104 | } |
| 4105 | } |
| 4106 | return accountsToReturnList.toArray(new Account[accountsToReturnList.size()]); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4107 | } finally { |
| 4108 | restoreCallingIdentity(identityToken); |
| 4109 | } |
| 4110 | } |
| 4111 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4112 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4113 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4114 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4115 | int callingUid, |
| 4116 | String callingPackage, |
| 4117 | List<String> visibleAccountTypes) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4118 | synchronized (userAccounts.cacheLock) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4119 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4120 | for (String visibleType : visibleAccountTypes) { |
| 4121 | Account[] accountsForType = getAccountsFromCacheLocked( |
| 4122 | userAccounts, visibleType, callingUid, callingPackage); |
| 4123 | if (accountsForType != null) { |
| 4124 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
| 4125 | } |
| 4126 | } |
| 4127 | Account[] result = new Account[visibleAccounts.size()]; |
| 4128 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4129 | result[i] = visibleAccounts.get(i); |
| 4130 | } |
| 4131 | return result; |
| 4132 | } |
| 4133 | } |
| 4134 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4135 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4136 | public void addSharedAccountsFromParentUser(int parentUserId, int userId) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4137 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4138 | Account[] accounts = getAccountsAsUser(null, parentUserId, mContext.getOpPackageName()); |
| 4139 | for (Account account : accounts) { |
| 4140 | addSharedAccountAsUser(account, userId); |
| 4141 | } |
| 4142 | } |
| 4143 | |
| 4144 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4145 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4146 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 4147 | accounts.accountsDb.deleteSharedAccount(account); |
| 4148 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4149 | if (accountId < 0) { |
| 4150 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4151 | + ", skipping the DB insert failed"); |
| 4152 | return false; |
| 4153 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4154 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4155 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4156 | return true; |
| 4157 | } |
| 4158 | |
| 4159 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4160 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4161 | userId = handleIncomingUser(userId); |
| 4162 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 4163 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4164 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4165 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4166 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4167 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4168 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4169 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4170 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4171 | } |
| 4172 | return r > 0; |
| 4173 | } |
| 4174 | |
| 4175 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4176 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4177 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4178 | } |
| 4179 | |
| 4180 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4181 | userId = handleIncomingUser(userId); |
| 4182 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 4183 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4184 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4185 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4186 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4187 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4188 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4189 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4190 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | @Override |
| 4194 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4195 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 4196 | UserAccounts accounts = getUserAccounts(userId); |
| 4197 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4198 | Account[] accountArray = new Account[accountList.size()]; |
| 4199 | accountList.toArray(accountArray); |
| 4200 | return accountArray; |
| 4201 | } |
| 4202 | |
| 4203 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4204 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4205 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4206 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4207 | } |
| 4208 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4209 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4210 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4211 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4212 | int callingUid = Binder.getCallingUid(); |
| 4213 | if (!UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 4214 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4215 | + callingUid + " with uid=" + uid); |
| 4216 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4217 | return getAccountsAsUser(null, UserHandle.getCallingUserId(), packageName, uid, |
| 4218 | opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4219 | } |
| 4220 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4221 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4222 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4223 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4224 | String opPackageName) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4225 | int packageUid = -1; |
| 4226 | try { |
| 4227 | packageUid = AppGlobals.getPackageManager().getPackageUid( |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 4228 | packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 4229 | UserHandle.getCallingUserId()); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4230 | } catch (RemoteException re) { |
| 4231 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
| 4232 | return new Account[0]; |
| 4233 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4234 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), packageName, |
| 4235 | packageUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4236 | } |
| 4237 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4238 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4239 | public void getAccountsByFeatures( |
| 4240 | IAccountManagerResponse response, |
| 4241 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4242 | String[] features, |
| 4243 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4244 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4245 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4246 | Log.v(TAG, "getAccounts: accountType " + type |
| 4247 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4248 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4249 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4250 | + ", pid " + Binder.getCallingPid()); |
| 4251 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4252 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4253 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4254 | int userId = UserHandle.getCallingUserId(); |
| 4255 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4256 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4257 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4258 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4259 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4260 | // Need to return just the accounts that are from matching signatures. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4261 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, new Account[0]); |
| 4262 | try { |
| 4263 | response.onResult(result); |
| 4264 | } catch (RemoteException e) { |
| 4265 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4266 | } |
| 4267 | return; |
| 4268 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4269 | long identityToken = clearCallingIdentity(); |
| 4270 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4271 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4272 | if (features == null || features.length == 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4273 | Account[] accounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4274 | synchronized (userAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4275 | accounts = getAccountsFromCacheLocked(userAccounts, type, callingUid, null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4276 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4277 | Bundle result = new Bundle(); |
| 4278 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4279 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4280 | return; |
| 4281 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4282 | new GetAccountsByTypeAndFeatureSession( |
| 4283 | userAccounts, |
| 4284 | response, |
| 4285 | type, |
| 4286 | features, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4287 | callingUid).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4288 | } finally { |
| 4289 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4290 | } |
| 4291 | } |
| 4292 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4293 | @Override |
| 4294 | public void onAccountAccessed(String token) throws RemoteException { |
| 4295 | final int uid = Binder.getCallingUid(); |
| 4296 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4297 | return; |
| 4298 | } |
| 4299 | final int userId = UserHandle.getCallingUserId(); |
| 4300 | final long identity = Binder.clearCallingIdentity(); |
| 4301 | try { |
| 4302 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4303 | if (Objects.equals(account.getAccessId(), token)) { |
| 4304 | // An app just accessed the account. At this point it knows about |
| 4305 | // it and there is not need to hide this account from the app. |
| 4306 | if (!hasAccountAccess(account, null, uid)) { |
| 4307 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4308 | uid, true); |
| 4309 | } |
| 4310 | } |
| 4311 | } |
| 4312 | } finally { |
| 4313 | Binder.restoreCallingIdentity(identity); |
| 4314 | } |
| 4315 | } |
| 4316 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4317 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4318 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4319 | IAccountManagerResponse mResponse; |
| 4320 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4321 | final boolean mExpectActivityLaunch; |
| 4322 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4323 | final String mAccountName; |
| 4324 | // Indicates if we need to add auth details(like last credential time) |
| 4325 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4326 | // If set, we need to update the last authenticated time. This is |
| 4327 | // currently |
| 4328 | // used on |
| 4329 | // successful confirming credentials. |
| 4330 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4331 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4332 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4333 | private int mNumRequestContinued = 0; |
| 4334 | private int mNumErrors = 0; |
| 4335 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4336 | IAccountAuthenticator mAuthenticator = null; |
| 4337 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4338 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4339 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4340 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4341 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4342 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4343 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4344 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4345 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4346 | } |
| 4347 | |
| 4348 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4349 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4350 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4351 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4352 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4353 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4354 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4355 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4356 | mResponse = response; |
| 4357 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4358 | mExpectActivityLaunch = expectActivityLaunch; |
| 4359 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4360 | mAccountName = accountName; |
| 4361 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4362 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4363 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4364 | synchronized (mSessions) { |
| 4365 | mSessions.put(toString(), this); |
| 4366 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4367 | if (response != null) { |
| 4368 | try { |
| 4369 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4370 | } catch (RemoteException e) { |
| 4371 | mResponse = null; |
| 4372 | binderDied(); |
| 4373 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4374 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4375 | } |
| 4376 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4377 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4378 | if (mResponse == null) { |
| 4379 | // this session has already been closed |
| 4380 | return null; |
| 4381 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4382 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4383 | 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] | 4384 | return response; |
| 4385 | } |
| 4386 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4387 | /** |
| 4388 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4389 | * security policy. |
| 4390 | * |
| 4391 | * In particular we want to make sure that the Authenticator doesn't try to trick users |
| 4392 | * into launching aribtrary intents on the device via by tricking to click authenticator |
| 4393 | * supplied entries in the system Settings app. |
| 4394 | */ |
| 4395 | protected void checkKeyIntent( |
| 4396 | int authUid, |
| 4397 | Intent intent) throws SecurityException { |
| 4398 | long bid = Binder.clearCallingIdentity(); |
| 4399 | try { |
| 4400 | PackageManager pm = mContext.getPackageManager(); |
| 4401 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 4402 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4403 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Sandra Kwan | 0e961a1 | 2016-06-30 14:34:01 -0700 | [diff] [blame] | 4404 | if (!GrantCredentialsPermissionActivity.class.getName().equals( |
| 4405 | targetActivityInfo.getClass().getName()) |
| 4406 | && !CantAddAccountActivity.class |
| 4407 | .equals(targetActivityInfo.getClass().getName()) |
| 4408 | && PackageManager.SIGNATURE_MATCH != pm.checkSignatures(authUid, |
| 4409 | targetUid)) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4410 | String pkgName = targetActivityInfo.packageName; |
| 4411 | String activityName = targetActivityInfo.name; |
| 4412 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4413 | + "does not share a signature with the supplying authenticator (%s)."; |
| 4414 | throw new SecurityException( |
| 4415 | String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4416 | } |
| 4417 | } finally { |
| 4418 | Binder.restoreCallingIdentity(bid); |
| 4419 | } |
| 4420 | } |
| 4421 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4422 | private void close() { |
| 4423 | synchronized (mSessions) { |
| 4424 | if (mSessions.remove(toString()) == null) { |
| 4425 | // the session was already closed, so bail out now |
| 4426 | return; |
| 4427 | } |
| 4428 | } |
| 4429 | if (mResponse != null) { |
| 4430 | // stop listening for response deaths |
| 4431 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4432 | |
| 4433 | // clear this so that we don't accidentally send any further results |
| 4434 | mResponse = null; |
| 4435 | } |
| 4436 | cancelTimeout(); |
| 4437 | unbind(); |
| 4438 | } |
| 4439 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4440 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4441 | public void binderDied() { |
| 4442 | mResponse = null; |
| 4443 | close(); |
| 4444 | } |
| 4445 | |
| 4446 | protected String toDebugString() { |
| 4447 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4448 | } |
| 4449 | |
| 4450 | protected String toDebugString(long now) { |
| 4451 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4452 | + ", connected " + (mAuthenticator != null) |
| 4453 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4454 | + "/" + mNumErrors + ")" |
| 4455 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4456 | } |
| 4457 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4458 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4459 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4460 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4461 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4462 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4463 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4464 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | private void unbind() { |
| 4469 | if (mAuthenticator != null) { |
| 4470 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4471 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4472 | } |
| 4473 | } |
| 4474 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4475 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4476 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4477 | } |
| 4478 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4479 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4480 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4481 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4482 | try { |
| 4483 | run(); |
| 4484 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4485 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4486 | "remote exception"); |
| 4487 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4488 | } |
| 4489 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4490 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4491 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4492 | mAuthenticator = null; |
| 4493 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4494 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4495 | try { |
| 4496 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4497 | "disconnected"); |
| 4498 | } catch (RemoteException e) { |
| 4499 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4500 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4501 | + "caught RemoteException while responding", e); |
| 4502 | } |
| 4503 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4504 | } |
| 4505 | } |
| 4506 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4507 | public abstract void run() throws RemoteException; |
| 4508 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4509 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4510 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4511 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4512 | try { |
| 4513 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4514 | "timeout"); |
| 4515 | } catch (RemoteException e) { |
| 4516 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4517 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4518 | e); |
| 4519 | } |
| 4520 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4521 | } |
| 4522 | } |
| 4523 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4524 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4525 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4526 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4527 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4528 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4529 | if (result != null) { |
| 4530 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4531 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4532 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4533 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4534 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4535 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4536 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4537 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4538 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4539 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4540 | if (needUpdate || mAuthDetailsRequired) { |
| 4541 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4542 | if (needUpdate && accountPresent) { |
| 4543 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4544 | } |
| 4545 | if (mAuthDetailsRequired) { |
| 4546 | long lastAuthenticatedTime = -1; |
| 4547 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 4548 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4549 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4550 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4551 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4552 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4553 | lastAuthenticatedTime); |
| 4554 | } |
| 4555 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4556 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4557 | if (result != null |
| 4558 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4559 | checkKeyIntent( |
| 4560 | Binder.getCallingUid(), |
| 4561 | intent); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4562 | } |
| 4563 | if (result != null |
| 4564 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4565 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4566 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4567 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4568 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4569 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4570 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4571 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4572 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4573 | IAccountManagerResponse response; |
| 4574 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4575 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4576 | response = mResponse; |
| 4577 | } else { |
| 4578 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4579 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4580 | if (response != null) { |
| 4581 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4582 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4583 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4584 | Log.v(TAG, getClass().getSimpleName() |
| 4585 | + " calling onError() on response " + response); |
| 4586 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4587 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4588 | "null bundle returned"); |
| 4589 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4590 | if (mStripAuthTokenFromResult) { |
| 4591 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4592 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4593 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4594 | Log.v(TAG, getClass().getSimpleName() |
| 4595 | + " calling onResult() on response " + response); |
| 4596 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4597 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4598 | (intent == null)) { |
| 4599 | // All AccountManager error codes are greater than 0 |
| 4600 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4601 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4602 | } else { |
| 4603 | response.onResult(result); |
| 4604 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4605 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4606 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4607 | // if the caller is dead then there is no one to care about remote exceptions |
| 4608 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4609 | Log.v(TAG, "failure while notifying response", e); |
| 4610 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4611 | } |
| 4612 | } |
| 4613 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4614 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4615 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4616 | public void onRequestContinued() { |
| 4617 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4618 | } |
| 4619 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4620 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4621 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4622 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4623 | IAccountManagerResponse response = getResponseAndClose(); |
| 4624 | if (response != null) { |
| 4625 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4626 | Log.v(TAG, getClass().getSimpleName() |
| 4627 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4628 | } |
| 4629 | try { |
| 4630 | response.onError(errorCode, errorMessage); |
| 4631 | } catch (RemoteException e) { |
| 4632 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4633 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4634 | } |
| 4635 | } |
| 4636 | } else { |
| 4637 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4638 | Log.v(TAG, "Session.onError: already closed"); |
| 4639 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4640 | } |
| 4641 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4642 | |
| 4643 | /** |
| 4644 | * find the component name for the authenticator and initiate a bind |
| 4645 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4646 | */ |
| 4647 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4648 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4649 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4650 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4651 | if (authenticatorInfo == null) { |
| 4652 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4653 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 4654 | + ", bailing out"); |
| 4655 | } |
| 4656 | return false; |
| 4657 | } |
| 4658 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 4659 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 4660 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 4661 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 4662 | + " which isn't encryption aware"); |
| 4663 | return false; |
| 4664 | } |
| 4665 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4666 | Intent intent = new Intent(); |
| 4667 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 4668 | intent.setComponent(authenticatorInfo.componentName); |
| 4669 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4670 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 4671 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 4672 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4673 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4674 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4675 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 4676 | } |
| 4677 | return false; |
| 4678 | } |
| 4679 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4680 | return true; |
| 4681 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4682 | } |
| 4683 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 4684 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4685 | MessageHandler(Looper looper) { |
| 4686 | super(looper); |
| 4687 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4688 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4689 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4690 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4691 | switch (msg.what) { |
| 4692 | case MESSAGE_TIMED_OUT: |
| 4693 | Session session = (Session)msg.obj; |
| 4694 | session.onTimedOut(); |
| 4695 | break; |
| 4696 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4697 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 4698 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4699 | break; |
| 4700 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4701 | default: |
| 4702 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 4703 | } |
| 4704 | } |
| 4705 | } |
| 4706 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4707 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4708 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4709 | } |
| 4710 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4711 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4712 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4715 | /* |
| 4716 | * This function receives an opened writable database. |
| 4717 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4718 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4719 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4720 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4724 | * 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] | 4725 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4726 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4727 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4728 | |
| 4729 | class LogRecordTask implements Runnable { |
| 4730 | private final String action; |
| 4731 | private final String tableName; |
| 4732 | private final long accountId; |
| 4733 | private final UserAccounts userAccount; |
| 4734 | private final int callingUid; |
| 4735 | private final long userDebugDbInsertionPoint; |
| 4736 | |
| 4737 | LogRecordTask(final String action, |
| 4738 | final String tableName, |
| 4739 | final long accountId, |
| 4740 | final UserAccounts userAccount, |
| 4741 | final int callingUid, |
| 4742 | final long userDebugDbInsertionPoint) { |
| 4743 | this.action = action; |
| 4744 | this.tableName = tableName; |
| 4745 | this.accountId = accountId; |
| 4746 | this.userAccount = userAccount; |
| 4747 | this.callingUid = callingUid; |
| 4748 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 4749 | } |
| 4750 | |
| 4751 | public void run() { |
| 4752 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 4753 | logStatement.bindLong(1, accountId); |
| 4754 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4755 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4756 | logStatement.bindLong(4, callingUid); |
| 4757 | logStatement.bindString(5, tableName); |
| 4758 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 4759 | logStatement.execute(); |
| 4760 | logStatement.clearBindings(); |
| 4761 | } |
| 4762 | } |
| 4763 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4764 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 4765 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4766 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 4767 | % AccountsDb.MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4768 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | /* |
| 4772 | * This should only be called once to compile the sql statement for logging |
| 4773 | * and to find the insertion point. |
| 4774 | */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 4775 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(UserAccounts userAccount) { |
| 4776 | userAccount.debugDbInsertionPoint = userAccount.accountsDb |
| 4777 | .calculateDebugTableInsertionPoint(); |
| 4778 | userAccount.statementForLogging = userAccount.accountsDb.compileSqlStatementForLogging(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4779 | } |
| 4780 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4781 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4782 | return asBinder(); |
| 4783 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4784 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4785 | /** |
| 4786 | * Searches array of arguments for the specified string |
| 4787 | * @param args array of argument strings |
| 4788 | * @param value value to search for |
| 4789 | * @return true if the value is contained in the array |
| 4790 | */ |
| 4791 | private static boolean scanArgs(String[] args, String value) { |
| 4792 | if (args != null) { |
| 4793 | for (String arg : args) { |
| 4794 | if (value.equals(arg)) { |
| 4795 | return true; |
| 4796 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4797 | } |
| 4798 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4799 | return false; |
| 4800 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4801 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4802 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4803 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4804 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 4805 | != PackageManager.PERMISSION_GRANTED) { |
| 4806 | fout.println("Permission Denial: can't dump AccountsManager from from pid=" |
| 4807 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 4808 | + " without permission " + android.Manifest.permission.DUMP); |
| 4809 | return; |
| 4810 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4811 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4812 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4813 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4814 | final List<UserInfo> users = getUserManager().getUsers(); |
| 4815 | for (UserInfo user : users) { |
| 4816 | ipw.println("User " + user + ":"); |
| 4817 | ipw.increaseIndent(); |
| 4818 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 4819 | ipw.println(); |
| 4820 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4821 | } |
| 4822 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4823 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4824 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 4825 | String[] args, boolean isCheckinRequest) { |
| 4826 | synchronized (userAccounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4827 | if (isCheckinRequest) { |
| 4828 | // 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^] | 4829 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4830 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4831 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4832 | Process.myUid(), null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4833 | fout.println("Accounts: " + accounts.length); |
| 4834 | for (Account account : accounts) { |
| 4835 | fout.println(" " + account); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4836 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 4837 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4838 | // Add debug information. |
| 4839 | fout.println(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 4840 | userAccounts.accountsDb.dumpDebugTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4841 | fout.println(); |
| 4842 | synchronized (mSessions) { |
| 4843 | final long now = SystemClock.elapsedRealtime(); |
| 4844 | fout.println("Active Sessions: " + mSessions.size()); |
| 4845 | for (Session session : mSessions.values()) { |
| 4846 | fout.println(" " + session.toDebugString(now)); |
| 4847 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4848 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4849 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4850 | fout.println(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4851 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4852 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4853 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4854 | } |
| 4855 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4856 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4857 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4858 | long identityToken = clearCallingIdentity(); |
| 4859 | try { |
| 4860 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4861 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 4862 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4863 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4864 | if (intent.getComponent() != null && |
| 4865 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 4866 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4867 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4868 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4869 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4870 | final Integer notificationId = getSigninRequiredNotificationId(accounts, account); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4871 | intent.addCategory(String.valueOf(notificationId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4872 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4873 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4874 | contextForUser.getText(R.string.notification_title).toString(); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4875 | Notification n = new Notification.Builder(contextForUser) |
| 4876 | .setWhen(0) |
| 4877 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 4878 | .setColor(contextForUser.getColor( |
| 4879 | com.android.internal.R.color.system_notification_accent_color)) |
| 4880 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 4881 | .setContentText(message) |
| 4882 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 4883 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4884 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4885 | .build(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4886 | installNotification(notificationId, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4887 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4888 | } finally { |
| 4889 | restoreCallingIdentity(identityToken); |
| 4890 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4891 | } |
| 4892 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4893 | private void installNotification(int notificationId, final Notification notification, |
| 4894 | String packageName, int userId) { |
| 4895 | final long token = clearCallingIdentity(); |
| 4896 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4897 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4898 | try { |
| 4899 | notificationManager.enqueueNotificationWithTag(packageName, packageName, null, |
| 4900 | notificationId, notification, new int[1], userId); |
| 4901 | } catch (RemoteException e) { |
| 4902 | /* ignore - local call */ |
| 4903 | } |
| 4904 | } finally { |
| 4905 | Binder.restoreCallingIdentity(token); |
| 4906 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4907 | } |
| 4908 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4909 | private void cancelNotification(int id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4910 | cancelNotification(id, mContext.getPackageName(), user); |
| 4911 | } |
| 4912 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4913 | private void cancelNotification(int id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4914 | long identityToken = clearCallingIdentity(); |
| 4915 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4916 | INotificationManager service = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4917 | service.cancelNotificationWithTag(packageName, null, id, user.getIdentifier()); |
| 4918 | } catch (RemoteException e) { |
| 4919 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4920 | } finally { |
| 4921 | restoreCallingIdentity(identityToken); |
| 4922 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4923 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4924 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 4925 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 4926 | for (String perm : permissions) { |
| 4927 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 4928 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4929 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 4930 | } |
| 4931 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 4932 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 4933 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 4934 | return true; |
| 4935 | } |
| 4936 | } |
| 4937 | } |
| 4938 | return false; |
| 4939 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4940 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4941 | private int handleIncomingUser(int userId) { |
| 4942 | try { |
| 4943 | return ActivityManagerNative.getDefault().handleIncomingUser( |
| 4944 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 4945 | } catch (RemoteException re) { |
| 4946 | // Shouldn't happen, local. |
| 4947 | } |
| 4948 | return userId; |
| 4949 | } |
| 4950 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 4951 | private boolean isPrivileged(int callingUid) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4952 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 4953 | |
| 4954 | final PackageManager userPackageManager; |
| 4955 | try { |
| 4956 | userPackageManager = mContext.createPackageContextAsUser( |
| 4957 | "android", 0, new UserHandle(callingUserId)).getPackageManager(); |
| 4958 | } catch (NameNotFoundException e) { |
| 4959 | return false; |
| 4960 | } |
| 4961 | |
| 4962 | String[] packages = userPackageManager.getPackagesForUid(callingUid); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4963 | for (String name : packages) { |
| 4964 | try { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4965 | PackageInfo packageInfo = userPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4966 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 4967 | && (packageInfo.applicationInfo.privateFlags |
| 4968 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4969 | return true; |
| 4970 | } |
| 4971 | } catch (PackageManager.NameNotFoundException e) { |
| 4972 | return false; |
| 4973 | } |
| 4974 | } |
| 4975 | return false; |
| 4976 | } |
| 4977 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4978 | private boolean permissionIsGranted( |
| 4979 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4980 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 4981 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4982 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 4983 | } |
| 4984 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4985 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4986 | |
| 4987 | if (isPrivileged(callerUid)) { |
| 4988 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4989 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 4990 | + callerUid + " privileged"); |
| 4991 | } |
| 4992 | return true; |
| 4993 | } |
| 4994 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 4995 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4996 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 4997 | + callerUid + " manages the account"); |
| 4998 | } |
| 4999 | return true; |
| 5000 | } |
| 5001 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5002 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5003 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5004 | + callerUid + " user granted access"); |
| 5005 | } |
| 5006 | return true; |
| 5007 | } |
| 5008 | |
| 5009 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5010 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5011 | } |
| 5012 | |
| 5013 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5014 | } |
| 5015 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5016 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5017 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5018 | if (accountType == null) { |
| 5019 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5020 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5021 | return getTypesVisibleToCaller(callingUid, userId, |
| 5022 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5023 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5027 | if (accountType == null) { |
| 5028 | return false; |
| 5029 | } else { |
| 5030 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5031 | } |
| 5032 | } |
| 5033 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5034 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5035 | String opPackageName) { |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5036 | boolean isPermitted = |
| 5037 | isPermitted(opPackageName, callingUid, Manifest.permission.GET_ACCOUNTS, |
| 5038 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5039 | return getTypesForCaller(callingUid, userId, isPermitted); |
| 5040 | } |
| 5041 | |
| 5042 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
| 5043 | return getTypesForCaller(callingUid, userId, false); |
| 5044 | } |
| 5045 | |
| 5046 | private List<String> getTypesForCaller( |
| 5047 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5048 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5049 | long identityToken = Binder.clearCallingIdentity(); |
| 5050 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5051 | try { |
| 5052 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5053 | } finally { |
| 5054 | Binder.restoreCallingIdentity(identityToken); |
| 5055 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5056 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5057 | serviceInfos) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5058 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 5059 | if (isOtherwisePermitted || sigChk == PackageManager.SIGNATURE_MATCH) { |
| 5060 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5061 | } |
| 5062 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5063 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5064 | } |
| 5065 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5066 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5067 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5068 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5069 | if (account.name.equals(accountName)) { |
| 5070 | return true; |
| 5071 | } |
| 5072 | } |
| 5073 | } |
| 5074 | return false; |
| 5075 | } |
| 5076 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5077 | private static void checkManageUsersPermission(String message) { |
| 5078 | if (ActivityManager.checkComponentPermission( |
| 5079 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5080 | != PackageManager.PERMISSION_GRANTED) { |
| 5081 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5082 | } |
| 5083 | } |
| 5084 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5085 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5086 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5087 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5088 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5089 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5090 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5091 | + message); |
| 5092 | } |
| 5093 | } |
| 5094 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5095 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5096 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5097 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5098 | return true; |
| 5099 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5100 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5101 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5102 | long grantsCount; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5103 | if (authTokenType != null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5104 | grantsCount = accounts.accountsDb.findMatchingGrantsCount(callerUid, authTokenType, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5105 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5106 | } else { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5107 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5108 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5109 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5110 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5111 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5112 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5113 | // TODO: Skip this check when running automated tests. Replace this |
| 5114 | // with a more general solution. |
| 5115 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5116 | + authTokenType + " by uid " + callerUid |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5117 | + " but ignoring since device is in test harness."); |
| 5118 | return true; |
| 5119 | } |
| 5120 | return permissionGranted; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5121 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5122 | } |
| 5123 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5124 | private boolean isSystemUid(int callingUid) { |
| 5125 | String[] packages = null; |
| 5126 | long ident = Binder.clearCallingIdentity(); |
| 5127 | try { |
| 5128 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5129 | } finally { |
| 5130 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5131 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5132 | if (packages != null) { |
| 5133 | for (String name : packages) { |
| 5134 | try { |
| 5135 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5136 | if (packageInfo != null |
| 5137 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5138 | != 0) { |
| 5139 | return true; |
| 5140 | } |
| 5141 | } catch (PackageManager.NameNotFoundException e) { |
| 5142 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5143 | } |
| 5144 | } |
| 5145 | } else { |
| 5146 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5147 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5148 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5149 | } |
| 5150 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5151 | /** Succeeds if any of the specified permissions are granted. */ |
| 5152 | private void checkReadAccountsPermitted( |
| 5153 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5154 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5155 | int userId, |
| 5156 | String opPackageName) { |
| 5157 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5158 | String msg = String.format( |
| 5159 | "caller uid %s cannot access %s accounts", |
| 5160 | callingUid, |
| 5161 | accountType); |
| 5162 | Log.w(TAG, " " + msg); |
| 5163 | throw new SecurityException(msg); |
| 5164 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5165 | } |
| 5166 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5167 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5168 | // the managing app can always modify accounts |
| 5169 | if (isProfileOwner(callingUid)) { |
| 5170 | return true; |
| 5171 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5172 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5173 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5174 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5175 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5176 | return true; |
| 5177 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5178 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5179 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5180 | // the managing app can always modify accounts |
| 5181 | if (isProfileOwner(callingUid)) { |
| 5182 | return true; |
| 5183 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5184 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5185 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5186 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5187 | if (typesArray == null) { |
| 5188 | return true; |
| 5189 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5190 | for (String forbiddenType : typesArray) { |
| 5191 | if (forbiddenType.equals(accountType)) { |
| 5192 | return false; |
| 5193 | } |
| 5194 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5195 | return true; |
| 5196 | } |
| 5197 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5198 | private boolean isProfileOwner(int uid) { |
| 5199 | final DevicePolicyManagerInternal dpmi = |
| 5200 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5201 | return (dpmi != null) |
| 5202 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5203 | } |
| 5204 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5205 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5206 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5207 | throws RemoteException { |
| 5208 | final int callingUid = getCallingUid(); |
| 5209 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5210 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5211 | throw new SecurityException(); |
| 5212 | } |
| 5213 | |
| 5214 | if (value) { |
| 5215 | grantAppPermission(account, authTokenType, uid); |
| 5216 | } else { |
| 5217 | revokeAppPermission(account, authTokenType, uid); |
| 5218 | } |
| 5219 | } |
| 5220 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5221 | /** |
| 5222 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5223 | * <p> |
| 5224 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5225 | * which is in the system. This means we don't need to protect it with permissions. |
| 5226 | * @hide |
| 5227 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5228 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5229 | if (account == null || authTokenType == null) { |
| 5230 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5231 | return; |
| 5232 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5233 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5234 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5235 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5236 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5237 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5238 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5239 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5240 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5241 | |
| 5242 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5243 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5244 | |
| 5245 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5246 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5247 | : mAppPermissionChangeListeners) { |
| 5248 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5249 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | /** |
| 5253 | * Don't allow callers with the given uid permission to get credentials for |
| 5254 | * account/authTokenType. |
| 5255 | * <p> |
| 5256 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5257 | * which is in the system. This means we don't need to protect it with permissions. |
| 5258 | * @hide |
| 5259 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5260 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5261 | if (account == null || authTokenType == null) { |
| 5262 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5263 | return; |
| 5264 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5265 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5266 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5267 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5268 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5269 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5270 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5271 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5272 | accountId, authTokenType, uid); |
| 5273 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5274 | } |
| 5275 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5276 | accounts.accountsDb.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5277 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5278 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5279 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5280 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5281 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5282 | |
| 5283 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5284 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5285 | : mAppPermissionChangeListeners) { |
| 5286 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5287 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5288 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5289 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5290 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5291 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5292 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5293 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5294 | for (Account curAccount : oldAccountsForType) { |
| 5295 | if (!curAccount.equals(account)) { |
| 5296 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5297 | } |
| 5298 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5299 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5300 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5301 | } else { |
| 5302 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5303 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5304 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5305 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5306 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5307 | accounts.userDataCache.remove(account); |
| 5308 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5309 | accounts.previousNameCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | /** |
| 5313 | * 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] | 5314 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5315 | * processes and if you will return this account to apps you should return the result. |
| 5316 | * @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] | 5317 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5318 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5319 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5320 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5321 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5322 | if (accountsForType != null) { |
| 5323 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5324 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5325 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5326 | : UUID.randomUUID().toString(); |
| 5327 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5328 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5329 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5330 | } |
| 5331 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5332 | private Account[] filterSharedAccounts(UserAccounts userAccounts, Account[] unfiltered, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5333 | int callingUid, String callingPackage) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5334 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5335 | || callingUid == Process.myUid()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5336 | return unfiltered; |
| 5337 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5338 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5339 | if (user != null && user.isRestricted()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5340 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5341 | // 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] | 5342 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5343 | // This might be a temporary way to specify a visible list |
| 5344 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5345 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5346 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5347 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5348 | return unfiltered; |
| 5349 | } |
| 5350 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5351 | ArrayList<Account> allowed = new ArrayList<>(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5352 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
| 5353 | if (sharedAccounts == null || sharedAccounts.length == 0) return unfiltered; |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5354 | String requiredAccountType = ""; |
| 5355 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5356 | // If there's an explicit callingPackage specified, check if that package |
| 5357 | // opted in to see restricted accounts. |
| 5358 | if (callingPackage != null) { |
| 5359 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5360 | if (pi != null && pi.restrictedAccountType != null) { |
| 5361 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5362 | } |
| 5363 | } else { |
| 5364 | // Otherwise check if the callingUid has a package that has opted in |
| 5365 | for (String packageName : packages) { |
| 5366 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5367 | if (pi != null && pi.restrictedAccountType != null) { |
| 5368 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5369 | break; |
| 5370 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5371 | } |
| 5372 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5373 | } catch (NameNotFoundException nnfe) { |
| 5374 | } |
| 5375 | for (Account account : unfiltered) { |
| 5376 | if (account.type.equals(requiredAccountType)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5377 | allowed.add(account); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5378 | } else { |
| 5379 | boolean found = false; |
| 5380 | for (Account shared : sharedAccounts) { |
| 5381 | if (shared.equals(account)) { |
| 5382 | found = true; |
| 5383 | break; |
| 5384 | } |
| 5385 | } |
| 5386 | if (!found) { |
| 5387 | allowed.add(account); |
| 5388 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5389 | } |
| 5390 | } |
| 5391 | Account[] filtered = new Account[allowed.size()]; |
| 5392 | allowed.toArray(filtered); |
| 5393 | return filtered; |
| 5394 | } else { |
| 5395 | return unfiltered; |
| 5396 | } |
| 5397 | } |
| 5398 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5399 | /* |
| 5400 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5401 | * that the package is not allowed to access. |
| 5402 | */ |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5403 | protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5404 | int callingUid, String callingPackage) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5405 | if (accountType != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5406 | final Account[] accounts = userAccounts.accountCache.get(accountType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5407 | if (accounts == null) { |
| 5408 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5409 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5410 | return filterSharedAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5411 | callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5412 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5413 | } else { |
| 5414 | int totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5415 | for (Account[] accounts : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5416 | totalLength += accounts.length; |
| 5417 | } |
| 5418 | if (totalLength == 0) { |
| 5419 | return EMPTY_ACCOUNT_ARRAY; |
| 5420 | } |
| 5421 | Account[] accounts = new Account[totalLength]; |
| 5422 | totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5423 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5424 | System.arraycopy(accountsOfType, 0, accounts, totalLength, |
| 5425 | accountsOfType.length); |
| 5426 | totalLength += accountsOfType.length; |
| 5427 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5428 | return filterSharedAccounts(userAccounts, accounts, callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5429 | } |
| 5430 | } |
| 5431 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5432 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5433 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5434 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5435 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5436 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5437 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5438 | } |
| 5439 | if (value == null) { |
| 5440 | userDataForAccount.remove(key); |
| 5441 | } else { |
| 5442 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5443 | } |
| 5444 | } |
| 5445 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5446 | protected String readCachedTokenInternal( |
| 5447 | UserAccounts accounts, |
| 5448 | Account account, |
| 5449 | String tokenType, |
| 5450 | String callingPackage, |
| 5451 | byte[] pkgSigDigest) { |
| 5452 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5453 | return accounts.accountTokenCaches.get( |
| 5454 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5455 | } |
| 5456 | } |
| 5457 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5458 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5459 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5460 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5461 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5462 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5463 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5464 | } |
| 5465 | if (value == null) { |
| 5466 | authTokensForAccount.remove(key); |
| 5467 | } else { |
| 5468 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5469 | } |
| 5470 | } |
| 5471 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5472 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 5473 | String authTokenType) { |
| 5474 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5475 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5476 | if (authTokensForAccount == null) { |
| 5477 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5478 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5479 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5480 | } |
| 5481 | return authTokensForAccount.get(authTokenType); |
| 5482 | } |
| 5483 | } |
| 5484 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5485 | protected String readUserDataInternalLocked( |
| 5486 | UserAccounts accounts, Account account, String key) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5487 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5488 | if (userDataForAccount == null) { |
| 5489 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame^] | 5490 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5491 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5492 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5493 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5494 | } |
| 5495 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5496 | private Context getContextForUser(UserHandle user) { |
| 5497 | try { |
| 5498 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 5499 | } catch (NameNotFoundException e) { |
| 5500 | // Default to mContext, not finding the package system is running as is unlikely. |
| 5501 | return mContext; |
| 5502 | } |
| 5503 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 5504 | |
| 5505 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 5506 | try { |
| 5507 | response.onResult(result); |
| 5508 | } catch (RemoteException e) { |
| 5509 | // if the caller is dead then there is no one to care about remote |
| 5510 | // exceptions |
| 5511 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5512 | Log.v(TAG, "failure while notifying response", e); |
| 5513 | } |
| 5514 | } |
| 5515 | } |
| 5516 | |
| 5517 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 5518 | String errorMessage) { |
| 5519 | try { |
| 5520 | response.onError(errorCode, errorMessage); |
| 5521 | } catch (RemoteException e) { |
| 5522 | // if the caller is dead then there is no one to care about remote |
| 5523 | // exceptions |
| 5524 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5525 | Log.v(TAG, "failure while notifying response", e); |
| 5526 | } |
| 5527 | } |
| 5528 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5529 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5530 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5531 | private final Object mLock = new Object(); |
| 5532 | |
| 5533 | @GuardedBy("mLock") |
| 5534 | private AccountManagerBackupHelper mBackupHelper; |
| 5535 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5536 | @Override |
| 5537 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 5538 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 5539 | if (account == null) { |
| 5540 | Slog.w(TAG, "account cannot be null"); |
| 5541 | return; |
| 5542 | } |
| 5543 | if (packageName == null) { |
| 5544 | Slog.w(TAG, "packageName cannot be null"); |
| 5545 | return; |
| 5546 | } |
| 5547 | if (userId < UserHandle.USER_SYSTEM) { |
| 5548 | Slog.w(TAG, "user id must be concrete"); |
| 5549 | return; |
| 5550 | } |
| 5551 | if (callback == null) { |
| 5552 | Slog.w(TAG, "callback cannot be null"); |
| 5553 | return; |
| 5554 | } |
| 5555 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5556 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 5557 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5558 | Bundle result = new Bundle(); |
| 5559 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 5560 | callback.sendResult(result); |
| 5561 | return; |
| 5562 | } |
| 5563 | |
| 5564 | final int uid; |
| 5565 | try { |
| 5566 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 5567 | } catch (NameNotFoundException e) { |
| 5568 | Slog.e(TAG, "Unknown package " + packageName); |
| 5569 | return; |
| 5570 | } |
| 5571 | |
| 5572 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5573 | final UserAccounts userAccounts; |
| 5574 | synchronized (mUsers) { |
| 5575 | userAccounts = mUsers.get(userId); |
| 5576 | } |
| 5577 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 5578 | } |
| 5579 | |
| 5580 | @Override |
| 5581 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 5582 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5583 | mAppPermissionChangeListeners.add(listener); |
| 5584 | } |
| 5585 | |
| 5586 | @Override |
| 5587 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 5588 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5589 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5590 | |
| 5591 | @Override |
| 5592 | public byte[] backupAccountAccessPermissions(int userId) { |
| 5593 | synchronized (mLock) { |
| 5594 | if (mBackupHelper == null) { |
| 5595 | mBackupHelper = new AccountManagerBackupHelper( |
| 5596 | AccountManagerService.this, this); |
| 5597 | } |
| 5598 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 5599 | } |
| 5600 | } |
| 5601 | |
| 5602 | @Override |
| 5603 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 5604 | synchronized (mLock) { |
| 5605 | if (mBackupHelper == null) { |
| 5606 | mBackupHelper = new AccountManagerBackupHelper( |
| 5607 | AccountManagerService.this, this); |
| 5608 | } |
| 5609 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 5610 | } |
| 5611 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5612 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5613 | |
| 5614 | @VisibleForTesting |
| 5615 | static class Injector { |
| 5616 | private final Context mContext; |
| 5617 | |
| 5618 | public Injector(Context context) { |
| 5619 | mContext = context; |
| 5620 | } |
| 5621 | |
| 5622 | Looper getMessageHandlerLooper() { |
| 5623 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 5624 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 5625 | serviceThread.start(); |
| 5626 | return serviceThread.getLooper(); |
| 5627 | } |
| 5628 | |
| 5629 | Context getContext() { |
| 5630 | return mContext; |
| 5631 | } |
| 5632 | |
| 5633 | void addLocalService(AccountManagerInternal service) { |
| 5634 | LocalServices.addService(AccountManagerInternal.class, service); |
| 5635 | } |
| 5636 | |
| 5637 | String getDeDatabaseName(int userId) { |
| 5638 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 5639 | AccountsDb.DE_DATABASE_NAME); |
| 5640 | return databaseFile.getPath(); |
| 5641 | } |
| 5642 | |
| 5643 | String getCeDatabaseName(int userId) { |
| 5644 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 5645 | AccountsDb.CE_DATABASE_NAME); |
| 5646 | return databaseFile.getPath(); |
| 5647 | } |
| 5648 | |
| 5649 | String getPreNDatabaseName(int userId) { |
| 5650 | File systemDir = Environment.getDataSystemDirectory(); |
| 5651 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 5652 | PRE_N_DATABASE_NAME); |
| 5653 | if (userId == 0) { |
| 5654 | // Migrate old file, if it exists, to the new location. |
| 5655 | // Make sure the new file doesn't already exist. A dummy file could have been |
| 5656 | // accidentally created in the old location, causing the new one to become corrupted |
| 5657 | // as well. |
| 5658 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 5659 | if (oldFile.exists() && !databaseFile.exists()) { |
| 5660 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 5661 | File userDir = Environment.getUserSystemDirectory(userId); |
| 5662 | if (!userDir.exists()) { |
| 5663 | if (!userDir.mkdirs()) { |
| 5664 | throw new IllegalStateException("User dir cannot be created: " + userDir); |
| 5665 | } |
| 5666 | } |
| 5667 | if (!oldFile.renameTo(databaseFile)) { |
| 5668 | throw new IllegalStateException("User dir cannot be migrated: " + databaseFile); |
| 5669 | } |
| 5670 | } |
| 5671 | } |
| 5672 | return databaseFile.getPath(); |
| 5673 | } |
| 5674 | |
| 5675 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 5676 | return new AccountAuthenticatorCache(mContext); |
| 5677 | } |
| 5678 | |
| 5679 | INotificationManager getNotificationManager() { |
| 5680 | return NotificationManager.getService(); |
| 5681 | } |
| 5682 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5683 | } |