The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | #include <stdio.h> |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 19 | #include <sys/stat.h> |
| 20 | #include <errno.h> |
| 21 | #include <unistd.h> |
| 22 | #include <string.h> |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
| 24 | |
| 25 | #include <selinux/selinux.h> |
| 26 | #include <selinux/label.h> |
| 27 | #include <selinux/android.h> |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | |
| 29 | #include "private/android_filesystem_config.h" |
| 30 | |
| 31 | // This program takes a list of files and directories (indicated by a |
| 32 | // trailing slash) on the stdin, and prints to stdout each input |
| 33 | // filename along with its desired uid, gid, and mode (in octal). |
| 34 | // The leading slash should be stripped from the input. |
| 35 | // |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 36 | // After the first 4 columns, optional key=value pairs are emitted |
| 37 | // for each file. Currently, the following keys are supported: |
| 38 | // * -S: selabel=[selinux_label] |
| 39 | // * -C: capabilities=[hex capabilities value] |
| 40 | // |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 41 | // Example input: |
| 42 | // |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 43 | // system/etc/dbus.conf |
| 44 | // data/app/ |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | // |
| 46 | // Output: |
| 47 | // |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 48 | // system/etc/dbus.conf 1002 1002 440 |
| 49 | // data/app 1000 1000 771 |
| 50 | // |
| 51 | // or if, for example, -S is used: |
| 52 | // |
| 53 | // system/etc/dbus.conf 1002 1002 440 selabel=u:object_r:system_file:s0 |
| 54 | // data/app 1000 1000 771 selabel=u:object_r:apk_data_file:s0 |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 55 | // |
| 56 | // Note that the output will omit the trailing slash from |
| 57 | // directories. |
| 58 | |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 59 | static struct selabel_handle* get_sehnd(const char* context_file) { |
| 60 | struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, context_file } }; |
| 61 | struct selabel_handle* sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1); |
| 62 | |
| 63 | if (!sehnd) { |
| 64 | perror("error running selabel_open"); |
| 65 | exit(EXIT_FAILURE); |
| 66 | } |
| 67 | return sehnd; |
| 68 | } |
| 69 | |
| 70 | static void usage() { |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 71 | fprintf(stderr, "Usage: fs_config [-D product_out_path] [-S context_file] [-C]\n"); |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 72 | } |
| 73 | |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 74 | int main(int argc, char** argv) { |
| 75 | char buffer[1024]; |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 76 | const char* context_file = NULL; |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 77 | const char* product_out_path = NULL; |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 78 | struct selabel_handle* sehnd = NULL; |
| 79 | int print_capabilities = 0; |
| 80 | int opt; |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 81 | while((opt = getopt(argc, argv, "CS:D:")) != -1) { |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 82 | switch(opt) { |
| 83 | case 'C': |
| 84 | print_capabilities = 1; |
| 85 | break; |
| 86 | case 'S': |
| 87 | context_file = optarg; |
| 88 | break; |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 89 | case 'D': |
| 90 | product_out_path = optarg; |
| 91 | break; |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 92 | default: |
| 93 | usage(); |
| 94 | exit(EXIT_FAILURE); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (context_file != NULL) { |
| 99 | sehnd = get_sehnd(context_file); |
| 100 | } |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 101 | |
| 102 | while (fgets(buffer, 1023, stdin) != NULL) { |
| 103 | int is_dir = 0; |
| 104 | int i; |
| 105 | for (i = 0; i < 1024 && buffer[i]; ++i) { |
| 106 | switch (buffer[i]) { |
| 107 | case '\n': |
| 108 | buffer[i-is_dir] = '\0'; |
| 109 | i = 1025; |
| 110 | break; |
| 111 | case '/': |
| 112 | is_dir = 1; |
| 113 | break; |
| 114 | default: |
| 115 | is_dir = 0; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | unsigned uid = 0, gid = 0, mode = 0; |
Nick Kralevich | fa79809 | 2013-02-20 12:43:51 -0800 | [diff] [blame] | 121 | uint64_t capabilities; |
Thierry Strudel | 74a81e6 | 2015-07-09 09:54:55 -0700 | [diff] [blame] | 122 | fs_config(buffer, is_dir, product_out_path, &uid, &gid, &mode, &capabilities); |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 123 | printf("%s %d %d %o", buffer, uid, gid, mode); |
| 124 | |
| 125 | if (sehnd != NULL) { |
| 126 | size_t buffer_strlen = strnlen(buffer, sizeof(buffer)); |
| 127 | if (buffer_strlen >= sizeof(buffer)) { |
| 128 | fprintf(stderr, "non null terminated buffer, aborting\n"); |
| 129 | exit(EXIT_FAILURE); |
| 130 | } |
| 131 | size_t full_name_size = buffer_strlen + 2; |
| 132 | char* full_name = (char*) malloc(full_name_size); |
| 133 | if (full_name == NULL) { |
| 134 | perror("malloc"); |
| 135 | exit(EXIT_FAILURE); |
| 136 | } |
| 137 | |
| 138 | full_name[0] = '/'; |
| 139 | strncpy(full_name + 1, buffer, full_name_size - 1); |
| 140 | full_name[full_name_size - 1] = '\0'; |
| 141 | |
| 142 | char* secontext; |
| 143 | if (selabel_lookup(sehnd, &secontext, full_name, ( mode | (is_dir ? S_IFDIR : S_IFREG)))) { |
| 144 | secontext = strdup("u:object_r:unlabeled:s0"); |
| 145 | } |
| 146 | |
| 147 | printf(" selabel=%s", secontext); |
| 148 | free(full_name); |
| 149 | freecon(secontext); |
| 150 | } |
| 151 | |
| 152 | if (print_capabilities) { |
| 153 | printf(" capabilities=0x%" PRIx64, capabilities); |
| 154 | } |
| 155 | |
| 156 | printf("\n"); |
The Android Open Source Project | 88b6079 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 157 | } |
| 158 | return 0; |
| 159 | } |