blob: 89ebc6c1aad8340218f4e7f59211a23e0dc0c8d4 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/IPermissionController.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <utils/Log.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/String8.h>
24
Mathias Agopian208059f2009-05-18 15:08:03 -070025#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27namespace android {
28
29// ----------------------------------------------------------------------
30
31class BpPermissionController : public BpInterface<IPermissionController>
32{
33public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -070034 explicit BpPermissionController(const sp<IBinder>& impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035 : BpInterface<IPermissionController>(impl)
36 {
37 }
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -070038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039 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 Fitzpatrick837a0d02010-07-13 15:33:35 -070048 if (reply.readExceptionCode() != 0) return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 return reply.readInt32() != 0;
50 }
Svet Ganovf1377f52015-04-28 12:09:01 -070051
Jeff Sharkey7afcb3f2018-04-09 12:58:40 -060052 virtual int32_t noteOp(const String16& op, int32_t uid, const String16& packageName)
53 {
54 Parcel data, reply;
55 data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
56 data.writeString16(op);
57 data.writeInt32(uid);
58 data.writeString16(packageName);
59 remote()->transact(NOTE_OP_TRANSACTION, data, &reply);
60 // fail on exception
61 if (reply.readExceptionCode() != 0) return 2; // MODE_ERRORED
62 return reply.readInt32();
63 }
64
Svet Ganovf1377f52015-04-28 12:09:01 -070065 virtual void getPackagesForUid(const uid_t uid, Vector<String16>& packages)
66 {
67 Parcel data, reply;
68 data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
69 data.writeInt32(uid);
70 remote()->transact(GET_PACKAGES_FOR_UID_TRANSACTION, data, &reply);
71 // fail on exception
72 if (reply.readExceptionCode() != 0) {
73 return;
74 }
75 const int32_t size = reply.readInt32();
76 if (size <= 0) {
77 return;
78 }
79 for (int i = 0; i < size; i++) {
80 packages.push(reply.readString16());
81 }
82 }
Svetoslavb412f6e2015-04-29 16:50:41 -070083
84 virtual bool isRuntimePermission(const String16& permission)
85 {
86 Parcel data, reply;
87 data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
88 data.writeString16(permission);
89 remote()->transact(IS_RUNTIME_PERMISSION_TRANSACTION, data, &reply);
90 // fail on exception
91 if (reply.readExceptionCode() != 0) return false;
92 return reply.readInt32() != 0;
93 }
Svet Ganove752a5c2018-01-15 17:14:20 -080094
95 virtual int getPackageUid(const String16& package, int flags)
96 {
97 Parcel data, reply;
98 data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
99 data.writeString16(package);
100 data.writeInt32(flags);
101 remote()->transact(GET_PACKAGE_UID_TRANSACTION, data, &reply);
102 // fail on exception
103 if (reply.readExceptionCode() != 0) return false;
104 return reply.readInt32();
105 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106};
107
108IMPLEMENT_META_INTERFACE(PermissionController, "android.os.IPermissionController");
109
110// ----------------------------------------------------------------------
111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112status_t BnPermissionController::onTransact(
113 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
114{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 switch(code) {
116 case CHECK_PERMISSION_TRANSACTION: {
117 CHECK_INTERFACE(IPermissionController, data, reply);
118 String16 permission = data.readString16();
119 int32_t pid = data.readInt32();
120 int32_t uid = data.readInt32();
121 bool res = checkPermission(permission, pid, uid);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700122 reply->writeNoException();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123 reply->writeInt32(res ? 1 : 0);
124 return NO_ERROR;
125 } break;
Svet Ganovf1377f52015-04-28 12:09:01 -0700126
Jeff Sharkey7afcb3f2018-04-09 12:58:40 -0600127 case NOTE_OP_TRANSACTION: {
128 CHECK_INTERFACE(IPermissionController, data, reply);
129 String16 op = data.readString16();
130 int32_t uid = data.readInt32();
131 String16 packageName = data.readString16();
132 int32_t res = noteOp(op, uid, packageName);
133 reply->writeNoException();
134 reply->writeInt32(res);
135 return NO_ERROR;
136 } break;
137
Svet Ganovf1377f52015-04-28 12:09:01 -0700138 case GET_PACKAGES_FOR_UID_TRANSACTION: {
139 CHECK_INTERFACE(IPermissionController, data, reply);
140 int32_t uid = data.readInt32();
141 Vector<String16> packages;
142 getPackagesForUid(uid, packages);
143 reply->writeNoException();
144 size_t size = packages.size();
145 reply->writeInt32(size);
146 for (size_t i = 0; i < size; i++) {
147 reply->writeString16(packages[i]);
148 }
149 return NO_ERROR;
150 } break;
151
Svetoslavb412f6e2015-04-29 16:50:41 -0700152 case IS_RUNTIME_PERMISSION_TRANSACTION: {
153 CHECK_INTERFACE(IPermissionController, data, reply);
154 String16 permission = data.readString16();
155 const bool res = isRuntimePermission(permission);
156 reply->writeNoException();
157 reply->writeInt32(res ? 1 : 0);
158 return NO_ERROR;
159 } break;
160
Svet Ganove752a5c2018-01-15 17:14:20 -0800161 case GET_PACKAGE_UID_TRANSACTION: {
162 CHECK_INTERFACE(IPermissionController, data, reply);
163 String16 package = data.readString16();
164 int flags = data.readInt32();
165 const int uid = getPackageUid(package, flags);
166 reply->writeNoException();
167 reply->writeInt32(uid);
168 return NO_ERROR;
169 } break;
170
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 default:
172 return BBinder::onTransact(code, data, reply, flags);
173 }
174}
175
176}; // namespace android