Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | TWRP is free software: you can redistribute it and/or modify |
| 3 | it under the terms of the GNU General Public License as published by |
| 4 | the Free Software Foundation, either version 3 of the License, or |
| 5 | (at your option) any later version. |
| 6 | |
| 7 | TWRP is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 14 | */ |
| 15 | |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 16 | #define __STDC_FORMAT_MACROS 1 |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include <string.h> |
| 22 | #include <fcntl.h> |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | // for setcap and getcap |
| 27 | #include <sys/capability.h> |
| 28 | #include <sys/xattr.h> |
| 29 | #include <linux/xattr.h> |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 30 | |
| 31 | #include "orscmd.h" |
| 32 | #include "../variables.h" |
| 33 | |
| 34 | void print_version(void) { |
| 35 | printf("TWRP openrecoveryscript command line tool, TWRP version %s\n\n", TW_VERSION_STR); |
| 36 | } |
| 37 | |
| 38 | void print_usage(void) { |
| 39 | print_version(); |
| 40 | printf("Allows command line usage of TWRP via openrecoveryscript commands.\n"); |
| 41 | printf("Some common commands include:\n"); |
| 42 | printf(" install /path/to/update.zip\n"); |
| 43 | printf(" backup BSDC backupname\n"); |
| 44 | printf(" restore backupname BSDC\n"); |
| 45 | printf(" factoryreset\n"); |
| 46 | printf(" wipe cache\n"); |
| 47 | printf(" sideload\n"); |
| 48 | printf(" set variable value\n"); |
| 49 | printf(" get variable\n"); |
| 50 | printf(" decrypt password\n"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 51 | printf(" remountrw\n"); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 52 | printf("\nSee more documentation at http://teamw.in/openrecoveryscript\n"); |
| 53 | } |
| 54 | |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 55 | int do_setcap(const char* filename, const char* capabilities) |
| 56 | { |
| 57 | uint64_t caps; |
| 58 | if (sscanf(capabilities, "%" SCNi64, &caps) != 1) |
| 59 | { |
| 60 | printf("setcap: invalid capabilities \"%s\"\n", filename); |
| 61 | return 1; |
| 62 | } |
| 63 | struct vfs_cap_data cap_data; |
| 64 | memset(&cap_data, 0, sizeof(cap_data)); |
| 65 | cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; |
| 66 | cap_data.data[0].permitted = (uint32_t) (caps & 0xffffffff); |
| 67 | cap_data.data[0].inheritable = 0; |
| 68 | cap_data.data[1].permitted = (uint32_t) (caps >> 32); |
| 69 | cap_data.data[1].inheritable = 0; |
| 70 | if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) { |
| 71 | printf("setcap of %s to %" PRIx64 " failed: %s\n", |
| 72 | filename, caps, strerror(errno)); |
| 73 | return 1; |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int do_getcap(const char* filename) |
| 79 | { |
| 80 | struct vfs_cap_data cap_data; |
| 81 | memset(&cap_data, 0, sizeof(cap_data)); |
| 82 | int rc = getxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(vfs_cap_data)); |
| 83 | if (rc > 0) |
| 84 | { |
| 85 | uint64_t caps = (uint64_t) cap_data.data[1].permitted << 32 | cap_data.data[0].permitted; |
| 86 | printf("0x%" PRIx64 "\n", caps); |
| 87 | } |
| 88 | else |
| 89 | printf("getcap of %s failed: %s\n", filename, strerror(errno)); |
| 90 | |
| 91 | return rc > 0; |
| 92 | } |
| 93 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 94 | int main(int argc, char **argv) { |
| 95 | int read_fd, write_fd, index; |
| 96 | char command[1024], result[512]; |
| 97 | |
| 98 | if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "?") == 0 || strcmp(argv[1], "-h") == 0) { |
| 99 | print_usage(); |
| 100 | return 0; |
| 101 | } |
| 102 | if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) { |
| 103 | print_version(); |
| 104 | return 0; |
| 105 | } |
| 106 | |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 107 | if (strcmp(argv[1], "setcap") == 0) { |
| 108 | if (argc != 4) |
| 109 | { |
| 110 | printf("Usage: setcap filename capabilities\n\n" |
| 111 | "capabilities must be specified as a number. Prefix with 0x for hexadecimal.\n"); |
| 112 | return 1; |
| 113 | } |
| 114 | return do_setcap(argv[2], argv[3]); |
| 115 | } |
| 116 | |
| 117 | if (strcmp(argv[1], "getcap") == 0) { |
| 118 | if (argc != 3) |
| 119 | { |
| 120 | printf("Usage: getcap filename\n"); |
| 121 | return 1; |
| 122 | } |
| 123 | return do_getcap(argv[2]); |
| 124 | } |
| 125 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 126 | sprintf(command, "%s", argv[1]); |
| 127 | for (index = 2; index < argc; index++) { |
| 128 | sprintf(command, "%s %s", command, argv[index]); |
| 129 | } |
| 130 | |
| 131 | write_fd = open(ORS_INPUT_FILE, O_WRONLY); |
| 132 | if (write_fd < 0) { |
| 133 | printf("TWRP does not appear to be running. Waiting for TWRP to start . . .\n"); |
| 134 | printf("Press CTRL + C to quit.\n"); |
| 135 | while (write_fd < 0) |
| 136 | write_fd = open(ORS_INPUT_FILE, O_WRONLY); |
| 137 | } |
| 138 | if (write(write_fd, command, sizeof(command)) != sizeof(command)) { |
| 139 | printf("Error sending command.\n"); |
| 140 | close(write_fd); |
| 141 | return -1; |
| 142 | } |
| 143 | read_fd = open(ORS_OUTPUT_FILE, O_RDONLY); |
| 144 | if (read_fd < 0) { |
| 145 | printf("Unable to open %s for read.\n", ORS_OUTPUT_FILE); |
| 146 | return -1; |
| 147 | } |
| 148 | memset(&result, 0, sizeof(result)); |
| 149 | while (read(read_fd, &result, sizeof(result)) > 0) { |
| 150 | result[510] = '\n'; |
| 151 | result[511] = '\0'; |
| 152 | printf("%s", result); |
| 153 | memset(&result, 0, sizeof(result)); |
| 154 | } |
| 155 | close(write_fd); |
| 156 | close(read_fd); |
| 157 | return 0; |
| 158 | } |