Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "cmd" |
| 18 | |
| 19 | #include <utils/Log.h> |
| 20 | #include <binder/Parcel.h> |
| 21 | #include <binder/ProcessState.h> |
| 22 | #include <binder/IResultReceiver.h> |
| 23 | #include <binder/IServiceManager.h> |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 24 | #include <binder/IShellCallback.h> |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 25 | #include <binder/TextOutput.h> |
| 26 | #include <utils/Vector.h> |
| 27 | |
| 28 | #include <getopt.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <unistd.h> |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 33 | #include <fcntl.h> |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 34 | #include <sys/time.h> |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 35 | #include <errno.h> |
| 36 | |
| 37 | #include "selinux/selinux.h" |
| 38 | #include "selinux/android.h" |
| 39 | |
| 40 | #include <UniquePtr.h> |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 41 | |
| 42 | using namespace android; |
| 43 | |
| 44 | static int sort_func(const String16* lhs, const String16* rhs) |
| 45 | { |
| 46 | return lhs->compare(*rhs); |
| 47 | } |
| 48 | |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 49 | struct SecurityContext_Delete { |
| 50 | void operator()(security_context_t p) const { |
| 51 | freecon(p); |
| 52 | } |
| 53 | }; |
| 54 | typedef UniquePtr<char[], SecurityContext_Delete> Unique_SecurityContext; |
| 55 | |
| 56 | class MyShellCallback : public BnShellCallback |
| 57 | { |
| 58 | public: |
Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame^] | 59 | bool mActive = true; |
| 60 | |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 61 | virtual int openOutputFile(const String16& path, const String16& seLinuxContext) { |
| 62 | String8 path8(path); |
| 63 | char cwd[256]; |
| 64 | getcwd(cwd, 256); |
| 65 | String8 fullPath(cwd); |
| 66 | fullPath.appendPath(path8); |
Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame^] | 67 | if (!mActive) { |
| 68 | aerr << "Open attempt after active for: " << fullPath << endl; |
| 69 | return -EPERM; |
| 70 | } |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 71 | int fd = open(fullPath.string(), O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG); |
| 72 | if (fd < 0) { |
| 73 | return fd; |
| 74 | } |
| 75 | if (is_selinux_enabled() && seLinuxContext.size() > 0) { |
| 76 | String8 seLinuxContext8(seLinuxContext); |
| 77 | security_context_t tmp = NULL; |
| 78 | int ret = getfilecon(fullPath.string(), &tmp); |
| 79 | Unique_SecurityContext context(tmp); |
| 80 | int accessGranted = selinux_check_access(seLinuxContext8.string(), context.get(), |
| 81 | "file", "write", NULL); |
| 82 | if (accessGranted != 0) { |
| 83 | close(fd); |
| 84 | aerr << "System server has no access to file context " << context.get() |
| 85 | << " (from path " << fullPath.string() << ", context " |
| 86 | << seLinuxContext8.string() << ")" << endl; |
| 87 | return -EPERM; |
| 88 | } |
| 89 | } |
| 90 | return fd; |
| 91 | } |
| 92 | }; |
| 93 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 94 | class MyResultReceiver : public BnResultReceiver |
| 95 | { |
| 96 | public: |
| 97 | virtual void send(int32_t /*resultCode*/) { |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | int main(int argc, char* const argv[]) |
| 102 | { |
| 103 | signal(SIGPIPE, SIG_IGN); |
| 104 | sp<ProcessState> proc = ProcessState::self(); |
| 105 | proc->startThreadPool(); |
| 106 | |
| 107 | sp<IServiceManager> sm = defaultServiceManager(); |
| 108 | fflush(stdout); |
| 109 | if (sm == NULL) { |
| 110 | ALOGE("Unable to get default service manager!"); |
| 111 | aerr << "cmd: Unable to get default service manager!" << endl; |
| 112 | return 20; |
| 113 | } |
| 114 | |
| 115 | if (argc == 1) { |
| 116 | aout << "cmd: no service specified; use -l to list all services" << endl; |
| 117 | return 20; |
| 118 | } |
| 119 | |
| 120 | if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) { |
| 121 | Vector<String16> services = sm->listServices(); |
| 122 | services.sort(sort_func); |
| 123 | aout << "Currently running services:" << endl; |
| 124 | |
| 125 | for (size_t i=0; i<services.size(); i++) { |
| 126 | sp<IBinder> service = sm->checkService(services[i]); |
| 127 | if (service != NULL) { |
| 128 | aout << " " << services[i] << endl; |
| 129 | } |
| 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | Vector<String16> args; |
| 135 | for (int i=2; i<argc; i++) { |
| 136 | args.add(String16(argv[i])); |
| 137 | } |
| 138 | String16 cmd = String16(argv[1]); |
| 139 | sp<IBinder> service = sm->checkService(cmd); |
| 140 | if (service == NULL) { |
| 141 | aerr << "Can't find service: " << argv[1] << endl; |
| 142 | return 20; |
| 143 | } |
| 144 | |
Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame^] | 145 | sp<MyShellCallback> cb = new MyShellCallback(); |
| 146 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 147 | // TODO: block until a result is returned to MyResultReceiver. |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 148 | IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, args, |
Dianne Hackborn | e5ed199 | 2016-10-10 16:35:45 -0700 | [diff] [blame^] | 149 | cb, new MyResultReceiver()); |
| 150 | |
| 151 | cb->mActive = false; |
| 152 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | } |