Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.accounts; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 18 | |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 20 | import android.accounts.AbstractAccountAuthenticator; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 21 | import android.accounts.Account; |
| 22 | import android.accounts.AccountAndUser; |
| 23 | import android.accounts.AccountAuthenticatorResponse; |
| 24 | import android.accounts.AccountManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 25 | import android.accounts.AccountManagerInternal; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 26 | import android.accounts.AuthenticatorDescription; |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 27 | import android.accounts.CantAddAccountActivity; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 28 | import android.accounts.GrantCredentialsPermissionActivity; |
| 29 | import android.accounts.IAccountAuthenticator; |
| 30 | import android.accounts.IAccountAuthenticatorResponse; |
| 31 | import android.accounts.IAccountManager; |
| 32 | import android.accounts.IAccountManagerResponse; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 33 | import android.annotation.IntRange; |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 34 | import android.annotation.NonNull; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 35 | import android.annotation.Nullable; |
Brett Chabot | 3b4fcbc | 2011-01-09 13:41:02 -0800 | [diff] [blame] | 36 | import android.app.ActivityManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 37 | import android.app.ActivityThread; |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 38 | import android.app.AppGlobals; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 39 | import android.app.AppOpsManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 40 | import android.app.INotificationManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 41 | import android.app.Notification; |
| 42 | import android.app.NotificationManager; |
| 43 | import android.app.PendingIntent; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 44 | import android.app.admin.DeviceAdminInfo; |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 45 | import android.app.admin.DevicePolicyManager; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 46 | import android.app.admin.DevicePolicyManagerInternal; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 47 | import android.content.BroadcastReceiver; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 48 | import android.content.ComponentName; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 49 | import android.content.Context; |
| 50 | import android.content.Intent; |
| 51 | import android.content.IntentFilter; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 52 | import android.content.IntentSender; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 53 | import android.content.ServiceConnection; |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 54 | import android.content.pm.ActivityInfo; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 55 | import android.content.pm.ApplicationInfo; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 56 | import android.content.pm.IPackageManager; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 57 | import android.content.pm.PackageInfo; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 58 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 59 | import android.content.pm.PackageManager.NameNotFoundException; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 60 | import android.content.pm.RegisteredServicesCache; |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 61 | import android.content.pm.RegisteredServicesCacheListener; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 62 | import android.content.pm.ResolveInfo; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 63 | import android.content.pm.Signature; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 64 | import android.content.pm.UserInfo; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 65 | import android.database.Cursor; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 66 | import android.database.sqlite.SQLiteDatabase; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 67 | import android.database.sqlite.SQLiteStatement; |
Doug Zongker | 885cfc23 | 2009-10-21 16:52:44 -0700 | [diff] [blame] | 68 | import android.os.Binder; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 69 | import android.os.Bundle; |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 70 | import android.os.Environment; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 71 | import android.os.Handler; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 72 | import android.os.IBinder; |
| 73 | import android.os.Looper; |
| 74 | import android.os.Message; |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 75 | import android.os.Parcel; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 76 | import android.os.Process; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 77 | import android.os.RemoteCallback; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 78 | import android.os.RemoteException; |
| 79 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 80 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 81 | import android.os.UserManager; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 82 | import android.os.storage.StorageManager; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 83 | import android.text.TextUtils; |
| 84 | import android.util.Log; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 85 | import android.util.Pair; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 86 | import android.util.Slog; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 87 | import android.util.SparseArray; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 88 | import android.util.SparseBooleanArray; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 89 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 90 | import com.android.internal.R; |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 91 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 92 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 93 | import com.android.internal.content.PackageMonitor; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 94 | import com.android.internal.util.ArrayUtils; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 95 | import com.android.internal.util.IndentingPrintWriter; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 96 | import com.android.internal.util.Preconditions; |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 97 | import com.android.server.LocalServices; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 98 | import com.android.server.ServiceThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 99 | import com.android.server.SystemService; |
| 100 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 101 | import com.google.android.collect.Lists; |
| 102 | import com.google.android.collect.Sets; |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 103 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 104 | import java.io.File; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 105 | import java.io.FileDescriptor; |
| 106 | import java.io.PrintWriter; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 107 | import java.security.GeneralSecurityException; |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 108 | import java.security.MessageDigest; |
| 109 | import java.security.NoSuchAlgorithmException; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 110 | import java.text.SimpleDateFormat; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 111 | import java.util.ArrayList; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 112 | import java.util.Arrays; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 113 | import java.util.Collection; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 114 | import java.util.Date; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 115 | import java.util.HashMap; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 116 | import java.util.HashSet; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 117 | import java.util.LinkedHashMap; |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 118 | import java.util.List; |
Andy McFadden | 2f36229 | 2012-01-20 14:43:38 -0800 | [diff] [blame] | 119 | import java.util.Map; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 120 | import java.util.Map.Entry; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 121 | import java.util.Objects; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 122 | import java.util.Set; |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 123 | import java.util.UUID; |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 124 | import java.util.concurrent.CopyOnWriteArrayList; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 125 | import java.util.concurrent.atomic.AtomicInteger; |
| 126 | import java.util.concurrent.atomic.AtomicReference; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 127 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 128 | /** |
| 129 | * A system service that provides account, password, and authtoken management for all |
| 130 | * accounts on the device. Some of these calls are implemented with the help of the corresponding |
| 131 | * {@link IAccountAuthenticator} services. This service is not accessed by users directly, |
| 132 | * instead one uses an instance of {@link AccountManager}, which can be accessed as follows: |
Brian Carlstrom | 46703b0 | 2011-04-06 15:41:29 -0700 | [diff] [blame] | 133 | * AccountManager accountManager = AccountManager.get(context); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 134 | * @hide |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 135 | */ |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 136 | public class AccountManagerService |
| 137 | extends IAccountManager.Stub |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 138 | implements RegisteredServicesCacheListener<AuthenticatorDescription> { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 139 | private static final String TAG = "AccountManagerService"; |
| 140 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 141 | public static class Lifecycle extends SystemService { |
| 142 | private AccountManagerService mService; |
| 143 | |
| 144 | public Lifecycle(Context context) { |
| 145 | super(context); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onStart() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 150 | mService = new AccountManagerService(new Injector(getContext())); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 151 | publishBinderService(Context.ACCOUNT_SERVICE, mService); |
| 152 | } |
| 153 | |
| 154 | @Override |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 155 | public void onUnlockUser(int userHandle) { |
| 156 | mService.onUnlockUser(userHandle); |
| 157 | } |
| 158 | } |
| 159 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 160 | final Context mContext; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 161 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 162 | private final PackageManager mPackageManager; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 163 | private final AppOpsManager mAppOpsManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 164 | private UserManager mUserManager; |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 165 | private final Injector mInjector; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 166 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 167 | final MessageHandler mHandler; |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 168 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 169 | // Messages that can be sent on mHandler |
| 170 | private static final int MESSAGE_TIMED_OUT = 3; |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 171 | private static final int MESSAGE_COPY_SHARED_ACCOUNT = 4; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 172 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 173 | private final IAccountAuthenticatorCache mAuthenticatorCache; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 174 | private static final String PRE_N_DATABASE_NAME = "accounts.db"; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 175 | private static final Intent ACCOUNTS_CHANGED_INTENT; |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 176 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 177 | static { |
| 178 | ACCOUNTS_CHANGED_INTENT = new Intent(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION); |
| 179 | ACCOUNTS_CHANGED_INTENT.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 180 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 181 | |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 182 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 183 | private final LinkedHashMap<String, Session> mSessions = new LinkedHashMap<String, Session>(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 184 | private final AtomicInteger mNotificationIds = new AtomicInteger(1); |
| 185 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 186 | private static final String NEW_ACCOUNT_VISIBLE = "android.accounts.NEW_ACCOUNT_VISIBLE"; |
| 187 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 188 | static class UserAccounts { |
| 189 | private final int userId; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 190 | final AccountsDb accountsDb; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 191 | private final HashMap<Pair<Pair<Account, String>, Integer>, Integer> |
| 192 | credentialsPermissionNotificationIds = |
| 193 | new HashMap<Pair<Pair<Account, String>, Integer>, Integer>(); |
| 194 | private final HashMap<Account, Integer> signinRequiredNotificationIds = |
| 195 | new HashMap<Account, Integer>(); |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 196 | final Object cacheLock = new Object(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 197 | /** protected by the {@link #cacheLock} */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 198 | final HashMap<String, Account[]> accountCache = |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 199 | new LinkedHashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 200 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 201 | private final Map<Account, Map<String, String>> userDataCache = new HashMap<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 202 | /** protected by the {@link #cacheLock} */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 203 | private final Map<Account, Map<String, String>> authTokenCache = new HashMap<>(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 204 | |
| 205 | /** protected by the {@link #cacheLock} */ |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 206 | private final TokenCache accountTokenCaches = new TokenCache(); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 207 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 208 | /** protected by the {@link #cacheLock} */ |
| 209 | private final Map<String, ArrayList<Integer>> mApplicationAccountRequestMappings = |
| 210 | new HashMap<>(); |
| 211 | |
| 212 | /* Together the below two Sparse Arrays serve as visible list. One maps UID to account |
| 213 | number. Another maps Account number to Account.*/ |
| 214 | |
| 215 | /** protected by the {@link #cacheLock} */ |
| 216 | private final SparseArray<ArrayList<Integer>> mVisibleListUidToMockAccountNumbers = |
| 217 | new SparseArray<>(); |
| 218 | |
| 219 | //TODO: Instead of using Mock Account IDs, use the actual account IDs. |
| 220 | /** protected by the {@link #cacheLock} */ |
| 221 | private final SparseArray<Account> mMockAccountIdToAccount = new SparseArray<>(); |
| 222 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 223 | /** |
| 224 | * protected by the {@link #cacheLock} |
| 225 | * |
| 226 | * Caches the previous names associated with an account. Previous names |
| 227 | * should be cached because we expect that when an Account is renamed, |
| 228 | * many clients will receive a LOGIN_ACCOUNTS_CHANGED broadcast and |
| 229 | * want to know if the accounts they care about have been renamed. |
| 230 | * |
| 231 | * The previous names are wrapped in an {@link AtomicReference} so that |
| 232 | * we can distinguish between those accounts with no previous names and |
| 233 | * those whose previous names haven't been cached (yet). |
| 234 | */ |
| 235 | private final HashMap<Account, AtomicReference<String>> previousNameCache = |
| 236 | new HashMap<Account, AtomicReference<String>>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 237 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 238 | private int debugDbInsertionPoint = -1; |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 239 | private SQLiteStatement statementForLogging; // TODO Move to AccountsDb |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 240 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 241 | UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 242 | this.userId = userId; |
| 243 | synchronized (cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 244 | accountsDb = AccountsDb.create(context, userId, preNDbFile, deDbFile); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 249 | private final SparseArray<UserAccounts> mUsers = new SparseArray<>(); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 250 | private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 251 | // Not thread-safe. Only use in synchronized context |
| 252 | private final SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 253 | private CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener> |
| 254 | mAppPermissionChangeListeners = new CopyOnWriteArrayList<>(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 255 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 256 | private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>(); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 257 | private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{}; |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 258 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 259 | /** |
| 260 | * This should only be called by system code. One should only call this after the service |
| 261 | * has started. |
| 262 | * @return a reference to the AccountManagerService instance |
| 263 | * @hide |
| 264 | */ |
| 265 | public static AccountManagerService getSingleton() { |
| 266 | return sThis.get(); |
| 267 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 268 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 269 | public AccountManagerService(Injector injector) { |
| 270 | mInjector = injector; |
| 271 | mContext = injector.getContext(); |
| 272 | mPackageManager = mContext.getPackageManager(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 273 | mAppOpsManager = mContext.getSystemService(AppOpsManager.class); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 274 | mHandler = new MessageHandler(injector.getMessageHandlerLooper()); |
| 275 | mAuthenticatorCache = mInjector.getAccountAuthenticatorCache(); |
Fred Quintana | 5ebbb4a | 2009-11-09 15:42:20 -0800 | [diff] [blame] | 276 | mAuthenticatorCache.setListener(this, null /* Handler */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 277 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 278 | sThis.set(this); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 279 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 280 | addRequestsForPreInstalledApplications(); |
| 281 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 282 | IntentFilter intentFilter = new IntentFilter(); |
| 283 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 284 | intentFilter.addDataScheme("package"); |
| 285 | mContext.registerReceiver(new BroadcastReceiver() { |
| 286 | @Override |
| 287 | public void onReceive(Context context1, Intent intent) { |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 288 | // Don't delete accounts when updating a authenticator's |
| 289 | // package. |
| 290 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 291 | /* Purging data requires file io, don't block the main thread. This is probably |
| 292 | * less than ideal because we are introducing a race condition where old grants |
| 293 | * could be exercised until they are purged. But that race condition existed |
| 294 | * anyway with the broadcast receiver. |
| 295 | * |
| 296 | * Ideally, we would completely clear the cache, purge data from the database, |
| 297 | * and then rebuild the cache. All under the cache lock. But that change is too |
| 298 | * large at this point. |
| 299 | */ |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 300 | Runnable purgingRunnable = new Runnable() { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 301 | @Override |
| 302 | public void run() { |
| 303 | purgeOldGrantsAll(); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 304 | |
| 305 | /* clears application request's for account types supported */ |
| 306 | int uidOfUninstalledApplication = |
| 307 | intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 308 | if(uidOfUninstalledApplication != -1) { |
| 309 | clearRequestedAccountVisibility(uidOfUninstalledApplication, |
| 310 | getUserAccounts(UserHandle.getUserId( |
| 311 | uidOfUninstalledApplication))); |
| 312 | } |
| 313 | |
| 314 | /* removes visibility of previous UID of this uninstalled application*/ |
| 315 | removeAccountVisibilityAllAccounts(uidOfUninstalledApplication, |
| 316 | getUserAccounts(UserHandle.getUserId( |
| 317 | uidOfUninstalledApplication))); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 318 | } |
| 319 | }; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 320 | mHandler.post(purgingRunnable); |
Carlos Valdivia | 23f5826 | 2014-09-05 10:52:41 -0700 | [diff] [blame] | 321 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 322 | |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 323 | } |
| 324 | }, intentFilter); |
Fred Quintana | c1a4e5d | 2011-02-25 10:44:38 -0800 | [diff] [blame] | 325 | |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 326 | IntentFilter packageAddedOrChangedFilter = new IntentFilter(); |
| 327 | intentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 328 | packageAddedOrChangedFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 329 | packageAddedOrChangedFilter.addDataScheme("package"); |
| 330 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
| 331 | @Override |
| 332 | public void onReceive(Context context1, Intent intent) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 333 | mHandler.post(new Runnable() { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 334 | @Override |
| 335 | public void run() { |
| 336 | int uidOfInstalledApplication = |
| 337 | intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 338 | if(uidOfInstalledApplication != -1) { |
| 339 | registerAccountTypesSupported( |
| 340 | uidOfInstalledApplication, |
| 341 | getUserAccounts( |
| 342 | UserHandle.getUserId(uidOfInstalledApplication))); |
| 343 | } |
| 344 | } |
| 345 | }); |
| 346 | } |
| 347 | }, UserHandle.ALL, packageAddedOrChangedFilter, null, null); |
| 348 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 349 | IntentFilter userFilter = new IntentFilter(); |
| 350 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 351 | mContext.registerReceiverAsUser(new BroadcastReceiver() { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 352 | @Override |
| 353 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 354 | String action = intent.getAction(); |
| 355 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 356 | onUserRemoved(intent); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 357 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 358 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 359 | }, UserHandle.ALL, userFilter, null, null); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 360 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 361 | injector.addLocalService(new AccountManagerInternalImpl()); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 362 | |
| 363 | // Need to cancel account request notifications if the update/install can access the account |
| 364 | new PackageMonitor() { |
| 365 | @Override |
| 366 | public void onPackageAdded(String packageName, int uid) { |
| 367 | // Called on a handler, and running as the system |
| 368 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 373 | // Called on a handler, and running as the system |
| 374 | cancelAccountAccessRequestNotificationIfNeeded(uid, true); |
| 375 | } |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 376 | }.register(mContext, mHandler.getLooper(), UserHandle.ALL, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 377 | |
| 378 | // Cancel account request notification if an app op was preventing the account access |
| 379 | mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null, |
| 380 | new AppOpsManager.OnOpChangedInternalListener() { |
| 381 | @Override |
| 382 | public void onOpChanged(int op, String packageName) { |
| 383 | try { |
| 384 | final int userId = ActivityManager.getCurrentUser(); |
| 385 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 386 | final int mode = mAppOpsManager.checkOpNoThrow( |
| 387 | AppOpsManager.OP_GET_ACCOUNTS, uid, packageName); |
| 388 | if (mode == AppOpsManager.MODE_ALLOWED) { |
| 389 | final long identity = Binder.clearCallingIdentity(); |
| 390 | try { |
| 391 | cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true); |
| 392 | } finally { |
| 393 | Binder.restoreCallingIdentity(identity); |
| 394 | } |
| 395 | } |
| 396 | } catch (NameNotFoundException e) { |
| 397 | /* ignore */ |
| 398 | } |
| 399 | } |
| 400 | }); |
| 401 | |
| 402 | // Cancel account request notification if a permission was preventing the account access |
| 403 | mPackageManager.addOnPermissionsChangeListener( |
| 404 | (int uid) -> { |
| 405 | Account[] accounts = null; |
| 406 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 407 | if (packageNames != null) { |
| 408 | final int userId = UserHandle.getUserId(uid); |
| 409 | final long identity = Binder.clearCallingIdentity(); |
| 410 | try { |
| 411 | for (String packageName : packageNames) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 412 | if (mPackageManager.checkPermission( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 413 | Manifest.permission.GET_ACCOUNTS, packageName) |
| 414 | != PackageManager.PERMISSION_GRANTED) { |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | if (accounts == null) { |
| 419 | accounts = getAccountsAsUser(null, userId, "android"); |
| 420 | if (ArrayUtils.isEmpty(accounts)) { |
| 421 | return; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | for (Account account : accounts) { |
| 426 | cancelAccountAccessRequestNotificationIfNeeded( |
| 427 | account, uid, packageName, true); |
| 428 | } |
| 429 | } |
| 430 | } finally { |
| 431 | Binder.restoreCallingIdentity(identity); |
| 432 | } |
| 433 | } |
| 434 | }); |
| 435 | } |
| 436 | |
| 437 | private void cancelAccountAccessRequestNotificationIfNeeded(int uid, |
| 438 | boolean checkAccess) { |
| 439 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 440 | for (Account account : accounts) { |
| 441 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid, |
| 446 | boolean checkAccess) { |
| 447 | Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android"); |
| 448 | for (Account account : accounts) { |
| 449 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid, |
| 454 | boolean checkAccess) { |
| 455 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 456 | if (packageNames != null) { |
| 457 | for (String packageName : packageNames) { |
| 458 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, |
| 459 | packageName, checkAccess); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | private void cancelAccountAccessRequestNotificationIfNeeded(Account account, |
| 465 | int uid, String packageName, boolean checkAccess) { |
| 466 | if (!checkAccess || hasAccountAccess(account, packageName, |
| 467 | UserHandle.getUserHandleForUid(uid))) { |
| 468 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 469 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 470 | UserHandle.getUserHandleForUid(uid)); |
| 471 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 474 | @Override |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 475 | public boolean addAccountExplicitlyWithUid(Account account, String password, Bundle extras, |
| 476 | int[] selectedUids) { |
| 477 | if(addAccountExplicitly(account,password,extras)) { |
| 478 | for(int thisUid : selectedUids) { |
| 479 | makeAccountVisible(account, thisUid); |
| 480 | } |
| 481 | return true; |
| 482 | } |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public int[] getRequestingUidsForType(String accountType) { |
| 488 | int callingUid = Binder.getCallingUid(); |
| 489 | if (!isAccountManagedByCaller(accountType, callingUid, UserHandle.getUserId(callingUid))) { |
| 490 | String msg = String.format( |
| 491 | "uid %s cannot get secrets for accounts of type: %s", |
| 492 | callingUid, |
| 493 | accountType); |
| 494 | throw new SecurityException(msg); |
| 495 | } |
| 496 | return getRequestingUidsForType(accountType, getUserAccounts( |
| 497 | UserHandle.getUserId(callingUid))); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Returns all UIDs for applications that requested the account type. This method |
| 502 | * is called indirectly by the Authenticator and AccountManager |
| 503 | * |
| 504 | * @param accountType authenticator would like to know the requesting apps of |
| 505 | * @param ua UserAccount that currently hosts the account and application |
| 506 | * |
| 507 | * @return ArrayList of all UIDs that support accounts of this |
| 508 | * account type that seek approval (to be used to know which accounts for |
| 509 | * the authenticator to include in addAccountExplicitly). Null if none. |
| 510 | */ |
| 511 | private int[] getRequestingUidsForType(String accountType, UserAccounts ua) { |
| 512 | synchronized(ua.cacheLock) { |
| 513 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 514 | ua.mApplicationAccountRequestMappings; |
| 515 | ArrayList<Integer> allUidsForAccountType = userApplicationAccountRequestMappings.get( |
| 516 | accountType); |
| 517 | if(allUidsForAccountType == null) { |
| 518 | return null; |
| 519 | } |
| 520 | int[] toReturn = new int[allUidsForAccountType.size()]; |
| 521 | for(int i = 0 ; i < toReturn.length ; i++) { |
| 522 | toReturn[i] = allUidsForAccountType.get(i); |
| 523 | } |
| 524 | return toReturn; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | @Override |
| 529 | public boolean isAccountVisible(Account a, int uid) { |
| 530 | int callingUid = Binder.getCallingUid(); |
| 531 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 532 | String msg = String.format( |
| 533 | "uid %s cannot get secrets for accounts of type: %s", |
| 534 | callingUid, |
| 535 | a.type); |
| 536 | throw new SecurityException(msg); |
| 537 | } |
| 538 | return isAccountVisible(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Checks visibility of certain account of a process identified |
| 543 | * by a given UID. This is called by the Authenticator indirectly. |
| 544 | * |
| 545 | * @param a The account to check visibility of |
| 546 | * @param uid UID to check visibility of |
| 547 | * @param ua UserAccount that currently hosts the account and application |
| 548 | * |
| 549 | * @return True if application has access to the account |
| 550 | * |
| 551 | */ |
| 552 | private boolean isAccountVisible(Account a, int uid, UserAccounts ua) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 553 | if(isAccountManagedByCaller(a.type, uid, UserHandle.getUserId(uid))) { |
| 554 | return true; |
| 555 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 556 | int accountMapping = getMockAccountNumber(a, ua); |
| 557 | if(accountMapping < 0) { |
| 558 | return true; |
| 559 | } |
| 560 | synchronized(ua.cacheLock) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 561 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 562 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 563 | ua.mVisibleListUidToMockAccountNumbers; |
| 564 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 565 | int indexOfAccountMapping = userAcctIdToAcctMap.indexOfValueByValue(a); |
| 566 | return indexOfAccountMapping == -1 || (linkedAccountsToUid != null |
| 567 | && linkedAccountsToUid.contains(accountMapping)); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public boolean makeAccountVisible(Account a, int uid) { |
| 573 | int callingUid = Binder.getCallingUid(); |
| 574 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 575 | String msg = String.format( |
| 576 | "uid %s cannot get secrets for accounts of type: %s", |
| 577 | callingUid, |
| 578 | a.type); |
| 579 | throw new SecurityException(msg); |
| 580 | } |
| 581 | return makeAccountVisible(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Gives a certain UID, represented a application, access to an account. This method |
| 586 | * is called indirectly by the Authenticator. |
| 587 | * |
| 588 | * @param a Account to make visible |
| 589 | * @param uid to add visibility of the Account from |
| 590 | * @param ua UserAccount that currently hosts the account and application |
| 591 | * |
| 592 | * @return True if account made visible to application and was not previously visible. |
| 593 | */ |
| 594 | private boolean makeAccountVisible(Account a, int uid, UserAccounts ua) { |
| 595 | int accountMapping = getMockAccountNumber(a, ua); |
| 596 | if(accountMapping < 0) { |
| 597 | accountMapping = makeAccountNumber(a, ua); |
| 598 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 599 | synchronized(ua.cacheLock) { |
| 600 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 601 | ua.mVisibleListUidToMockAccountNumbers; |
| 602 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
| 603 | if(linkedAccountsToUid == null) { |
| 604 | linkedAccountsToUid = new ArrayList<>(); |
| 605 | linkedAccountsToUid.add(accountMapping); |
| 606 | userWlUidToMockAccountNums.put(uid, linkedAccountsToUid); |
| 607 | } else if(!linkedAccountsToUid.contains(accountMapping)) { |
| 608 | linkedAccountsToUid.add(accountMapping); |
| 609 | } else { |
| 610 | return false; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | String[] subPackages = mPackageManager.getPackagesForUid(uid); |
| 615 | if(subPackages != null) { |
| 616 | for(String subPackage : subPackages) { |
| 617 | sendNotification(subPackage, a); |
| 618 | } |
| 619 | } |
| 620 | return true; |
| 621 | } |
| 622 | |
| 623 | @Override |
| 624 | public boolean removeAccountVisibility(Account a, int uid) { |
| 625 | int callingUid = Binder.getCallingUid(); |
| 626 | if (!isAccountManagedByCaller(a.type, callingUid, UserHandle.getUserId(callingUid))) { |
| 627 | String msg = String.format( |
| 628 | "uid %s cannot get secrets for accounts of type: %s", |
| 629 | callingUid, |
| 630 | a.type); |
| 631 | throw new SecurityException(msg); |
| 632 | } |
| 633 | return removeAccountVisibility(a, uid, getUserAccounts(UserHandle.getUserId(callingUid))); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Removes visibility of certain account of a process identified |
| 638 | * by a given UID to an application. This is called directly by the |
| 639 | * AccountManager and indirectly by the Authenticator. |
| 640 | * |
| 641 | * @param a Account to remove visibility from |
| 642 | * @param uid UID to remove visibility of the Account from |
| 643 | * @param ua UserAccount that hosts the account and application |
| 644 | * |
| 645 | * @return True if application access to account removed and was previously visible. |
| 646 | */ |
| 647 | private boolean removeAccountVisibility(Account a, int uid, UserAccounts ua) { |
| 648 | int accountMapping = getMockAccountNumber(a, ua); |
| 649 | if(accountMapping < 0) { |
| 650 | return false; |
| 651 | } |
| 652 | synchronized(ua.cacheLock) { |
| 653 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 654 | ua.mVisibleListUidToMockAccountNumbers; |
| 655 | ArrayList<Integer> linkedAccountsToUid = userWlUidToMockAccountNums.get(uid); |
| 656 | if(linkedAccountsToUid != null) { |
| 657 | boolean toReturn = linkedAccountsToUid.remove((Integer) accountMapping); |
| 658 | if(linkedAccountsToUid.size() == 0) { |
| 659 | userWlUidToMockAccountNums.remove(uid); |
| 660 | } |
| 661 | return toReturn; |
| 662 | } |
| 663 | } |
| 664 | return false; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Registers an application's preferences for supported account types for login. This is |
| 669 | * a helper method of requestAccountVisibility and indirectly called by AccountManager. |
| 670 | * |
| 671 | * @param accountTypes account types third party app is willing to support |
| 672 | * @param uid of application requesting account visibility |
| 673 | * @param ua UserAccount that hosts the account and application |
| 674 | */ |
| 675 | private void addRequestedAccountsVisibility(String[] accountTypes, int uid, UserAccounts ua) { |
| 676 | synchronized(ua.cacheLock) { |
| 677 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 678 | ua.mApplicationAccountRequestMappings; |
| 679 | for(String accountType : accountTypes) { |
| 680 | ArrayList<Integer> accountUidAppList = userApplicationAccountRequestMappings |
| 681 | .get(accountType); |
| 682 | if(accountUidAppList == null) { |
| 683 | accountUidAppList = new ArrayList<>(); |
| 684 | accountUidAppList.add(uid); |
| 685 | userApplicationAccountRequestMappings.put(accountType, accountUidAppList); |
| 686 | } else if (!accountUidAppList.contains(uid)) { |
| 687 | accountUidAppList.add(uid); |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Registers the requested login account types requested by all the applications already |
| 695 | * installed on the device. |
| 696 | */ |
| 697 | private void addRequestsForPreInstalledApplications() { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 698 | List<PackageInfo> allInstalledPackages = mPackageManager.getInstalledPackages(0); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 699 | for(PackageInfo pi : allInstalledPackages) { |
| 700 | int currentUid = pi.applicationInfo.uid; |
| 701 | if(currentUid != -1) { |
| 702 | registerAccountTypesSupported(currentUid, |
| 703 | getUserAccounts(UserHandle.getUserId(currentUid))); |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Clears all preferences an application had for login account types it offered |
| 710 | * support for. This method is used by AccountManager after application is |
| 711 | * uninstalled. |
| 712 | * |
| 713 | * @param uid Uid of the application to clear account type preferences |
| 714 | * @param ua UserAccount that hosted the account and application |
| 715 | * |
| 716 | * @return true if any previous settings were overridden. |
| 717 | */ |
| 718 | private boolean clearRequestedAccountVisibility(int uid, UserAccounts ua) { |
| 719 | boolean accountsDeleted = false; |
| 720 | ArrayList<String> accountTypesToRemove = new ArrayList<>(); |
| 721 | synchronized(ua.cacheLock) { |
| 722 | Map<String, ArrayList<Integer>> userApplicationAccountRequestMappings = |
| 723 | ua.mApplicationAccountRequestMappings; |
| 724 | Set<Entry<String, ArrayList<Integer>>> accountTypeAppListEntries = |
| 725 | userApplicationAccountRequestMappings.entrySet(); |
| 726 | |
| 727 | for(Entry<String, ArrayList<Integer>> entry : accountTypeAppListEntries) { |
| 728 | ArrayList<Integer> supportedApps = entry.getValue(); |
| 729 | if(supportedApps.remove((Integer) uid)) { |
| 730 | accountsDeleted = true; |
| 731 | } |
| 732 | |
| 733 | if(supportedApps.isEmpty()) { |
| 734 | accountTypesToRemove.add(entry.getKey()); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | for(String s : accountTypesToRemove) { |
| 739 | userApplicationAccountRequestMappings.remove(s); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | return accountsDeleted; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * Retrieves the mock account number associated with an Account in order to later retrieve |
| 748 | * the account from the Integer-Account Mapping. An account number is not the same as |
| 749 | * accountId in the database. This method can be indirectly called by AccountManager and |
| 750 | * indirectly by the Authenticator. |
| 751 | * |
| 752 | * @param a account to retrieve account number mapping |
| 753 | * @param ua UserAccount that currently hosts the account and application |
| 754 | * |
| 755 | * @return account number affiliated with the Account in question. Negative number if none. |
| 756 | */ |
| 757 | private int getMockAccountNumber(Account a, UserAccounts ua) { |
| 758 | //TODO: Each account is linked to AccountId rather than generated mock account numbers |
| 759 | SparseArray<Account> userAcctIdToAcctMap = |
| 760 | ua.mMockAccountIdToAccount; |
| 761 | synchronized(ua.cacheLock) { |
| 762 | int indexOfAccount = userAcctIdToAcctMap.indexOfValueByValue(a); |
| 763 | if(indexOfAccount < 0) { |
| 764 | return -1; |
| 765 | } |
| 766 | return userAcctIdToAcctMap.keyAt(indexOfAccount); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Returns a full list of accounts that a certain UID is allowed access |
| 772 | * based on the visible list entries. |
| 773 | * |
| 774 | * @param uid of application to retrieve visible listed accounts for |
| 775 | * @param ua UserAccount that currently hosts the account and application |
| 776 | * |
| 777 | * @return array of Account values that are accessible by the given uids |
| 778 | */ |
| 779 | private Account[] getVisibleListedAccounts(int uid, UserAccounts ua) { |
| 780 | ArrayList<Account> visibleListedAccounts = new ArrayList<>(); |
| 781 | synchronized(ua.cacheLock) { |
| 782 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 783 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 784 | ua.mVisibleListUidToMockAccountNumbers; |
| 785 | ArrayList<Integer> visibleListedUidAccountNumbers = |
| 786 | userWlUidToMockAccountNums.get(uid); |
| 787 | if(visibleListedUidAccountNumbers != null) { |
| 788 | for(Integer accountNumber : visibleListedUidAccountNumbers) { |
| 789 | Account currentAccount = userAcctIdToAcctMap.get(accountNumber); |
| 790 | visibleListedAccounts.add(currentAccount); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | Account[] arrVisibleListedAccounts = new Account[visibleListedAccounts.size()]; |
| 795 | return visibleListedAccounts.toArray(arrVisibleListedAccounts); |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * Makes an account number for a given Account to be mapped to. |
| 800 | * This method is called by makeVisible if an Account does not have |
| 801 | * a mapping for the visible list. This method is thus indirectly |
| 802 | * called by the Authenticator. |
| 803 | * |
| 804 | * @param a account to make an account number mapping of |
| 805 | * @param ua UserAccount that currently hosts the account and application |
| 806 | * |
| 807 | * @return account number created to map to the given account |
| 808 | */ |
| 809 | // TODO: Remove this method and use accountId from DB. |
| 810 | private int makeAccountNumber(Account a, UserAccounts ua) { |
| 811 | synchronized(ua.cacheLock) { |
| 812 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 813 | int newAccountMapping = 0; |
| 814 | while(userAcctIdToAcctMap.get(newAccountMapping) != null) { |
| 815 | newAccountMapping++; |
| 816 | } |
| 817 | userAcctIdToAcctMap.put(newAccountMapping, a); |
| 818 | return newAccountMapping; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | |
| 823 | |
| 824 | /** |
| 825 | * Registers an application, represented by a UID, to support account types detailed in |
| 826 | * the applications manifest as well as allowing it to opt for notifications. |
| 827 | * |
| 828 | * @param uid UID of application |
| 829 | * @param ua UserAccount that currently hosts the account and application |
| 830 | */ |
| 831 | private void registerAccountTypesSupported(int uid, UserAccounts ua) { |
| 832 | /* Account types supported are drawn from the Android Manifest of the Application */ |
| 833 | String interestedPackages = null; |
| 834 | try { |
| 835 | String[] allPackages = mPackageManager.getPackagesForUid(uid); |
Nicolas Prevot | f7d8df1 | 2016-09-16 17:45:34 +0100 | [diff] [blame] | 836 | if (allPackages != null) { |
| 837 | for(String aPackage : allPackages) { |
| 838 | ApplicationInfo ai = mPackageManager.getApplicationInfo(aPackage, |
| 839 | PackageManager.GET_META_DATA); |
| 840 | Bundle b = ai.metaData; |
| 841 | if(b == null) { |
| 842 | return; |
| 843 | } |
| 844 | interestedPackages = b.getString("android.accounts.SupportedLoginTypes"); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 845 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 846 | } |
| 847 | } catch (PackageManager.NameNotFoundException e) { |
| 848 | Log.d("NameNotFoundException", e.getMessage()); |
| 849 | } |
| 850 | if(interestedPackages != null) { |
| 851 | /* request remote account types directly from here. Reads from Android Manifest */ |
| 852 | requestAccountVisibility(interestedPackages.split(";"), uid, ua); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * Allows AccountManager to register account types that an application has login |
| 858 | * support for. This method over-writes all of the application's previous settings |
| 859 | * for accounts it supported. |
| 860 | * |
| 861 | * @param accountTypes array of account types application wishes to support |
| 862 | * @param uid of application registering requested account types |
| 863 | * @param ua UserAccount that hosts the account and application |
| 864 | */ |
| 865 | private void requestAccountVisibility(String[] accountTypes, int uid, UserAccounts ua) { |
| 866 | if(accountTypes.length > 0) { |
| 867 | clearRequestedAccountVisibility(uid, ua); |
| 868 | addRequestedAccountsVisibility(accountTypes, uid, ua); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Removes visibility of all Accounts to this particular UID. This is called when an |
| 874 | * application is uninstalled so another application that is installed with the same |
| 875 | * UID cannot access Accounts. This is called by AccountManager. |
| 876 | * |
| 877 | * @param uid of application to remove all Account visibility to |
| 878 | * @param ua UserAccount that hosts the current Account |
| 879 | */ |
| 880 | private void removeAccountVisibilityAllAccounts(int uid, UserAccounts ua) { |
| 881 | synchronized(ua.cacheLock) { |
| 882 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 883 | ua.mVisibleListUidToMockAccountNumbers; |
| 884 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 885 | ArrayList<Integer> allAccountNumbersList = userWlUidToMockAccountNums.get(uid); |
| 886 | if(allAccountNumbersList != null) { |
| 887 | Integer[] allAccountNumbers = allAccountNumbersList.toArray( |
| 888 | new Integer[allAccountNumbersList.size()]); |
| 889 | for(int accountNum : allAccountNumbers) { |
| 890 | removeAccountVisibility(userAcctIdToAcctMap.get(accountNum), uid, ua); |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * Removes visible list functionality of a certain Account. |
| 898 | * This method is currently called by (1) addAccountExplicitly (as opposed to |
| 899 | * addAccountExplicitlyWithUid) and (2) removeAccountExplicitly. |
| 900 | * |
| 901 | * @param a the account to clear the visible list functionality for |
| 902 | * @param ua currently UserAccounts profile containing Account |
| 903 | * |
| 904 | * @return true if account previously had visible list functionality |
| 905 | */ |
| 906 | private boolean removeVisibleListFunctionality(Account a, UserAccounts ua) { |
| 907 | int mockAccountNum = getMockAccountNumber(a, ua); |
| 908 | if(mockAccountNum < 0) { |
| 909 | return false; |
| 910 | } |
| 911 | synchronized(ua.cacheLock) { |
| 912 | SparseArray<ArrayList<Integer>> userWlUidToMockAccountNums = |
| 913 | ua.mVisibleListUidToMockAccountNumbers; |
| 914 | SparseArray<Account> userAcctIdToAcctMap = ua.mMockAccountIdToAccount; |
| 915 | |
| 916 | /* Removing mapping from account number to account removes visible list functionality*/ |
| 917 | userAcctIdToAcctMap.remove(mockAccountNum); |
| 918 | |
| 919 | for(int i = userWlUidToMockAccountNums.size() - 1 ; i >= 0 ; i--) { |
| 920 | int uidKey = userWlUidToMockAccountNums.keyAt(i); |
| 921 | ArrayList<Integer> allAccountNumbers = userWlUidToMockAccountNums.get(uidKey); |
| 922 | if(allAccountNumbers != null) { |
| 923 | allAccountNumbers.remove(mockAccountNum); |
| 924 | if(allAccountNumbers.isEmpty()) { |
| 925 | userWlUidToMockAccountNums.remove(uidKey); |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | return true; |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * Sends a direct intent to a package, notifying it of a visible account. This |
| 935 | * method is a helper method of makeAccountVisible. |
| 936 | * |
| 937 | * @param desiredPackage to send Account to |
| 938 | * @param visibleAccount to send to package |
| 939 | */ |
| 940 | private void sendNotification(String desiredPackage, Account visibleAccount) { |
| 941 | Intent intent = new Intent(); |
| 942 | intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); |
| 943 | intent.setAction(NEW_ACCOUNT_VISIBLE); |
| 944 | intent.setPackage(desiredPackage); |
| 945 | intent.putExtra("android.accounts.KEY_ACCOUNT", (Account) visibleAccount); |
| 946 | mContext.sendBroadcast(intent); |
| 947 | } |
| 948 | |
| 949 | @Override |
Dianne Hackborn | 164371f | 2013-10-01 19:10:13 -0700 | [diff] [blame] | 950 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 951 | throws RemoteException { |
| 952 | try { |
| 953 | return super.onTransact(code, data, reply, flags); |
| 954 | } catch (RuntimeException e) { |
| 955 | // The account manager only throws security exceptions, so let's |
| 956 | // log all others. |
| 957 | if (!(e instanceof SecurityException)) { |
| 958 | Slog.wtf(TAG, "Account Manager Crash", e); |
| 959 | } |
| 960 | throw e; |
| 961 | } |
| 962 | } |
| 963 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 964 | private UserManager getUserManager() { |
| 965 | if (mUserManager == null) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 966 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 967 | } |
| 968 | return mUserManager; |
| 969 | } |
| 970 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 971 | /** |
| 972 | * Validate internal set of accounts against installed authenticators for |
| 973 | * given user. Clears cached authenticators before validating. |
| 974 | */ |
| 975 | public void validateAccounts(int userId) { |
| 976 | final UserAccounts accounts = getUserAccounts(userId); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 977 | // Invalidate user-specific cache to make sure we catch any |
| 978 | // removed authenticators. |
| 979 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * Validate internal set of accounts against installed authenticators for |
| 984 | * given user. Clear cached authenticators before validating when requested. |
| 985 | */ |
| 986 | private void validateAccountsInternal( |
| 987 | UserAccounts accounts, boolean invalidateAuthenticatorCache) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 988 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 989 | Log.d(TAG, "validateAccountsInternal " + accounts.userId |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 990 | + " isCeDatabaseAttached=" + accounts.accountsDb.isCeDatabaseAttached() |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 991 | + " userLocked=" + mLocalUnlockedUsers.get(accounts.userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 992 | } |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 993 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 994 | if (invalidateAuthenticatorCache) { |
| 995 | mAuthenticatorCache.invalidateCache(accounts.userId); |
| 996 | } |
| 997 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 998 | final HashMap<String, Integer> knownAuth = getAuthenticatorTypeAndUIDForUser( |
| 999 | mAuthenticatorCache, accounts.userId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1000 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1001 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1002 | synchronized (accounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1003 | boolean accountDeleted = false; |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1004 | |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1005 | // Get a map of stored authenticator types to UID |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1006 | final AccountsDb accountsDb = accounts.accountsDb; |
| 1007 | Map<String, Integer> metaAuthUid = accountsDb.findMetaAuthUid(); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1008 | // Create a list of authenticator type whose previous uid no longer exists |
| 1009 | HashSet<String> obsoleteAuthType = Sets.newHashSet(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1010 | SparseBooleanArray knownUids = null; |
| 1011 | for (Entry<String, Integer> authToUidEntry : metaAuthUid.entrySet()) { |
| 1012 | String type = authToUidEntry.getKey(); |
| 1013 | int uid = authToUidEntry.getValue(); |
| 1014 | Integer knownUid = knownAuth.get(type); |
| 1015 | if (knownUid != null && uid == knownUid) { |
| 1016 | // Remove it from the knownAuth list if it's unchanged. |
| 1017 | knownAuth.remove(type); |
| 1018 | } else { |
| 1019 | /* |
| 1020 | * The authenticator is presently not cached and should only be triggered |
| 1021 | * when we think an authenticator has been removed (or is being updated). |
| 1022 | * But we still want to check if any data with the associated uid is |
| 1023 | * around. This is an (imperfect) signal that the package may be updating. |
| 1024 | * |
| 1025 | * A side effect of this is that an authenticator sharing a uid with |
| 1026 | * multiple apps won't get its credentials wiped as long as some app with |
| 1027 | * that uid is still on the device. But I suspect that this is a rare case. |
| 1028 | * And it isn't clear to me how an attacker could really exploit that |
| 1029 | * feature. |
| 1030 | * |
| 1031 | * The upshot is that we don't have to worry about accounts getting |
| 1032 | * uninstalled while the authenticator's package is being updated. |
| 1033 | * |
| 1034 | */ |
| 1035 | if (knownUids == null) { |
| 1036 | knownUids = getUidsOfInstalledOrUpdatedPackagesAsUser(accounts.userId); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1037 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1038 | if (!knownUids.get(uid)) { |
| 1039 | // The authenticator is not presently available to the cache. And the |
| 1040 | // package no longer has a data directory (so we surmise it isn't updating). |
| 1041 | // So purge its data from the account databases. |
| 1042 | obsoleteAuthType.add(type); |
| 1043 | // And delete it from the TABLE_META |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1044 | accountsDb.deleteMetaByAuthTypeAndUid(type, uid); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1045 | } |
| 1046 | } |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1049 | // Add the newly registered authenticator to TABLE_META. If old authenticators have |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1050 | // 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] | 1051 | // values. |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1052 | for (Entry<String, Integer> entry : knownAuth.entrySet()) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1053 | accountsDb.insertOrReplaceMetaAuthTypeAndUid(entry.getKey(), entry.getValue()); |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1056 | final Map<Long, Account> accountsMap = accountsDb.findAllDeAccounts(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1057 | try { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1058 | accounts.accountCache.clear(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1059 | final HashMap<String, ArrayList<String>> accountNamesByType = new LinkedHashMap<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1060 | for (Entry<Long, Account> accountEntry : accountsMap.entrySet()) { |
| 1061 | final long accountId = accountEntry.getKey(); |
| 1062 | final Account account = accountEntry.getValue(); |
| 1063 | if (obsoleteAuthType.contains(account.type)) { |
| 1064 | Slog.w(TAG, "deleting account " + account.name + " because type " |
| 1065 | + account.type + "'s registered authenticator no longer exist."); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1066 | accountsDb.beginTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1067 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1068 | accountsDb.deleteDeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1069 | // Also delete from CE table if user is unlocked; if user is currently |
| 1070 | // locked the account will be removed later by syncDeCeAccountsLocked |
| 1071 | if (userUnlocked) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1072 | accountsDb.deleteCeAccount(accountId); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1073 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1074 | accountsDb.setTransactionSuccessful(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1075 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1076 | accountsDb.endTransaction(); |
Fyodor Kupolov | 627fc20 | 2016-06-03 11:03:03 -0700 | [diff] [blame] | 1077 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1078 | accountDeleted = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1079 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1080 | logRecord(AccountsDb.DEBUG_ACTION_AUTHENTICATOR_REMOVE, |
| 1081 | AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1082 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1083 | accounts.userDataCache.remove(account); |
| 1084 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 1085 | accounts.accountTokenCaches.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1086 | } else { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1087 | ArrayList<String> accountNames = accountNamesByType.get(account.type); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1088 | if (accountNames == null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1089 | accountNames = new ArrayList<>(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1090 | accountNamesByType.put(account.type, accountNames); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1091 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1092 | accountNames.add(account.name); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1093 | } |
| 1094 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1095 | for (Map.Entry<String, ArrayList<String>> cur : accountNamesByType.entrySet()) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1096 | final String accountType = cur.getKey(); |
| 1097 | final ArrayList<String> accountNames = cur.getValue(); |
| 1098 | final Account[] accountsForType = new Account[accountNames.size()]; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1099 | for (int i = 0; i < accountsForType.length; i++) { |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1100 | accountsForType[i] = new Account(accountNames.get(i), accountType, |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1101 | UUID.randomUUID().toString()); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1102 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1103 | accounts.accountCache.put(accountType, accountsForType); |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1104 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1105 | } finally { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1106 | if (accountDeleted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1107 | sendAccountsChangedBroadcast(accounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1108 | } |
Fred Quintana | afa92b8 | 2009-12-01 16:27:03 -0800 | [diff] [blame] | 1109 | } |
| 1110 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1113 | private SparseBooleanArray getUidsOfInstalledOrUpdatedPackagesAsUser(int userId) { |
| 1114 | // Get the UIDs of all apps that might have data on the device. We want |
| 1115 | // to preserve user data if the app might otherwise be storing data. |
| 1116 | List<PackageInfo> pkgsWithData = |
| 1117 | mPackageManager.getInstalledPackagesAsUser( |
| 1118 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 1119 | SparseBooleanArray knownUids = new SparseBooleanArray(pkgsWithData.size()); |
| 1120 | for (PackageInfo pkgInfo : pkgsWithData) { |
| 1121 | if (pkgInfo.applicationInfo != null |
| 1122 | && (pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) { |
| 1123 | knownUids.put(pkgInfo.applicationInfo.uid, true); |
| 1124 | } |
| 1125 | } |
| 1126 | return knownUids; |
| 1127 | } |
| 1128 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1129 | static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1130 | Context context, |
| 1131 | int userId) { |
| 1132 | AccountAuthenticatorCache authCache = new AccountAuthenticatorCache(context); |
Carlos Valdivia | a46b112 | 2016-04-26 19:36:50 -0700 | [diff] [blame] | 1133 | return getAuthenticatorTypeAndUIDForUser(authCache, userId); |
| 1134 | } |
| 1135 | |
| 1136 | private static HashMap<String, Integer> getAuthenticatorTypeAndUIDForUser( |
| 1137 | IAccountAuthenticatorCache authCache, |
| 1138 | int userId) { |
Sandra Kwan | 1c9026d | 2016-02-23 10:22:15 -0800 | [diff] [blame] | 1139 | HashMap<String, Integer> knownAuth = new HashMap<>(); |
| 1140 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> service : authCache |
| 1141 | .getAllServices(userId)) { |
| 1142 | knownAuth.put(service.type.type, service.uid); |
| 1143 | } |
| 1144 | return knownAuth; |
| 1145 | } |
| 1146 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1147 | private UserAccounts getUserAccountsForCaller() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1148 | return getUserAccounts(UserHandle.getCallingUserId()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | protected UserAccounts getUserAccounts(int userId) { |
| 1152 | synchronized (mUsers) { |
| 1153 | UserAccounts accounts = mUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1154 | boolean validateAccounts = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1155 | if (accounts == null) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1156 | File preNDbFile = new File(mInjector.getPreNDatabaseName(userId)); |
| 1157 | File deDbFile = new File(mInjector.getDeDatabaseName(userId)); |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1158 | accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1159 | initializeDebugDbSizeAndCompileSqlStatementForLogging(accounts); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1160 | mUsers.append(userId, accounts); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1161 | purgeOldGrants(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1162 | validateAccounts = true; |
| 1163 | } |
| 1164 | // open CE database if necessary |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1165 | if (!accounts.accountsDb.isCeDatabaseAttached() && mLocalUnlockedUsers.get(userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1166 | Log.i(TAG, "User " + userId + " is unlocked - opening CE database"); |
| 1167 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1168 | File ceDatabaseFile = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1169 | accounts.accountsDb.attachCeDatabase(ceDatabaseFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1170 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1171 | syncDeCeAccountsLocked(accounts); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1172 | } |
| 1173 | if (validateAccounts) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1174 | validateAccountsInternal(accounts, true /* invalidateAuthenticatorCache */); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1175 | } |
| 1176 | return accounts; |
| 1177 | } |
| 1178 | } |
| 1179 | |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1180 | private void syncDeCeAccountsLocked(UserAccounts accounts) { |
| 1181 | Preconditions.checkState(Thread.holdsLock(mUsers), "mUsers lock must be held"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1182 | List<Account> accountsToRemove = accounts.accountsDb.findCeAccountsNotInDe(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1183 | if (!accountsToRemove.isEmpty()) { |
| 1184 | Slog.i(TAG, "Accounts " + accountsToRemove + " were previously deleted while user " |
| 1185 | + accounts.userId + " was locked. Removing accounts from CE tables"); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1186 | logRecord(accounts, AccountsDb.DEBUG_ACTION_SYNC_DE_CE_ACCOUNTS, |
| 1187 | AccountsDb.TABLE_ACCOUNTS); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1188 | |
| 1189 | for (Account account : accountsToRemove) { |
| 1190 | removeAccountInternal(accounts, account, Process.myUid()); |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1195 | private void purgeOldGrantsAll() { |
| 1196 | synchronized (mUsers) { |
| 1197 | for (int i = 0; i < mUsers.size(); i++) { |
| 1198 | purgeOldGrants(mUsers.valueAt(i)); |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | private void purgeOldGrants(UserAccounts accounts) { |
| 1204 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1205 | List<Integer> uids = accounts.accountsDb.findAllUidGrants(); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1206 | for (int uid : uids) { |
| 1207 | final boolean packageExists = mPackageManager.getPackagesForUid(uid) != null; |
| 1208 | if (packageExists) { |
| 1209 | continue; |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1210 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1211 | Log.d(TAG, "deleting grants for UID " + uid |
| 1212 | + " because its package is no longer installed"); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1213 | accounts.accountsDb.deleteGrantsByUid(uid); |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1218 | private void onUserRemoved(Intent intent) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1219 | int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1220 | if (userId < 1) return; |
| 1221 | |
| 1222 | UserAccounts accounts; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1223 | boolean userUnlocked; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1224 | synchronized (mUsers) { |
| 1225 | accounts = mUsers.get(userId); |
| 1226 | mUsers.remove(userId); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1227 | userUnlocked = mLocalUnlockedUsers.get(userId); |
| 1228 | mLocalUnlockedUsers.delete(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1229 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1230 | if (accounts != null) { |
| 1231 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1232 | accounts.accountsDb.close(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1233 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1234 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1235 | Log.i(TAG, "Removing database files for user " + userId); |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1236 | File dbFile = new File(mInjector.getDeDatabaseName(userId)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1237 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1238 | AccountsDb.deleteDbFileWarnIfFailed(dbFile); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1239 | // Remove CE file if user is unlocked, or FBE is not enabled |
| 1240 | boolean fbeEnabled = StorageManager.isFileEncryptedNativeOrEmulated(); |
| 1241 | if (!fbeEnabled || userUnlocked) { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 1242 | File ceDb = new File(mInjector.getCeDatabaseName(userId)); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1243 | if (ceDb.exists()) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1244 | AccountsDb.deleteDbFileWarnIfFailed(ceDb); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 1249 | @VisibleForTesting |
| 1250 | void onUserUnlocked(Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1251 | onUnlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1)); |
| 1252 | } |
| 1253 | |
| 1254 | void onUnlockUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1255 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1256 | Log.v(TAG, "onUserUnlocked " + userId); |
| 1257 | } |
| 1258 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1259 | mLocalUnlockedUsers.put(userId, true); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1260 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1261 | if (userId < 1) return; |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1262 | syncSharedAccounts(userId); |
| 1263 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1264 | |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1265 | private void syncSharedAccounts(int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1266 | // Check if there's a shared account that needs to be created as an account |
| 1267 | Account[] sharedAccounts = getSharedAccountsAsUser(userId); |
| 1268 | if (sharedAccounts == null || sharedAccounts.length == 0) return; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1269 | Account[] accounts = getAccountsAsUser(null, userId, mContext.getOpPackageName()); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1270 | int parentUserId = UserManager.isSplitSystemUser() |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1271 | ? getUserManager().getUserInfo(userId).restrictedProfileParentId |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1272 | : UserHandle.USER_SYSTEM; |
| 1273 | if (parentUserId < 0) { |
| 1274 | Log.w(TAG, "User " + userId + " has shared accounts, but no parent user"); |
| 1275 | return; |
| 1276 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1277 | for (Account sa : sharedAccounts) { |
| 1278 | if (ArrayUtils.contains(accounts, sa)) continue; |
| 1279 | // Account doesn't exist. Copy it now. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1280 | copyAccountToUser(null /*no response*/, sa, parentUserId, userId); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1284 | @Override |
| 1285 | public void onServiceChanged(AuthenticatorDescription desc, int userId, boolean removed) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1286 | validateAccountsInternal(getUserAccounts(userId), false /* invalidateAuthenticatorCache */); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1289 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1290 | public String getPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1291 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1292 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1293 | Log.v(TAG, "getPassword: " + account |
| 1294 | + ", caller's uid " + Binder.getCallingUid() |
| 1295 | + ", pid " + Binder.getCallingPid()); |
| 1296 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1297 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1298 | int userId = UserHandle.getCallingUserId(); |
| 1299 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1300 | String msg = String.format( |
| 1301 | "uid %s cannot get secrets for accounts of type: %s", |
| 1302 | callingUid, |
| 1303 | account.type); |
| 1304 | throw new SecurityException(msg); |
| 1305 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1306 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1307 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1308 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1309 | return readPasswordInternal(accounts, account); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1310 | } finally { |
| 1311 | restoreCallingIdentity(identityToken); |
| 1312 | } |
| 1313 | } |
| 1314 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1315 | private String readPasswordInternal(UserAccounts accounts, Account account) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1316 | if (account == null) { |
| 1317 | return null; |
| 1318 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1319 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1320 | Log.w(TAG, "Password is not available - user " + accounts.userId + " data is locked"); |
| 1321 | return null; |
| 1322 | } |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 1323 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1324 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1325 | return accounts.accountsDb.findAccountPasswordByNameAndType(account.name, account.type); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1329 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1330 | public String getPreviousName(Account account) { |
| 1331 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1332 | Log.v(TAG, "getPreviousName: " + account |
| 1333 | + ", caller's uid " + Binder.getCallingUid() |
| 1334 | + ", pid " + Binder.getCallingPid()); |
| 1335 | } |
| 1336 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1337 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1338 | long identityToken = clearCallingIdentity(); |
| 1339 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1340 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1341 | return readPreviousNameInternal(accounts, account); |
| 1342 | } finally { |
| 1343 | restoreCallingIdentity(identityToken); |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | private String readPreviousNameInternal(UserAccounts accounts, Account account) { |
| 1348 | if (account == null) { |
| 1349 | return null; |
| 1350 | } |
| 1351 | synchronized (accounts.cacheLock) { |
| 1352 | AtomicReference<String> previousNameRef = accounts.previousNameCache.get(account); |
| 1353 | if (previousNameRef == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1354 | String previousName = accounts.accountsDb.findDeAccountPreviousName(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1355 | previousNameRef = new AtomicReference<>(previousName); |
| 1356 | accounts.previousNameCache.put(account, previousNameRef); |
| 1357 | return previousName; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1358 | } else { |
| 1359 | return previousNameRef.get(); |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1365 | public String getUserData(Account account, String key) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1366 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1367 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1368 | String msg = String.format("getUserData( account: %s, key: %s, callerUid: %s, pid: %s", |
| 1369 | account, key, callingUid, Binder.getCallingPid()); |
| 1370 | Log.v(TAG, msg); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1371 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1372 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1373 | if (key == null) throw new IllegalArgumentException("key is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1374 | int userId = UserHandle.getCallingUserId(); |
| 1375 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1376 | String msg = String.format( |
| 1377 | "uid %s cannot get user data for accounts of type: %s", |
| 1378 | callingUid, |
| 1379 | account.type); |
| 1380 | throw new SecurityException(msg); |
| 1381 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1382 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 1383 | Log.w(TAG, "User " + userId + " data is locked. callingUid " + callingUid); |
| 1384 | return null; |
| 1385 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1386 | long identityToken = clearCallingIdentity(); |
| 1387 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1388 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 1389 | synchronized (accounts.cacheLock) { |
| 1390 | if (!accountExistsCacheLocked(accounts, account)) { |
| 1391 | return null; |
| 1392 | } |
| 1393 | return readUserDataInternalLocked(accounts, account, key); |
| 1394 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1395 | } finally { |
| 1396 | restoreCallingIdentity(identityToken); |
| 1397 | } |
| 1398 | } |
| 1399 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1400 | @Override |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1401 | public AuthenticatorDescription[] getAuthenticatorTypes(int userId) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1402 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1403 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1404 | Log.v(TAG, "getAuthenticatorTypes: " |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1405 | + "for user id " + userId |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 1406 | + " caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1407 | + ", pid " + Binder.getCallingPid()); |
| 1408 | } |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1409 | // Only allow the system process to read accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1410 | if (isCrossUser(callingUid, userId)) { |
| 1411 | throw new SecurityException( |
| 1412 | String.format( |
| 1413 | "User %s tying to get authenticator types for %s" , |
| 1414 | UserHandle.getCallingUserId(), |
| 1415 | userId)); |
| 1416 | } |
| 1417 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1418 | final long identityToken = clearCallingIdentity(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1419 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1420 | return getAuthenticatorTypesInternal(userId); |
| 1421 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1422 | } finally { |
| 1423 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1424 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1427 | /** |
| 1428 | * Should only be called inside of a clearCallingIdentity block. |
| 1429 | */ |
| 1430 | private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) { |
Fyodor Kupolov | 8144648 | 2016-08-24 11:27:49 -0700 | [diff] [blame] | 1431 | mAuthenticatorCache.updateServices(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1432 | Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>> |
| 1433 | authenticatorCollection = mAuthenticatorCache.getAllServices(userId); |
| 1434 | AuthenticatorDescription[] types = |
| 1435 | new AuthenticatorDescription[authenticatorCollection.size()]; |
| 1436 | int i = 0; |
| 1437 | for (AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticator |
| 1438 | : authenticatorCollection) { |
| 1439 | types[i] = authenticator.type; |
| 1440 | i++; |
| 1441 | } |
| 1442 | return types; |
| 1443 | } |
| 1444 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1445 | private boolean isCrossUser(int callingUid, int userId) { |
| 1446 | return (userId != UserHandle.getCallingUserId() |
| 1447 | && callingUid != Process.myUid() |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1448 | && mContext.checkCallingOrSelfPermission( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1449 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 1450 | != PackageManager.PERMISSION_GRANTED); |
Alexandra Gherghina | c1cf161 | 2014-06-05 10:49:14 +0100 | [diff] [blame] | 1451 | } |
| 1452 | |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1453 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1454 | public boolean addAccountExplicitly(Account account, String password, Bundle extras) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1455 | Bundle.setDefusable(extras, true); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1456 | // clears the visible list functionality for this account because this method allows |
| 1457 | // default account access to all applications for account. |
| 1458 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1459 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1460 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 1461 | Log.v(TAG, "addAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1462 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1463 | + ", pid " + Binder.getCallingPid()); |
| 1464 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1465 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1466 | int userId = UserHandle.getCallingUserId(); |
| 1467 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1468 | String msg = String.format( |
| 1469 | "uid %s cannot explicitly add accounts of type: %s", |
| 1470 | callingUid, |
| 1471 | account.type); |
| 1472 | throw new SecurityException(msg); |
| 1473 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 1474 | removeVisibleListFunctionality(account, getUserAccounts(UserHandle.getUserId(callingUid))); |
Jatin Lodhia | 3df7d69 | 2013-03-27 10:57:23 -0700 | [diff] [blame] | 1475 | /* |
| 1476 | * Child users are not allowed to add accounts. Only the accounts that are |
| 1477 | * shared by the parent profile can be added to child profile. |
| 1478 | * |
| 1479 | * TODO: Only allow accounts that were shared to be added by |
| 1480 | * a limited user. |
| 1481 | */ |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 1482 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1483 | // fails if the account already exists |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1484 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1485 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1486 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1487 | return addAccountInternal(accounts, account, password, extras, callingUid); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1488 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 1489 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1493 | @Override |
| 1494 | public void copyAccountToUser(final IAccountManagerResponse response, final Account account, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1495 | final int userFrom, int userTo) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1496 | int callingUid = Binder.getCallingUid(); |
| 1497 | if (isCrossUser(callingUid, UserHandle.USER_ALL)) { |
| 1498 | throw new SecurityException("Calling copyAccountToUser requires " |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1499 | + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1500 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1501 | final UserAccounts fromAccounts = getUserAccounts(userFrom); |
| 1502 | final UserAccounts toAccounts = getUserAccounts(userTo); |
| 1503 | if (fromAccounts == null || toAccounts == null) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1504 | if (response != null) { |
| 1505 | Bundle result = new Bundle(); |
| 1506 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); |
| 1507 | try { |
| 1508 | response.onResult(result); |
| 1509 | } catch (RemoteException e) { |
| 1510 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 1511 | } |
| 1512 | } |
| 1513 | return; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1516 | Slog.d(TAG, "Copying account " + account.name |
| 1517 | + " from user " + userFrom + " to user " + userTo); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1518 | long identityToken = clearCallingIdentity(); |
| 1519 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1520 | new Session(fromAccounts, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1521 | false /* stripAuthTokenFromResult */, account.name, |
| 1522 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1523 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1524 | protected String toDebugString(long now) { |
| 1525 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1526 | + ", " + account.type; |
| 1527 | } |
| 1528 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1529 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1530 | public void run() throws RemoteException { |
| 1531 | mAuthenticator.getAccountCredentialsForCloning(this, account); |
| 1532 | } |
| 1533 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1534 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1535 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1536 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1537 | if (result != null |
| 1538 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
| 1539 | // Create a Session for the target user and pass in the bundle |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1540 | completeCloningAccount(response, result, account, toAccounts, userFrom); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1541 | } else { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1542 | super.onResult(result); |
| 1543 | } |
| 1544 | } |
| 1545 | }.bind(); |
| 1546 | } finally { |
| 1547 | restoreCallingIdentity(identityToken); |
| 1548 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1551 | @Override |
| 1552 | public boolean accountAuthenticated(final Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1553 | final int callingUid = Binder.getCallingUid(); |
| 1554 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1555 | String msg = String.format( |
| 1556 | "accountAuthenticated( account: %s, callerUid: %s)", |
| 1557 | account, |
| 1558 | callingUid); |
| 1559 | Log.v(TAG, msg); |
| 1560 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1561 | if (account == null) { |
| 1562 | throw new IllegalArgumentException("account is null"); |
| 1563 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1564 | int userId = UserHandle.getCallingUserId(); |
| 1565 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1566 | String msg = String.format( |
| 1567 | "uid %s cannot notify authentication for accounts of type: %s", |
| 1568 | callingUid, |
| 1569 | account.type); |
| 1570 | throw new SecurityException(msg); |
| 1571 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1572 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1573 | if (!canUserModifyAccounts(userId, callingUid) || |
| 1574 | !canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1575 | return false; |
| 1576 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1577 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1578 | long identityToken = clearCallingIdentity(); |
| 1579 | try { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1580 | UserAccounts accounts = getUserAccounts(userId); |
| 1581 | return updateLastAuthenticatedTime(account); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1582 | } finally { |
| 1583 | restoreCallingIdentity(identityToken); |
| 1584 | } |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | private boolean updateLastAuthenticatedTime(Account account) { |
| 1588 | final UserAccounts accounts = getUserAccountsForCaller(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1589 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1590 | return accounts.accountsDb.updateAccountLastAuthenticatedTime(account); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1591 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1592 | } |
| 1593 | |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1594 | private void completeCloningAccount(IAccountManagerResponse response, |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1595 | final Bundle accountCredentials, final Account account, final UserAccounts targetUser, |
| 1596 | final int parentUserId){ |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1597 | Bundle.setDefusable(accountCredentials, true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1598 | long id = clearCallingIdentity(); |
| 1599 | try { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1600 | new Session(targetUser, response, account.type, false, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1601 | false /* stripAuthTokenFromResult */, account.name, |
| 1602 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1603 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1604 | protected String toDebugString(long now) { |
| 1605 | return super.toDebugString(now) + ", getAccountCredentialsForClone" |
| 1606 | + ", " + account.type; |
| 1607 | } |
| 1608 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1609 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1610 | public void run() throws RemoteException { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1611 | // Confirm that the owner's account still exists before this step. |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1612 | UserAccounts owner = getUserAccounts(parentUserId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1613 | synchronized (owner.cacheLock) { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1614 | for (Account acc : getAccounts(parentUserId, |
| 1615 | mContext.getOpPackageName())) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1616 | if (acc.equals(account)) { |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1617 | mAuthenticator.addAccountFromCredentials( |
| 1618 | this, account, accountCredentials); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1619 | break; |
| 1620 | } |
| 1621 | } |
| 1622 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1623 | } |
| 1624 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1625 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1626 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1627 | Bundle.setDefusable(result, true); |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 1628 | // TODO: Anything to do if if succedded? |
| 1629 | // TODO: If it failed: Show error notification? Should we remove the shadow |
| 1630 | // account to avoid retries? |
| 1631 | super.onResult(result); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1632 | } |
| 1633 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1634 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1635 | public void onError(int errorCode, String errorMessage) { |
| 1636 | super.onError(errorCode, errorMessage); |
| 1637 | // TODO: Show error notification to user |
| 1638 | // TODO: Should we remove the shadow account so that it doesn't keep trying? |
| 1639 | } |
| 1640 | |
| 1641 | }.bind(); |
| 1642 | } finally { |
| 1643 | restoreCallingIdentity(id); |
| 1644 | } |
| 1645 | } |
| 1646 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1647 | private boolean addAccountInternal(UserAccounts accounts, Account account, String password, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1648 | Bundle extras, int callingUid) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1649 | Bundle.setDefusable(extras, true); |
Fred Quintana | 743dfad | 2010-07-15 10:59:25 -0700 | [diff] [blame] | 1650 | if (account == null) { |
| 1651 | return false; |
| 1652 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1653 | if (!isLocalUnlockedUser(accounts.userId)) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1654 | Log.w(TAG, "Account " + account + " cannot be added - user " + accounts.userId |
| 1655 | + " is locked. callingUid=" + callingUid); |
| 1656 | return false; |
| 1657 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1658 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1659 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1660 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1661 | if (accounts.accountsDb.findCeAccountId(account) >= 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1662 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1663 | + ", skipping since the account already exists"); |
| 1664 | return false; |
| 1665 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1666 | long accountId = accounts.accountsDb.insertCeAccount(account, password); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1667 | if (accountId < 0) { |
| 1668 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1669 | + ", skipping the DB insert failed"); |
| 1670 | return false; |
| 1671 | } |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1672 | // Insert into DE table |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1673 | if (accounts.accountsDb.insertDeAccount(account, accountId) < 0) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1674 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1675 | + ", skipping the DB insert failed"); |
| 1676 | return false; |
| 1677 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1678 | if (extras != null) { |
| 1679 | for (String key : extras.keySet()) { |
| 1680 | final String value = extras.getString(key); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1681 | if (accounts.accountsDb.insertExtra(accountId, key, value) < 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1682 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 1683 | + ", skipping since insertExtra failed for key " + key); |
| 1684 | return false; |
| 1685 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1686 | } |
| 1687 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1688 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1689 | |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1690 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 1691 | accountId, accounts, callingUid); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 1692 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1693 | insertAccountIntoCacheLocked(accounts, account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 1694 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1695 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1696 | } |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1697 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1698 | if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) { |
| 1699 | addAccountToLinkedRestrictedUsers(account, accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1700 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1701 | |
| 1702 | // Only send LOGIN_ACCOUNTS_CHANGED when the database changed. |
| 1703 | sendAccountsChangedBroadcast(accounts.userId); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1704 | return true; |
| 1705 | } |
| 1706 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1707 | private boolean isLocalUnlockedUser(int userId) { |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1708 | synchronized (mUsers) { |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1709 | return mLocalUnlockedUsers.get(userId); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1713 | /** |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1714 | * 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] | 1715 | * running, then clone the account too. |
| 1716 | * @param account the account to share with limited users |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1717 | * |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1718 | */ |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1719 | private void addAccountToLinkedRestrictedUsers(Account account, int parentUserId) { |
Mita Yun | f4c240e | 2013-04-01 21:12:43 -0700 | [diff] [blame] | 1720 | List<UserInfo> users = getUserManager().getUsers(); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1721 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1722 | if (user.isRestricted() && (parentUserId == user.restrictedProfileParentId)) { |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1723 | addSharedAccountAsUser(account, user.id); |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 1724 | if (isLocalUnlockedUser(user.id)) { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 1725 | mHandler.sendMessage(mHandler.obtainMessage( |
Fyodor Kupolov | 041232a | 2016-02-22 15:01:45 -0800 | [diff] [blame] | 1726 | MESSAGE_COPY_SHARED_ACCOUNT, parentUserId, user.id, account)); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 1727 | } |
| 1728 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1732 | @Override |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1733 | public void hasFeatures(IAccountManagerResponse response, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1734 | Account account, String[] features, String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1735 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1736 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1737 | Log.v(TAG, "hasFeatures: " + account |
| 1738 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 1739 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1740 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1741 | + ", pid " + Binder.getCallingPid()); |
| 1742 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 1743 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1744 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 1745 | if (features == null) throw new IllegalArgumentException("features is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1746 | int userId = UserHandle.getCallingUserId(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1747 | checkReadAccountsPermitted(callingUid, account.type, userId, |
| 1748 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1749 | |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1750 | long identityToken = clearCallingIdentity(); |
| 1751 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1752 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1753 | new TestFeaturesSession(accounts, response, account, features).bind(); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1754 | } finally { |
| 1755 | restoreCallingIdentity(identityToken); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | private class TestFeaturesSession extends Session { |
| 1760 | private final String[] mFeatures; |
| 1761 | private final Account mAccount; |
| 1762 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1763 | public TestFeaturesSession(UserAccounts accounts, IAccountManagerResponse response, |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1764 | Account account, String[] features) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1765 | super(accounts, response, account.type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 1766 | true /* stripAuthTokenFromResult */, account.name, |
| 1767 | false /* authDetailsRequired */); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1768 | mFeatures = features; |
| 1769 | mAccount = account; |
| 1770 | } |
| 1771 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1772 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1773 | public void run() throws RemoteException { |
| 1774 | try { |
| 1775 | mAuthenticator.hasFeatures(this, mAccount, mFeatures); |
| 1776 | } catch (RemoteException e) { |
| 1777 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
| 1778 | } |
| 1779 | } |
| 1780 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1781 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1782 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 1783 | Bundle.setDefusable(result, true); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1784 | IAccountManagerResponse response = getResponseAndClose(); |
| 1785 | if (response != null) { |
| 1786 | try { |
| 1787 | if (result == null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 1788 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1789 | return; |
| 1790 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 1791 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1792 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 1793 | + response); |
| 1794 | } |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1795 | final Bundle newResult = new Bundle(); |
| 1796 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 1797 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 1798 | response.onResult(newResult); |
| 1799 | } catch (RemoteException e) { |
| 1800 | // if the caller is dead then there is no one to care about remote exceptions |
| 1801 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1802 | Log.v(TAG, "failure while notifying response", e); |
| 1803 | } |
| 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 1808 | @Override |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1809 | protected String toDebugString(long now) { |
Fred Quintana | 3084a6f | 2010-01-14 18:02:03 -0800 | [diff] [blame] | 1810 | return super.toDebugString(now) + ", hasFeatures" |
Fred Quintana | bb68a4f | 2010-01-13 17:28:39 -0800 | [diff] [blame] | 1811 | + ", " + mAccount |
| 1812 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 1813 | } |
| 1814 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1815 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 1816 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1817 | public void renameAccount( |
| 1818 | IAccountManagerResponse response, Account accountToRename, String newName) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1819 | final int callingUid = Binder.getCallingUid(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1820 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1821 | Log.v(TAG, "renameAccount: " + accountToRename + " -> " + newName |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1822 | + ", caller's uid " + callingUid |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1823 | + ", pid " + Binder.getCallingPid()); |
| 1824 | } |
| 1825 | if (accountToRename == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 1826 | int userId = UserHandle.getCallingUserId(); |
| 1827 | if (!isAccountManagedByCaller(accountToRename.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1828 | String msg = String.format( |
| 1829 | "uid %s cannot rename accounts of type: %s", |
| 1830 | callingUid, |
| 1831 | accountToRename.type); |
| 1832 | throw new SecurityException(msg); |
| 1833 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1834 | long identityToken = clearCallingIdentity(); |
| 1835 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1836 | UserAccounts accounts = getUserAccounts(userId); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1837 | Account resultingAccount = renameAccountInternal(accounts, accountToRename, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1838 | Bundle result = new Bundle(); |
| 1839 | result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name); |
| 1840 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type); |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 1841 | result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID, |
| 1842 | resultingAccount.getAccessId()); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1843 | try { |
| 1844 | response.onResult(result); |
| 1845 | } catch (RemoteException e) { |
| 1846 | Log.w(TAG, e.getMessage()); |
| 1847 | } |
| 1848 | } finally { |
| 1849 | restoreCallingIdentity(identityToken); |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | private Account renameAccountInternal( |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1854 | UserAccounts accounts, Account accountToRename, String newName) { |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1855 | Account resultAccount = null; |
| 1856 | /* |
| 1857 | * Cancel existing notifications. Let authenticators |
| 1858 | * re-post notifications as required. But we don't know if |
| 1859 | * the authenticators have bound their notifications to |
| 1860 | * now stale account name data. |
| 1861 | * |
| 1862 | * With a rename api, we might not need to do this anymore but it |
| 1863 | * shouldn't hurt. |
| 1864 | */ |
| 1865 | cancelNotification( |
| 1866 | getSigninRequiredNotificationId(accounts, accountToRename), |
| 1867 | new UserHandle(accounts.userId)); |
| 1868 | synchronized(accounts.credentialsPermissionNotificationIds) { |
| 1869 | for (Pair<Pair<Account, String>, Integer> pair: |
| 1870 | accounts.credentialsPermissionNotificationIds.keySet()) { |
| 1871 | if (accountToRename.equals(pair.first.first)) { |
| 1872 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
| 1873 | cancelNotification(id, new UserHandle(accounts.userId)); |
| 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1878 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1879 | Account renamedAccount = new Account(newName, accountToRename.type); |
| 1880 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1881 | final long accountId = accounts.accountsDb.findDeAccountId(accountToRename); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1882 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1883 | accounts.accountsDb.renameCeAccount(accountId, newName); |
| 1884 | accounts.accountsDb.renameDeAccount(accountId, newName, accountToRename.name); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1885 | } |
| 1886 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 1887 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1888 | } |
| 1889 | /* |
| 1890 | * Database transaction was successful. Clean up cached |
| 1891 | * data associated with the account in the user profile. |
| 1892 | */ |
Svet Ganov | 5c4a512 | 2016-09-23 19:29:11 -0700 | [diff] [blame] | 1893 | renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1894 | /* |
| 1895 | * Extract the data and token caches before removing the |
| 1896 | * old account to preserve the user data associated with |
| 1897 | * the account. |
| 1898 | */ |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1899 | Map<String, String> tmpData = accounts.userDataCache.get(accountToRename); |
| 1900 | Map<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1901 | removeAccountFromCacheLocked(accounts, accountToRename); |
| 1902 | /* |
| 1903 | * Update the cached data associated with the renamed |
| 1904 | * account. |
| 1905 | */ |
| 1906 | accounts.userDataCache.put(renamedAccount, tmpData); |
| 1907 | accounts.authTokenCache.put(renamedAccount, tmpTokens); |
| 1908 | accounts.previousNameCache.put( |
| 1909 | renamedAccount, |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 1910 | new AtomicReference<>(accountToRename.name)); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1911 | resultAccount = renamedAccount; |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1912 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1913 | int parentUserId = accounts.userId; |
| 1914 | if (canHaveProfile(parentUserId)) { |
| 1915 | /* |
| 1916 | * Owner or system user account was renamed, rename the account for |
| 1917 | * those users with which the account was shared. |
| 1918 | */ |
| 1919 | List<UserInfo> users = getUserManager().getUsers(true); |
| 1920 | for (UserInfo user : users) { |
| 1921 | if (user.isRestricted() |
| 1922 | && (user.restrictedProfileParentId == parentUserId)) { |
| 1923 | renameSharedAccountAsUser(accountToRename, newName, user.id); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1924 | } |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1925 | } |
| 1926 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 1927 | sendAccountsChangedBroadcast(accounts.userId); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1928 | } |
| 1929 | return resultAccount; |
| 1930 | } |
| 1931 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1932 | private boolean canHaveProfile(final int parentUserId) { |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 1933 | final UserInfo userInfo = getUserManager().getUserInfo(parentUserId); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1934 | return userInfo != null && userInfo.canHaveProfile(); |
| 1935 | } |
| 1936 | |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 1937 | @Override |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1938 | public void removeAccount(IAccountManagerResponse response, Account account, |
| 1939 | boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1940 | removeAccountAsUser( |
| 1941 | response, |
| 1942 | account, |
| 1943 | expectActivityLaunch, |
| 1944 | UserHandle.getCallingUserId()); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | @Override |
| 1948 | public void removeAccountAsUser(IAccountManagerResponse response, Account account, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 1949 | boolean expectActivityLaunch, int userId) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1950 | final int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1951 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1952 | Log.v(TAG, "removeAccount: " + account |
| 1953 | + ", response " + response |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1954 | + ", caller's uid " + callingUid |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1955 | + ", pid " + Binder.getCallingPid() |
| 1956 | + ", for user id " + userId); |
| 1957 | } |
| 1958 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 1959 | if (account == null) throw new IllegalArgumentException("account is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1960 | // Only allow the system process to modify accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 1961 | if (isCrossUser(callingUid, userId)) { |
| 1962 | throw new SecurityException( |
| 1963 | String.format( |
| 1964 | "User %s tying remove account for %s" , |
| 1965 | UserHandle.getCallingUserId(), |
| 1966 | userId)); |
| 1967 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1968 | /* |
| 1969 | * Only the system or authenticator should be allowed to remove accounts for that |
| 1970 | * authenticator. This will let users remove accounts (via Settings in the system) but not |
| 1971 | * arbitrary applications (like competing authenticators). |
| 1972 | */ |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 1973 | UserHandle user = UserHandle.of(userId); |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 1974 | if (!isAccountManagedByCaller(account.type, callingUid, user.getIdentifier()) |
| 1975 | && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 1976 | String msg = String.format( |
| 1977 | "uid %s cannot remove accounts of type: %s", |
| 1978 | callingUid, |
| 1979 | account.type); |
| 1980 | throw new SecurityException(msg); |
| 1981 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1982 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1983 | try { |
| 1984 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 1985 | "User cannot modify accounts"); |
| 1986 | } catch (RemoteException re) { |
| 1987 | } |
| 1988 | return; |
| 1989 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 1990 | if (!canUserModifyAccountsForType(userId, account.type, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1991 | try { |
| 1992 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 1993 | "User cannot modify accounts of this type (policy)."); |
| 1994 | } catch (RemoteException re) { |
| 1995 | } |
| 1996 | return; |
| 1997 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 1998 | long identityToken = clearCallingIdentity(); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 1999 | UserAccounts accounts = getUserAccounts(userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2000 | cancelNotification(getSigninRequiredNotificationId(accounts, account), user); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2001 | synchronized(accounts.credentialsPermissionNotificationIds) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2002 | for (Pair<Pair<Account, String>, Integer> pair: |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2003 | accounts.credentialsPermissionNotificationIds.keySet()) { |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2004 | if (account.equals(pair.first.first)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2005 | int id = accounts.credentialsPermissionNotificationIds.get(pair); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2006 | cancelNotification(id, user); |
Costin Manolache | ec0c4f4 | 2010-11-16 09:57:28 -0800 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2010 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2011 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2012 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2013 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2014 | accountId, |
| 2015 | accounts, |
| 2016 | callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2017 | try { |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2018 | new RemoveAccountSession(accounts, response, account, expectActivityLaunch).bind(); |
| 2019 | } finally { |
| 2020 | restoreCallingIdentity(identityToken); |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | @Override |
| 2025 | public boolean removeAccountExplicitly(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2026 | final int callingUid = Binder.getCallingUid(); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2027 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2028 | Log.v(TAG, "removeAccountExplicitly: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2029 | + ", caller's uid " + callingUid |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2030 | + ", pid " + Binder.getCallingPid()); |
| 2031 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2032 | int userId = Binder.getCallingUserHandle().getIdentifier(); |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2033 | if (account == null) { |
| 2034 | /* |
| 2035 | * Null accounts should result in returning false, as per |
| 2036 | * AccountManage.addAccountExplicitly(...) java doc. |
| 2037 | */ |
| 2038 | Log.e(TAG, "account is null"); |
| 2039 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2040 | } else if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2041 | String msg = String.format( |
| 2042 | "uid %s cannot explicitly add accounts of type: %s", |
| 2043 | callingUid, |
| 2044 | account.type); |
| 2045 | throw new SecurityException(msg); |
| 2046 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 2047 | removeVisibleListFunctionality(account, getUserAccounts(UserHandle.getUserId(callingUid))); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2048 | UserAccounts accounts = getUserAccountsForCaller(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2049 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2050 | logRecord( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2051 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_REMOVE, |
| 2052 | AccountsDb.TABLE_ACCOUNTS, |
Dmitry Dementyev | e59fc5f | 2016-07-08 10:46:22 -0700 | [diff] [blame] | 2053 | accountId, |
| 2054 | accounts, |
| 2055 | callingUid); |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2056 | long identityToken = clearCallingIdentity(); |
| 2057 | try { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2058 | return removeAccountInternal(accounts, account, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2059 | } finally { |
| 2060 | restoreCallingIdentity(identityToken); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2061 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2064 | private class RemoveAccountSession extends Session { |
| 2065 | final Account mAccount; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2066 | public RemoveAccountSession(UserAccounts accounts, IAccountManagerResponse response, |
Simranjit Singh Kohli | 8778f99 | 2014-11-05 21:33:55 -0800 | [diff] [blame] | 2067 | Account account, boolean expectActivityLaunch) { |
| 2068 | super(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2069 | true /* stripAuthTokenFromResult */, account.name, |
| 2070 | false /* authDetailsRequired */); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2071 | mAccount = account; |
| 2072 | } |
| 2073 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2074 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2075 | protected String toDebugString(long now) { |
| 2076 | return super.toDebugString(now) + ", removeAccount" |
| 2077 | + ", account " + mAccount; |
| 2078 | } |
| 2079 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2080 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2081 | public void run() throws RemoteException { |
| 2082 | mAuthenticator.getAccountRemovalAllowed(this, mAccount); |
| 2083 | } |
| 2084 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2085 | @Override |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2086 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2087 | Bundle.setDefusable(result, true); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2088 | if (result != null && result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) |
| 2089 | && !result.containsKey(AccountManager.KEY_INTENT)) { |
| 2090 | final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2091 | if (removalAllowed) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2092 | removeAccountInternal(mAccounts, mAccount, getCallingUid()); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2093 | } |
| 2094 | IAccountManagerResponse response = getResponseAndClose(); |
| 2095 | if (response != null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2096 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2097 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2098 | + response); |
| 2099 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2100 | Bundle result2 = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2101 | result2.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, removalAllowed); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2102 | try { |
| 2103 | response.onResult(result2); |
| 2104 | } catch (RemoteException e) { |
| 2105 | // ignore |
| 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | super.onResult(result); |
| 2110 | } |
| 2111 | } |
| 2112 | |
Fyodor Kupolov | eeca658 | 2016-04-08 12:14:04 -0700 | [diff] [blame] | 2113 | @VisibleForTesting |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2114 | protected void removeAccountInternal(Account account) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2115 | removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid()); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2116 | } |
| 2117 | |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2118 | private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) { |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2119 | boolean isChanged = false; |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2120 | boolean userUnlocked = isLocalUnlockedUser(accounts.userId); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2121 | if (!userUnlocked) { |
| 2122 | Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId |
| 2123 | + " is still locked. CE data will be removed later"); |
| 2124 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2125 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2126 | accounts.accountsDb.beginTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2127 | // Set to a dummy value, this will only be used if the database |
| 2128 | // transaction succeeds. |
| 2129 | long accountId = -1; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2130 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2131 | accountId = accounts.accountsDb.findDeAccountId(account); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2132 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2133 | accounts.accountsDb.deleteDeAccount(accountId); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2134 | if (userUnlocked) { |
| 2135 | // Delete from CE table |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2136 | accounts.accountsDb.deleteCeAccount(accountId); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2137 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2138 | accounts.accountsDb.setTransactionSuccessful(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2139 | isChanged = true; |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2140 | } |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2141 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2142 | accounts.accountsDb.endTransaction(); |
Fyodor Kupolov | 35f6808 | 2016-04-06 12:14:17 -0700 | [diff] [blame] | 2143 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2144 | if (isChanged) { |
| 2145 | removeAccountFromCacheLocked(accounts, account); |
| 2146 | // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occured. |
| 2147 | sendAccountsChangedBroadcast(accounts.userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2148 | String action = userUnlocked ? AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE |
| 2149 | : AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE_DE; |
| 2150 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2151 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2152 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2153 | long id = Binder.clearCallingIdentity(); |
| 2154 | try { |
| 2155 | int parentUserId = accounts.userId; |
| 2156 | if (canHaveProfile(parentUserId)) { |
| 2157 | // Remove from any restricted profiles that are sharing this account. |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 2158 | List<UserInfo> users = getUserManager().getUsers(true); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2159 | for (UserInfo user : users) { |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2160 | if (user.isRestricted() && parentUserId == (user.restrictedProfileParentId)) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2161 | removeSharedAccountAsUser(account, user.id, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2162 | } |
| 2163 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2164 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 2165 | } finally { |
| 2166 | Binder.restoreCallingIdentity(id); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 2167 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2168 | |
| 2169 | if (isChanged) { |
| 2170 | synchronized (accounts.credentialsPermissionNotificationIds) { |
| 2171 | for (Pair<Pair<Account, String>, Integer> key |
| 2172 | : accounts.credentialsPermissionNotificationIds.keySet()) { |
| 2173 | if (account.equals(key.first.first) |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 2174 | && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2175 | final int uid = (Integer) key.second; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 2176 | mHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2177 | account, uid, false)); |
| 2178 | } |
| 2179 | } |
| 2180 | } |
| 2181 | } |
| 2182 | |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2183 | return isChanged; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2186 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2187 | public void invalidateAuthToken(String accountType, String authToken) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2188 | int callerUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2189 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2190 | Log.v(TAG, "invalidateAuthToken: accountType " + accountType |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2191 | + ", caller's uid " + callerUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2192 | + ", pid " + Binder.getCallingPid()); |
| 2193 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2194 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
| 2195 | if (authToken == null) throw new IllegalArgumentException("authToken is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2196 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2197 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2198 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2199 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2200 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2201 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2202 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2203 | invalidateAuthTokenLocked(accounts, accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2204 | invalidateCustomTokenLocked(accounts, accountType, authToken); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2205 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2206 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2207 | accounts.accountsDb.endTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2208 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2209 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2210 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2211 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2215 | private void invalidateCustomTokenLocked( |
| 2216 | UserAccounts accounts, |
| 2217 | String accountType, |
| 2218 | String authToken) { |
| 2219 | if (authToken == null || accountType == null) { |
| 2220 | return; |
| 2221 | } |
| 2222 | // Also wipe out cached token in memory. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2223 | accounts.accountTokenCaches.remove(accountType, authToken); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2224 | } |
| 2225 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2226 | private void invalidateAuthTokenLocked(UserAccounts accounts, String accountType, |
| 2227 | String authToken) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2228 | if (authToken == null || accountType == null) { |
| 2229 | return; |
| 2230 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2231 | Cursor cursor = accounts.accountsDb.findAuthtokenForAllAccounts(accountType, authToken); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2232 | try { |
| 2233 | while (cursor.moveToNext()) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2234 | String authTokenId = cursor.getString(0); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2235 | String accountName = cursor.getString(1); |
| 2236 | String authTokenType = cursor.getString(2); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2237 | accounts.accountsDb.deleteAuthToken(authTokenId); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2238 | writeAuthTokenIntoCacheLocked( |
| 2239 | accounts, |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2240 | new Account(accountName, accountType), |
| 2241 | authTokenType, |
| 2242 | null); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2243 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2244 | } finally { |
| 2245 | cursor.close(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2246 | } |
| 2247 | } |
| 2248 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2249 | private void saveCachedToken( |
| 2250 | UserAccounts accounts, |
| 2251 | Account account, |
| 2252 | String callerPkg, |
| 2253 | byte[] callerSigDigest, |
| 2254 | String tokenType, |
| 2255 | String token, |
| 2256 | long expiryMillis) { |
| 2257 | |
| 2258 | if (account == null || tokenType == null || callerPkg == null || callerSigDigest == null) { |
| 2259 | return; |
| 2260 | } |
| 2261 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2262 | UserHandle.of(accounts.userId)); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2263 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2264 | accounts.accountTokenCaches.put( |
| 2265 | account, token, tokenType, callerPkg, callerSigDigest, expiryMillis); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2269 | private boolean saveAuthTokenToDatabase(UserAccounts accounts, Account account, String type, |
| 2270 | String authToken) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2271 | if (account == null || type == null) { |
| 2272 | return false; |
| 2273 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2274 | cancelNotification(getSigninRequiredNotificationId(accounts, account), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 2275 | UserHandle.of(accounts.userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2276 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2277 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2278 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2279 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2280 | if (accountId < 0) { |
| 2281 | return false; |
| 2282 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2283 | accounts.accountsDb.deleteAuthtokensByAccountIdAndType(accountId, type); |
| 2284 | if (accounts.accountsDb.insertAuthToken(accountId, type, authToken) >= 0) { |
| 2285 | accounts.accountsDb.setTransactionSuccessful(); |
| 2286 | writeAuthTokenIntoCacheLocked(accounts, account, type, authToken); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2287 | return true; |
| 2288 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2289 | return false; |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2290 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2291 | accounts.accountsDb.endTransaction(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2292 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2296 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2297 | public String peekAuthToken(Account account, String authTokenType) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2298 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2299 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2300 | Log.v(TAG, "peekAuthToken: " + account |
| 2301 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2302 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2303 | + ", pid " + Binder.getCallingPid()); |
| 2304 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2305 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2306 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2307 | int userId = UserHandle.getCallingUserId(); |
| 2308 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2309 | String msg = String.format( |
| 2310 | "uid %s cannot peek the authtokens associated with accounts of type: %s", |
| 2311 | callingUid, |
| 2312 | account.type); |
| 2313 | throw new SecurityException(msg); |
| 2314 | } |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 2315 | if (!isLocalUnlockedUser(userId)) { |
Fyodor Kupolov | c86c3fd | 2016-04-18 13:57:31 -0700 | [diff] [blame] | 2316 | Log.w(TAG, "Authtoken not available - user " + userId + " data is locked. callingUid " |
| 2317 | + callingUid); |
| 2318 | return null; |
| 2319 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2320 | long identityToken = clearCallingIdentity(); |
| 2321 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2322 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2323 | return readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2324 | } finally { |
| 2325 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2326 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2327 | } |
| 2328 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2329 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2330 | public void setAuthToken(Account account, String authTokenType, String authToken) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2331 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2332 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2333 | Log.v(TAG, "setAuthToken: " + account |
| 2334 | + ", authTokenType " + authTokenType |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2335 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2336 | + ", pid " + Binder.getCallingPid()); |
| 2337 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2338 | if (account == null) throw new IllegalArgumentException("account is null"); |
| 2339 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2340 | int userId = UserHandle.getCallingUserId(); |
| 2341 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2342 | String msg = String.format( |
| 2343 | "uid %s cannot set auth tokens associated with accounts of type: %s", |
| 2344 | callingUid, |
| 2345 | account.type); |
| 2346 | throw new SecurityException(msg); |
| 2347 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2348 | long identityToken = clearCallingIdentity(); |
| 2349 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2350 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2351 | saveAuthTokenToDatabase(accounts, account, authTokenType, authToken); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2352 | } finally { |
| 2353 | restoreCallingIdentity(identityToken); |
| 2354 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2355 | } |
| 2356 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2357 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2358 | public void setPassword(Account account, String password) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2359 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2360 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2361 | Log.v(TAG, "setAuthToken: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2362 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2363 | + ", pid " + Binder.getCallingPid()); |
| 2364 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2365 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2366 | int userId = UserHandle.getCallingUserId(); |
| 2367 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2368 | String msg = String.format( |
| 2369 | "uid %s cannot set secrets for accounts of type: %s", |
| 2370 | callingUid, |
| 2371 | account.type); |
| 2372 | throw new SecurityException(msg); |
| 2373 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2374 | long identityToken = clearCallingIdentity(); |
| 2375 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2376 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2377 | setPasswordInternal(accounts, account, password, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2378 | } finally { |
| 2379 | restoreCallingIdentity(identityToken); |
| 2380 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2383 | private void setPasswordInternal(UserAccounts accounts, Account account, String password, |
| 2384 | int callingUid) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2385 | if (account == null) { |
| 2386 | return; |
| 2387 | } |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2388 | boolean isChanged = false; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2389 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2390 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2391 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2392 | final long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2393 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2394 | accounts.accountsDb.updateCeAccountPassword(accountId, password); |
| 2395 | accounts.accountsDb.deleteAuthTokensByAccountId(accountId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2396 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2397 | accounts.accountTokenCaches.remove(account); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2398 | accounts.accountsDb.setTransactionSuccessful(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2399 | // If there is an account whose password will be updated and the database |
| 2400 | // transactions succeed, then we say that a change has occured. Even if the |
| 2401 | // new password is the same as the old and there were no authtokens to delete. |
| 2402 | isChanged = true; |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2403 | String action = (password == null || password.length() == 0) ? |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2404 | AccountsDb.DEBUG_ACTION_CLEAR_PASSWORD |
| 2405 | : AccountsDb.DEBUG_ACTION_SET_PASSWORD; |
| 2406 | logRecord(action, AccountsDb.TABLE_ACCOUNTS, accountId, accounts, callingUid); |
Costin Manolache | f5ffe89 | 2011-01-19 09:35:32 -0800 | [diff] [blame] | 2407 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 2408 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2409 | accounts.accountsDb.endTransaction(); |
Carlos Valdivia | 98b5f9d | 2016-07-07 17:47:12 -0700 | [diff] [blame] | 2410 | if (isChanged) { |
| 2411 | // Send LOGIN_ACCOUNTS_CHANGED only if the something changed. |
| 2412 | sendAccountsChangedBroadcast(accounts.userId); |
| 2413 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2414 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 2415 | } |
Fred Quintana | 3ecd5f4 | 2009-09-17 12:42:35 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2418 | private void sendAccountsChangedBroadcast(int userId) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2419 | Log.i(TAG, "the accounts changed, sending broadcast of " |
| 2420 | + ACCOUNTS_CHANGED_INTENT.getAction()); |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 2421 | mContext.sendBroadcastAsUser(ACCOUNTS_CHANGED_INTENT, new UserHandle(userId)); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2424 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2425 | public void clearPassword(Account account) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2426 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2427 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2428 | Log.v(TAG, "clearPassword: " + account |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2429 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2430 | + ", pid " + Binder.getCallingPid()); |
| 2431 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2432 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2433 | int userId = UserHandle.getCallingUserId(); |
| 2434 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2435 | String msg = String.format( |
| 2436 | "uid %s cannot clear passwords for accounts of type: %s", |
| 2437 | callingUid, |
| 2438 | account.type); |
| 2439 | throw new SecurityException(msg); |
| 2440 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2441 | long identityToken = clearCallingIdentity(); |
| 2442 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2443 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 2444 | setPasswordInternal(accounts, account, null, callingUid); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2445 | } finally { |
| 2446 | restoreCallingIdentity(identityToken); |
| 2447 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2448 | } |
| 2449 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2450 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2451 | public void setUserData(Account account, String key, String value) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2452 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2453 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2454 | Log.v(TAG, "setUserData: " + account |
| 2455 | + ", key " + key |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2456 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2457 | + ", pid " + Binder.getCallingPid()); |
| 2458 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2459 | if (key == null) throw new IllegalArgumentException("key is null"); |
| 2460 | if (account == null) throw new IllegalArgumentException("account is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2461 | int userId = UserHandle.getCallingUserId(); |
| 2462 | if (!isAccountManagedByCaller(account.type, callingUid, userId)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 2463 | String msg = String.format( |
| 2464 | "uid %s cannot set user data for accounts of type: %s", |
| 2465 | callingUid, |
| 2466 | account.type); |
| 2467 | throw new SecurityException(msg); |
| 2468 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2469 | long identityToken = clearCallingIdentity(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2470 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2471 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2472 | synchronized (accounts.cacheLock) { |
| 2473 | if (!accountExistsCacheLocked(accounts, account)) { |
| 2474 | return; |
| 2475 | } |
| 2476 | setUserdataInternalLocked(accounts, account, key, value); |
| 2477 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2478 | } finally { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2479 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2480 | } |
| 2481 | } |
| 2482 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2483 | private boolean accountExistsCacheLocked(UserAccounts accounts, Account account) { |
| 2484 | if (accounts.accountCache.containsKey(account.type)) { |
| 2485 | for (Account acc : accounts.accountCache.get(account.type)) { |
| 2486 | if (acc.name.equals(account.name)) { |
| 2487 | return true; |
| 2488 | } |
| 2489 | } |
| 2490 | } |
| 2491 | return false; |
| 2492 | } |
| 2493 | |
| 2494 | private void setUserdataInternalLocked(UserAccounts accounts, Account account, String key, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2495 | String value) { |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 2496 | if (account == null || key == null) { |
| 2497 | return; |
| 2498 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2499 | accounts.accountsDb.beginTransaction(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2500 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2501 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2502 | if (accountId < 0) { |
| 2503 | return; |
| 2504 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2505 | long extrasId = accounts.accountsDb.findExtrasIdByAccountId(accountId, key); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2506 | if (extrasId < 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2507 | extrasId = accounts.accountsDb.insertExtra(accountId, key, value); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2508 | if (extrasId < 0) { |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2509 | return; |
| 2510 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2511 | } else if (!accounts.accountsDb.updateExtra(extrasId, value)) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2512 | return; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2513 | } |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2514 | writeUserDataIntoCacheLocked(accounts, account, key, value); |
| 2515 | accounts.accountsDb.setTransactionSuccessful(); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 2516 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 2517 | accounts.accountsDb.endTransaction(); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 2518 | } |
| 2519 | } |
| 2520 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2521 | private void onResult(IAccountManagerResponse response, Bundle result) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2522 | if (result == null) { |
| 2523 | Log.e(TAG, "the result is unexpectedly null", new Exception()); |
| 2524 | } |
| 2525 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2526 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 2527 | + response); |
| 2528 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2529 | try { |
| 2530 | response.onResult(result); |
| 2531 | } catch (RemoteException e) { |
| 2532 | // if the caller is dead then there is no one to care about remote |
| 2533 | // exceptions |
| 2534 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2535 | Log.v(TAG, "failure while notifying response", e); |
| 2536 | } |
| 2537 | } |
| 2538 | } |
| 2539 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2540 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2541 | public void getAuthTokenLabel(IAccountManagerResponse response, final String accountType, |
| 2542 | final String authTokenType) |
| 2543 | throws RemoteException { |
| 2544 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2545 | if (authTokenType == null) throw new IllegalArgumentException("authTokenType is null"); |
| 2546 | |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2547 | final int callingUid = getCallingUid(); |
| 2548 | clearCallingIdentity(); |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 2549 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2550 | throw new SecurityException("can only call from system"); |
| 2551 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2552 | int userId = UserHandle.getUserId(callingUid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2553 | long identityToken = clearCallingIdentity(); |
| 2554 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2555 | UserAccounts accounts = getUserAccounts(userId); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2556 | new Session(accounts, response, accountType, false /* expectActivityLaunch */, |
| 2557 | false /* stripAuthTokenFromResult */, null /* accountName */, |
| 2558 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2559 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2560 | protected String toDebugString(long now) { |
| 2561 | return super.toDebugString(now) + ", getAuthTokenLabel" |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 2562 | + ", " + accountType |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2563 | + ", authTokenType " + authTokenType; |
| 2564 | } |
| 2565 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2566 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2567 | public void run() throws RemoteException { |
| 2568 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2569 | } |
| 2570 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2571 | @Override |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2572 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2573 | Bundle.setDefusable(result, true); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2574 | if (result != null) { |
| 2575 | String label = result.getString(AccountManager.KEY_AUTH_TOKEN_LABEL); |
| 2576 | Bundle bundle = new Bundle(); |
| 2577 | bundle.putString(AccountManager.KEY_AUTH_TOKEN_LABEL, label); |
| 2578 | super.onResult(bundle); |
| 2579 | return; |
| 2580 | } else { |
| 2581 | super.onResult(result); |
| 2582 | } |
| 2583 | } |
| 2584 | }.bind(); |
| 2585 | } finally { |
| 2586 | restoreCallingIdentity(identityToken); |
| 2587 | } |
| 2588 | } |
| 2589 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2590 | @Override |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2591 | public void getAuthToken( |
| 2592 | IAccountManagerResponse response, |
| 2593 | final Account account, |
| 2594 | final String authTokenType, |
| 2595 | final boolean notifyOnAuthFailure, |
| 2596 | final boolean expectActivityLaunch, |
| 2597 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2598 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2599 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2600 | Log.v(TAG, "getAuthToken: " + account |
| 2601 | + ", response " + response |
| 2602 | + ", authTokenType " + authTokenType |
| 2603 | + ", notifyOnAuthFailure " + notifyOnAuthFailure |
| 2604 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2605 | + ", caller's uid " + Binder.getCallingUid() |
| 2606 | + ", pid " + Binder.getCallingPid()); |
| 2607 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2608 | if (response == null) throw new IllegalArgumentException("response is null"); |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2609 | try { |
| 2610 | if (account == null) { |
| 2611 | Slog.w(TAG, "getAuthToken called with null account"); |
| 2612 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "account is null"); |
| 2613 | return; |
| 2614 | } |
| 2615 | if (authTokenType == null) { |
| 2616 | Slog.w(TAG, "getAuthToken called with null authTokenType"); |
| 2617 | response.onError(AccountManager.ERROR_CODE_BAD_ARGUMENTS, "authTokenType is null"); |
| 2618 | return; |
| 2619 | } |
| 2620 | } catch (RemoteException e) { |
| 2621 | Slog.w(TAG, "Failed to report error back to the client." + e); |
| 2622 | return; |
| 2623 | } |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2624 | int userId = UserHandle.getCallingUserId(); |
| 2625 | long ident = Binder.clearCallingIdentity(); |
| 2626 | final UserAccounts accounts; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2627 | final RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2628 | try { |
| 2629 | accounts = getUserAccounts(userId); |
| 2630 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 2631 | AuthenticatorDescription.newKey(account.type), accounts.userId); |
| 2632 | } finally { |
| 2633 | Binder.restoreCallingIdentity(ident); |
| 2634 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2635 | |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2636 | final boolean customTokens = |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2637 | authenticatorInfo != null && authenticatorInfo.type.customTokens; |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2638 | |
| 2639 | // skip the check if customTokens |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2640 | final int callerUid = Binder.getCallingUid(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 2641 | final boolean permissionGranted = |
| 2642 | customTokens || permissionIsGranted(account, authTokenType, callerUid, userId); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2643 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2644 | // Get the calling package. We will use it for the purpose of caching. |
| 2645 | final String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2646 | List<String> callerOwnedPackageNames; |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2647 | ident = Binder.clearCallingIdentity(); |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2648 | try { |
| 2649 | callerOwnedPackageNames = Arrays.asList(mPackageManager.getPackagesForUid(callerUid)); |
| 2650 | } finally { |
| 2651 | Binder.restoreCallingIdentity(ident); |
| 2652 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2653 | if (callerPkg == null || !callerOwnedPackageNames.contains(callerPkg)) { |
| 2654 | String msg = String.format( |
| 2655 | "Uid %s is attempting to illegally masquerade as package %s!", |
| 2656 | callerUid, |
| 2657 | callerPkg); |
| 2658 | throw new SecurityException(msg); |
| 2659 | } |
| 2660 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2661 | // let authenticator know the identity of the caller |
| 2662 | loginOptions.putInt(AccountManager.KEY_CALLER_UID, callerUid); |
| 2663 | loginOptions.putInt(AccountManager.KEY_CALLER_PID, Binder.getCallingPid()); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2664 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2665 | if (notifyOnAuthFailure) { |
| 2666 | loginOptions.putBoolean(AccountManager.KEY_NOTIFY_ON_FAILURE, true); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2667 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2668 | |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2669 | long identityToken = clearCallingIdentity(); |
| 2670 | try { |
Amith Yamasani | e736001 | 2015-06-03 17:39:40 -0700 | [diff] [blame] | 2671 | // Distill the caller's package signatures into a single digest. |
| 2672 | final byte[] callerPkgSigDigest = calculatePackageSignatureDigest(callerPkg); |
| 2673 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2674 | // if the caller has permission, do the peek. otherwise go the more expensive |
| 2675 | // route of starting a Session |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2676 | if (!customTokens && permissionGranted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2677 | String authToken = readAuthTokenInternal(accounts, account, authTokenType); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2678 | if (authToken != null) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2679 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2680 | result.putString(AccountManager.KEY_AUTHTOKEN, authToken); |
| 2681 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2682 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2683 | onResult(response, result); |
| 2684 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2685 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2688 | if (customTokens) { |
| 2689 | /* |
| 2690 | * Look up tokens in the new cache only if the loginOptions don't have parameters |
| 2691 | * outside of those expected to be injected by the AccountManager, e.g. |
| 2692 | * ANDORID_PACKAGE_NAME. |
| 2693 | */ |
| 2694 | String token = readCachedTokenInternal( |
| 2695 | accounts, |
| 2696 | account, |
| 2697 | authTokenType, |
| 2698 | callerPkg, |
| 2699 | callerPkgSigDigest); |
| 2700 | if (token != null) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2701 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2702 | Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator."); |
| 2703 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2704 | Bundle result = new Bundle(); |
| 2705 | result.putString(AccountManager.KEY_AUTHTOKEN, token); |
| 2706 | result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); |
| 2707 | result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); |
| 2708 | onResult(response, result); |
| 2709 | return; |
| 2710 | } |
| 2711 | } |
| 2712 | |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2713 | new Session( |
| 2714 | accounts, |
| 2715 | response, |
| 2716 | account.type, |
| 2717 | expectActivityLaunch, |
| 2718 | false /* stripAuthTokenFromResult */, |
| 2719 | account.name, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2720 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2721 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2722 | protected String toDebugString(long now) { |
| 2723 | if (loginOptions != null) loginOptions.keySet(); |
| 2724 | return super.toDebugString(now) + ", getAuthToken" |
| 2725 | + ", " + account |
| 2726 | + ", authTokenType " + authTokenType |
| 2727 | + ", loginOptions " + loginOptions |
| 2728 | + ", notifyOnAuthFailure " + notifyOnAuthFailure; |
| 2729 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2730 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2731 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2732 | public void run() throws RemoteException { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2733 | // If the caller doesn't have permission then create and return the |
| 2734 | // "grant permission" intent instead of the "getAuthToken" intent. |
| 2735 | if (!permissionGranted) { |
| 2736 | mAuthenticator.getAuthTokenLabel(this, authTokenType); |
| 2737 | } else { |
| 2738 | mAuthenticator.getAuthToken(this, account, authTokenType, loginOptions); |
| 2739 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2740 | } |
| 2741 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2742 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2743 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2744 | Bundle.setDefusable(result, true); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2745 | if (result != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2746 | if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2747 | Intent intent = newGrantCredentialsPermissionIntent( |
| 2748 | account, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2749 | null, |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2750 | callerUid, |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2751 | new AccountAuthenticatorResponse(this), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2752 | authTokenType, |
| 2753 | true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2754 | Bundle bundle = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2755 | bundle.putParcelable(AccountManager.KEY_INTENT, intent); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2756 | onResult(bundle); |
| 2757 | return; |
| 2758 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2759 | String authToken = result.getString(AccountManager.KEY_AUTHTOKEN); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2760 | if (authToken != null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2761 | String name = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 2762 | String type = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2763 | if (TextUtils.isEmpty(type) || TextUtils.isEmpty(name)) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2764 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2765 | "the type and name should not be empty"); |
| 2766 | return; |
| 2767 | } |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2768 | Account resultAccount = new Account(name, type); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2769 | if (!customTokens) { |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2770 | saveAuthTokenToDatabase( |
| 2771 | mAccounts, |
| 2772 | resultAccount, |
| 2773 | authTokenType, |
| 2774 | authToken); |
| 2775 | } |
| 2776 | long expiryMillis = result.getLong( |
| 2777 | AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY, 0L); |
| 2778 | if (customTokens |
| 2779 | && expiryMillis > System.currentTimeMillis()) { |
| 2780 | saveCachedToken( |
| 2781 | mAccounts, |
| 2782 | account, |
| 2783 | callerPkg, |
| 2784 | callerPkgSigDigest, |
| 2785 | authTokenType, |
| 2786 | authToken, |
| 2787 | expiryMillis); |
Costin Manolache | a40c630 | 2010-12-13 14:50:45 -0800 | [diff] [blame] | 2788 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2789 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2790 | |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2791 | Intent intent = result.getParcelable(AccountManager.KEY_INTENT); |
Costin Manolache | d606045 | 2011-01-24 16:11:36 -0800 | [diff] [blame] | 2792 | if (intent != null && notifyOnAuthFailure && !customTokens) { |
Carlos Valdivia | 06329e5f | 2016-05-07 21:46:15 -0700 | [diff] [blame] | 2793 | /* |
| 2794 | * Make sure that the supplied intent is owned by the authenticator |
| 2795 | * giving it to the system. Otherwise a malicious authenticator could |
| 2796 | * have users launching arbitrary activities by tricking users to |
| 2797 | * interact with malicious notifications. |
| 2798 | */ |
| 2799 | checkKeyIntent( |
| 2800 | Binder.getCallingUid(), |
| 2801 | intent); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2802 | doNotification(mAccounts, |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 2803 | account, result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE), |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2804 | intent, "android", accounts.userId); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2805 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2806 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2807 | super.onResult(result); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2808 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2809 | }.bind(); |
| 2810 | } finally { |
| 2811 | restoreCallingIdentity(identityToken); |
| 2812 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 2815 | private byte[] calculatePackageSignatureDigest(String callerPkg) { |
| 2816 | MessageDigest digester; |
| 2817 | try { |
| 2818 | digester = MessageDigest.getInstance("SHA-256"); |
| 2819 | PackageInfo pkgInfo = mPackageManager.getPackageInfo( |
| 2820 | callerPkg, PackageManager.GET_SIGNATURES); |
| 2821 | for (Signature sig : pkgInfo.signatures) { |
| 2822 | digester.update(sig.toByteArray()); |
| 2823 | } |
| 2824 | } catch (NoSuchAlgorithmException x) { |
| 2825 | Log.wtf(TAG, "SHA-256 should be available", x); |
| 2826 | digester = null; |
| 2827 | } catch (NameNotFoundException e) { |
| 2828 | Log.w(TAG, "Could not find packageinfo for: " + callerPkg); |
| 2829 | digester = null; |
| 2830 | } |
| 2831 | return (digester == null) ? null : digester.digest(); |
| 2832 | } |
| 2833 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2834 | private void createNoCredentialsPermissionNotification(Account account, Intent intent, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2835 | String packageName, int userId) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2836 | int uid = intent.getIntExtra( |
| 2837 | GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1); |
| 2838 | String authTokenType = intent.getStringExtra( |
| 2839 | GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE); |
Eric Fischer | ee452ee | 2009-08-31 17:58:06 -0700 | [diff] [blame] | 2840 | final String titleAndSubtitle = |
| 2841 | mContext.getString(R.string.permission_request_notification_with_subtitle, |
| 2842 | account.name); |
| 2843 | final int index = titleAndSubtitle.indexOf('\n'); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2844 | String title = titleAndSubtitle; |
| 2845 | String subtitle = ""; |
| 2846 | if (index > 0) { |
| 2847 | title = titleAndSubtitle.substring(0, index); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 2848 | subtitle = titleAndSubtitle.substring(index + 1); |
Costin Manolache | 85e7279 | 2011-10-07 09:42:49 -0700 | [diff] [blame] | 2849 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 2850 | UserHandle user = UserHandle.of(userId); |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 2851 | Context contextForUser = getContextForUser(user); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 2852 | Notification n = new Notification.Builder(contextForUser) |
| 2853 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 2854 | .setWhen(0) |
| 2855 | .setColor(contextForUser.getColor( |
| 2856 | com.android.internal.R.color.system_notification_accent_color)) |
| 2857 | .setContentTitle(title) |
| 2858 | .setContentText(subtitle) |
| 2859 | .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent, |
| 2860 | PendingIntent.FLAG_CANCEL_CURRENT, null, user)) |
| 2861 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 2862 | installNotification(getCredentialPermissionNotificationId( |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2863 | account, authTokenType, uid), n, packageName, user.getIdentifier()); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2864 | } |
| 2865 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2866 | private Intent newGrantCredentialsPermissionIntent(Account account, String packageName, |
| 2867 | int uid, AccountAuthenticatorResponse response, String authTokenType, |
| 2868 | boolean startInNewTask) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2869 | |
| 2870 | Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2871 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 2872 | if (startInNewTask) { |
| 2873 | // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag. |
| 2874 | // Since it was set in Eclair+ we can't change it without breaking apps using |
| 2875 | // the intent from a non-Activity context. This is the default behavior. |
| 2876 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2877 | } |
| 2878 | intent.addCategory(String.valueOf(getCredentialPermissionNotificationId(account, |
| 2879 | authTokenType, uid) + (packageName != null ? packageName : ""))); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2880 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2881 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType); |
| 2882 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2883 | intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, uid); |
Costin Manolache | 5f383ad9 | 2010-12-02 16:44:46 -0800 | [diff] [blame] | 2884 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2885 | return intent; |
| 2886 | } |
| 2887 | |
| 2888 | private Integer getCredentialPermissionNotificationId(Account account, String authTokenType, |
| 2889 | int uid) { |
| 2890 | Integer id; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2891 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2892 | synchronized (accounts.credentialsPermissionNotificationIds) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2893 | final Pair<Pair<Account, String>, Integer> key = |
| 2894 | new Pair<Pair<Account, String>, Integer>( |
| 2895 | new Pair<Account, String>(account, authTokenType), uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2896 | id = accounts.credentialsPermissionNotificationIds.get(key); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2897 | if (id == null) { |
| 2898 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2899 | accounts.credentialsPermissionNotificationIds.put(key, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2900 | } |
| 2901 | } |
| 2902 | return id; |
| 2903 | } |
| 2904 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2905 | private Integer getSigninRequiredNotificationId(UserAccounts accounts, Account account) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2906 | Integer id; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2907 | synchronized (accounts.signinRequiredNotificationIds) { |
| 2908 | id = accounts.signinRequiredNotificationIds.get(account); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2909 | if (id == null) { |
| 2910 | id = mNotificationIds.incrementAndGet(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2911 | accounts.signinRequiredNotificationIds.put(account, id); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 2912 | } |
| 2913 | } |
| 2914 | return id; |
| 2915 | } |
| 2916 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 2917 | @Override |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 2918 | public void addAccount(final IAccountManagerResponse response, final String accountType, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2919 | final String authTokenType, final String[] requiredFeatures, |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2920 | final boolean expectActivityLaunch, final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2921 | Bundle.setDefusable(optionsIn, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2922 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2923 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2924 | + ", response " + response |
| 2925 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2926 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 2927 | + ", expectActivityLaunch " + expectActivityLaunch |
| 2928 | + ", caller's uid " + Binder.getCallingUid() |
| 2929 | + ", pid " + Binder.getCallingPid()); |
| 2930 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 2931 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 2932 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2933 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2934 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2935 | final int uid = Binder.getCallingUid(); |
| 2936 | final int userId = UserHandle.getUserId(uid); |
| 2937 | if (!canUserModifyAccounts(userId, uid)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2938 | try { |
| 2939 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 2940 | "User is not allowed to add an account!"); |
| 2941 | } catch (RemoteException re) { |
| 2942 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2943 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2944 | return; |
| 2945 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 2946 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2947 | try { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2948 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2949 | "User cannot modify accounts of this type (policy)."); |
| 2950 | } catch (RemoteException re) { |
Amith Yamasani | 23c8b96 | 2013-04-10 13:37:18 -0700 | [diff] [blame] | 2951 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 2952 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 2953 | userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2954 | return; |
| 2955 | } |
| 2956 | |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2957 | final int pid = Binder.getCallingPid(); |
Costin Manolache | b61e8fb | 2011-09-08 11:26:09 -0700 | [diff] [blame] | 2958 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 2959 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 2960 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 2961 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2962 | int usrId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2963 | long identityToken = clearCallingIdentity(); |
| 2964 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 2965 | UserAccounts accounts = getUserAccounts(usrId); |
| 2966 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2967 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 2968 | uid); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2969 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 2970 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 2971 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2972 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2973 | public void run() throws RemoteException { |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 2974 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2975 | options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2976 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 2977 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 2978 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2979 | protected String toDebugString(long now) { |
| 2980 | return super.toDebugString(now) + ", addAccount" |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 2981 | + ", accountType " + accountType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 2982 | + ", requiredFeatures " + Arrays.toString(requiredFeatures); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 2983 | } |
| 2984 | }.bind(); |
| 2985 | } finally { |
| 2986 | restoreCallingIdentity(identityToken); |
| 2987 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 2990 | @Override |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2991 | public void addAccountAsUser(final IAccountManagerResponse response, final String accountType, |
| 2992 | final String authTokenType, final String[] requiredFeatures, |
| 2993 | final boolean expectActivityLaunch, final Bundle optionsIn, int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 2994 | Bundle.setDefusable(optionsIn, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 2995 | int callingUid = Binder.getCallingUid(); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 2996 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2997 | Log.v(TAG, "addAccount: accountType " + accountType |
| 2998 | + ", response " + response |
| 2999 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3000 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3001 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3002 | + ", caller's uid " + Binder.getCallingUid() |
| 3003 | + ", pid " + Binder.getCallingPid() |
| 3004 | + ", for user id " + userId); |
| 3005 | } |
| 3006 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3007 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3008 | // Only allow the system process to add accounts of other users |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3009 | if (isCrossUser(callingUid, userId)) { |
| 3010 | throw new SecurityException( |
| 3011 | String.format( |
| 3012 | "User %s trying to add account for %s" , |
| 3013 | UserHandle.getCallingUserId(), |
| 3014 | userId)); |
| 3015 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3016 | |
| 3017 | // Is user disallowed from modifying accounts? |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3018 | if (!canUserModifyAccounts(userId, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3019 | try { |
| 3020 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3021 | "User is not allowed to add an account!"); |
| 3022 | } catch (RemoteException re) { |
| 3023 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3024 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3025 | return; |
| 3026 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3027 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3028 | try { |
| 3029 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3030 | "User cannot modify accounts of this type (policy)."); |
| 3031 | } catch (RemoteException re) { |
| 3032 | } |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3033 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3034 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3035 | return; |
| 3036 | } |
| 3037 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3038 | final int pid = Binder.getCallingPid(); |
| 3039 | final int uid = Binder.getCallingUid(); |
| 3040 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3041 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3042 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3043 | |
| 3044 | long identityToken = clearCallingIdentity(); |
| 3045 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3046 | UserAccounts accounts = getUserAccounts(userId); |
| 3047 | logRecordWithUid( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3048 | accounts, AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_ADD, AccountsDb.TABLE_ACCOUNTS, |
| 3049 | userId); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3050 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3051 | true /* stripAuthTokenFromResult */, null /* accountName */, |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 3052 | false /* authDetailsRequired */, true /* updateLastAuthenticationTime */) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3053 | @Override |
| 3054 | public void run() throws RemoteException { |
| 3055 | mAuthenticator.addAccount(this, mAccountType, authTokenType, requiredFeatures, |
| 3056 | options); |
| 3057 | } |
| 3058 | |
| 3059 | @Override |
| 3060 | protected String toDebugString(long now) { |
| 3061 | return super.toDebugString(now) + ", addAccount" |
| 3062 | + ", accountType " + accountType |
| 3063 | + ", requiredFeatures " |
| 3064 | + (requiredFeatures != null |
| 3065 | ? TextUtils.join(",", requiredFeatures) |
| 3066 | : null); |
| 3067 | } |
| 3068 | }.bind(); |
| 3069 | } finally { |
| 3070 | restoreCallingIdentity(identityToken); |
| 3071 | } |
| 3072 | } |
| 3073 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3074 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3075 | public void startAddAccountSession( |
| 3076 | final IAccountManagerResponse response, |
| 3077 | final String accountType, |
| 3078 | final String authTokenType, |
| 3079 | final String[] requiredFeatures, |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3080 | final boolean expectActivityLaunch, |
| 3081 | final Bundle optionsIn) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3082 | Bundle.setDefusable(optionsIn, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3083 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3084 | Log.v(TAG, |
| 3085 | "startAddAccountSession: accountType " + accountType |
| 3086 | + ", response " + response |
| 3087 | + ", authTokenType " + authTokenType |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3088 | + ", requiredFeatures " + Arrays.toString(requiredFeatures) |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3089 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3090 | + ", caller's uid " + Binder.getCallingUid() |
| 3091 | + ", pid " + Binder.getCallingPid()); |
| 3092 | } |
| 3093 | if (response == null) { |
| 3094 | throw new IllegalArgumentException("response is null"); |
| 3095 | } |
| 3096 | if (accountType == null) { |
| 3097 | throw new IllegalArgumentException("accountType is null"); |
| 3098 | } |
| 3099 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3100 | final int uid = Binder.getCallingUid(); |
| 3101 | final int userId = UserHandle.getUserId(uid); |
| 3102 | if (!canUserModifyAccounts(userId, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3103 | try { |
| 3104 | response.onError(AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3105 | "User is not allowed to add an account!"); |
| 3106 | } catch (RemoteException re) { |
| 3107 | } |
| 3108 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3109 | return; |
| 3110 | } |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 3111 | if (!canUserModifyAccountsForType(userId, accountType, uid)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3112 | try { |
| 3113 | response.onError(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3114 | "User cannot modify accounts of this type (policy)."); |
| 3115 | } catch (RemoteException re) { |
| 3116 | } |
| 3117 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3118 | userId); |
| 3119 | return; |
| 3120 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3121 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3122 | final Bundle options = (optionsIn == null) ? new Bundle() : optionsIn; |
| 3123 | options.putInt(AccountManager.KEY_CALLER_UID, uid); |
| 3124 | options.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3125 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3126 | // Check to see if the Password should be included to the caller. |
| 3127 | String callerPkg = optionsIn.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3128 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3129 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3130 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3131 | long identityToken = clearCallingIdentity(); |
| 3132 | try { |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3133 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3134 | logRecordWithUid(accounts, AccountsDb.DEBUG_ACTION_CALLED_START_ACCOUNT_ADD, |
| 3135 | AccountsDb.TABLE_ACCOUNTS, uid); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3136 | new StartAccountSession( |
| 3137 | accounts, |
| 3138 | response, |
| 3139 | accountType, |
| 3140 | expectActivityLaunch, |
| 3141 | null /* accountName */, |
| 3142 | false /* authDetailsRequired */, |
| 3143 | true /* updateLastAuthenticationTime */, |
| 3144 | isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3145 | @Override |
| 3146 | public void run() throws RemoteException { |
| 3147 | mAuthenticator.startAddAccountSession(this, mAccountType, authTokenType, |
| 3148 | requiredFeatures, options); |
| 3149 | } |
| 3150 | |
| 3151 | @Override |
| 3152 | protected String toDebugString(long now) { |
| 3153 | String requiredFeaturesStr = TextUtils.join(",", requiredFeatures); |
| 3154 | return super.toDebugString(now) + ", startAddAccountSession" + ", accountType " |
| 3155 | + accountType + ", requiredFeatures " |
| 3156 | + (requiredFeatures != null ? requiredFeaturesStr : null); |
| 3157 | } |
| 3158 | }.bind(); |
| 3159 | } finally { |
| 3160 | restoreCallingIdentity(identityToken); |
| 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | /** Session that will encrypt the KEY_ACCOUNT_SESSION_BUNDLE in result. */ |
| 3165 | private abstract class StartAccountSession extends Session { |
| 3166 | |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3167 | private final boolean mIsPasswordForwardingAllowed; |
| 3168 | |
| 3169 | public StartAccountSession( |
| 3170 | UserAccounts accounts, |
| 3171 | IAccountManagerResponse response, |
| 3172 | String accountType, |
| 3173 | boolean expectActivityLaunch, |
| 3174 | String accountName, |
| 3175 | boolean authDetailsRequired, |
| 3176 | boolean updateLastAuthenticationTime, |
| 3177 | boolean isPasswordForwardingAllowed) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3178 | super(accounts, response, accountType, expectActivityLaunch, |
| 3179 | true /* stripAuthTokenFromResult */, accountName, authDetailsRequired, |
| 3180 | updateLastAuthenticationTime); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3181 | mIsPasswordForwardingAllowed = isPasswordForwardingAllowed; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | @Override |
| 3185 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3186 | Bundle.setDefusable(result, true); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3187 | mNumResults++; |
| 3188 | Intent intent = null; |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3189 | if (result != null |
| 3190 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 3191 | checkKeyIntent( |
| 3192 | Binder.getCallingUid(), |
| 3193 | intent); |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3194 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3195 | IAccountManagerResponse response; |
| 3196 | if (mExpectActivityLaunch && result != null |
| 3197 | && result.containsKey(AccountManager.KEY_INTENT)) { |
| 3198 | response = mResponse; |
| 3199 | } else { |
| 3200 | response = getResponseAndClose(); |
| 3201 | } |
| 3202 | if (response == null) { |
| 3203 | return; |
| 3204 | } |
| 3205 | if (result == null) { |
| 3206 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3207 | Log.v(TAG, getClass().getSimpleName() + " calling onError() on response " |
| 3208 | + response); |
| 3209 | } |
| 3210 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3211 | "null bundle returned"); |
| 3212 | return; |
| 3213 | } |
| 3214 | |
| 3215 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && (intent == null)) { |
| 3216 | // All AccountManager error codes are greater |
| 3217 | // than 0 |
| 3218 | sendErrorResponse(response, result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3219 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3220 | return; |
| 3221 | } |
| 3222 | |
Hongming Jin | 368aa19 | 2016-07-29 14:29:54 -0700 | [diff] [blame] | 3223 | // Omit passwords if the caller isn't permitted to see them. |
| 3224 | if (!mIsPasswordForwardingAllowed) { |
| 3225 | result.remove(AccountManager.KEY_PASSWORD); |
| 3226 | } |
| 3227 | |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3228 | // Strip auth token from result. |
| 3229 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 3230 | |
| 3231 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3232 | Log.v(TAG, |
| 3233 | getClass().getSimpleName() + " calling onResult() on response " + response); |
| 3234 | } |
| 3235 | |
| 3236 | // Get the session bundle created by authenticator. The |
| 3237 | // bundle contains data necessary for finishing the session |
| 3238 | // later. The session bundle will be encrypted here and |
| 3239 | // decrypted later when trying to finish the session. |
| 3240 | Bundle sessionBundle = result.getBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE); |
| 3241 | if (sessionBundle != null) { |
| 3242 | String accountType = sessionBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3243 | if (TextUtils.isEmpty(accountType) |
Andreas Gampe | 9b04174 | 2015-12-11 17:23:33 -0800 | [diff] [blame] | 3244 | || !mAccountType.equalsIgnoreCase(accountType)) { |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 3245 | Log.w(TAG, "Account type in session bundle doesn't match request."); |
| 3246 | } |
| 3247 | // Add accountType info to session bundle. This will |
| 3248 | // override any value set by authenticator. |
| 3249 | sessionBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType); |
| 3250 | |
| 3251 | // Encrypt session bundle before returning to caller. |
| 3252 | try { |
| 3253 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3254 | Bundle encryptedBundle = cryptoHelper.encryptBundle(sessionBundle); |
| 3255 | result.putBundle(AccountManager.KEY_ACCOUNT_SESSION_BUNDLE, encryptedBundle); |
| 3256 | } catch (GeneralSecurityException e) { |
| 3257 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3258 | Log.v(TAG, "Failed to encrypt session bundle!", e); |
| 3259 | } |
| 3260 | sendErrorResponse(response, AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3261 | "failed to encrypt session bundle"); |
| 3262 | return; |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | sendResponse(response, result); |
| 3267 | } |
| 3268 | } |
| 3269 | |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3270 | @Override |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3271 | public void finishSessionAsUser(IAccountManagerResponse response, |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3272 | @NonNull Bundle sessionBundle, |
| 3273 | boolean expectActivityLaunch, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3274 | Bundle appInfo, |
| 3275 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3276 | Bundle.setDefusable(sessionBundle, true); |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3277 | int callingUid = Binder.getCallingUid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3278 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3279 | Log.v(TAG, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3280 | "finishSession: response "+ response |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3281 | + ", expectActivityLaunch " + expectActivityLaunch |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3282 | + ", caller's uid " + callingUid |
| 3283 | + ", caller's user id " + UserHandle.getCallingUserId() |
| 3284 | + ", pid " + Binder.getCallingPid() |
| 3285 | + ", for user id " + userId); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3286 | } |
| 3287 | if (response == null) { |
| 3288 | throw new IllegalArgumentException("response is null"); |
| 3289 | } |
| 3290 | |
| 3291 | // Session bundle is the encrypted bundle of the original bundle created by authenticator. |
| 3292 | // Account type is added to it before encryption. |
| 3293 | if (sessionBundle == null || sessionBundle.size() == 0) { |
| 3294 | throw new IllegalArgumentException("sessionBundle is empty"); |
| 3295 | } |
| 3296 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3297 | // Only allow the system process to finish session for other users |
| 3298 | if (isCrossUser(callingUid, userId)) { |
| 3299 | throw new SecurityException( |
| 3300 | String.format( |
| 3301 | "User %s trying to finish session for %s without cross user permission", |
| 3302 | UserHandle.getCallingUserId(), |
| 3303 | userId)); |
| 3304 | } |
| 3305 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3306 | if (!canUserModifyAccounts(userId, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3307 | sendErrorResponse(response, |
| 3308 | AccountManager.ERROR_CODE_USER_RESTRICTED, |
| 3309 | "User is not allowed to add an account!"); |
| 3310 | showCantAddAccount(AccountManager.ERROR_CODE_USER_RESTRICTED, userId); |
| 3311 | return; |
| 3312 | } |
| 3313 | |
| 3314 | final int pid = Binder.getCallingPid(); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3315 | final Bundle decryptedBundle; |
| 3316 | final String accountType; |
| 3317 | // First decrypt session bundle to get account type for checking permission. |
| 3318 | try { |
| 3319 | CryptoHelper cryptoHelper = CryptoHelper.getInstance(); |
| 3320 | decryptedBundle = cryptoHelper.decryptBundle(sessionBundle); |
| 3321 | if (decryptedBundle == null) { |
| 3322 | sendErrorResponse( |
| 3323 | response, |
| 3324 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3325 | "failed to decrypt session bundle"); |
| 3326 | return; |
| 3327 | } |
| 3328 | accountType = decryptedBundle.getString(AccountManager.KEY_ACCOUNT_TYPE); |
| 3329 | // Account type cannot be null. This should not happen if session bundle was created |
| 3330 | // properly by #StartAccountSession. |
| 3331 | if (TextUtils.isEmpty(accountType)) { |
| 3332 | sendErrorResponse( |
| 3333 | response, |
| 3334 | AccountManager.ERROR_CODE_BAD_ARGUMENTS, |
| 3335 | "accountType is empty"); |
| 3336 | return; |
| 3337 | } |
| 3338 | |
| 3339 | // If by any chances, decryptedBundle contains colliding keys with |
| 3340 | // system info |
| 3341 | // such as AccountManager.KEY_ANDROID_PACKAGE_NAME required by the add account flow or |
| 3342 | // update credentials flow, we should replace with the new values of the current call. |
| 3343 | if (appInfo != null) { |
| 3344 | decryptedBundle.putAll(appInfo); |
| 3345 | } |
| 3346 | |
| 3347 | // 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] | 3348 | decryptedBundle.putInt(AccountManager.KEY_CALLER_UID, callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3349 | decryptedBundle.putInt(AccountManager.KEY_CALLER_PID, pid); |
| 3350 | } catch (GeneralSecurityException e) { |
| 3351 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3352 | Log.v(TAG, "Failed to decrypt session bundle!", e); |
| 3353 | } |
| 3354 | sendErrorResponse( |
| 3355 | response, |
| 3356 | AccountManager.ERROR_CODE_BAD_REQUEST, |
| 3357 | "failed to decrypt session bundle"); |
| 3358 | return; |
| 3359 | } |
| 3360 | |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3361 | if (!canUserModifyAccountsForType(userId, accountType, callingUid)) { |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3362 | sendErrorResponse( |
| 3363 | response, |
| 3364 | AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3365 | "User cannot modify accounts of this type (policy)."); |
| 3366 | showCantAddAccount(AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE, |
| 3367 | userId); |
| 3368 | return; |
| 3369 | } |
| 3370 | |
| 3371 | long identityToken = clearCallingIdentity(); |
| 3372 | try { |
| 3373 | UserAccounts accounts = getUserAccounts(userId); |
| 3374 | logRecordWithUid( |
| 3375 | accounts, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 3376 | AccountsDb.DEBUG_ACTION_CALLED_ACCOUNT_SESSION_FINISH, |
| 3377 | AccountsDb.TABLE_ACCOUNTS, |
Sandra Kwan | 0b84b45 | 2016-01-20 15:25:42 -0800 | [diff] [blame] | 3378 | callingUid); |
Sandra Kwan | 920f6ef | 2015-11-10 14:13:29 -0800 | [diff] [blame] | 3379 | new Session( |
| 3380 | accounts, |
| 3381 | response, |
| 3382 | accountType, |
| 3383 | expectActivityLaunch, |
| 3384 | true /* stripAuthTokenFromResult */, |
| 3385 | null /* accountName */, |
| 3386 | false /* authDetailsRequired */, |
| 3387 | true /* updateLastAuthenticationTime */) { |
| 3388 | @Override |
| 3389 | public void run() throws RemoteException { |
| 3390 | mAuthenticator.finishSession(this, mAccountType, decryptedBundle); |
| 3391 | } |
| 3392 | |
| 3393 | @Override |
| 3394 | protected String toDebugString(long now) { |
| 3395 | return super.toDebugString(now) |
| 3396 | + ", finishSession" |
| 3397 | + ", accountType " + accountType; |
| 3398 | } |
| 3399 | }.bind(); |
| 3400 | } finally { |
| 3401 | restoreCallingIdentity(identityToken); |
| 3402 | } |
| 3403 | } |
| 3404 | |
Amith Yamasani | ae7034a | 2014-09-22 12:42:12 -0700 | [diff] [blame] | 3405 | private void showCantAddAccount(int errorCode, int userId) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3406 | final DevicePolicyManagerInternal dpmi = |
| 3407 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 3408 | Intent intent = null; |
Nicolas Prevot | 14fc197 | 2016-08-24 14:21:38 +0100 | [diff] [blame] | 3409 | if (dpmi == null) { |
| 3410 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3411 | } else if (errorCode == AccountManager.ERROR_CODE_USER_RESTRICTED) { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3412 | intent = dpmi.createUserRestrictionSupportIntent(userId, |
| 3413 | UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 3414 | } else if (errorCode == AccountManager.ERROR_CODE_MANAGEMENT_DISABLED_FOR_ACCOUNT_TYPE) { |
| 3415 | intent = dpmi.createShowAdminSupportIntent(userId, false); |
| 3416 | } |
| 3417 | if (intent == null) { |
| 3418 | intent = getDefaultCantAddAccountIntent(errorCode); |
| 3419 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3420 | long identityToken = clearCallingIdentity(); |
| 3421 | try { |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3422 | mContext.startActivityAsUser(intent, new UserHandle(userId)); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3423 | } finally { |
| 3424 | restoreCallingIdentity(identityToken); |
| 3425 | } |
| 3426 | } |
| 3427 | |
Nicolas Prevot | 709a63d | 2016-06-09 13:14:00 +0100 | [diff] [blame] | 3428 | /** |
| 3429 | * Called when we don't know precisely who is preventing us from adding an account. |
| 3430 | */ |
| 3431 | private Intent getDefaultCantAddAccountIntent(int errorCode) { |
| 3432 | Intent cantAddAccount = new Intent(mContext, CantAddAccountActivity.class); |
| 3433 | cantAddAccount.putExtra(CantAddAccountActivity.EXTRA_ERROR_CODE, errorCode); |
| 3434 | cantAddAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3435 | return cantAddAccount; |
| 3436 | } |
| 3437 | |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 3438 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3439 | public void confirmCredentialsAsUser( |
| 3440 | IAccountManagerResponse response, |
| 3441 | final Account account, |
| 3442 | final Bundle options, |
| 3443 | final boolean expectActivityLaunch, |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 3444 | int userId) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3445 | Bundle.setDefusable(options, true); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3446 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3447 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3448 | Log.v(TAG, "confirmCredentials: " + account |
| 3449 | + ", response " + response |
| 3450 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3451 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3452 | + ", pid " + Binder.getCallingPid()); |
| 3453 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3454 | // Only allow the system process to read accounts of other users |
| 3455 | if (isCrossUser(callingUid, userId)) { |
| 3456 | throw new SecurityException( |
| 3457 | String.format( |
| 3458 | "User %s trying to confirm account credentials for %s" , |
| 3459 | UserHandle.getCallingUserId(), |
| 3460 | userId)); |
| 3461 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3462 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3463 | if (account == null) throw new IllegalArgumentException("account is null"); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3464 | long identityToken = clearCallingIdentity(); |
| 3465 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3466 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3467 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3468 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3469 | true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3470 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3471 | public void run() throws RemoteException { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3472 | mAuthenticator.confirmCredentials(this, account, options); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3473 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3474 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3475 | protected String toDebugString(long now) { |
| 3476 | return super.toDebugString(now) + ", confirmCredentials" |
| 3477 | + ", " + account; |
| 3478 | } |
| 3479 | }.bind(); |
| 3480 | } finally { |
| 3481 | restoreCallingIdentity(identityToken); |
| 3482 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3483 | } |
| 3484 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3485 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3486 | public void updateCredentials(IAccountManagerResponse response, final Account account, |
| 3487 | final String authTokenType, final boolean expectActivityLaunch, |
| 3488 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3489 | Bundle.setDefusable(loginOptions, true); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3490 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3491 | Log.v(TAG, "updateCredentials: " + account |
| 3492 | + ", response " + response |
| 3493 | + ", authTokenType " + authTokenType |
| 3494 | + ", expectActivityLaunch " + expectActivityLaunch |
| 3495 | + ", caller's uid " + Binder.getCallingUid() |
| 3496 | + ", pid " + Binder.getCallingPid()); |
| 3497 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3498 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3499 | if (account == null) throw new IllegalArgumentException("account is null"); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3500 | int userId = UserHandle.getCallingUserId(); |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3501 | long identityToken = clearCallingIdentity(); |
| 3502 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3503 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3504 | new Session(accounts, response, account.type, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3505 | true /* stripAuthTokenFromResult */, account.name, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 3506 | false /* authDetailsRequired */, true /* updateLastCredentialTime */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3507 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3508 | public void run() throws RemoteException { |
| 3509 | mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions); |
| 3510 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3511 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3512 | protected String toDebugString(long now) { |
| 3513 | if (loginOptions != null) loginOptions.keySet(); |
| 3514 | return super.toDebugString(now) + ", updateCredentials" |
| 3515 | + ", " + account |
| 3516 | + ", authTokenType " + authTokenType |
| 3517 | + ", loginOptions " + loginOptions; |
| 3518 | } |
| 3519 | }.bind(); |
| 3520 | } finally { |
| 3521 | restoreCallingIdentity(identityToken); |
| 3522 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3523 | } |
| 3524 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3525 | @Override |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3526 | public void startUpdateCredentialsSession( |
| 3527 | IAccountManagerResponse response, |
| 3528 | final Account account, |
| 3529 | final String authTokenType, |
| 3530 | final boolean expectActivityLaunch, |
| 3531 | final Bundle loginOptions) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3532 | Bundle.setDefusable(loginOptions, true); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3533 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3534 | Log.v(TAG, |
| 3535 | "startUpdateCredentialsSession: " + account + ", response " + response |
| 3536 | + ", authTokenType " + authTokenType + ", expectActivityLaunch " |
| 3537 | + expectActivityLaunch + ", caller's uid " + Binder.getCallingUid() |
| 3538 | + ", pid " + Binder.getCallingPid()); |
| 3539 | } |
| 3540 | if (response == null) { |
| 3541 | throw new IllegalArgumentException("response is null"); |
| 3542 | } |
| 3543 | if (account == null) { |
| 3544 | throw new IllegalArgumentException("account is null"); |
| 3545 | } |
Sandra Kwan | a578d11 | 2015-12-16 16:01:43 -0800 | [diff] [blame] | 3546 | |
| 3547 | final int uid = Binder.getCallingUid(); |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3548 | int userId = UserHandle.getCallingUserId(); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3549 | |
| 3550 | // Check to see if the Password should be included to the caller. |
| 3551 | String callerPkg = loginOptions.getString(AccountManager.KEY_ANDROID_PACKAGE_NAME); |
| 3552 | boolean isPasswordForwardingAllowed = isPermitted( |
Carlos Valdivia | 714bbd8 | 2016-04-22 14:10:40 -0700 | [diff] [blame] | 3553 | callerPkg, uid, Manifest.permission.GET_PASSWORD); |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3554 | |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3555 | long identityToken = clearCallingIdentity(); |
| 3556 | try { |
| 3557 | UserAccounts accounts = getUserAccounts(userId); |
| 3558 | new StartAccountSession( |
| 3559 | accounts, |
| 3560 | response, |
| 3561 | account.type, |
| 3562 | expectActivityLaunch, |
| 3563 | account.name, |
| 3564 | false /* authDetailsRequired */, |
Carlos Valdivia | 51b651a | 2016-03-30 13:44:28 -0700 | [diff] [blame] | 3565 | true /* updateLastCredentialTime */, |
| 3566 | isPasswordForwardingAllowed) { |
Sandra Kwan | e68c37e | 2015-11-12 17:11:49 -0800 | [diff] [blame] | 3567 | @Override |
| 3568 | public void run() throws RemoteException { |
| 3569 | mAuthenticator.startUpdateCredentialsSession(this, account, authTokenType, |
| 3570 | loginOptions); |
| 3571 | } |
| 3572 | |
| 3573 | @Override |
| 3574 | protected String toDebugString(long now) { |
| 3575 | if (loginOptions != null) |
| 3576 | loginOptions.keySet(); |
| 3577 | return super.toDebugString(now) |
| 3578 | + ", startUpdateCredentialsSession" |
| 3579 | + ", " + account |
| 3580 | + ", authTokenType " + authTokenType |
| 3581 | + ", loginOptions " + loginOptions; |
| 3582 | } |
| 3583 | }.bind(); |
| 3584 | } finally { |
| 3585 | restoreCallingIdentity(identityToken); |
| 3586 | } |
| 3587 | } |
| 3588 | |
| 3589 | @Override |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3590 | public void isCredentialsUpdateSuggested( |
| 3591 | IAccountManagerResponse response, |
| 3592 | final Account account, |
| 3593 | final String statusToken) { |
| 3594 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3595 | Log.v(TAG, |
| 3596 | "isCredentialsUpdateSuggested: " + account + ", response " + response |
| 3597 | + ", caller's uid " + Binder.getCallingUid() |
| 3598 | + ", pid " + Binder.getCallingPid()); |
| 3599 | } |
| 3600 | if (response == null) { |
| 3601 | throw new IllegalArgumentException("response is null"); |
| 3602 | } |
| 3603 | if (account == null) { |
| 3604 | throw new IllegalArgumentException("account is null"); |
| 3605 | } |
| 3606 | if (TextUtils.isEmpty(statusToken)) { |
| 3607 | throw new IllegalArgumentException("status token is empty"); |
| 3608 | } |
| 3609 | |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3610 | int usrId = UserHandle.getCallingUserId(); |
| 3611 | long identityToken = clearCallingIdentity(); |
| 3612 | try { |
| 3613 | UserAccounts accounts = getUserAccounts(usrId); |
| 3614 | new Session(accounts, response, account.type, false /* expectActivityLaunch */, |
| 3615 | false /* stripAuthTokenFromResult */, account.name, |
| 3616 | false /* authDetailsRequired */) { |
| 3617 | @Override |
| 3618 | protected String toDebugString(long now) { |
| 3619 | return super.toDebugString(now) + ", isCredentialsUpdateSuggested" |
| 3620 | + ", " + account; |
| 3621 | } |
| 3622 | |
| 3623 | @Override |
| 3624 | public void run() throws RemoteException { |
| 3625 | mAuthenticator.isCredentialsUpdateSuggested(this, account, statusToken); |
| 3626 | } |
| 3627 | |
| 3628 | @Override |
| 3629 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3630 | Bundle.setDefusable(result, true); |
Sandra Kwan | 390c9d2 | 2016-01-12 14:13:37 -0800 | [diff] [blame] | 3631 | IAccountManagerResponse response = getResponseAndClose(); |
| 3632 | if (response == null) { |
| 3633 | return; |
| 3634 | } |
| 3635 | |
| 3636 | if (result == null) { |
| 3637 | sendErrorResponse( |
| 3638 | response, |
| 3639 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3640 | "null bundle"); |
| 3641 | return; |
| 3642 | } |
| 3643 | |
| 3644 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3645 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3646 | + response); |
| 3647 | } |
| 3648 | // Check to see if an error occurred. We know if an error occurred because all |
| 3649 | // error codes are greater than 0. |
| 3650 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0)) { |
| 3651 | sendErrorResponse(response, |
| 3652 | result.getInt(AccountManager.KEY_ERROR_CODE), |
| 3653 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 3654 | return; |
| 3655 | } |
| 3656 | if (!result.containsKey(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3657 | sendErrorResponse( |
| 3658 | response, |
| 3659 | AccountManager.ERROR_CODE_INVALID_RESPONSE, |
| 3660 | "no result in response"); |
| 3661 | return; |
| 3662 | } |
| 3663 | final Bundle newResult = new Bundle(); |
| 3664 | newResult.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, |
| 3665 | result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)); |
| 3666 | sendResponse(response, newResult); |
| 3667 | } |
| 3668 | }.bind(); |
| 3669 | } finally { |
| 3670 | restoreCallingIdentity(identityToken); |
| 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3675 | public void editProperties(IAccountManagerResponse response, final String accountType, |
| 3676 | final boolean expectActivityLaunch) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3677 | final int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3678 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3679 | Log.v(TAG, "editProperties: accountType " + accountType |
| 3680 | + ", response " + response |
| 3681 | + ", expectActivityLaunch " + expectActivityLaunch |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3682 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3683 | + ", pid " + Binder.getCallingPid()); |
| 3684 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 3685 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 3686 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3687 | int userId = UserHandle.getCallingUserId(); |
| 3688 | if (!isAccountManagedByCaller(accountType, callingUid, userId) && !isSystemUid(callingUid)) { |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 3689 | String msg = String.format( |
| 3690 | "uid %s cannot edit authenticator properites for account type: %s", |
| 3691 | callingUid, |
| 3692 | accountType); |
| 3693 | throw new SecurityException(msg); |
| 3694 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3695 | long identityToken = clearCallingIdentity(); |
| 3696 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 3697 | UserAccounts accounts = getUserAccounts(userId); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3698 | new Session(accounts, response, accountType, expectActivityLaunch, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3699 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3700 | false /* authDetailsRequired */) { |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3701 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3702 | public void run() throws RemoteException { |
| 3703 | mAuthenticator.editProperties(this, mAccountType); |
| 3704 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3705 | @Override |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 3706 | protected String toDebugString(long now) { |
| 3707 | return super.toDebugString(now) + ", editProperties" |
| 3708 | + ", accountType " + accountType; |
| 3709 | } |
| 3710 | }.bind(); |
| 3711 | } finally { |
| 3712 | restoreCallingIdentity(identityToken); |
| 3713 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 3714 | } |
| 3715 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3716 | @Override |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3717 | public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 3718 | @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3719 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3720 | throw new SecurityException("Can be called only by system UID"); |
| 3721 | } |
| 3722 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3723 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3724 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3725 | |
| 3726 | final int userId = userHandle.getIdentifier(); |
| 3727 | |
| 3728 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3729 | |
| 3730 | try { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3731 | final int uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3732 | return hasAccountAccess(account, packageName, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3733 | } catch (NameNotFoundException e) { |
| 3734 | return false; |
| 3735 | } |
| 3736 | } |
| 3737 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3738 | private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName, |
| 3739 | int uid) { |
| 3740 | if (packageName == null) { |
| 3741 | String[] packageNames = mPackageManager.getPackagesForUid(uid); |
| 3742 | if (ArrayUtils.isEmpty(packageNames)) { |
| 3743 | return false; |
| 3744 | } |
| 3745 | // For app op checks related to permissions all packages in the UID |
| 3746 | // have the same app op state, so doesn't matter which one we pick. |
| 3747 | packageName = packageNames[0]; |
| 3748 | } |
| 3749 | |
| 3750 | // Use null token which means any token. Having a token means the package |
| 3751 | // is trusted by the authenticator, hence it is fine to access the account. |
| 3752 | if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) { |
| 3753 | return true; |
| 3754 | } |
| 3755 | // In addition to the permissions required to get an auth token we also allow |
| 3756 | // the account to be accessed by holders of the get accounts permissions. |
| 3757 | return checkUidPermission(Manifest.permission.GET_ACCOUNTS_PRIVILEGED, uid, packageName) |
| 3758 | || checkUidPermission(Manifest.permission.GET_ACCOUNTS, uid, packageName); |
| 3759 | } |
| 3760 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3761 | private boolean checkUidPermission(String permission, int uid, String opPackageName) { |
| 3762 | final long identity = Binder.clearCallingIdentity(); |
| 3763 | try { |
| 3764 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 3765 | if (pm.checkUidPermission(permission, uid) != PackageManager.PERMISSION_GRANTED) { |
| 3766 | return false; |
| 3767 | } |
| 3768 | final int opCode = AppOpsManager.permissionToOpCode(permission); |
| 3769 | return (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOpNoThrow( |
| 3770 | opCode, uid, opPackageName) == AppOpsManager.MODE_ALLOWED); |
| 3771 | } catch (RemoteException e) { |
| 3772 | /* ignore - local call */ |
| 3773 | } finally { |
| 3774 | Binder.restoreCallingIdentity(identity); |
| 3775 | } |
| 3776 | return false; |
| 3777 | } |
| 3778 | |
| 3779 | @Override |
| 3780 | public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account, |
| 3781 | @NonNull String packageName, @NonNull UserHandle userHandle) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3782 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3783 | throw new SecurityException("Can be called only by system UID"); |
| 3784 | } |
| 3785 | |
| 3786 | Preconditions.checkNotNull(account, "account cannot be null"); |
| 3787 | Preconditions.checkNotNull(packageName, "packageName cannot be null"); |
| 3788 | Preconditions.checkNotNull(userHandle, "userHandle cannot be null"); |
| 3789 | |
| 3790 | final int userId = userHandle.getIdentifier(); |
| 3791 | |
| 3792 | Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete"); |
| 3793 | |
| 3794 | final int uid; |
| 3795 | try { |
| 3796 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 3797 | } catch (NameNotFoundException e) { |
| 3798 | Slog.e(TAG, "Unknown package " + packageName); |
| 3799 | return null; |
| 3800 | } |
| 3801 | |
| 3802 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null); |
| 3803 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 3804 | final long identity = Binder.clearCallingIdentity(); |
| 3805 | try { |
| 3806 | return PendingIntent.getActivityAsUser( |
| 3807 | mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 3808 | | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE, |
| 3809 | null, new UserHandle(userId)).getIntentSender(); |
| 3810 | } finally { |
| 3811 | Binder.restoreCallingIdentity(identity); |
| 3812 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | private Intent newRequestAccountAccessIntent(Account account, String packageName, |
| 3816 | int uid, RemoteCallback callback) { |
| 3817 | return newGrantCredentialsPermissionIntent(account, packageName, uid, |
| 3818 | new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() { |
| 3819 | @Override |
| 3820 | public void onResult(Bundle value) throws RemoteException { |
| 3821 | handleAuthenticatorResponse(true); |
| 3822 | } |
| 3823 | |
| 3824 | @Override |
| 3825 | public void onRequestContinued() { |
| 3826 | /* ignore */ |
| 3827 | } |
| 3828 | |
| 3829 | @Override |
| 3830 | public void onError(int errorCode, String errorMessage) throws RemoteException { |
| 3831 | handleAuthenticatorResponse(false); |
| 3832 | } |
| 3833 | |
| 3834 | private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException { |
| 3835 | cancelNotification(getCredentialPermissionNotificationId(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3836 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3837 | UserHandle.getUserHandleForUid(uid)); |
| 3838 | if (callback != null) { |
| 3839 | Bundle result = new Bundle(); |
| 3840 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted); |
| 3841 | callback.sendResult(result); |
| 3842 | } |
| 3843 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3844 | }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 3848 | public boolean someUserHasAccount(@NonNull final Account account) { |
| 3849 | if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) { |
| 3850 | throw new SecurityException("Only system can check for accounts across users"); |
| 3851 | } |
| 3852 | final long token = Binder.clearCallingIdentity(); |
| 3853 | try { |
| 3854 | AccountAndUser[] allAccounts = getAllAccounts(); |
| 3855 | for (int i = allAccounts.length - 1; i >= 0; i--) { |
| 3856 | if (allAccounts[i].account.equals(account)) { |
| 3857 | return true; |
| 3858 | } |
| 3859 | } |
| 3860 | return false; |
| 3861 | } finally { |
| 3862 | Binder.restoreCallingIdentity(token); |
| 3863 | } |
| 3864 | } |
| 3865 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3866 | private class GetAccountsByTypeAndFeatureSession extends Session { |
| 3867 | private final String[] mFeatures; |
| 3868 | private volatile Account[] mAccountsOfType = null; |
| 3869 | private volatile ArrayList<Account> mAccountsWithFeatures = null; |
| 3870 | private volatile int mCurrentAccount = 0; |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 3871 | private final int mCallingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3872 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3873 | public GetAccountsByTypeAndFeatureSession(UserAccounts accounts, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3874 | IAccountManagerResponse response, String type, String[] features, int callingUid) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3875 | super(accounts, response, type, false /* expectActivityLaunch */, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 3876 | true /* stripAuthTokenFromResult */, null /* accountName */, |
| 3877 | false /* authDetailsRequired */); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3878 | mCallingUid = callingUid; |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3879 | mFeatures = features; |
| 3880 | } |
| 3881 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3882 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3883 | public void run() throws RemoteException { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3884 | synchronized (mAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 3885 | mAccountsOfType = getAccountsFromCacheLocked(mAccounts, mAccountType, mCallingUid, |
| 3886 | null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 3887 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3888 | // check whether each account matches the requested features |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 3889 | mAccountsWithFeatures = new ArrayList<>(mAccountsOfType.length); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3890 | mCurrentAccount = 0; |
| 3891 | |
| 3892 | checkAccount(); |
| 3893 | } |
| 3894 | |
| 3895 | public void checkAccount() { |
| 3896 | if (mCurrentAccount >= mAccountsOfType.length) { |
| 3897 | sendResult(); |
| 3898 | return; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 3899 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3900 | |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3901 | final IAccountAuthenticator accountAuthenticator = mAuthenticator; |
| 3902 | if (accountAuthenticator == null) { |
| 3903 | // It is possible that the authenticator has died, which is indicated by |
| 3904 | // mAuthenticator being set to null. If this happens then just abort. |
| 3905 | // There is no need to send back a result or error in this case since |
| 3906 | // that already happened when mAuthenticator was cleared. |
| 3907 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3908 | Log.v(TAG, "checkAccount: aborting session since we are no longer" |
| 3909 | + " connected to the authenticator, " + toDebugString()); |
| 3910 | } |
| 3911 | return; |
| 3912 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3913 | try { |
Fred Quintana | 29e94b8 | 2010-03-10 12:11:51 -0800 | [diff] [blame] | 3914 | accountAuthenticator.hasFeatures(this, mAccountsOfType[mCurrentAccount], mFeatures); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3915 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3916 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "remote exception"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3917 | } |
| 3918 | } |
| 3919 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3920 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3921 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 3922 | Bundle.setDefusable(result, true); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3923 | mNumResults++; |
| 3924 | if (result == null) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3925 | onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, "null bundle"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3926 | return; |
| 3927 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3928 | if (result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT, false)) { |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3929 | mAccountsWithFeatures.add(mAccountsOfType[mCurrentAccount]); |
| 3930 | } |
| 3931 | mCurrentAccount++; |
| 3932 | checkAccount(); |
| 3933 | } |
| 3934 | |
| 3935 | public void sendResult() { |
| 3936 | IAccountManagerResponse response = getResponseAndClose(); |
| 3937 | if (response != null) { |
| 3938 | try { |
| 3939 | Account[] accounts = new Account[mAccountsWithFeatures.size()]; |
| 3940 | for (int i = 0; i < accounts.length; i++) { |
| 3941 | accounts[i] = mAccountsWithFeatures.get(i); |
| 3942 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 3943 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3944 | Log.v(TAG, getClass().getSimpleName() + " calling onResult() on response " |
| 3945 | + response); |
| 3946 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3947 | Bundle result = new Bundle(); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 3948 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3949 | response.onResult(result); |
| 3950 | } catch (RemoteException e) { |
| 3951 | // if the caller is dead then there is no one to care about remote exceptions |
| 3952 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3953 | Log.v(TAG, "failure while notifying response", e); |
| 3954 | } |
| 3955 | } |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 3960 | @Override |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 3961 | protected String toDebugString(long now) { |
| 3962 | return super.toDebugString(now) + ", getAccountsByTypeAndFeatures" |
| 3963 | + ", " + (mFeatures != null ? TextUtils.join(",", mFeatures) : null); |
| 3964 | } |
| 3965 | } |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 3966 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3967 | /** |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3968 | * 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] | 3969 | * @hide |
| 3970 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3971 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3972 | public Account[] getAccounts(int userId, String opPackageName) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 3973 | int callingUid = Binder.getCallingUid(); |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 3974 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 3975 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3976 | if (visibleAccountTypes.isEmpty()) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 3977 | return new Account[0]; |
| 3978 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3979 | long identityToken = clearCallingIdentity(); |
| 3980 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3981 | UserAccounts accounts = getUserAccounts(userId); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3982 | return getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 3983 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 3984 | callingUid, |
| 3985 | null, // packageName |
| 3986 | visibleAccountTypes); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3987 | } finally { |
| 3988 | restoreCallingIdentity(identityToken); |
| 3989 | } |
| 3990 | } |
| 3991 | |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3992 | /** |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3993 | * Returns accounts for all running users. |
| 3994 | * |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 3995 | * @hide |
| 3996 | */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3997 | @NonNull |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 3998 | public AccountAndUser[] getRunningAccounts() { |
| 3999 | final int[] runningUserIds; |
| 4000 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame^] | 4001 | runningUserIds = ActivityManager.getService().getRunningUserIds(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4002 | } catch (RemoteException e) { |
| 4003 | // Running in system_server; should never happen |
| 4004 | throw new RuntimeException(e); |
| 4005 | } |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4006 | return getAccounts(runningUserIds); |
| 4007 | } |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4008 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4009 | /** {@hide} */ |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4010 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4011 | public AccountAndUser[] getAllAccounts() { |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 4012 | final List<UserInfo> users = getUserManager().getUsers(true); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4013 | final int[] userIds = new int[users.size()]; |
| 4014 | for (int i = 0; i < userIds.length; i++) { |
| 4015 | userIds[i] = users.get(i).id; |
| 4016 | } |
| 4017 | return getAccounts(userIds); |
| 4018 | } |
| 4019 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4020 | @NonNull |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4021 | private AccountAndUser[] getAccounts(int[] userIds) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4022 | final ArrayList<AccountAndUser> runningAccounts = Lists.newArrayList(); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 4023 | for (int userId : userIds) { |
| 4024 | UserAccounts userAccounts = getUserAccounts(userId); |
| 4025 | if (userAccounts == null) continue; |
| 4026 | synchronized (userAccounts.cacheLock) { |
| 4027 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null, |
| 4028 | Binder.getCallingUid(), null); |
| 4029 | for (int a = 0; a < accounts.length; a++) { |
| 4030 | runningAccounts.add(new AccountAndUser(accounts[a], userId)); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4031 | } |
| 4032 | } |
| 4033 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4034 | |
| 4035 | AccountAndUser[] accountsArray = new AccountAndUser[runningAccounts.size()]; |
| 4036 | return runningAccounts.toArray(accountsArray); |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 4037 | } |
| 4038 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4039 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4040 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4041 | public Account[] getAccountsAsUser(String type, int userId, String opPackageName) { |
| 4042 | return getAccountsAsUser(type, userId, null, -1, opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4043 | } |
| 4044 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4045 | @NonNull |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4046 | private Account[] getAccountsAsUser( |
| 4047 | String type, |
| 4048 | int userId, |
| 4049 | String callingPackage, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4050 | int packageUid, |
| 4051 | String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4052 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4053 | // Only allow the system process to read accounts of other users |
| 4054 | if (userId != UserHandle.getCallingUserId() |
Amith Yamasani | bb49e85 | 2013-03-30 19:20:18 -0700 | [diff] [blame] | 4055 | && callingUid != Process.myUid() |
Jim Miller | 464f530 | 2013-02-27 18:33:25 -0800 | [diff] [blame] | 4056 | && mContext.checkCallingOrSelfPermission( |
| 4057 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL) |
| 4058 | != PackageManager.PERMISSION_GRANTED) { |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4059 | throw new SecurityException("User " + UserHandle.getCallingUserId() |
| 4060 | + " trying to get account for " + userId); |
| 4061 | } |
| 4062 | |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4063 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4064 | Log.v(TAG, "getAccounts: accountType " + type |
| 4065 | + ", caller's uid " + Binder.getCallingUid() |
| 4066 | + ", pid " + Binder.getCallingPid()); |
| 4067 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4068 | // If the original calling app was using the framework account chooser activity, we'll |
| 4069 | // be passed in the original caller's uid here, which is what should be used for filtering. |
| 4070 | if (packageUid != -1 && UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 4071 | callingUid = packageUid; |
Svetoslav | 5579e41 | 2015-09-10 15:30:45 -0700 | [diff] [blame] | 4072 | opPackageName = callingPackage; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4073 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4074 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4075 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4076 | if (visibleAccountTypes.isEmpty() |
| 4077 | || (type != null && !visibleAccountTypes.contains(type))) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4078 | return new Account[0]; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4079 | } else if (visibleAccountTypes.contains(type)) { |
| 4080 | // Prune the list down to just the requested type. |
| 4081 | visibleAccountTypes = new ArrayList<>(); |
| 4082 | visibleAccountTypes.add(type); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 4083 | } // else aggregate all the visible accounts (it won't matter if the |
| 4084 | // list is empty). |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4085 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4086 | long identityToken = clearCallingIdentity(); |
| 4087 | try { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4088 | UserAccounts accounts = getUserAccounts(userId); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4089 | Account[] accountsToReturn = getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4090 | accounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4091 | callingUid, |
| 4092 | callingPackage, |
| 4093 | visibleAccountTypes); |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4094 | ArrayList<Account> accountsToReturnList = new |
| 4095 | ArrayList<Account>(Arrays.asList(accountsToReturn)); |
| 4096 | for(int i = accountsToReturnList.size() - 1; i >= 0 ; i--) { |
| 4097 | // if account not visible to caller or managed by caller, remove from |
| 4098 | // accounts to return. Note that all accounts visible by default unless |
| 4099 | // visible list functionality implemented |
| 4100 | if(!(isAccountVisible(accountsToReturnList.get(i), callingUid, |
| 4101 | getUserAccounts(userId)))) { |
| 4102 | accountsToReturnList.remove(i); |
| 4103 | } |
| 4104 | } |
| 4105 | return accountsToReturnList.toArray(new Account[accountsToReturnList.size()]); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4106 | } finally { |
| 4107 | restoreCallingIdentity(identityToken); |
| 4108 | } |
| 4109 | } |
| 4110 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4111 | @NonNull |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4112 | private Account[] getAccountsInternal( |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4113 | UserAccounts userAccounts, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4114 | int callingUid, |
| 4115 | String callingPackage, |
| 4116 | List<String> visibleAccountTypes) { |
Carlos Valdivia | a3721e1 | 2015-08-10 18:40:06 -0700 | [diff] [blame] | 4117 | synchronized (userAccounts.cacheLock) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4118 | ArrayList<Account> visibleAccounts = new ArrayList<>(); |
| 4119 | for (String visibleType : visibleAccountTypes) { |
| 4120 | Account[] accountsForType = getAccountsFromCacheLocked( |
| 4121 | userAccounts, visibleType, callingUid, callingPackage); |
| 4122 | if (accountsForType != null) { |
| 4123 | visibleAccounts.addAll(Arrays.asList(accountsForType)); |
| 4124 | } |
| 4125 | } |
| 4126 | Account[] result = new Account[visibleAccounts.size()]; |
| 4127 | for (int i = 0; i < visibleAccounts.size(); i++) { |
| 4128 | result[i] = visibleAccounts.get(i); |
| 4129 | } |
| 4130 | return result; |
| 4131 | } |
| 4132 | } |
| 4133 | |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4134 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4135 | public void addSharedAccountsFromParentUser(int parentUserId, int userId) { |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 4136 | checkManageOrCreateUsersPermission("addSharedAccountsFromParentUser"); |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4137 | Account[] accounts = getAccountsAsUser(null, parentUserId, mContext.getOpPackageName()); |
| 4138 | for (Account account : accounts) { |
| 4139 | addSharedAccountAsUser(account, userId); |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | private boolean addSharedAccountAsUser(Account account, int userId) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4144 | userId = handleIncomingUser(userId); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4145 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4146 | accounts.accountsDb.deleteSharedAccount(account); |
| 4147 | long accountId = accounts.accountsDb.insertSharedAccount(account); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4148 | if (accountId < 0) { |
| 4149 | Log.w(TAG, "insertAccountIntoDatabase: " + account |
| 4150 | + ", skipping the DB insert failed"); |
| 4151 | return false; |
| 4152 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4153 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_ADD, AccountsDb.TABLE_SHARED_ACCOUNTS, accountId, |
| 4154 | accounts); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4155 | return true; |
| 4156 | } |
| 4157 | |
| 4158 | @Override |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4159 | public boolean renameSharedAccountAsUser(Account account, String newName, int userId) { |
| 4160 | userId = handleIncomingUser(userId); |
| 4161 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4162 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4163 | int r = accounts.accountsDb.renameSharedAccount(account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4164 | if (r > 0) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4165 | int callingUid = getCallingUid(); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4166 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_RENAME, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4167 | sharedTableAccountId, accounts, callingUid); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4168 | // Recursively rename the account. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4169 | renameAccountInternal(accounts, account, newName); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 4170 | } |
| 4171 | return r > 0; |
| 4172 | } |
| 4173 | |
| 4174 | @Override |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4175 | public boolean removeSharedAccountAsUser(Account account, int userId) { |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4176 | return removeSharedAccountAsUser(account, userId, getCallingUid()); |
| 4177 | } |
| 4178 | |
| 4179 | private boolean removeSharedAccountAsUser(Account account, int userId, int callingUid) { |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4180 | userId = handleIncomingUser(userId); |
| 4181 | UserAccounts accounts = getUserAccounts(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4182 | long sharedTableAccountId = accounts.accountsDb.findSharedAccountId(account); |
| 4183 | boolean deleted = accounts.accountsDb.deleteSharedAccount(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4184 | if (deleted) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4185 | logRecord(AccountsDb.DEBUG_ACTION_ACCOUNT_REMOVE, AccountsDb.TABLE_SHARED_ACCOUNTS, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4186 | sharedTableAccountId, accounts, callingUid); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 4187 | removeAccountInternal(accounts, account, callingUid); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4188 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 4189 | return deleted; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | @Override |
| 4193 | public Account[] getSharedAccountsAsUser(int userId) { |
| 4194 | userId = handleIncomingUser(userId); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4195 | UserAccounts accounts = getUserAccounts(userId); |
| 4196 | List<Account> accountList = accounts.accountsDb.getSharedAccounts(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4197 | Account[] accountArray = new Account[accountList.size()]; |
| 4198 | accountList.toArray(accountArray); |
| 4199 | return accountArray; |
| 4200 | } |
| 4201 | |
| 4202 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4203 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4204 | public Account[] getAccounts(String type, String opPackageName) { |
Tejas Khorana | 69990d9 | 2016-08-03 11:19:40 -0700 | [diff] [blame] | 4205 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), opPackageName); |
Amith Yamasani | 2c7bc26 | 2012-11-05 16:46:02 -0800 | [diff] [blame] | 4206 | } |
| 4207 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4208 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4209 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4210 | public Account[] getAccountsForPackage(String packageName, int uid, String opPackageName) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4211 | int callingUid = Binder.getCallingUid(); |
| 4212 | if (!UserHandle.isSameApp(callingUid, Process.myUid())) { |
| 4213 | throw new SecurityException("getAccountsForPackage() called from unauthorized uid " |
| 4214 | + callingUid + " with uid=" + uid); |
| 4215 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4216 | return getAccountsAsUser(null, UserHandle.getCallingUserId(), packageName, uid, |
| 4217 | opPackageName); |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4218 | } |
| 4219 | |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4220 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 4221 | @NonNull |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4222 | public Account[] getAccountsByTypeForPackage(String type, String packageName, |
| 4223 | String opPackageName) { |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4224 | int packageUid = -1; |
| 4225 | try { |
| 4226 | packageUid = AppGlobals.getPackageManager().getPackageUid( |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 4227 | packageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| 4228 | UserHandle.getCallingUserId()); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4229 | } catch (RemoteException re) { |
| 4230 | Slog.e(TAG, "Couldn't determine the packageUid for " + packageName + re); |
| 4231 | return new Account[0]; |
| 4232 | } |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4233 | return getAccountsAsUser(type, UserHandle.getCallingUserId(), packageName, |
| 4234 | packageUid, opPackageName); |
Amith Yamasani | 3b458ad | 2013-04-18 18:40:07 -0700 | [diff] [blame] | 4235 | } |
| 4236 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4237 | @Override |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4238 | public void getAccountsByFeatures( |
| 4239 | IAccountManagerResponse response, |
| 4240 | String type, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4241 | String[] features, |
| 4242 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4243 | int callingUid = Binder.getCallingUid(); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4244 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4245 | Log.v(TAG, "getAccounts: accountType " + type |
| 4246 | + ", response " + response |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4247 | + ", features " + Arrays.toString(features) |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4248 | + ", caller's uid " + callingUid |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4249 | + ", pid " + Binder.getCallingPid()); |
| 4250 | } |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 4251 | if (response == null) throw new IllegalArgumentException("response is null"); |
| 4252 | if (type == null) throw new IllegalArgumentException("accountType is null"); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4253 | int userId = UserHandle.getCallingUserId(); |
| 4254 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 4255 | List<String> visibleAccountTypes = getTypesVisibleToCaller(callingUid, userId, |
| 4256 | opPackageName); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4257 | if (!visibleAccountTypes.contains(type)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4258 | Bundle result = new Bundle(); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4259 | // Need to return just the accounts that are from matching signatures. |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4260 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, new Account[0]); |
| 4261 | try { |
| 4262 | response.onResult(result); |
| 4263 | } catch (RemoteException e) { |
| 4264 | Log.e(TAG, "Cannot respond to caller do to exception." , e); |
| 4265 | } |
| 4266 | return; |
| 4267 | } |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4268 | long identityToken = clearCallingIdentity(); |
| 4269 | try { |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4270 | UserAccounts userAccounts = getUserAccounts(userId); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4271 | if (features == null || features.length == 0) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4272 | Account[] accounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4273 | synchronized (userAccounts.cacheLock) { |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4274 | accounts = getAccountsFromCacheLocked(userAccounts, type, callingUid, null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4275 | } |
Fred Quintana | d4a9d6c | 2010-02-24 12:07:53 -0800 | [diff] [blame] | 4276 | Bundle result = new Bundle(); |
| 4277 | result.putParcelableArray(AccountManager.KEY_ACCOUNTS, accounts); |
| 4278 | onResult(response, result); |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 4279 | return; |
| 4280 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4281 | new GetAccountsByTypeAndFeatureSession( |
| 4282 | userAccounts, |
| 4283 | response, |
| 4284 | type, |
| 4285 | features, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4286 | callingUid).bind(); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4287 | } finally { |
| 4288 | restoreCallingIdentity(identityToken); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4289 | } |
| 4290 | } |
| 4291 | |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 4292 | @Override |
| 4293 | public void onAccountAccessed(String token) throws RemoteException { |
| 4294 | final int uid = Binder.getCallingUid(); |
| 4295 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) { |
| 4296 | return; |
| 4297 | } |
| 4298 | final int userId = UserHandle.getCallingUserId(); |
| 4299 | final long identity = Binder.clearCallingIdentity(); |
| 4300 | try { |
| 4301 | for (Account account : getAccounts(userId, mContext.getOpPackageName())) { |
| 4302 | if (Objects.equals(account.getAccessId(), token)) { |
| 4303 | // An app just accessed the account. At this point it knows about |
| 4304 | // it and there is not need to hide this account from the app. |
| 4305 | if (!hasAccountAccess(account, null, uid)) { |
| 4306 | updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, |
| 4307 | uid, true); |
| 4308 | } |
| 4309 | } |
| 4310 | } |
| 4311 | } finally { |
| 4312 | Binder.restoreCallingIdentity(identity); |
| 4313 | } |
| 4314 | } |
| 4315 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4316 | private abstract class Session extends IAccountAuthenticatorResponse.Stub |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4317 | implements IBinder.DeathRecipient, ServiceConnection { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4318 | IAccountManagerResponse mResponse; |
| 4319 | final String mAccountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4320 | final boolean mExpectActivityLaunch; |
| 4321 | final long mCreationTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4322 | final String mAccountName; |
| 4323 | // Indicates if we need to add auth details(like last credential time) |
| 4324 | final boolean mAuthDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4325 | // If set, we need to update the last authenticated time. This is |
| 4326 | // currently |
| 4327 | // used on |
| 4328 | // successful confirming credentials. |
| 4329 | final boolean mUpdateLastAuthenticatedTime; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4330 | |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4331 | public int mNumResults = 0; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4332 | private int mNumRequestContinued = 0; |
| 4333 | private int mNumErrors = 0; |
| 4334 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4335 | IAccountAuthenticator mAuthenticator = null; |
| 4336 | |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4337 | private final boolean mStripAuthTokenFromResult; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4338 | protected final UserAccounts mAccounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4339 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4340 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4341 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4342 | boolean authDetailsRequired) { |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4343 | this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult, |
| 4344 | accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */); |
| 4345 | } |
| 4346 | |
| 4347 | public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType, |
| 4348 | boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName, |
| 4349 | boolean authDetailsRequired, boolean updateLastAuthenticatedTime) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4350 | super(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4351 | //if (response == null) throw new IllegalArgumentException("response is null"); |
Fred Quintana | 3326920 | 2009-04-20 16:05:10 -0700 | [diff] [blame] | 4352 | if (accountType == null) throw new IllegalArgumentException("accountType is null"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4353 | mAccounts = accounts; |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4354 | mStripAuthTokenFromResult = stripAuthTokenFromResult; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4355 | mResponse = response; |
| 4356 | mAccountType = accountType; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4357 | mExpectActivityLaunch = expectActivityLaunch; |
| 4358 | mCreationTime = SystemClock.elapsedRealtime(); |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4359 | mAccountName = accountName; |
| 4360 | mAuthDetailsRequired = authDetailsRequired; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4361 | mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime; |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4362 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4363 | synchronized (mSessions) { |
| 4364 | mSessions.put(toString(), this); |
| 4365 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4366 | if (response != null) { |
| 4367 | try { |
| 4368 | response.asBinder().linkToDeath(this, 0 /* flags */); |
| 4369 | } catch (RemoteException e) { |
| 4370 | mResponse = null; |
| 4371 | binderDied(); |
| 4372 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4373 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4374 | } |
| 4375 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4376 | IAccountManagerResponse getResponseAndClose() { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4377 | if (mResponse == null) { |
| 4378 | // this session has already been closed |
| 4379 | return null; |
| 4380 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4381 | IAccountManagerResponse response = mResponse; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4382 | 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] | 4383 | return response; |
| 4384 | } |
| 4385 | |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4386 | /** |
| 4387 | * Checks Intents, supplied via KEY_INTENT, to make sure that they don't violate our |
| 4388 | * security policy. |
| 4389 | * |
| 4390 | * In particular we want to make sure that the Authenticator doesn't try to trick users |
| 4391 | * into launching aribtrary intents on the device via by tricking to click authenticator |
| 4392 | * supplied entries in the system Settings app. |
| 4393 | */ |
| 4394 | protected void checkKeyIntent( |
| 4395 | int authUid, |
| 4396 | Intent intent) throws SecurityException { |
| 4397 | long bid = Binder.clearCallingIdentity(); |
| 4398 | try { |
| 4399 | PackageManager pm = mContext.getPackageManager(); |
| 4400 | ResolveInfo resolveInfo = pm.resolveActivityAsUser(intent, 0, mAccounts.userId); |
| 4401 | ActivityInfo targetActivityInfo = resolveInfo.activityInfo; |
| 4402 | int targetUid = targetActivityInfo.applicationInfo.uid; |
Sandra Kwan | 0e961a1 | 2016-06-30 14:34:01 -0700 | [diff] [blame] | 4403 | if (!GrantCredentialsPermissionActivity.class.getName().equals( |
| 4404 | targetActivityInfo.getClass().getName()) |
| 4405 | && !CantAddAccountActivity.class |
| 4406 | .equals(targetActivityInfo.getClass().getName()) |
| 4407 | && PackageManager.SIGNATURE_MATCH != pm.checkSignatures(authUid, |
| 4408 | targetUid)) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4409 | String pkgName = targetActivityInfo.packageName; |
| 4410 | String activityName = targetActivityInfo.name; |
| 4411 | String tmpl = "KEY_INTENT resolved to an Activity (%s) in a package (%s) that " |
| 4412 | + "does not share a signature with the supplying authenticator (%s)."; |
| 4413 | throw new SecurityException( |
| 4414 | String.format(tmpl, activityName, pkgName, mAccountType)); |
| 4415 | } |
| 4416 | } finally { |
| 4417 | Binder.restoreCallingIdentity(bid); |
| 4418 | } |
| 4419 | } |
| 4420 | |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4421 | private void close() { |
| 4422 | synchronized (mSessions) { |
| 4423 | if (mSessions.remove(toString()) == null) { |
| 4424 | // the session was already closed, so bail out now |
| 4425 | return; |
| 4426 | } |
| 4427 | } |
| 4428 | if (mResponse != null) { |
| 4429 | // stop listening for response deaths |
| 4430 | mResponse.asBinder().unlinkToDeath(this, 0 /* flags */); |
| 4431 | |
| 4432 | // clear this so that we don't accidentally send any further results |
| 4433 | mResponse = null; |
| 4434 | } |
| 4435 | cancelTimeout(); |
| 4436 | unbind(); |
| 4437 | } |
| 4438 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4439 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4440 | public void binderDied() { |
| 4441 | mResponse = null; |
| 4442 | close(); |
| 4443 | } |
| 4444 | |
| 4445 | protected String toDebugString() { |
| 4446 | return toDebugString(SystemClock.elapsedRealtime()); |
| 4447 | } |
| 4448 | |
| 4449 | protected String toDebugString(long now) { |
| 4450 | return "Session: expectLaunch " + mExpectActivityLaunch |
| 4451 | + ", connected " + (mAuthenticator != null) |
| 4452 | + ", stats (" + mNumResults + "/" + mNumRequestContinued |
| 4453 | + "/" + mNumErrors + ")" |
| 4454 | + ", lifetime " + ((now - mCreationTime) / 1000.0); |
| 4455 | } |
| 4456 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4457 | void bind() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4458 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4459 | Log.v(TAG, "initiating bind to authenticator type " + mAccountType); |
| 4460 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4461 | if (!bindToAuthenticator(mAccountType)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4462 | Log.d(TAG, "bind attempt failed for " + toDebugString()); |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4463 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, "bind failure"); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4464 | } |
| 4465 | } |
| 4466 | |
| 4467 | private void unbind() { |
| 4468 | if (mAuthenticator != null) { |
| 4469 | mAuthenticator = null; |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4470 | mContext.unbindService(this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4471 | } |
| 4472 | } |
| 4473 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4474 | public void cancelTimeout() { |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4475 | mHandler.removeMessages(MESSAGE_TIMED_OUT, this); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4476 | } |
| 4477 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4478 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4479 | public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4480 | mAuthenticator = IAccountAuthenticator.Stub.asInterface(service); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4481 | try { |
| 4482 | run(); |
| 4483 | } catch (RemoteException e) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4484 | onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4485 | "remote exception"); |
| 4486 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4487 | } |
| 4488 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4489 | @Override |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4490 | public void onServiceDisconnected(ComponentName name) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4491 | mAuthenticator = null; |
| 4492 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4493 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4494 | try { |
| 4495 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4496 | "disconnected"); |
| 4497 | } catch (RemoteException e) { |
| 4498 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4499 | Log.v(TAG, "Session.onServiceDisconnected: " |
| 4500 | + "caught RemoteException while responding", e); |
| 4501 | } |
| 4502 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4503 | } |
| 4504 | } |
| 4505 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4506 | public abstract void run() throws RemoteException; |
| 4507 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4508 | public void onTimedOut() { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4509 | IAccountManagerResponse response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4510 | if (response != null) { |
Fred Quintana | 166466d | 2011-10-24 14:51:40 -0700 | [diff] [blame] | 4511 | try { |
| 4512 | response.onError(AccountManager.ERROR_CODE_REMOTE_EXCEPTION, |
| 4513 | "timeout"); |
| 4514 | } catch (RemoteException e) { |
| 4515 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4516 | Log.v(TAG, "Session.onTimedOut: caught RemoteException while responding", |
| 4517 | e); |
| 4518 | } |
| 4519 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4520 | } |
| 4521 | } |
| 4522 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4523 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4524 | public void onResult(Bundle result) { |
Jeff Sharkey | a04c7a7 | 2016-03-18 12:20:36 -0600 | [diff] [blame] | 4525 | Bundle.setDefusable(result, true); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4526 | mNumResults++; |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4527 | Intent intent = null; |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4528 | if (result != null) { |
| 4529 | boolean isSuccessfulConfirmCreds = result.getBoolean( |
| 4530 | AccountManager.KEY_BOOLEAN_RESULT, false); |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4531 | boolean isSuccessfulUpdateCredsOrAddAccount = |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4532 | result.containsKey(AccountManager.KEY_ACCOUNT_NAME) |
| 4533 | && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4534 | // We should only update lastAuthenticated time, if |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4535 | // mUpdateLastAuthenticatedTime is true and the confirmRequest |
| 4536 | // or updateRequest was successful |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 4537 | boolean needUpdate = mUpdateLastAuthenticatedTime |
Simranjit Singh Kohli | 0b8a7c0 | 2015-06-19 12:45:27 -0700 | [diff] [blame] | 4538 | && (isSuccessfulConfirmCreds || isSuccessfulUpdateCredsOrAddAccount); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4539 | if (needUpdate || mAuthDetailsRequired) { |
| 4540 | boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType); |
| 4541 | if (needUpdate && accountPresent) { |
| 4542 | updateLastAuthenticatedTime(new Account(mAccountName, mAccountType)); |
| 4543 | } |
| 4544 | if (mAuthDetailsRequired) { |
| 4545 | long lastAuthenticatedTime = -1; |
| 4546 | if (accountPresent) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4547 | lastAuthenticatedTime = mAccounts.accountsDb |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4548 | .findAccountLastAuthenticatedTime( |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4549 | new Account(mAccountName, mAccountType)); |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4550 | } |
Simranjit Singh Kohli | 1663b44 | 2015-04-28 11:11:12 -0700 | [diff] [blame] | 4551 | result.putLong(AccountManager.KEY_LAST_AUTHENTICATED_TIME, |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 4552 | lastAuthenticatedTime); |
| 4553 | } |
| 4554 | } |
Simranjit Singh Kohli | 6c7c4ad | 2015-02-23 18:11:14 -0800 | [diff] [blame] | 4555 | } |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4556 | if (result != null |
| 4557 | && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) { |
Carlos Valdivia | 6ede9c3 | 2016-03-10 20:12:32 -0800 | [diff] [blame] | 4558 | checkKeyIntent( |
| 4559 | Binder.getCallingUid(), |
| 4560 | intent); |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4561 | } |
| 4562 | if (result != null |
| 4563 | && !TextUtils.isEmpty(result.getString(AccountManager.KEY_AUTHTOKEN))) { |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4564 | String accountName = result.getString(AccountManager.KEY_ACCOUNT_NAME); |
| 4565 | String accountType = result.getString(AccountManager.KEY_ACCOUNT_TYPE); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4566 | if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) { |
| 4567 | Account account = new Account(accountName, accountType); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 4568 | cancelNotification(getSigninRequiredNotificationId(mAccounts, account), |
| 4569 | new UserHandle(mAccounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4570 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4571 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4572 | IAccountManagerResponse response; |
| 4573 | if (mExpectActivityLaunch && result != null |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4574 | && result.containsKey(AccountManager.KEY_INTENT)) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4575 | response = mResponse; |
| 4576 | } else { |
| 4577 | response = getResponseAndClose(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4578 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4579 | if (response != null) { |
| 4580 | try { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4581 | if (result == null) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4582 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4583 | Log.v(TAG, getClass().getSimpleName() |
| 4584 | + " calling onError() on response " + response); |
| 4585 | } |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 4586 | response.onError(AccountManager.ERROR_CODE_INVALID_RESPONSE, |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4587 | "null bundle returned"); |
| 4588 | } else { |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 4589 | if (mStripAuthTokenFromResult) { |
| 4590 | result.remove(AccountManager.KEY_AUTHTOKEN); |
| 4591 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4592 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4593 | Log.v(TAG, getClass().getSimpleName() |
| 4594 | + " calling onResult() on response " + response); |
| 4595 | } |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4596 | if ((result.getInt(AccountManager.KEY_ERROR_CODE, -1) > 0) && |
| 4597 | (intent == null)) { |
| 4598 | // All AccountManager error codes are greater than 0 |
| 4599 | response.onError(result.getInt(AccountManager.KEY_ERROR_CODE), |
| 4600 | result.getString(AccountManager.KEY_ERROR_MESSAGE)); |
| 4601 | } else { |
| 4602 | response.onResult(result); |
| 4603 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4604 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4605 | } catch (RemoteException e) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4606 | // if the caller is dead then there is no one to care about remote exceptions |
| 4607 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4608 | Log.v(TAG, "failure while notifying response", e); |
| 4609 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4610 | } |
| 4611 | } |
| 4612 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4613 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4614 | @Override |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4615 | public void onRequestContinued() { |
| 4616 | mNumRequestContinued++; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4617 | } |
| 4618 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 4619 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4620 | public void onError(int errorCode, String errorMessage) { |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4621 | mNumErrors++; |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4622 | IAccountManagerResponse response = getResponseAndClose(); |
| 4623 | if (response != null) { |
| 4624 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4625 | Log.v(TAG, getClass().getSimpleName() |
| 4626 | + " calling onError() on response " + response); |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4627 | } |
| 4628 | try { |
| 4629 | response.onError(errorCode, errorMessage); |
| 4630 | } catch (RemoteException e) { |
| 4631 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4632 | Log.v(TAG, "Session.onError: caught RemoteException while responding", e); |
| 4633 | } |
| 4634 | } |
| 4635 | } else { |
| 4636 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4637 | Log.v(TAG, "Session.onError: already closed"); |
| 4638 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4639 | } |
| 4640 | } |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4641 | |
| 4642 | /** |
| 4643 | * find the component name for the authenticator and initiate a bind |
| 4644 | * if no authenticator or the bind fails then return false, otherwise return true |
| 4645 | */ |
| 4646 | private boolean bindToAuthenticator(String authenticatorType) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4647 | final AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription> authenticatorInfo; |
| 4648 | authenticatorInfo = mAuthenticatorCache.getServiceInfo( |
| 4649 | AuthenticatorDescription.newKey(authenticatorType), mAccounts.userId); |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4650 | if (authenticatorInfo == null) { |
| 4651 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4652 | Log.v(TAG, "there is no authenticator for " + authenticatorType |
| 4653 | + ", bailing out"); |
| 4654 | } |
| 4655 | return false; |
| 4656 | } |
| 4657 | |
Jeff Sharkey | ce18c81 | 2016-04-27 16:00:41 -0600 | [diff] [blame] | 4658 | if (!isLocalUnlockedUser(mAccounts.userId) |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 4659 | && !authenticatorInfo.componentInfo.directBootAware) { |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 4660 | Slog.w(TAG, "Blocking binding to authenticator " + authenticatorInfo.componentName |
| 4661 | + " which isn't encryption aware"); |
| 4662 | return false; |
| 4663 | } |
| 4664 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4665 | Intent intent = new Intent(); |
| 4666 | intent.setAction(AccountManager.ACTION_AUTHENTICATOR_INTENT); |
| 4667 | intent.setComponent(authenticatorInfo.componentName); |
| 4668 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4669 | Log.v(TAG, "performing bindService to " + authenticatorInfo.componentName); |
| 4670 | } |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 4671 | if (!mContext.bindServiceAsUser(intent, this, Context.BIND_AUTO_CREATE, |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4672 | UserHandle.of(mAccounts.userId))) { |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4673 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4674 | Log.v(TAG, "bindService to " + authenticatorInfo.componentName + " failed"); |
| 4675 | } |
| 4676 | return false; |
| 4677 | } |
| 4678 | |
Fred Quintana | b839afc | 2009-10-14 15:57:28 -0700 | [diff] [blame] | 4679 | return true; |
| 4680 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4681 | } |
| 4682 | |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 4683 | class MessageHandler extends Handler { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4684 | MessageHandler(Looper looper) { |
| 4685 | super(looper); |
| 4686 | } |
Costin Manolache | 3348f14 | 2009-09-29 18:58:36 -0700 | [diff] [blame] | 4687 | |
Carlos Valdivia | 5bab9da | 2013-09-29 05:11:56 -0700 | [diff] [blame] | 4688 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4689 | public void handleMessage(Message msg) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4690 | switch (msg.what) { |
| 4691 | case MESSAGE_TIMED_OUT: |
| 4692 | Session session = (Session)msg.obj; |
| 4693 | session.onTimedOut(); |
| 4694 | break; |
| 4695 | |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4696 | case MESSAGE_COPY_SHARED_ACCOUNT: |
Esteban Talavera | 22dc3b7 | 2014-10-31 15:41:12 +0000 | [diff] [blame] | 4697 | copyAccountToUser(/*no response*/ null, (Account) msg.obj, msg.arg1, msg.arg2); |
Amith Yamasani | 5be347b | 2013-03-31 17:44:31 -0700 | [diff] [blame] | 4698 | break; |
| 4699 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4700 | default: |
| 4701 | throw new IllegalStateException("unhandled message: " + msg.what); |
| 4702 | } |
| 4703 | } |
| 4704 | } |
| 4705 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4706 | private void logRecord(UserAccounts accounts, String action, String tableName) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4707 | logRecord(action, tableName, -1, accounts); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4708 | } |
| 4709 | |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4710 | private void logRecordWithUid(UserAccounts accounts, String action, String tableName, int uid) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4711 | logRecord(action, tableName, -1, accounts, uid); |
Simranjit Singh Kohli | ba0b10a | 2015-07-16 20:33:14 -0700 | [diff] [blame] | 4712 | } |
| 4713 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4714 | /* |
| 4715 | * This function receives an opened writable database. |
| 4716 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4717 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4718 | UserAccounts userAccount) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4719 | logRecord(action, tableName, accountId, userAccount, getCallingUid()); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | /* |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4723 | * 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] | 4724 | */ |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4725 | private void logRecord(String action, String tableName, long accountId, |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4726 | UserAccounts userAccount, int callingUid) { |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4727 | |
| 4728 | class LogRecordTask implements Runnable { |
| 4729 | private final String action; |
| 4730 | private final String tableName; |
| 4731 | private final long accountId; |
| 4732 | private final UserAccounts userAccount; |
| 4733 | private final int callingUid; |
| 4734 | private final long userDebugDbInsertionPoint; |
| 4735 | |
| 4736 | LogRecordTask(final String action, |
| 4737 | final String tableName, |
| 4738 | final long accountId, |
| 4739 | final UserAccounts userAccount, |
| 4740 | final int callingUid, |
| 4741 | final long userDebugDbInsertionPoint) { |
| 4742 | this.action = action; |
| 4743 | this.tableName = tableName; |
| 4744 | this.accountId = accountId; |
| 4745 | this.userAccount = userAccount; |
| 4746 | this.callingUid = callingUid; |
| 4747 | this.userDebugDbInsertionPoint = userDebugDbInsertionPoint; |
| 4748 | } |
| 4749 | |
| 4750 | public void run() { |
| 4751 | SQLiteStatement logStatement = userAccount.statementForLogging; |
| 4752 | logStatement.bindLong(1, accountId); |
| 4753 | logStatement.bindString(2, action); |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 4754 | logStatement.bindString(3, mDateFormat.format(new Date())); |
Tejas Khorana | 7b88f0e | 2016-06-13 13:06:35 -0700 | [diff] [blame] | 4755 | logStatement.bindLong(4, callingUid); |
| 4756 | logStatement.bindString(5, tableName); |
| 4757 | logStatement.bindLong(6, userDebugDbInsertionPoint); |
| 4758 | logStatement.execute(); |
| 4759 | logStatement.clearBindings(); |
| 4760 | } |
| 4761 | } |
| 4762 | |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4763 | LogRecordTask logTask = new LogRecordTask(action, tableName, accountId, userAccount, |
| 4764 | callingUid, userAccount.debugDbInsertionPoint); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4765 | userAccount.debugDbInsertionPoint = (userAccount.debugDbInsertionPoint + 1) |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4766 | % AccountsDb.MAX_DEBUG_DB_SIZE; |
Fyodor Kupolov | 8873aa3 | 2016-08-25 15:25:40 -0700 | [diff] [blame] | 4767 | mHandler.post(logTask); |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | /* |
| 4771 | * This should only be called once to compile the sql statement for logging |
| 4772 | * and to find the insertion point. |
| 4773 | */ |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4774 | private void initializeDebugDbSizeAndCompileSqlStatementForLogging(UserAccounts userAccount) { |
| 4775 | userAccount.debugDbInsertionPoint = userAccount.accountsDb |
| 4776 | .calculateDebugTableInsertionPoint(); |
| 4777 | userAccount.statementForLogging = userAccount.accountsDb.compileSqlStatementForLogging(); |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 4778 | } |
| 4779 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4780 | public IBinder onBind(@SuppressWarnings("unused") Intent intent) { |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 4781 | return asBinder(); |
| 4782 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4783 | |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4784 | /** |
| 4785 | * Searches array of arguments for the specified string |
| 4786 | * @param args array of argument strings |
| 4787 | * @param value value to search for |
| 4788 | * @return true if the value is contained in the array |
| 4789 | */ |
| 4790 | private static boolean scanArgs(String[] args, String value) { |
| 4791 | if (args != null) { |
| 4792 | for (String arg : args) { |
| 4793 | if (value.equals(arg)) { |
| 4794 | return true; |
| 4795 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4796 | } |
| 4797 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4798 | return false; |
| 4799 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4800 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4801 | @Override |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4802 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4803 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 4804 | != PackageManager.PERMISSION_GRANTED) { |
| 4805 | fout.println("Permission Denial: can't dump AccountsManager from from pid=" |
| 4806 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 4807 | + " without permission " + android.Manifest.permission.DUMP); |
| 4808 | return; |
| 4809 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4810 | final boolean isCheckinRequest = scanArgs(args, "--checkin") || scanArgs(args, "-c"); |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4811 | final IndentingPrintWriter ipw = new IndentingPrintWriter(fout, " "); |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 4812 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 4813 | final List<UserInfo> users = getUserManager().getUsers(); |
| 4814 | for (UserInfo user : users) { |
| 4815 | ipw.println("User " + user + ":"); |
| 4816 | ipw.increaseIndent(); |
| 4817 | dumpUser(getUserAccounts(user.id), fd, ipw, args, isCheckinRequest); |
| 4818 | ipw.println(); |
| 4819 | ipw.decreaseIndent(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4820 | } |
| 4821 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4822 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4823 | private void dumpUser(UserAccounts userAccounts, FileDescriptor fd, PrintWriter fout, |
| 4824 | String[] args, boolean isCheckinRequest) { |
| 4825 | synchronized (userAccounts.cacheLock) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4826 | if (isCheckinRequest) { |
| 4827 | // 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] | 4828 | userAccounts.accountsDb.dumpDeAccountsTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4829 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 4830 | Account[] accounts = getAccountsFromCacheLocked(userAccounts, null /* type */, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 4831 | Process.myUid(), null); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4832 | fout.println("Accounts: " + accounts.length); |
| 4833 | for (Account account : accounts) { |
| 4834 | fout.println(" " + account); |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4835 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 4836 | |
Simranjit Singh Kohli | 1d0c1a6 | 2015-04-09 13:58:44 -0700 | [diff] [blame] | 4837 | // Add debug information. |
| 4838 | fout.println(); |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 4839 | userAccounts.accountsDb.dumpDebugTable(fout); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4840 | fout.println(); |
| 4841 | synchronized (mSessions) { |
| 4842 | final long now = SystemClock.elapsedRealtime(); |
| 4843 | fout.println("Active Sessions: " + mSessions.size()); |
| 4844 | for (Session session : mSessions.values()) { |
| 4845 | fout.println(" " + session.toDebugString(now)); |
| 4846 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4847 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4848 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4849 | fout.println(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4850 | mAuthenticatorCache.dump(fd, fout, args, userAccounts.userId); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 4851 | } |
Jason Parks | 1cd7d0e | 2009-09-28 14:48:34 -0700 | [diff] [blame] | 4852 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4853 | } |
| 4854 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4855 | private void doNotification(UserAccounts accounts, Account account, CharSequence message, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4856 | Intent intent, String packageName, final int userId) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4857 | long identityToken = clearCallingIdentity(); |
| 4858 | try { |
| 4859 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4860 | Log.v(TAG, "doNotification: " + message + " intent:" + intent); |
| 4861 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4862 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4863 | if (intent.getComponent() != null && |
| 4864 | GrantCredentialsPermissionActivity.class.getName().equals( |
| 4865 | intent.getComponent().getClassName())) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4866 | createNoCredentialsPermissionNotification(account, intent, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4867 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4868 | Context contextForUser = getContextForUser(new UserHandle(userId)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 4869 | final Integer notificationId = getSigninRequiredNotificationId(accounts, account); |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4870 | intent.addCategory(String.valueOf(notificationId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4871 | |
Fred Quintana | 33f889a | 2009-09-14 17:31:26 -0700 | [diff] [blame] | 4872 | final String notificationTitleFormat = |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4873 | contextForUser.getText(R.string.notification_title).toString(); |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4874 | Notification n = new Notification.Builder(contextForUser) |
| 4875 | .setWhen(0) |
| 4876 | .setSmallIcon(android.R.drawable.stat_sys_warning) |
| 4877 | .setColor(contextForUser.getColor( |
| 4878 | com.android.internal.R.color.system_notification_accent_color)) |
| 4879 | .setContentTitle(String.format(notificationTitleFormat, account.name)) |
| 4880 | .setContentText(message) |
| 4881 | .setContentIntent(PendingIntent.getActivityAsUser( |
| 4882 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4883 | null, new UserHandle(userId))) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 4884 | .build(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4885 | installNotification(notificationId, n, packageName, userId); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4886 | } |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4887 | } finally { |
| 4888 | restoreCallingIdentity(identityToken); |
| 4889 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4890 | } |
| 4891 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4892 | private void installNotification(int notificationId, final Notification notification, |
| 4893 | String packageName, int userId) { |
| 4894 | final long token = clearCallingIdentity(); |
| 4895 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4896 | INotificationManager notificationManager = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4897 | try { |
| 4898 | notificationManager.enqueueNotificationWithTag(packageName, packageName, null, |
| 4899 | notificationId, notification, new int[1], userId); |
| 4900 | } catch (RemoteException e) { |
| 4901 | /* ignore - local call */ |
| 4902 | } |
| 4903 | } finally { |
| 4904 | Binder.restoreCallingIdentity(token); |
| 4905 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4906 | } |
| 4907 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4908 | private void cancelNotification(int id, UserHandle user) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4909 | cancelNotification(id, mContext.getPackageName(), user); |
| 4910 | } |
| 4911 | |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4912 | private void cancelNotification(int id, String packageName, UserHandle user) { |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4913 | long identityToken = clearCallingIdentity(); |
| 4914 | try { |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 4915 | INotificationManager service = mInjector.getNotificationManager(); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4916 | service.cancelNotificationWithTag(packageName, null, id, user.getIdentifier()); |
| 4917 | } catch (RemoteException e) { |
| 4918 | /* ignore - local call */ |
Fred Quintana | 26fc5eb | 2009-04-09 15:05:50 -0700 | [diff] [blame] | 4919 | } finally { |
| 4920 | restoreCallingIdentity(identityToken); |
| 4921 | } |
Fred Quintana | a698f42 | 2009-04-08 19:14:54 -0700 | [diff] [blame] | 4922 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4923 | |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 4924 | private boolean isPermitted(String opPackageName, int callingUid, String... permissions) { |
| 4925 | for (String perm : permissions) { |
| 4926 | if (mContext.checkCallingOrSelfPermission(perm) == PackageManager.PERMISSION_GRANTED) { |
| 4927 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4928 | Log.v(TAG, " caller uid " + callingUid + " has " + perm); |
| 4929 | } |
| 4930 | final int opCode = AppOpsManager.permissionToOpCode(perm); |
| 4931 | if (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOp( |
| 4932 | opCode, callingUid, opPackageName) == AppOpsManager.MODE_ALLOWED) { |
| 4933 | return true; |
| 4934 | } |
| 4935 | } |
| 4936 | } |
| 4937 | return false; |
| 4938 | } |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 4939 | |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4940 | private int handleIncomingUser(int userId) { |
| 4941 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame^] | 4942 | return ActivityManager.getService().handleIncomingUser( |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 4943 | Binder.getCallingPid(), Binder.getCallingUid(), userId, true, true, "", null); |
| 4944 | } catch (RemoteException re) { |
| 4945 | // Shouldn't happen, local. |
| 4946 | } |
| 4947 | return userId; |
| 4948 | } |
| 4949 | |
Christopher Tate | ccbf84f | 2013-05-08 15:25:41 -0700 | [diff] [blame] | 4950 | private boolean isPrivileged(int callingUid) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4951 | final int callingUserId = UserHandle.getUserId(callingUid); |
| 4952 | |
| 4953 | final PackageManager userPackageManager; |
| 4954 | try { |
| 4955 | userPackageManager = mContext.createPackageContextAsUser( |
| 4956 | "android", 0, new UserHandle(callingUserId)).getPackageManager(); |
| 4957 | } catch (NameNotFoundException e) { |
| 4958 | return false; |
| 4959 | } |
| 4960 | |
| 4961 | String[] packages = userPackageManager.getPackagesForUid(callingUid); |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4962 | for (String name : packages) { |
| 4963 | try { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 4964 | PackageInfo packageInfo = userPackageManager.getPackageInfo(name, 0 /* flags */); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 4965 | if (packageInfo != null |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 4966 | && (packageInfo.applicationInfo.privateFlags |
| 4967 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) { |
Fred Quintana | 7be5964 | 2009-08-24 18:29:25 -0700 | [diff] [blame] | 4968 | return true; |
| 4969 | } |
| 4970 | } catch (PackageManager.NameNotFoundException e) { |
| 4971 | return false; |
| 4972 | } |
| 4973 | } |
| 4974 | return false; |
| 4975 | } |
| 4976 | |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 4977 | private boolean permissionIsGranted( |
| 4978 | Account account, String authTokenType, int callerUid, int userId) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4979 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
| 4980 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4981 | Log.v(TAG, "Access to " + account + " granted calling uid is system"); |
| 4982 | } |
| 4983 | return true; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 4984 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 4985 | |
| 4986 | if (isPrivileged(callerUid)) { |
| 4987 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4988 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 4989 | + callerUid + " privileged"); |
| 4990 | } |
| 4991 | return true; |
| 4992 | } |
| 4993 | if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) { |
| 4994 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 4995 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 4996 | + callerUid + " manages the account"); |
| 4997 | } |
| 4998 | return true; |
| 4999 | } |
| 5000 | if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) { |
| 5001 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5002 | Log.v(TAG, "Access to " + account + " granted calling uid " |
| 5003 | + callerUid + " user granted access"); |
| 5004 | } |
| 5005 | return true; |
| 5006 | } |
| 5007 | |
| 5008 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5009 | Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid); |
| 5010 | } |
| 5011 | |
| 5012 | return false; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5013 | } |
| 5014 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5015 | private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId, |
| 5016 | String opPackageName) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5017 | if (accountType == null) { |
| 5018 | return false; |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5019 | } else { |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5020 | return getTypesVisibleToCaller(callingUid, userId, |
| 5021 | opPackageName).contains(accountType); |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5022 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | private boolean isAccountManagedByCaller(String accountType, int callingUid, int userId) { |
| 5026 | if (accountType == null) { |
| 5027 | return false; |
| 5028 | } else { |
| 5029 | return getTypesManagedByCaller(callingUid, userId).contains(accountType); |
| 5030 | } |
| 5031 | } |
| 5032 | |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5033 | private List<String> getTypesVisibleToCaller(int callingUid, int userId, |
| 5034 | String opPackageName) { |
Ian Pedowitz | 358e51f | 2016-03-15 17:08:27 +0000 | [diff] [blame] | 5035 | boolean isPermitted = |
| 5036 | isPermitted(opPackageName, callingUid, Manifest.permission.GET_ACCOUNTS, |
| 5037 | Manifest.permission.GET_ACCOUNTS_PRIVILEGED); |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5038 | return getTypesForCaller(callingUid, userId, isPermitted); |
| 5039 | } |
| 5040 | |
| 5041 | private List<String> getTypesManagedByCaller(int callingUid, int userId) { |
| 5042 | return getTypesForCaller(callingUid, userId, false); |
| 5043 | } |
| 5044 | |
| 5045 | private List<String> getTypesForCaller( |
| 5046 | int callingUid, int userId, boolean isOtherwisePermitted) { |
| 5047 | List<String> managedAccountTypes = new ArrayList<>(); |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5048 | long identityToken = Binder.clearCallingIdentity(); |
| 5049 | Collection<RegisteredServicesCache.ServiceInfo<AuthenticatorDescription>> serviceInfos; |
| 5050 | try { |
| 5051 | serviceInfos = mAuthenticatorCache.getAllServices(userId); |
| 5052 | } finally { |
| 5053 | Binder.restoreCallingIdentity(identityToken); |
| 5054 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5055 | for (RegisteredServicesCache.ServiceInfo<AuthenticatorDescription> serviceInfo : |
Simranjit Singh Kohli | b77d8b6 | 2015-08-07 17:07:23 -0700 | [diff] [blame] | 5056 | serviceInfos) { |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5057 | final int sigChk = mPackageManager.checkSignatures(serviceInfo.uid, callingUid); |
| 5058 | if (isOtherwisePermitted || sigChk == PackageManager.SIGNATURE_MATCH) { |
| 5059 | managedAccountTypes.add(serviceInfo.type.type); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5060 | } |
| 5061 | } |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5062 | return managedAccountTypes; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5063 | } |
| 5064 | |
Simranjit Singh Kohli | 82d0178 | 2015-04-09 17:27:06 -0700 | [diff] [blame] | 5065 | private boolean isAccountPresentForCaller(String accountName, String accountType) { |
| 5066 | if (getUserAccountsForCaller().accountCache.containsKey(accountType)) { |
| 5067 | for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) { |
| 5068 | if (account.name.equals(accountName)) { |
| 5069 | return true; |
| 5070 | } |
| 5071 | } |
| 5072 | } |
| 5073 | return false; |
| 5074 | } |
| 5075 | |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 5076 | private static void checkManageUsersPermission(String message) { |
| 5077 | if (ActivityManager.checkComponentPermission( |
| 5078 | android.Manifest.permission.MANAGE_USERS, Binder.getCallingUid(), -1, true) |
| 5079 | != PackageManager.PERMISSION_GRANTED) { |
| 5080 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 5081 | } |
| 5082 | } |
| 5083 | |
Sudheer Shanka | 3b2297d | 2016-06-20 10:44:30 -0700 | [diff] [blame] | 5084 | private static void checkManageOrCreateUsersPermission(String message) { |
| 5085 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.MANAGE_USERS, |
| 5086 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED && |
| 5087 | ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 5088 | Binder.getCallingUid(), -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 5089 | throw new SecurityException("You need MANAGE_USERS or CREATE_USERS permission to: " |
| 5090 | + message); |
| 5091 | } |
| 5092 | } |
| 5093 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5094 | private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType, |
| 5095 | int callerUid) { |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5096 | if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) { |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5097 | return true; |
| 5098 | } |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5099 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5100 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5101 | long grantsCount; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5102 | if (authTokenType != null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5103 | grantsCount = accounts.accountsDb.findMatchingGrantsCount(callerUid, authTokenType, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5104 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5105 | } else { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5106 | grantsCount = accounts.accountsDb.findMatchingGrantsCountAnyToken(callerUid, |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5107 | account); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5108 | } |
Fyodor Kupolov | 1ce0161 | 2016-08-26 11:39:07 -0700 | [diff] [blame] | 5109 | final boolean permissionGranted = grantsCount > 0; |
Svet Ganov | 890a210 | 2016-08-24 00:08:00 -0700 | [diff] [blame] | 5110 | |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5111 | if (!permissionGranted && ActivityManager.isRunningInTestHarness()) { |
| 5112 | // TODO: Skip this check when running automated tests. Replace this |
| 5113 | // with a more general solution. |
| 5114 | Log.d(TAG, "no credentials permission for usage of " + account + ", " |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5115 | + authTokenType + " by uid " + callerUid |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5116 | + " but ignoring since device is in test harness."); |
| 5117 | return true; |
| 5118 | } |
| 5119 | return permissionGranted; |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5120 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5121 | } |
| 5122 | |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5123 | private boolean isSystemUid(int callingUid) { |
| 5124 | String[] packages = null; |
| 5125 | long ident = Binder.clearCallingIdentity(); |
| 5126 | try { |
| 5127 | packages = mPackageManager.getPackagesForUid(callingUid); |
| 5128 | } finally { |
| 5129 | Binder.restoreCallingIdentity(ident); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5130 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5131 | if (packages != null) { |
| 5132 | for (String name : packages) { |
| 5133 | try { |
| 5134 | PackageInfo packageInfo = mPackageManager.getPackageInfo(name, 0 /* flags */); |
| 5135 | if (packageInfo != null |
| 5136 | && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 5137 | != 0) { |
| 5138 | return true; |
| 5139 | } |
| 5140 | } catch (PackageManager.NameNotFoundException e) { |
| 5141 | Log.w(TAG, String.format("Could not find package [%s]", name), e); |
| 5142 | } |
| 5143 | } |
| 5144 | } else { |
| 5145 | Log.w(TAG, "No known packages with uid " + callingUid); |
Carlos Valdivia | ffb4602 | 2015-06-08 19:07:54 -0700 | [diff] [blame] | 5146 | } |
Carlos Valdivia | 6eb73a5 | 2015-06-11 13:07:11 -0700 | [diff] [blame] | 5147 | return false; |
Carlos Valdivia | dcddc47 | 2015-06-11 20:04:04 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5150 | /** Succeeds if any of the specified permissions are granted. */ |
| 5151 | private void checkReadAccountsPermitted( |
| 5152 | int callingUid, |
Ian Pedowitz | 6cc066d | 2015-08-05 14:23:43 +0000 | [diff] [blame] | 5153 | String accountType, |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 5154 | int userId, |
| 5155 | String opPackageName) { |
| 5156 | if (!isAccountVisibleToCaller(accountType, callingUid, userId, opPackageName)) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5157 | String msg = String.format( |
| 5158 | "caller uid %s cannot access %s accounts", |
| 5159 | callingUid, |
| 5160 | accountType); |
| 5161 | Log.w(TAG, " " + msg); |
| 5162 | throw new SecurityException(msg); |
| 5163 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5164 | } |
| 5165 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5166 | private boolean canUserModifyAccounts(int userId, int callingUid) { |
| 5167 | // the managing app can always modify accounts |
| 5168 | if (isProfileOwner(callingUid)) { |
| 5169 | return true; |
| 5170 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5171 | if (getUserManager().getUserRestrictions(new UserHandle(userId)) |
| 5172 | .getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS)) { |
| 5173 | return false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5174 | } |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5175 | return true; |
| 5176 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5177 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5178 | private boolean canUserModifyAccountsForType(int userId, String accountType, int callingUid) { |
| 5179 | // the managing app can always modify accounts |
| 5180 | if (isProfileOwner(callingUid)) { |
| 5181 | return true; |
| 5182 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5183 | DevicePolicyManager dpm = (DevicePolicyManager) mContext |
| 5184 | .getSystemService(Context.DEVICE_POLICY_SERVICE); |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5185 | String[] typesArray = dpm.getAccountTypesWithManagementDisabledAsUser(userId); |
Adili Muguro | 4e68b65 | 2014-07-25 16:42:39 +0200 | [diff] [blame] | 5186 | if (typesArray == null) { |
| 5187 | return true; |
| 5188 | } |
Sander Alewijnse | da1350f | 2014-05-08 16:59:42 +0100 | [diff] [blame] | 5189 | for (String forbiddenType : typesArray) { |
| 5190 | if (forbiddenType.equals(accountType)) { |
| 5191 | return false; |
| 5192 | } |
| 5193 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 5194 | return true; |
| 5195 | } |
| 5196 | |
Benjamin Franz | b6c0ce4 | 2015-11-05 10:06:51 +0000 | [diff] [blame] | 5197 | private boolean isProfileOwner(int uid) { |
| 5198 | final DevicePolicyManagerInternal dpmi = |
| 5199 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 5200 | return (dpmi != null) |
| 5201 | && dpmi.isActiveAdminWithPolicy(uid, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5202 | } |
| 5203 | |
Jatin Lodhia | 09e7e0e | 2013-11-07 00:14:25 -0800 | [diff] [blame] | 5204 | @Override |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5205 | public void updateAppPermission(Account account, String authTokenType, int uid, boolean value) |
| 5206 | throws RemoteException { |
| 5207 | final int callingUid = getCallingUid(); |
| 5208 | |
Svetoslav Ganov | 7ee37f4 | 2016-08-24 14:40:16 -0700 | [diff] [blame] | 5209 | if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) { |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5210 | throw new SecurityException(); |
| 5211 | } |
| 5212 | |
| 5213 | if (value) { |
| 5214 | grantAppPermission(account, authTokenType, uid); |
| 5215 | } else { |
| 5216 | revokeAppPermission(account, authTokenType, uid); |
| 5217 | } |
| 5218 | } |
| 5219 | |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5220 | /** |
| 5221 | * Allow callers with the given uid permission to get credentials for account/authTokenType. |
| 5222 | * <p> |
| 5223 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5224 | * which is in the system. This means we don't need to protect it with permissions. |
| 5225 | * @hide |
| 5226 | */ |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5227 | void grantAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5228 | if (account == null || authTokenType == null) { |
| 5229 | Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5230 | return; |
| 5231 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5232 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5233 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5234 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5235 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5236 | accounts.accountsDb.insertGrant(accountId, authTokenType, uid); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5237 | } |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5238 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
Fyodor Kupolov | ef73aaa | 2016-03-25 10:23:42 -0700 | [diff] [blame] | 5239 | UserHandle.of(accounts.userId)); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5240 | |
| 5241 | cancelAccountAccessRequestNotificationIfNeeded(account, uid, true); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5242 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5243 | |
| 5244 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5245 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5246 | : mAppPermissionChangeListeners) { |
| 5247 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5248 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | /** |
| 5252 | * Don't allow callers with the given uid permission to get credentials for |
| 5253 | * account/authTokenType. |
| 5254 | * <p> |
| 5255 | * Although this is public it can only be accessed via the AccountManagerService object |
| 5256 | * which is in the system. This means we don't need to protect it with permissions. |
| 5257 | * @hide |
| 5258 | */ |
Fred Quintana | d9640ec | 2012-05-23 12:37:00 -0700 | [diff] [blame] | 5259 | private void revokeAppPermission(Account account, String authTokenType, int uid) { |
Fred Quintana | 382601f | 2010-03-25 12:25:10 -0700 | [diff] [blame] | 5260 | if (account == null || authTokenType == null) { |
| 5261 | Log.e(TAG, "revokeAppPermission: called with invalid arguments", new Exception()); |
Fred Quintana | 31957f1 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 5262 | return; |
| 5263 | } |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 5264 | UserAccounts accounts = getUserAccounts(UserHandle.getUserId(uid)); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5265 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5266 | accounts.accountsDb.beginTransaction(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5267 | try { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5268 | long accountId = accounts.accountsDb.findDeAccountId(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5269 | if (accountId >= 0) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5270 | accounts.accountsDb.deleteGrantsByAccountIdAuthTokenTypeAndUid( |
| 5271 | accountId, authTokenType, uid); |
| 5272 | accounts.accountsDb.setTransactionSuccessful(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5273 | } |
| 5274 | } finally { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5275 | accounts.accountsDb.endTransaction(); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5276 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5277 | |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 5278 | cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid), |
| 5279 | new UserHandle(accounts.userId)); |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5280 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5281 | |
| 5282 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5283 | for (AccountManagerInternal.OnAppPermissionChangeListener listener |
| 5284 | : mAppPermissionChangeListeners) { |
| 5285 | mHandler.post(() -> listener.onAppPermissionChanged(account, uid)); |
| 5286 | } |
Fred Quintana | d4a1d2e | 2009-07-16 16:36:38 -0700 | [diff] [blame] | 5287 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5288 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5289 | private void removeAccountFromCacheLocked(UserAccounts accounts, Account account) { |
| 5290 | final Account[] oldAccountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5291 | if (oldAccountsForType != null) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5292 | ArrayList<Account> newAccountsList = new ArrayList<>(); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5293 | for (Account curAccount : oldAccountsForType) { |
| 5294 | if (!curAccount.equals(account)) { |
| 5295 | newAccountsList.add(curAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5296 | } |
| 5297 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5298 | if (newAccountsList.isEmpty()) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5299 | accounts.accountCache.remove(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5300 | } else { |
| 5301 | Account[] newAccountsForType = new Account[newAccountsList.size()]; |
| 5302 | newAccountsForType = newAccountsList.toArray(newAccountsForType); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5303 | accounts.accountCache.put(account.type, newAccountsForType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5304 | } |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5305 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5306 | accounts.userDataCache.remove(account); |
| 5307 | accounts.authTokenCache.remove(account); |
Carlos Valdivia | f193b9a | 2014-07-18 01:34:57 -0700 | [diff] [blame] | 5308 | accounts.previousNameCache.remove(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | /** |
| 5312 | * 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] | 5313 | * IMPORTANT: The account being inserted will begin to be tracked for access in remote |
| 5314 | * processes and if you will return this account to apps you should return the result. |
| 5315 | * @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] | 5316 | */ |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5317 | private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5318 | Account[] accountsForType = accounts.accountCache.get(account.type); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5319 | int oldLength = (accountsForType != null) ? accountsForType.length : 0; |
| 5320 | Account[] newAccountsForType = new Account[oldLength + 1]; |
| 5321 | if (accountsForType != null) { |
| 5322 | System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5323 | } |
Svet Ganov | c1c0d1c | 2016-09-23 19:15:47 -0700 | [diff] [blame] | 5324 | String token = account.getAccessId() != null ? account.getAccessId() |
| 5325 | : UUID.randomUUID().toString(); |
| 5326 | newAccountsForType[oldLength] = new Account(account, token); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5327 | accounts.accountCache.put(account.type, newAccountsForType); |
Svetoslav Ganov | 57f6259 | 2016-09-16 17:29:05 -0700 | [diff] [blame] | 5328 | return newAccountsForType[oldLength]; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5329 | } |
| 5330 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5331 | private Account[] filterSharedAccounts(UserAccounts userAccounts, Account[] unfiltered, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5332 | int callingUid, String callingPackage) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5333 | if (getUserManager() == null || userAccounts == null || userAccounts.userId < 0 |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5334 | || callingUid == Process.myUid()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5335 | return unfiltered; |
| 5336 | } |
Erik Wolsheimer | ec1a918 | 2016-03-17 10:39:51 -0700 | [diff] [blame] | 5337 | UserInfo user = getUserManager().getUserInfo(userAccounts.userId); |
Amith Yamasani | 0c19bf5 | 2013-10-03 10:34:58 -0700 | [diff] [blame] | 5338 | if (user != null && user.isRestricted()) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5339 | String[] packages = mPackageManager.getPackagesForUid(callingUid); |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5340 | // 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] | 5341 | // otherwise return non-shared accounts only. |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5342 | // This might be a temporary way to specify a visible list |
| 5343 | String visibleList = mContext.getResources().getString( |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5344 | com.android.internal.R.string.config_appsAuthorizedForSharedAccounts); |
| 5345 | for (String packageName : packages) { |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5346 | if (visibleList.contains(";" + packageName + ";")) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5347 | return unfiltered; |
| 5348 | } |
| 5349 | } |
Tejas Khorana | 5edff3b | 2016-06-28 20:59:52 -0700 | [diff] [blame] | 5350 | ArrayList<Account> allowed = new ArrayList<>(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5351 | Account[] sharedAccounts = getSharedAccountsAsUser(userAccounts.userId); |
| 5352 | if (sharedAccounts == null || sharedAccounts.length == 0) return unfiltered; |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5353 | String requiredAccountType = ""; |
| 5354 | try { |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5355 | // If there's an explicit callingPackage specified, check if that package |
| 5356 | // opted in to see restricted accounts. |
| 5357 | if (callingPackage != null) { |
| 5358 | PackageInfo pi = mPackageManager.getPackageInfo(callingPackage, 0); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5359 | if (pi != null && pi.restrictedAccountType != null) { |
| 5360 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | e342309 | 2013-05-22 19:41:45 -0700 | [diff] [blame] | 5361 | } |
| 5362 | } else { |
| 5363 | // Otherwise check if the callingUid has a package that has opted in |
| 5364 | for (String packageName : packages) { |
| 5365 | PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0); |
| 5366 | if (pi != null && pi.restrictedAccountType != null) { |
| 5367 | requiredAccountType = pi.restrictedAccountType; |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5368 | break; |
| 5369 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5370 | } |
| 5371 | } |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5372 | } catch (NameNotFoundException nnfe) { |
| 5373 | } |
| 5374 | for (Account account : unfiltered) { |
| 5375 | if (account.type.equals(requiredAccountType)) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5376 | allowed.add(account); |
Amith Yamasani | 0ac1fc9 | 2013-03-27 18:56:08 -0700 | [diff] [blame] | 5377 | } else { |
| 5378 | boolean found = false; |
| 5379 | for (Account shared : sharedAccounts) { |
| 5380 | if (shared.equals(account)) { |
| 5381 | found = true; |
| 5382 | break; |
| 5383 | } |
| 5384 | } |
| 5385 | if (!found) { |
| 5386 | allowed.add(account); |
| 5387 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5388 | } |
| 5389 | } |
| 5390 | Account[] filtered = new Account[allowed.size()]; |
| 5391 | allowed.toArray(filtered); |
| 5392 | return filtered; |
| 5393 | } else { |
| 5394 | return unfiltered; |
| 5395 | } |
| 5396 | } |
| 5397 | |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5398 | /* |
| 5399 | * packageName can be null. If not null, it should be used to filter out restricted accounts |
| 5400 | * that the package is not allowed to access. |
| 5401 | */ |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5402 | protected Account[] getAccountsFromCacheLocked(UserAccounts userAccounts, String accountType, |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5403 | int callingUid, String callingPackage) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5404 | if (accountType != null) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5405 | final Account[] accounts = userAccounts.accountCache.get(accountType); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5406 | if (accounts == null) { |
| 5407 | return EMPTY_ACCOUNT_ARRAY; |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5408 | } else { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 5409 | return filterSharedAccounts(userAccounts, Arrays.copyOf(accounts, accounts.length), |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5410 | callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5411 | } |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5412 | } else { |
| 5413 | int totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5414 | for (Account[] accounts : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5415 | totalLength += accounts.length; |
| 5416 | } |
| 5417 | if (totalLength == 0) { |
| 5418 | return EMPTY_ACCOUNT_ARRAY; |
| 5419 | } |
| 5420 | Account[] accounts = new Account[totalLength]; |
| 5421 | totalLength = 0; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5422 | for (Account[] accountsOfType : userAccounts.accountCache.values()) { |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5423 | System.arraycopy(accountsOfType, 0, accounts, totalLength, |
| 5424 | accountsOfType.length); |
| 5425 | totalLength += accountsOfType.length; |
| 5426 | } |
Amith Yamasani | 27db468 | 2013-03-30 17:07:47 -0700 | [diff] [blame] | 5427 | return filterSharedAccounts(userAccounts, accounts, callingUid, callingPackage); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5428 | } |
| 5429 | } |
| 5430 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5431 | protected void writeUserDataIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5432 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5433 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5434 | if (userDataForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5435 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5436 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5437 | } |
| 5438 | if (value == null) { |
| 5439 | userDataForAccount.remove(key); |
| 5440 | } else { |
| 5441 | userDataForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5442 | } |
| 5443 | } |
| 5444 | |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5445 | protected String readCachedTokenInternal( |
| 5446 | UserAccounts accounts, |
| 5447 | Account account, |
| 5448 | String tokenType, |
| 5449 | String callingPackage, |
| 5450 | byte[] pkgSigDigest) { |
| 5451 | synchronized (accounts.cacheLock) { |
Carlos Valdivia | c37ee22 | 2015-06-17 20:17:37 -0700 | [diff] [blame] | 5452 | return accounts.accountTokenCaches.get( |
| 5453 | account, tokenType, callingPackage, pkgSigDigest); |
Carlos Valdivia | 91979be | 2015-05-22 14:11:35 -0700 | [diff] [blame] | 5454 | } |
| 5455 | } |
| 5456 | |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5457 | protected void writeAuthTokenIntoCacheLocked(UserAccounts accounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5458 | Account account, String key, String value) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5459 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5460 | if (authTokensForAccount == null) { |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5461 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5462 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | f9f240e | 2011-02-24 18:27:50 -0800 | [diff] [blame] | 5463 | } |
| 5464 | if (value == null) { |
| 5465 | authTokensForAccount.remove(key); |
| 5466 | } else { |
| 5467 | authTokensForAccount.put(key, value); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5468 | } |
| 5469 | } |
| 5470 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5471 | protected String readAuthTokenInternal(UserAccounts accounts, Account account, |
| 5472 | String authTokenType) { |
| 5473 | synchronized (accounts.cacheLock) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5474 | Map<String, String> authTokensForAccount = accounts.authTokenCache.get(account); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5475 | if (authTokensForAccount == null) { |
| 5476 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5477 | authTokensForAccount = accounts.accountsDb.findAuthTokensByAccount(account); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 5478 | accounts.authTokenCache.put(account, authTokensForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5479 | } |
| 5480 | return authTokensForAccount.get(authTokenType); |
| 5481 | } |
| 5482 | } |
| 5483 | |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5484 | protected String readUserDataInternalLocked( |
| 5485 | UserAccounts accounts, Account account, String key) { |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5486 | Map<String, String> userDataForAccount = accounts.userDataCache.get(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5487 | if (userDataForAccount == null) { |
| 5488 | // need to populate the cache for this account |
Fyodor Kupolov | 00de49e | 2016-09-23 13:10:27 -0700 | [diff] [blame] | 5489 | userDataForAccount = accounts.accountsDb.findUserExtrasForAccount(account); |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5490 | accounts.userDataCache.put(account, userDataForAccount); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5491 | } |
Simranjit Kohli | 858511c | 2016-03-10 18:36:11 +0000 | [diff] [blame] | 5492 | return userDataForAccount.get(key); |
Fred Quintana | 56285a6 | 2010-12-02 14:20:51 -0800 | [diff] [blame] | 5493 | } |
| 5494 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 5495 | private Context getContextForUser(UserHandle user) { |
| 5496 | try { |
| 5497 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 5498 | } catch (NameNotFoundException e) { |
| 5499 | // Default to mContext, not finding the package system is running as is unlikely. |
| 5500 | return mContext; |
| 5501 | } |
| 5502 | } |
Sandra Kwan | 7881228 | 2015-11-04 11:19:47 -0800 | [diff] [blame] | 5503 | |
| 5504 | private void sendResponse(IAccountManagerResponse response, Bundle result) { |
| 5505 | try { |
| 5506 | response.onResult(result); |
| 5507 | } catch (RemoteException e) { |
| 5508 | // if the caller is dead then there is no one to care about remote |
| 5509 | // exceptions |
| 5510 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5511 | Log.v(TAG, "failure while notifying response", e); |
| 5512 | } |
| 5513 | } |
| 5514 | } |
| 5515 | |
| 5516 | private void sendErrorResponse(IAccountManagerResponse response, int errorCode, |
| 5517 | String errorMessage) { |
| 5518 | try { |
| 5519 | response.onError(errorCode, errorMessage); |
| 5520 | } catch (RemoteException e) { |
| 5521 | // if the caller is dead then there is no one to care about remote |
| 5522 | // exceptions |
| 5523 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 5524 | Log.v(TAG, "failure while notifying response", e); |
| 5525 | } |
| 5526 | } |
| 5527 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5528 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5529 | private final class AccountManagerInternalImpl extends AccountManagerInternal { |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5530 | private final Object mLock = new Object(); |
| 5531 | |
| 5532 | @GuardedBy("mLock") |
| 5533 | private AccountManagerBackupHelper mBackupHelper; |
| 5534 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5535 | @Override |
| 5536 | public void requestAccountAccess(@NonNull Account account, @NonNull String packageName, |
| 5537 | @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) { |
| 5538 | if (account == null) { |
| 5539 | Slog.w(TAG, "account cannot be null"); |
| 5540 | return; |
| 5541 | } |
| 5542 | if (packageName == null) { |
| 5543 | Slog.w(TAG, "packageName cannot be null"); |
| 5544 | return; |
| 5545 | } |
| 5546 | if (userId < UserHandle.USER_SYSTEM) { |
| 5547 | Slog.w(TAG, "user id must be concrete"); |
| 5548 | return; |
| 5549 | } |
| 5550 | if (callback == null) { |
| 5551 | Slog.w(TAG, "callback cannot be null"); |
| 5552 | return; |
| 5553 | } |
| 5554 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5555 | if (AccountManagerService.this.hasAccountAccess(account, packageName, |
| 5556 | new UserHandle(userId))) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5557 | Bundle result = new Bundle(); |
| 5558 | result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true); |
| 5559 | callback.sendResult(result); |
| 5560 | return; |
| 5561 | } |
| 5562 | |
| 5563 | final int uid; |
| 5564 | try { |
| 5565 | uid = mPackageManager.getPackageUidAsUser(packageName, userId); |
| 5566 | } catch (NameNotFoundException e) { |
| 5567 | Slog.e(TAG, "Unknown package " + packageName); |
| 5568 | return; |
| 5569 | } |
| 5570 | |
| 5571 | Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 5572 | final UserAccounts userAccounts; |
| 5573 | synchronized (mUsers) { |
| 5574 | userAccounts = mUsers.get(userId); |
| 5575 | } |
| 5576 | doNotification(userAccounts, account, null, intent, packageName, userId); |
| 5577 | } |
| 5578 | |
| 5579 | @Override |
| 5580 | public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) { |
| 5581 | // Listeners are a final CopyOnWriteArrayList, hence no lock needed. |
| 5582 | mAppPermissionChangeListeners.add(listener); |
| 5583 | } |
| 5584 | |
| 5585 | @Override |
| 5586 | public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) { |
| 5587 | return AccountManagerService.this.hasAccountAccess(account, null, uid); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 5588 | } |
Svet Ganov | 5d09c99 | 2016-09-07 09:57:41 -0700 | [diff] [blame] | 5589 | |
| 5590 | @Override |
| 5591 | public byte[] backupAccountAccessPermissions(int userId) { |
| 5592 | synchronized (mLock) { |
| 5593 | if (mBackupHelper == null) { |
| 5594 | mBackupHelper = new AccountManagerBackupHelper( |
| 5595 | AccountManagerService.this, this); |
| 5596 | } |
| 5597 | return mBackupHelper.backupAccountAccessPermissions(userId); |
| 5598 | } |
| 5599 | } |
| 5600 | |
| 5601 | @Override |
| 5602 | public void restoreAccountAccessPermissions(byte[] data, int userId) { |
| 5603 | synchronized (mLock) { |
| 5604 | if (mBackupHelper == null) { |
| 5605 | mBackupHelper = new AccountManagerBackupHelper( |
| 5606 | AccountManagerService.this, this); |
| 5607 | } |
| 5608 | mBackupHelper.restoreAccountAccessPermissions(data, userId); |
| 5609 | } |
| 5610 | } |
Fyodor Kupolov | 1e8a94b | 2016-08-09 16:08:59 -0700 | [diff] [blame] | 5611 | } |
Fyodor Kupolov | da99380 | 2016-09-21 14:47:10 -0700 | [diff] [blame] | 5612 | |
| 5613 | @VisibleForTesting |
| 5614 | static class Injector { |
| 5615 | private final Context mContext; |
| 5616 | |
| 5617 | public Injector(Context context) { |
| 5618 | mContext = context; |
| 5619 | } |
| 5620 | |
| 5621 | Looper getMessageHandlerLooper() { |
| 5622 | ServiceThread serviceThread = new ServiceThread(TAG, |
| 5623 | android.os.Process.THREAD_PRIORITY_FOREGROUND, true /* allowIo */); |
| 5624 | serviceThread.start(); |
| 5625 | return serviceThread.getLooper(); |
| 5626 | } |
| 5627 | |
| 5628 | Context getContext() { |
| 5629 | return mContext; |
| 5630 | } |
| 5631 | |
| 5632 | void addLocalService(AccountManagerInternal service) { |
| 5633 | LocalServices.addService(AccountManagerInternal.class, service); |
| 5634 | } |
| 5635 | |
| 5636 | String getDeDatabaseName(int userId) { |
| 5637 | File databaseFile = new File(Environment.getDataSystemDeDirectory(userId), |
| 5638 | AccountsDb.DE_DATABASE_NAME); |
| 5639 | return databaseFile.getPath(); |
| 5640 | } |
| 5641 | |
| 5642 | String getCeDatabaseName(int userId) { |
| 5643 | File databaseFile = new File(Environment.getDataSystemCeDirectory(userId), |
| 5644 | AccountsDb.CE_DATABASE_NAME); |
| 5645 | return databaseFile.getPath(); |
| 5646 | } |
| 5647 | |
| 5648 | String getPreNDatabaseName(int userId) { |
| 5649 | File systemDir = Environment.getDataSystemDirectory(); |
| 5650 | File databaseFile = new File(Environment.getUserSystemDirectory(userId), |
| 5651 | PRE_N_DATABASE_NAME); |
| 5652 | if (userId == 0) { |
| 5653 | // Migrate old file, if it exists, to the new location. |
| 5654 | // Make sure the new file doesn't already exist. A dummy file could have been |
| 5655 | // accidentally created in the old location, causing the new one to become corrupted |
| 5656 | // as well. |
| 5657 | File oldFile = new File(systemDir, PRE_N_DATABASE_NAME); |
| 5658 | if (oldFile.exists() && !databaseFile.exists()) { |
| 5659 | // Check for use directory; create if it doesn't exist, else renameTo will fail |
| 5660 | File userDir = Environment.getUserSystemDirectory(userId); |
| 5661 | if (!userDir.exists()) { |
| 5662 | if (!userDir.mkdirs()) { |
| 5663 | throw new IllegalStateException("User dir cannot be created: " + userDir); |
| 5664 | } |
| 5665 | } |
| 5666 | if (!oldFile.renameTo(databaseFile)) { |
| 5667 | throw new IllegalStateException("User dir cannot be migrated: " + databaseFile); |
| 5668 | } |
| 5669 | } |
| 5670 | } |
| 5671 | return databaseFile.getPath(); |
| 5672 | } |
| 5673 | |
| 5674 | IAccountAuthenticatorCache getAccountAuthenticatorCache() { |
| 5675 | return new AccountAuthenticatorCache(mContext); |
| 5676 | } |
| 5677 | |
| 5678 | INotificationManager getNotificationManager() { |
| 5679 | return NotificationManager.getService(); |
| 5680 | } |
| 5681 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 5682 | } |