| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2005 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 |  | 
|  | 17 | #define LOG_TAG "PermissionController" | 
|  | 18 |  | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 19 | #include <binder/IPermissionController.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <utils/Log.h> | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/Parcel.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <utils/String8.h> | 
|  | 24 |  | 
| Mathias Agopian | 208059f | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 25 | #include <private/binder/Static.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 |  | 
|  | 27 | namespace android { | 
|  | 28 |  | 
|  | 29 | // ---------------------------------------------------------------------- | 
|  | 30 |  | 
|  | 31 | class BpPermissionController : public BpInterface<IPermissionController> | 
|  | 32 | { | 
|  | 33 | public: | 
| Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 34 | explicit BpPermissionController(const sp<IBinder>& impl) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | : BpInterface<IPermissionController>(impl) | 
|  | 36 | { | 
|  | 37 | } | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 38 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | virtual bool checkPermission(const String16& permission, int32_t pid, int32_t uid) | 
|  | 40 | { | 
|  | 41 | Parcel data, reply; | 
|  | 42 | data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor()); | 
|  | 43 | data.writeString16(permission); | 
|  | 44 | data.writeInt32(pid); | 
|  | 45 | data.writeInt32(uid); | 
|  | 46 | remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply); | 
|  | 47 | // fail on exception | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 48 | if (reply.readExceptionCode() != 0) return 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | return reply.readInt32() != 0; | 
|  | 50 | } | 
| Svet Ganov | f1377f5 | 2015-04-28 12:09:01 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | virtual void getPackagesForUid(const uid_t uid, Vector<String16>& packages) | 
|  | 53 | { | 
|  | 54 | Parcel data, reply; | 
|  | 55 | data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor()); | 
|  | 56 | data.writeInt32(uid); | 
|  | 57 | remote()->transact(GET_PACKAGES_FOR_UID_TRANSACTION, data, &reply); | 
|  | 58 | // fail on exception | 
|  | 59 | if (reply.readExceptionCode() != 0) { | 
|  | 60 | return; | 
|  | 61 | } | 
|  | 62 | const int32_t size = reply.readInt32(); | 
|  | 63 | if (size <= 0) { | 
|  | 64 | return; | 
|  | 65 | } | 
|  | 66 | for (int i = 0; i < size; i++) { | 
|  | 67 | packages.push(reply.readString16()); | 
|  | 68 | } | 
|  | 69 | } | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 70 |  | 
|  | 71 | virtual bool isRuntimePermission(const String16& permission) | 
|  | 72 | { | 
|  | 73 | Parcel data, reply; | 
|  | 74 | data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor()); | 
|  | 75 | data.writeString16(permission); | 
|  | 76 | remote()->transact(IS_RUNTIME_PERMISSION_TRANSACTION, data, &reply); | 
|  | 77 | // fail on exception | 
|  | 78 | if (reply.readExceptionCode() != 0) return false; | 
|  | 79 | return reply.readInt32() != 0; | 
|  | 80 | } | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 81 |  | 
|  | 82 | virtual int getPackageUid(const String16& package, int flags) | 
|  | 83 | { | 
|  | 84 | Parcel data, reply; | 
|  | 85 | data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor()); | 
|  | 86 | data.writeString16(package); | 
|  | 87 | data.writeInt32(flags); | 
|  | 88 | remote()->transact(GET_PACKAGE_UID_TRANSACTION, data, &reply); | 
|  | 89 | // fail on exception | 
|  | 90 | if (reply.readExceptionCode() != 0) return false; | 
|  | 91 | return reply.readInt32(); | 
|  | 92 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | }; | 
|  | 94 |  | 
|  | 95 | IMPLEMENT_META_INTERFACE(PermissionController, "android.os.IPermissionController"); | 
|  | 96 |  | 
|  | 97 | // ---------------------------------------------------------------------- | 
|  | 98 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | status_t BnPermissionController::onTransact( | 
|  | 100 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 101 | { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | switch(code) { | 
|  | 103 | case CHECK_PERMISSION_TRANSACTION: { | 
|  | 104 | CHECK_INTERFACE(IPermissionController, data, reply); | 
|  | 105 | String16 permission = data.readString16(); | 
|  | 106 | int32_t pid = data.readInt32(); | 
|  | 107 | int32_t uid = data.readInt32(); | 
|  | 108 | bool res = checkPermission(permission, pid, uid); | 
| Brad Fitzpatrick | 837a0d0 | 2010-07-13 15:33:35 -0700 | [diff] [blame] | 109 | reply->writeNoException(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | reply->writeInt32(res ? 1 : 0); | 
|  | 111 | return NO_ERROR; | 
|  | 112 | } break; | 
| Svet Ganov | f1377f5 | 2015-04-28 12:09:01 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | case GET_PACKAGES_FOR_UID_TRANSACTION: { | 
|  | 115 | CHECK_INTERFACE(IPermissionController, data, reply); | 
|  | 116 | int32_t uid = data.readInt32(); | 
|  | 117 | Vector<String16> packages; | 
|  | 118 | getPackagesForUid(uid, packages); | 
|  | 119 | reply->writeNoException(); | 
|  | 120 | size_t size = packages.size(); | 
|  | 121 | reply->writeInt32(size); | 
|  | 122 | for (size_t i = 0; i < size; i++) { | 
|  | 123 | reply->writeString16(packages[i]); | 
|  | 124 | } | 
|  | 125 | return NO_ERROR; | 
|  | 126 | } break; | 
|  | 127 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 128 | case IS_RUNTIME_PERMISSION_TRANSACTION: { | 
|  | 129 | CHECK_INTERFACE(IPermissionController, data, reply); | 
|  | 130 | String16 permission = data.readString16(); | 
|  | 131 | const bool res = isRuntimePermission(permission); | 
|  | 132 | reply->writeNoException(); | 
|  | 133 | reply->writeInt32(res ? 1 : 0); | 
|  | 134 | return NO_ERROR; | 
|  | 135 | } break; | 
|  | 136 |  | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 137 | case GET_PACKAGE_UID_TRANSACTION: { | 
|  | 138 | CHECK_INTERFACE(IPermissionController, data, reply); | 
|  | 139 | String16 package = data.readString16(); | 
|  | 140 | int flags = data.readInt32(); | 
|  | 141 | const int uid = getPackageUid(package, flags); | 
|  | 142 | reply->writeNoException(); | 
|  | 143 | reply->writeInt32(uid); | 
|  | 144 | return NO_ERROR; | 
|  | 145 | } break; | 
|  | 146 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | default: | 
|  | 148 | return BBinder::onTransact(code, data, reply, flags); | 
|  | 149 | } | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | }; // namespace android |